From 53b844792580ca094f93f0030c77297dc35b1cea Mon Sep 17 00:00:00 2001 From: Tony Lu Date: Fri, 24 Jul 2020 15:49:11 +0800 Subject: [PATCH 2413/2944] alinux: net: track the pid who created socks fix #29493552 This adds sk_pid in struct sock to store the pid who created this sock. Also a new INET type INET_DIAG_PID is included for userspace programs. sk_pid is initialized when process creates AF_INET or AF_INET6 socket. Given that the impact on performance, it won't be updated when read or write on this socket. This field stores the current namespace pid. Userspace programs, such as ss (iproute2) and NX (nx tcp top), could fetch this value with INET_DIAG_PID, and netlink payload contains the sock pid without iterating /proc/${pid}/fd. This records the pid who created the socket, it could be enough to help us trace the TCP flows with very low overhead. Be careful of the value of INET_DIAG_PID, upstream is introducing more and more types, so we leaves a hole in the enum for the future extension. Signed-off-by: Tony Lu Acked-by: Dust Li --- include/net/sock.h | 3 +++ include/uapi/linux/inet_diag.h | 1 + net/ipv4/af_inet.c | 3 +++ net/ipv4/inet_diag.c | 7 +++++++ 4 files changed, 14 insertions(+) diff --git a/include/net/sock.h b/include/net/sock.h index 9649196..862ae4c 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -324,6 +324,7 @@ struct sock_common { * @sk_txtime_deadline_mode: set deadline mode for SO_TXTIME * @sk_txtime_unused: unused txtime flags * @sk_toa_data: tcp option address (toa) data + * @sk_pid: for which process created this sock */ struct sock { /* @@ -512,6 +513,8 @@ struct sock { __be32 sk_toa_data[16]; struct rcu_head sk_rcu; + pid_t sk_pid; + ALI_HOTFIX_RESERVE(1) ALI_HOTFIX_RESERVE(2) }; diff --git a/include/uapi/linux/inet_diag.h b/include/uapi/linux/inet_diag.h index e8baca8..865118c 100644 --- a/include/uapi/linux/inet_diag.h +++ b/include/uapi/linux/inet_diag.h @@ -153,6 +153,7 @@ enum { INET_DIAG_BBRINFO, /* request as INET_DIAG_VEGASINFO */ INET_DIAG_CLASS_ID, /* request as INET_DIAG_TCLASS */ INET_DIAG_MD5SIG, + INET_DIAG_PID = 30, /* response attribute only for sk_pid */ __INET_DIAG_MAX, }; diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c index 1fbe2f8..996ca0b 100644 --- a/net/ipv4/af_inet.c +++ b/net/ipv4/af_inet.c @@ -350,6 +350,9 @@ static int inet_create(struct net *net, struct socket *sock, int protocol, sk->sk_protocol = protocol; sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv; + if (!kern) + sk->sk_pid = task_pid_vnr(current); + inet->uc_ttl = -1; inet->mc_loop = 1; inet->mc_ttl = 1; diff --git a/net/ipv4/inet_diag.c b/net/ipv4/inet_diag.c index 5731670..2736755 100644 --- a/net/ipv4/inet_diag.c +++ b/net/ipv4/inet_diag.c @@ -109,6 +109,7 @@ static size_t inet_sk_attr_size(struct sock *sk, + nla_total_size(1) /* INET_DIAG_TCLASS */ + nla_total_size(4) /* INET_DIAG_MARK */ + nla_total_size(4) /* INET_DIAG_CLASS_ID */ + + nla_total_size(4) /* INET_DIAG_PID */ + nla_total_size(sizeof(struct inet_diag_meminfo)) + nla_total_size(sizeof(struct inet_diag_msg)) + nla_total_size(SK_MEMINFO_VARS * sizeof(u32)) @@ -306,6 +307,12 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk, goto errout; } + if ((ext & (1 << (INET_DIAG_PID - 1)) || + ext & (1 << (INET_DIAG_INFO - 1))) && + net_admin && sk->sk_pid) + if (nla_put_u32(skb, INET_DIAG_PID, sk->sk_pid)) + goto errout; + out: nlmsg_end(skb, nlh); return 0; -- 1.8.3.1