From: Andy Lutomirski <luto@amacapital.net>
To: Felipe Gasper <felipe@felipegasper.com>
Cc: davem@davemloft.net, viro@zeniv.linux.org.uk,
linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
linux-api@vger.kernel.org
Subject: Re: [PATCH v2] net: Add UNIX_DIAG_UID to Netlink UNIX socket diagnostics.
Date: Sat, 18 May 2019 10:18:54 -0700 [thread overview]
Message-ID: <6CDA54A5-56FF-4B42-B6F5-762E72C3FC3B@amacapital.net> (raw)
In-Reply-To: <20190518034820.16500-1-felipe@felipegasper.com>
> On May 17, 2019, at 8:48 PM, Felipe Gasper <felipe@felipegasper.com> wrote:
>
> Author: Felipe Gasper <felipe@felipegasper.com>
> Date: Fri May 17 16:54:40 2019 -0500
>
> 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.
>
> Signed-off-by: Felipe Gasper <felipe@felipegasper.com>
>
> diff --git a/include/uapi/linux/unix_diag.h b/include/uapi/linux/unix_diag.h
> index 5c502fd..a198857 100644
> --- a/include/uapi/linux/unix_diag.h
> +++ b/include/uapi/linux/unix_diag.h
> @@ -20,6 +20,7 @@ struct unix_diag_req {
> #define UDIAG_SHOW_ICONS 0x00000008 /* show pending connections */
> #define UDIAG_SHOW_RQLEN 0x00000010 /* show skb receive queue len */
> #define UDIAG_SHOW_MEMINFO 0x00000020 /* show memory info of a socket */
> +#define UDIAG_SHOW_UID 0x00000040 /* show socket's UID */
>
> struct unix_diag_msg {
> __u8 udiag_family;
> @@ -40,6 +41,7 @@ enum {
> UNIX_DIAG_RQLEN,
> UNIX_DIAG_MEMINFO,
> UNIX_DIAG_SHUTDOWN,
> + UNIX_DIAG_UID,
>
> __UNIX_DIAG_MAX,
> };
> diff --git a/net/unix/diag.c b/net/unix/diag.c
> index 3183d9b..e40f348 100644
> --- a/net/unix/diag.c
> +++ b/net/unix/diag.c
> @@ -4,9 +4,11 @@
> #include <linux/unix_diag.h>
> #include <linux/skbuff.h>
> #include <linux/module.h>
> +#include <linux/uidgid.h>
> #include <net/netlink.h>
> #include <net/af_unix.h>
> #include <net/tcp_states.h>
> +#include <net/sock.h>
>
> static int sk_diag_dump_name(struct sock *sk, struct sk_buff *nlskb)
> {
> @@ -110,6 +112,12 @@ static int sk_diag_show_rqlen(struct sock *sk, struct sk_buff *nlskb)
> return nla_put(nlskb, UNIX_DIAG_RQLEN, sizeof(rql), &rql);
> }
>
> +static int sk_diag_dump_uid(struct sock *sk, struct sk_buff *nlskb)
> +{
> + uid_t uid = from_kuid_munged(sk_user_ns(sk), sock_i_uid(sk));
> + return nla_put(nlskb, UNIX_DIAG_UID, sizeof(uid_t), &uid);
This still looks wrong. You’re reporting the uid of the socket as seen by that socket. Presumably you actually want the uid of the socket as seen by the *diagnostic* socket. This might be sk_user_ns(nlskb->sk).
You can test this with a command like unshare -U -r nc -l 12345 run as no -root. Then run you user diagnostic tool to find the uid of the socket. It should not be zero. Similarly, if you run unshare -U -r bash and then open a socket and run your diagnostic tool, both from that same session, you should see 0 as the uid since bash and all its children think they’re uid 0.
> +}
> +
> static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
> u32 portid, u32 seq, u32 flags, int sk_ino)
> {
> @@ -156,6 +164,10 @@ static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, struct unix_diag_r
> if (nla_put_u8(skb, UNIX_DIAG_SHUTDOWN, sk->sk_shutdown))
> goto out_nlmsg_trim;
>
> + if ((req->udiag_show & UDIAG_SHOW_UID) &&
> + sk_diag_dump_uid(sk, skb))
> + goto out_nlmsg_trim;
> +
> nlmsg_end(skb, nlh);
> return 0;
>
prev parent reply other threads:[~2019-05-18 17:18 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-05-18 3:48 [PATCH v2] net: Add UNIX_DIAG_UID to Netlink UNIX socket diagnostics Felipe Gasper
2019-05-18 17:18 ` Andy Lutomirski [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6CDA54A5-56FF-4B42-B6F5-762E72C3FC3B@amacapital.net \
--to=luto@amacapital.net \
--cc=davem@davemloft.net \
--cc=felipe@felipegasper.com \
--cc=linux-api@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).