netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kuniyuki Iwashima <kuniyu@amazon.com>
To: <brauner@kernel.org>
Cc: <bluca@debian.org>, <daan.j.demeyer@gmail.com>,
	<davem@davemloft.net>, <david@readahead.eu>,
	<edumazet@google.com>, <horms@kernel.org>, <jack@suse.cz>,
	<jannh@google.com>, <kuba@kernel.org>, <kuniyu@amazon.com>,
	<lennart@poettering.net>, <linux-fsdevel@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, <me@yhndnzj.com>,
	<netdev@vger.kernel.org>, <oleg@redhat.com>, <pabeni@redhat.com>,
	<viro@zeniv.linux.org.uk>, <zbyszek@in.waw.pl>
Subject: Re: [PATCH RFC v2 3/6] coredump: support AF_UNIX sockets
Date: Mon, 5 May 2025 12:03:50 -0700	[thread overview]
Message-ID: <20250505190410.17360-1-kuniyu@amazon.com> (raw)
In-Reply-To: <20250503-gegessen-trugen-6474e70e59df@brauner>

From: Christian Brauner <brauner@kernel.org>
Date: Sat, 3 May 2025 07:17:10 +0200
> On Fri, May 02, 2025 at 10:23:44PM +0200, Jann Horn wrote:
> > On Fri, May 2, 2025 at 10:11 PM Christian Brauner <brauner@kernel.org> wrote:
> > > On Fri, May 02, 2025 at 04:04:32PM +0200, Jann Horn wrote:
> > > > On Fri, May 2, 2025 at 2:42 PM Christian Brauner <brauner@kernel.org> wrote:
> > > > > diff --git a/fs/coredump.c b/fs/coredump.c
> > > > [...]
> > > > > @@ -801,6 +841,73 @@ void do_coredump(const kernel_siginfo_t *siginfo)
> > > > >                 }
> > > > >                 break;
> > > > >         }
> > > > > +       case COREDUMP_SOCK: {
> > > > > +               struct file *file __free(fput) = NULL;
> > > > > +#ifdef CONFIG_UNIX
> > > > > +               ssize_t addr_size;
> > > > > +               struct sockaddr_un unix_addr = {
> > > > > +                       .sun_family = AF_UNIX,
> > > > > +               };
> > > > > +               struct sockaddr_storage *addr;
> > > > > +
> > > > > +               /*
> > > > > +                * TODO: We need to really support core_pipe_limit to
> > > > > +                * prevent the task from being reaped before userspace
> > > > > +                * had a chance to look at /proc/<pid>.
> > > > > +                *
> > > > > +                * I need help from the networking people (or maybe Oleg
> > > > > +                * also knows?) how to do this.
> > > > > +                *
> > > > > +                * IOW, we need to wait for the other side to shutdown
> > > > > +                * the socket/terminate the connection.
> > > > > +                *
> > > > > +                * We could just read but then userspace could sent us
> > > > > +                * SCM_RIGHTS and we just shouldn't need to deal with
> > > > > +                * any of that.
> > > > > +                */
> > > >
> > > > I don't think userspace can send you SCM_RIGHTS if you don't do a
> > > > recvmsg() with a control data buffer?
> > >
> > > Oh hm, then maybe just a regular read at the end would work. As soon as
> > > userspace send us anything or we get a close event we just disconnect.
> > >
> > > But btw, I think we really need a recvmsg() flag that allows a receiver
> > > to refuse SCM_RIGHTS/file descriptors from being sent to it. IIRC, right
> > > now this is a real issue that systemd works around by always calling its
> > > cmsg_close_all() helper after each recvmsg() to ensure that no one sent
> > > it file descriptors it didn't want. The problem there is that someone
> > > could have sent it an fd to a hanging NFS server or something and then
> > > it would hang in close() even though it never even wanted any file
> > > descriptors in the first place.
> > 
> > Would a recvmsg() flag really solve that aspect of NFS hangs? By the
> > time you read from the socket, the file is already attached to an SKB
> > queued up on the socket, and cleaning up the file is your task's
> > responsibility either way (which will either be done by the kernel for
> > you if you don't read it into a control message, or by userspace if it
> > was handed off through a control message).

Right.  recvmsg() is too late.  Once sendmsg() is done, the last
fput() responsibility could fall on the receiver.

Btw, I was able to implement the cmsg_close_all() equivalent at
sendmsg() with BPF LSM to completely remove the issue.

I will send a series shortly and hope you like it :)


> > The process that sent the
> > file to you might already be gone, it can't be on the hook for
> > cleaning up the file anymore.
> 
> Hm, I guess the unix_gc() runs in task context? I had thought that it
> might take care of that.

Note that unix_gc() is a garbage collector only for AF_UNIX fds
that have circular dependency:

  1) AF_UNIX sk1 sends its fd to itself

  2) AF_UNIX sk1 sends its fd to AF_UNIX sk2 and
     AF_UNIX sk2 sends its fd to AF_UNIX sk1

In these examples, file refcnts remain even after close() by all
users of fds.

So, the GC is not a mechanism to deligate fput() for fds sent
by SCM_RIGHTS.

  reply	other threads:[~2025-05-05 19:04 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-02 12:42 [PATCH RFC v2 0/6] coredump: support AF_UNIX sockets Christian Brauner
2025-05-02 12:42 ` [PATCH RFC v2 1/6] coredump: massage format_corname() Christian Brauner
2025-05-02 12:42 ` [PATCH RFC v2 2/6] coredump: massage do_coredump() Christian Brauner
2025-05-02 12:42 ` [PATCH RFC v2 3/6] coredump: support AF_UNIX sockets Christian Brauner
2025-05-02 14:04   ` Jann Horn
2025-05-02 20:10     ` Christian Brauner
2025-05-02 20:23       ` Jann Horn
2025-05-03  5:17         ` Christian Brauner
2025-05-05 19:03           ` Kuniyuki Iwashima [this message]
2025-05-02 12:42 ` [PATCH RFC v2 4/6] coredump: show supported coredump modes Christian Brauner
2025-05-02 14:07   ` Jann Horn
2025-05-02 20:11     ` Christian Brauner
2025-05-02 12:42 ` [PATCH RFC v2 5/6] pidfs, coredump: add PIDFD_INFO_COREDUMP Christian Brauner
2025-05-02 14:10   ` Jann Horn
2025-05-02 12:42 ` [PATCH RFC v2 6/6] selftests/coredump: add tests for AF_UNIX coredumps Christian Brauner
2025-05-02 14:04 ` [PATCH RFC v2 0/6] coredump: support AF_UNIX sockets Jann Horn
2025-05-02 19:25   ` Christian Brauner

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=20250505190410.17360-1-kuniyu@amazon.com \
    --to=kuniyu@amazon.com \
    --cc=bluca@debian.org \
    --cc=brauner@kernel.org \
    --cc=daan.j.demeyer@gmail.com \
    --cc=davem@davemloft.net \
    --cc=david@readahead.eu \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=jack@suse.cz \
    --cc=jannh@google.com \
    --cc=kuba@kernel.org \
    --cc=lennart@poettering.net \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=me@yhndnzj.com \
    --cc=netdev@vger.kernel.org \
    --cc=oleg@redhat.com \
    --cc=pabeni@redhat.com \
    --cc=viro@zeniv.linux.org.uk \
    --cc=zbyszek@in.waw.pl \
    /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).