lkml.org 
[lkml]   [2023]   [Mar]   [27]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
Date
From
SubjectRe: [PATCH] userfaultfd: don't fail on unrecognized features
I think I overlooked this patch..

Axel, could you explain why this patch is correct? Comments inline.

On Fri, Jul 22, 2022 at 01:15:13PM -0700, Axel Rasmussen wrote:
> The basic interaction for setting up a userfaultfd is, userspace issues
> a UFFDIO_API ioctl, and passes in a set of zero or more feature flags,
> indicating the features they would prefer to use.
>
> Of course, different kernels may support different sets of features
> (depending on kernel version, kconfig options, architecture, etc).
> Userspace's expectations may also not match: perhaps it was built
> against newer kernel headers, which defined some features the kernel
> it's running on doesn't support.
>
> Currently, if userspace passes in a flag we don't recognize, the
> initialization fails and we return -EINVAL. This isn't great, though.

Why? IIUC that's the major way for user app to detect any misconfig of
feature list so it can bail out early.

Quoting from man page (ioctl_userfaultfd(2)):

UFFDIO_API
(Since Linux 4.3.) Enable operation of the userfaultfd and perform API handshake.

...

struct uffdio_api {
__u64 api; /* Requested API version (input) */
__u64 features; /* Requested features (input/output) */
__u64 ioctls; /* Available ioctl() operations (output) */
};

...

For Linux kernel versions before 4.11, the features field must be
initialized to zero before the call to UFFDIO_API, and zero (i.e.,
no feature bits) is placed in the features field by the kernel upon
return from ioctl(2).

...

To enable userfaultfd features the application should set a bit
corresponding to each feature it wants to enable in the features
field. If the kernel supports all the requested features it will
enable them. Otherwise it will zero out the returned uffdio_api
structure and return EINVAL.

IIUC the right way to use this API is first probe with features==0, then
the kernel will return all the supported features, then the user app should
enable only a subset (or all, but not a superset) of supported ones in the
next UFFDIO_API with a new uffd.

> Userspace doesn't have an obvious way to react to this; sure, one of the
> features I asked for was unavailable, but which one? The only option it
> has is to turn off things "at random" and hope something works.
>
> Instead, modify UFFDIO_API to just ignore any unrecognized feature
> flags. The interaction is now that the initialization will succeed, and
> as always we return the *subset* of feature flags that can actually be
> used back to userspace.
>
> Now userspace has an obvious way to react: it checks if any flags it
> asked for are missing. If so, it can conclude this kernel doesn't
> support those, and it can either resign itself to not using them, or
> fail with an error on its own, or whatever else.
>
> Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
> ---
> fs/userfaultfd.c | 6 ++----
> 1 file changed, 2 insertions(+), 4 deletions(-)
>
> diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c
> index e943370107d0..4974da1f620c 100644
> --- a/fs/userfaultfd.c
> +++ b/fs/userfaultfd.c
> @@ -1923,10 +1923,8 @@ static int userfaultfd_api(struct userfaultfd_ctx *ctx,
> ret = -EFAULT;
> if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
> goto out;
> - features = uffdio_api.features;
> - ret = -EINVAL;
> - if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES))
> - goto err_out;

What's worse is that I think you removed the only UFFD_API check. Although
I'm not sure whether it'll be extended in the future or not at all (very
possible we keep using 0xaa forever..), but removing this means we won't be
able to extend it to a new api version in the future, and misconfig of
uffdio_api will wrongly succeed I think:

/* Test wrong UFFD_API */
uffdio_api.api = 0xab;
uffdio_api.features = 0;
if (ioctl(uffd, UFFDIO_API, &uffdio_api) == 0)
err("UFFDIO_API should fail but didn't");

> + /* Ignore unsupported features (userspace built against newer kernel) */
> + features = uffdio_api.features & UFFD_API_FEATURES;
> ret = -EPERM;
> if ((features & UFFD_FEATURE_EVENT_FORK) && !capable(CAP_SYS_PTRACE))
> goto err_out;
> --
> 2.37.1.359.gd136c6c3e2-goog
>

--
Peter Xu

\
 
 \ /
  Last update: 2023-03-27 23:02    [W:0.068 / U:1.872 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site