hc
2024-09-20 cf4ce59b3b70238352c7f1729f0f7223214828ad
kernel/net/unix/diag.c
....@@ -1,12 +1,15 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 #include <linux/types.h>
23 #include <linux/spinlock.h>
34 #include <linux/sock_diag.h>
45 #include <linux/unix_diag.h>
56 #include <linux/skbuff.h>
67 #include <linux/module.h>
8
+#include <linux/uidgid.h>
79 #include <net/netlink.h>
810 #include <net/af_unix.h>
911 #include <net/tcp_states.h>
12
+#include <net/sock.h>
1013
1114 static int sk_diag_dump_name(struct sock *sk, struct sk_buff *nlskb)
1215 {
....@@ -110,8 +113,16 @@
110113 return nla_put(nlskb, UNIX_DIAG_RQLEN, sizeof(rql), &rql);
111114 }
112115
116
+static int sk_diag_dump_uid(struct sock *sk, struct sk_buff *nlskb,
117
+ struct user_namespace *user_ns)
118
+{
119
+ uid_t uid = from_kuid_munged(user_ns, sock_i_uid(sk));
120
+ return nla_put(nlskb, UNIX_DIAG_UID, sizeof(uid_t), &uid);
121
+}
122
+
113123 static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
114
- u32 portid, u32 seq, u32 flags, int sk_ino)
124
+ struct user_namespace *user_ns,
125
+ u32 portid, u32 seq, u32 flags, int sk_ino)
115126 {
116127 struct nlmsghdr *nlh;
117128 struct unix_diag_msg *rep;
....@@ -156,6 +167,10 @@
156167 if (nla_put_u8(skb, UNIX_DIAG_SHUTDOWN, sk->sk_shutdown))
157168 goto out_nlmsg_trim;
158169
170
+ if ((req->udiag_show & UDIAG_SHOW_UID) &&
171
+ sk_diag_dump_uid(sk, skb, user_ns))
172
+ goto out_nlmsg_trim;
173
+
159174 nlmsg_end(skb, nlh);
160175 return 0;
161176
....@@ -165,7 +180,8 @@
165180 }
166181
167182 static int sk_diag_dump(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
168
- u32 portid, u32 seq, u32 flags)
183
+ struct user_namespace *user_ns,
184
+ u32 portid, u32 seq, u32 flags)
169185 {
170186 int sk_ino;
171187
....@@ -176,7 +192,7 @@
176192 if (!sk_ino)
177193 return 0;
178194
179
- return sk_diag_fill(sk, skb, req, portid, seq, flags, sk_ino);
195
+ return sk_diag_fill(sk, skb, req, user_ns, portid, seq, flags, sk_ino);
180196 }
181197
182198 static int unix_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
....@@ -204,7 +220,7 @@
204220 goto next;
205221 if (!(req->udiag_states & (1 << sk->sk_state)))
206222 goto next;
207
- if (sk_diag_dump(sk, skb, req,
223
+ if (sk_diag_dump(sk, skb, req, sk_user_ns(skb->sk),
208224 NETLINK_CB(cb->skb).portid,
209225 cb->nlh->nlmsg_seq,
210226 NLM_F_MULTI) < 0)
....@@ -272,7 +288,8 @@
272288 if (!rep)
273289 goto out;
274290
275
- err = sk_diag_fill(sk, rep, req, NETLINK_CB(in_skb).portid,
291
+ err = sk_diag_fill(sk, rep, req, sk_user_ns(NETLINK_CB(in_skb).sk),
292
+ NETLINK_CB(in_skb).portid,
276293 nlh->nlmsg_seq, 0, req->udiag_ino);
277294 if (err < 0) {
278295 nlmsg_free(rep);