Skip to content

Commit cae9910

Browse files
FGasperdavem330
authored andcommitted
net: Add UNIX_DIAG_UID to Netlink UNIX socket diagnostics.
This adds the ability for Netlink to report a socket's UID along with the other UNIX diagnostic information that is already available. This will allow diagnostic tools greater insight into which users control which socket. To test this, do the following as a non-root user: unshare -U -r bash nc -l -U user.socket.$$ & .. and verify from within that same session that Netlink UNIX socket diagnostics report the socket's UID as 0. Also verify that Netlink UNIX socket diagnostics report the socket's UID as the user's UID from an unprivileged process in a different session. Verify the same from a root process. Signed-off-by: Felipe Gasper <felipe@felipegasper.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 54dee40 commit cae9910

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

include/uapi/linux/unix_diag.h

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct unix_diag_req {
2020
#define UDIAG_SHOW_ICONS 0x00000008 /* show pending connections */
2121
#define UDIAG_SHOW_RQLEN 0x00000010 /* show skb receive queue len */
2222
#define UDIAG_SHOW_MEMINFO 0x00000020 /* show memory info of a socket */
23+
#define UDIAG_SHOW_UID 0x00000040 /* show socket's UID */
2324

2425
struct unix_diag_msg {
2526
__u8 udiag_family;
@@ -40,6 +41,7 @@ enum {
4041
UNIX_DIAG_RQLEN,
4142
UNIX_DIAG_MEMINFO,
4243
UNIX_DIAG_SHUTDOWN,
44+
UNIX_DIAG_UID,
4345

4446
__UNIX_DIAG_MAX,
4547
};

net/unix/diag.c

+12
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
#include <linux/unix_diag.h>
66
#include <linux/skbuff.h>
77
#include <linux/module.h>
8+
#include <linux/uidgid.h>
89
#include <net/netlink.h>
910
#include <net/af_unix.h>
1011
#include <net/tcp_states.h>
12+
#include <net/sock.h>
1113

1214
static int sk_diag_dump_name(struct sock *sk, struct sk_buff *nlskb)
1315
{
@@ -111,6 +113,12 @@ static int sk_diag_show_rqlen(struct sock *sk, struct sk_buff *nlskb)
111113
return nla_put(nlskb, UNIX_DIAG_RQLEN, sizeof(rql), &rql);
112114
}
113115

116+
static int sk_diag_dump_uid(struct sock *sk, struct sk_buff *nlskb)
117+
{
118+
uid_t uid = from_kuid_munged(sk_user_ns(nlskb->sk), sock_i_uid(sk));
119+
return nla_put(nlskb, UNIX_DIAG_UID, sizeof(uid_t), &uid);
120+
}
121+
114122
static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
115123
u32 portid, u32 seq, u32 flags, int sk_ino)
116124
{
@@ -157,6 +165,10 @@ static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, struct unix_diag_r
157165
if (nla_put_u8(skb, UNIX_DIAG_SHUTDOWN, sk->sk_shutdown))
158166
goto out_nlmsg_trim;
159167

168+
if ((req->udiag_show & UDIAG_SHOW_UID) &&
169+
sk_diag_dump_uid(sk, skb))
170+
goto out_nlmsg_trim;
171+
160172
nlmsg_end(skb, nlh);
161173
return 0;
162174

0 commit comments

Comments
 (0)