All of lore.kernel.org
 help / color / mirror / Atom feed
From: Al Viro <viro@zeniv.linux.org.uk>
To: Christian Brauner <brauner@kernel.org>
Cc: linux-fsdevel@vger.kernel.org, torvalds@linux-foundation.org
Subject: Re: [PATCH 11/19] switch simple users of fdget() to CLASS(fd, ...)
Date: Fri, 7 Jun 2024 17:10:43 +0100	[thread overview]
Message-ID: <20240607161043.GZ1629371@ZenIV> (raw)
In-Reply-To: <20240607-gelacht-enkel-06a7c9b31d4e@brauner>

On Fri, Jun 07, 2024 at 05:26:53PM +0200, Christian Brauner wrote:
> On Fri, Jun 07, 2024 at 02:59:49AM +0100, Al Viro wrote:
> > low-hanging fruit; that's another likely source of conflicts
> > over the cycle and it might make a lot of sense to split;
> > fortunately, it can be split pretty much on per-function
> > basis - chunks are independent from each other.
> > 
> > Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
> > ---
> 
> I can pick conversions from you for files where I already have changes
> in the tree anyway or already have done conversion as part of other
> patches.

Some notes:

	This kind of conversions depends upon fdput(empty) being not just
a no-op, but a no-op visible to compiler.  Representation change arranges
for that.

	CLASS(...) has some misfeatures or nearly C++ level of tastelessness;
for this series I decided to try it and see how it goes, but... AFAICS in
a lot of cases it's the wrong answer.

	1.  Variable declarations belong in the beginning of block.
CLASS use invites violating that; to make things worse, in-block goto
bypassing such declaration is only caught by current clang.  Two
examples got caught by Intel buildbot in this patch, actually - one
in fs/select.c, another in ocfs2.

	2. The loss of control over the location of destructor call can
be dangerous in some cases.  Delaying it is not a problem for struct
file references (well, except for goto problems above), but there's
an opposite issue.  Example from later in this series:
	if (arg != -1) {
		struct perf_event *output_event;
		struct fd output;
		ret = perf_fget_light(arg, &output);
		if (ret)
			return ret;
		output_event = output.file->private_data;
		ret = perf_event_set_output(event, output_event);
		fdput(output);
	} else {
		ret = perf_event_set_output(event, NULL);
	}
gets converted to
	if (arg != -1) {
		struct perf_event *output_event;
		CLASS(fd, output)(arg);
		if (!is_perf_file(output))
			return -EBADF;
		output_event = fd_file(output)->private_data;
		return perf_event_set_output(event, output_event);
	} else {
		return perf_event_set_output(event, NULL);
	}

Nice, but that invites the next step, doesn't it?  Like this:

	struct perf_event *output_event = NULL;
	if (arg != -1) {
		CLASS(fd, output)(arg);
		if (!is_perf_file(output))
			return -EBADF;
		output_event = fd_file(output)->private_data;
	}
	return perf_event_set_output(event, output_event);

See the trouble here?  The last variant would be broken - the value of
file->private_data escapes the scope and can end up used after
the file gets closed.

In the original variant it's easy to see - we have an explicit
fdput() marking the place where protection disappears.  After
the conversion to implicit cleanups we lose that.

Yes, use of __cleanup can nicely shrink the source and make
some leaks less likely.  But my impression from this experiment
is that we should be very cautious with using that technics ;-/

  reply	other threads:[~2024-06-07 16:10 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-07  1:56 [PATCHES][RFC] rework of struct fd handling Al Viro
2024-06-07  1:59 ` [PATCH 01/19] powerpc: fix a file leak in kvm_vcpu_ioctl_enable_cap() Al Viro
2024-06-07  1:59   ` [PATCH 02/19] lirc: rc_dev_get_from_fd(): fix file leak Al Viro
2024-06-07 15:17     ` Christian Brauner
2024-06-07  1:59   ` [PATCH 03/19] introduce fd_file(), convert all accessors to it Al Viro
2024-06-07  1:59   ` [PATCH 04/19] struct fd: representation change Al Viro
2024-06-07  5:55     ` Amir Goldstein
2024-06-07  1:59   ` [PATCH 05/19] add struct fd constructors, get rid of __to_fd() Al Viro
2024-06-07  1:59   ` [PATCH 06/19] net/socket.c: switch to CLASS(fd) Al Viro
2024-06-07  1:59   ` [PATCH 07/19] introduce struct fderr, convert overlayfs uses to that Al Viro
2024-06-07  1:59   ` [PATCH 08/19] fdget_raw() users: switch to CLASS(fd_raw, ...) Al Viro
2024-06-07 15:20     ` Christian Brauner
2024-06-07  1:59   ` [PATCH 09/19] css_set_fork(): " Al Viro
2024-06-07 15:21     ` Christian Brauner
2024-06-07  1:59   ` [PATCH 10/19] introduce "fd_pos" class Al Viro
2024-06-07 15:21     ` Christian Brauner
2024-06-07  1:59   ` [PATCH 11/19] switch simple users of fdget() to CLASS(fd, ...) Al Viro
2024-06-07 15:26     ` Christian Brauner
2024-06-07 16:10       ` Al Viro [this message]
2024-06-07 16:11         ` Al Viro
2024-06-07 21:08         ` Al Viro
2024-06-10  2:44           ` [RFC] potential UAF in kvm_spapr_tce_attach_iommu_group() (was Re: [PATCH 11/19] switch simple users of fdget() to CLASS(fd, ...)) Al Viro
2024-06-12 16:36             ` Linus Torvalds
2024-06-13 10:56               ` Michael Ellerman
2024-06-13 10:56                 ` Michael Ellerman
2024-06-10  5:12           ` [RFC] UAF in acrn_irqfd_assign() and vfio_virqfd_enable() Al Viro
2024-06-10 17:03             ` Al Viro
2024-06-10 20:09             ` Alex Williamson
2024-06-10 20:53               ` Al Viro
2024-06-11 23:04                 ` Alex Williamson
2024-06-12  2:16                   ` Al Viro
2024-06-07  1:59   ` [PATCH 12/19] bpf: switch to CLASS(fd, ...) Al Viro
2024-06-07 15:27     ` Christian Brauner
2024-06-07  1:59   ` [PATCH 13/19] convert vmsplice() " Al Viro
2024-06-07  1:59   ` [PATCH 14/19] finit_module(): convert " Al Viro
2024-06-07  1:59   ` [PATCH 15/19] timerfd: switch " Al Viro
2024-06-07  1:59   ` [PATCH 16/19] do_mq_notify(): " Al Viro
2024-06-07  1:59   ` [PATCH 17/19] simplify xfs_find_handle() a bit Al Viro
2024-06-07  1:59   ` [PATCH 18/19] convert kernel/events/core.c Al Viro
2024-06-07  1:59   ` [PATCH 19/19] deal with the last remaing boolean uses of fd_file() Al Viro
2024-06-07 15:16   ` [PATCH 01/19] powerpc: fix a file leak in kvm_vcpu_ioctl_enable_cap() Christian Brauner
2024-06-07 15:30 ` [PATCHES][RFC] rework of struct fd handling 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=20240607161043.GZ1629371@ZenIV \
    --to=viro@zeniv.linux.org.uk \
    --cc=brauner@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    /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.