From: Kuniyuki Iwashima <kuniyu@amazon.com>
To: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Cc: <andrii@kernel.org>, <ast@kernel.org>, <bpf@vger.kernel.org>,
<brauner@kernel.org>, <casey@schaufler-ca.com>,
<daniel@iogearbox.net>, <eddyz87@gmail.com>, <gnoack@google.com>,
<haoluo@google.com>, <jmorris@namei.org>,
<john.fastabend@gmail.com>, <jolsa@kernel.org>,
<kpsingh@kernel.org>, <kuni1840@gmail.com>, <kuniyu@amazon.com>,
<linux-security-module@vger.kernel.org>, <martin.lau@linux.dev>,
<mic@digikod.net>, <netdev@vger.kernel.org>,
<omosnace@redhat.com>, <paul@paul-moore.com>, <sdf@fomichev.me>,
<selinux@vger.kernel.org>, <serge@hallyn.com>, <song@kernel.org>,
<stephen.smalley.work@gmail.com>, <yonghong.song@linux.dev>
Subject: Re: [PATCH v1 bpf-next 0/5] af_unix: Allow BPF LSM to scrub SCM_RIGHTS at sendmsg().
Date: Mon, 5 May 2025 17:21:27 -0700 [thread overview]
Message-ID: <20250506002813.65225-1-kuniyu@amazon.com> (raw)
In-Reply-To: <CAP01T77STmncrPt=BsFfEY6SX1+oYNXhPeZ1HC9J=S2jhOwQoQ@mail.gmail.com>
From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Date: Tue, 6 May 2025 00:49:11 +0200
> On Mon, 5 May 2025 at 23:58, Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
> >
> > As long as recvmsg() or recvmmsg() is used with cmsg, it is not
> > possible to avoid receiving file descriptors via SCM_RIGHTS.
> >
> > This behaviour has occasionally been flagged as problematic.
> >
> > For instance, as noted on the uAPI Group page [0], an untrusted peer
> > could send a file descriptor pointing to a hung NFS mount and then
> > close it. Once the receiver calls recvmsg() with msg_control, the
> > descriptor is automatically installed, and then the responsibility
> > for the final close() now falls on the receiver, which may result
> > in blocking the process for a long time.
> >
> > systemd calls cmsg_close_all() [1] after each recvmsg() to close()
> > unwanted file descriptors sent via SCM_RIGHTS.
> >
> > However, this cannot work around the issue because the last fput()
> > could occur on the receiver side once sendmsg() with SCM_RIGHTS
> > succeeds. Also, even filtering by LSM at recvmsg() does not work
> > for the same reason.
> >
> > Thus, we need a better way to filter SCM_RIGHTS on the sender side.
> >
> > This series allows BPF LSM to inspect skb at sendmsg() and scrub
> > SCM_RIGHTS fds by kfunc.
> >
> > Link: https://uapi-group.org/kernel-features/#disabling-reception-of-scm_rights-for-af_unix-sockets #[0]
> > Link: https://github.com/systemd/systemd/blob/v257.5/src/basic/fd-util.c#L612-L628 #[1]
> >
>
> This sounds pretty useful!
>
> I think you should mention the cases of possible DoS on close() or
> flooding, e.g. with FUSE controlled fd/NFS hangs in the commit log
> itself.
> I think it's been an open problem for a while now with no good solution.
> Currently systemd's FDSTORE=1 for PID 1 is susceptible to the same
> problem, even if the underlying service isn't root.
Good point, will add the description in v2.
>
> I think it is also useful for restricting what individual file
> descriptors can be passed around by a process.
> Say restricting usage of an fd to a process and its children, but not
> allowing it to be shared with others.
> Send side hook is the right point to enforce it.
Agreed.
Actually, I tried per-fd filtering first and failed somehow so
wanted some advice from BPF folks :)
For example, I implemented kfunc like:
__bpf_kfunc int bpf_unix_scrub_file(struct sk_buff *skb, struct file *filp)
{
/* scrub fd matching file if exists */
}
and tried filp == NULL -> scrub all so that I can gradually extend
the functionality, but verifier didn't allow passing NULL.
Also, once a fd is scrubbed, I do not want to leave the array entry
empty to avoid adding unnecessary "if (fpl->fp[i] == -1)" test in
other places.
struct scm_fp_list *fpl = UNIXCB(skb).fp;
/* scrubbed fpl->fp[i] here. */
fpl->fp[i] = fpl->fp[fpl->count - 1];
fpl->count--;
But this could confuse BPF prog if it was iterating fpl->fp[] in for
loop and I was wondering how the interface should be like.
* Keep the empty index and ignore at core code ?
* Provide a fd iterator ?
* Scrub based on index ? matching fd ? or struct file ?
* -1 works as ALL_INDEX or ALL_FDS but NULL doesn't
* Invoke BPF LSM per-fd ?
* Maybe no as sender/receiver pair is always same for the same skb
I guess keeping the empty index as is and index based scrubbing
would be simpler and cleaner ?
>
> Therefore exercising scm_fp_list would be a good idea.
> We should provide some more examples of the filtering policy in the selftests.
> Maybe a simple example, e.g. only memfd or a pipe fd can be passed,
> and nothing else.
> It would require checking file->f_ops.
Yes, and I thought we need fd-to-file kfunc or BPF helper, but I was
not sure which would be better as both functionality should be stable.
But given the user needs to inspect the raw scm_fp_list, kfunc is better ?
* bpf_fd_to_file()
or
* bpf_unix_get_scm_rights() -> return struct file ?
plus
* bpf_unix_scrub_scm_rights() -> scrub based on fd or file ?
>
> I don't think "scrub all file descriptors" is the only possible usage scenario.
> In the case of FDSTORE=1, it might be "everything except fuse or NFS fds" etc.
>
> Eventually if file local storage happens, more interesting policies
> may be possible.
>
next prev parent reply other threads:[~2025-05-06 0:28 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-05 21:56 [PATCH v1 bpf-next 0/5] af_unix: Allow BPF LSM to scrub SCM_RIGHTS at sendmsg() Kuniyuki Iwashima
2025-05-05 21:56 ` [PATCH v1 bpf-next 1/5] af_unix: Call security_unix_may_send() in sendmsg() for all socket types Kuniyuki Iwashima
2025-05-05 21:56 ` [PATCH v1 bpf-next 2/5] af_unix: Pass skb to security_unix_may_send() Kuniyuki Iwashima
2025-05-05 21:56 ` [PATCH v1 bpf-next 3/5] af_unix: Remove redundant scm->fp check in __scm_destroy() Kuniyuki Iwashima
2025-05-09 14:13 ` kernel test robot
2025-05-05 21:56 ` [PATCH v1 bpf-next 4/5] bpf: Add kfunc to scrub SCM_RIGHTS at security_unix_may_send() Kuniyuki Iwashima
2025-05-06 0:13 ` Alexei Starovoitov
2025-05-06 8:25 ` Mickaël Salaün
2025-05-09 15:06 ` kernel test robot
2025-05-05 21:56 ` [PATCH v1 bpf-next 5/5] selftest: bpf: Add test for bpf_unix_scrub_fds() Kuniyuki Iwashima
2025-05-05 22:49 ` [PATCH v1 bpf-next 0/5] af_unix: Allow BPF LSM to scrub SCM_RIGHTS at sendmsg() Kumar Kartikeya Dwivedi
2025-05-06 0:21 ` Kuniyuki Iwashima [this message]
2025-05-06 16:25 ` Kumar Kartikeya Dwivedi
2025-05-06 18:16 ` Kuniyuki Iwashima
2025-05-06 9:15 ` Christian Brauner
2025-05-06 16:08 ` Kumar Kartikeya Dwivedi
2025-05-06 18:14 ` Kuniyuki Iwashima
2025-05-05 23:21 ` Paul Moore
2025-05-06 0:35 ` Kuniyuki Iwashima
2025-05-06 14:57 ` Paul Moore
2025-05-06 12:17 ` Lennart Poettering
2025-05-06 18:19 ` Kuniyuki Iwashima
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=20250506002813.65225-1-kuniyu@amazon.com \
--to=kuniyu@amazon.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=brauner@kernel.org \
--cc=casey@schaufler-ca.com \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=gnoack@google.com \
--cc=haoluo@google.com \
--cc=jmorris@namei.org \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=kuni1840@gmail.com \
--cc=linux-security-module@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=mic@digikod.net \
--cc=netdev@vger.kernel.org \
--cc=omosnace@redhat.com \
--cc=paul@paul-moore.com \
--cc=sdf@fomichev.me \
--cc=selinux@vger.kernel.org \
--cc=serge@hallyn.com \
--cc=song@kernel.org \
--cc=stephen.smalley.work@gmail.com \
--cc=yonghong.song@linux.dev \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.