Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp1826985ybn; Thu, 26 Sep 2019 02:50:32 -0700 (PDT) X-Google-Smtp-Source: APXvYqw6Q/f3sSbeGpV54UyZgupKLyUc/a6GnuIQnW1ZHjcAkhWwuhSR8BATsR5N1nbBs/pmpqg0 X-Received: by 2002:a50:f00c:: with SMTP id r12mr2586972edl.274.1569491432725; Thu, 26 Sep 2019 02:50:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569491432; cv=none; d=google.com; s=arc-20160816; b=tR6k7RTcFqcrXxRtEiCQ9YtO/2mzAaZHlaC3vJSZy/+JIK+b14a3bqX6VpJA5CBizt HJpwFxvPa2c4KDosG2tZ6hMzS3xBKYEXMzWrGTp3SXH3qX7r0jEPdFyd+cR5zQKZC/C/ dTPXuz3AVB88sG48nmxnMmuzZ8+Bi/gpfGN8zpsUI8emm29bOd5dFTi6+nWyZdagzG4l pmQZm2AroNuhgIEULQquZLVWFvZy30t3cbH1UuquFVtFA3siaIfyICPuNqqOKz9Hyr8K aeOwdggwKAjHT84E1w5PG29r63HHwKUcJmnMwOYheZGojo0u+Y7Ez6EeFTVRX6jxKFeQ 2Yrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=Dc/VYSfOQpGBJ5Q8wI1uoVbyeZw/wizPcp/1c2OzT0I=; b=LVKriJ0nmp6OI7yc1j9eUrG6ss9FcjMFU941+Tbv8UlgcAJG9cbY2Cvjgc/1vqtJcj QygBxgvYE01nOBXvHvwUGEFEaiB7BmoPP2qZ+z5cSkzj68tcQcZcaDLBHFb62NsJamtT nIp8vexPPClezWV7adgUfDkXTphNTqQ1BvEWFJTRJMjWKyPMWRovdYSrIRvGI2dnfoqW 9VQX2ocVz9cogc0ajPh9FVTXpzE3hUNTx8tzROhQ+zyQv9viOpj3ul1EFyosn7AqKJO6 iIbyv+UonCjARNMFO84SusKJy11kVibBy1/g6k0X4zXYaGPwrM0ouwHGEpHNYbDtK4GK J0Dg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h19si902302edv.393.2019.09.26.02.50.09; Thu, 26 Sep 2019 02:50:32 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2442941AbfIYRUR (ORCPT + 99 others); Wed, 25 Sep 2019 13:20:17 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:50917 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2442934AbfIYRUR (ORCPT ); Wed, 25 Sep 2019 13:20:17 -0400 Received: from [185.81.138.21] (helo=wittgenstein) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1iDAxP-0007bi-4j; Wed, 25 Sep 2019 17:20:03 +0000 Date: Wed, 25 Sep 2019 19:20:01 +0200 From: Christian Brauner To: Aleksa Sarai Cc: Ingo Molnar , Peter Zijlstra , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Rasmus Villemoes , Al Viro , Linus Torvalds , libc-alpha@sourceware.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v1 1/4] lib: introduce copy_struct_from_user() helper Message-ID: <20190925172000.5lgfi4vdkol5rlhs@wittgenstein> References: <20190925165915.8135-1-cyphar@cyphar.com> <20190925165915.8135-2-cyphar@cyphar.com> <20190925171810.ajfx4zlmj5scct4m@wittgenstein> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20190925171810.ajfx4zlmj5scct4m@wittgenstein> User-Agent: NeoMutt/20180716 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Sep 25, 2019 at 07:18:11PM +0200, Christian Brauner wrote: > On Wed, Sep 25, 2019 at 06:59:12PM +0200, Aleksa Sarai wrote: > > A common pattern for syscall extensions is increasing the size of a > > struct passed from userspace, such that the zero-value of the new fields > > result in the old kernel behaviour (allowing for a mix of userspace and > > kernel vintages to operate on one another in most cases). > > > > While this interface exists for communication in both directions, only > > one interface is straightforward to have reasonable semantics for > > (userspace passing a struct to the kernel). For kernel returns to > > userspace, what the correct semantics are (whether there should be an > > error if userspace is unaware of a new extension) is very > > syscall-dependent and thus probably cannot be unified between syscalls > > (a good example of this problem is [1]). > > > > Previously there was no common lib/ function that implemented > > the necessary extension-checking semantics (and different syscalls > > implemented them slightly differently or incompletely[2]). Future > > patches replace common uses of this pattern to make use of > > copy_struct_from_user(). > > > > [1]: commit 1251201c0d34 ("sched/core: Fix uclamp ABI bug, clean up and > > robustify sched_read_attr() ABI logic and code") > > > > [2]: For instance {sched_setattr,perf_event_open,clone3}(2) all do do > > similar checks to copy_struct_from_user() while rt_sigprocmask(2) > > always rejects differently-sized struct arguments. > > > > Suggested-by: Rasmus Villemoes > > Signed-off-by: Aleksa Sarai > > --- > > include/linux/uaccess.h | 4 +++ > > lib/Makefile | 2 +- > > lib/strnlen_user.c | 52 +++++++++++++++++++++++++++++ > > lib/struct_user.c | 73 +++++++++++++++++++++++++++++++++++++++++ > > 4 files changed, 130 insertions(+), 1 deletion(-) > > create mode 100644 lib/struct_user.c > > Hm, why the new file? > Couldn't this just live in usercopy.c? > > > > > diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h > > index 34a038563d97..824569e309e4 100644 > > --- a/include/linux/uaccess.h > > +++ b/include/linux/uaccess.h > > @@ -230,6 +230,10 @@ static inline unsigned long __copy_from_user_inatomic_nocache(void *to, > > > > #endif /* ARCH_HAS_NOCACHE_UACCESS */ > > > > +extern int is_zeroed_user(const void __user *from, size_t count); > > +extern int copy_struct_from_user(void *dst, size_t ksize, > > + const void __user *src, size_t usize); > > + > > /* > > * probe_kernel_read(): safely attempt to read from a location > > * @dst: pointer to the buffer that shall take the data > > diff --git a/lib/Makefile b/lib/Makefile > > index 29c02a924973..d86c71feaf0a 100644 > > --- a/lib/Makefile > > +++ b/lib/Makefile > > @@ -28,7 +28,7 @@ endif > > CFLAGS_string.o := $(call cc-option, -fno-stack-protector) > > endif > > > > -lib-y := ctype.o string.o vsprintf.o cmdline.o \ > > +lib-y := ctype.o string.o struct_user.o vsprintf.o cmdline.o \ > > rbtree.o radix-tree.o timerqueue.o xarray.o \ > > idr.o extable.o \ > > sha1.o chacha.o irq_regs.o argv_split.o \ > > diff --git a/lib/strnlen_user.c b/lib/strnlen_user.c > > index 7f2db3fe311f..7eb665732954 100644 > > --- a/lib/strnlen_user.c > > +++ b/lib/strnlen_user.c > > @@ -123,3 +123,55 @@ long strnlen_user(const char __user *str, long count) > > return 0; > > } > > EXPORT_SYMBOL(strnlen_user); > > + > > +/** > > + * is_zeroed_user: check if a userspace buffer is full of zeros > > + * @from: Source address, in userspace. > > + * @size: Size of buffer. > > + * > > + * This is effectively shorthand for "memchr_inv(from, 0, size) == NULL" for > > + * userspace addresses. If there are non-zero bytes present then false is > > + * returned, otherwise true is returned. > > + * > > + * Returns: > > + * * -EFAULT: access to userspace failed. > > + */ > > +int is_zeroed_user(const void __user *from, size_t size) > > *sigh*, I'm probably going to get yelled at because of this but: does > this really provide any _performance_ benefits over the dumb get_user() > loop that we currently have that we care about right now? My point > being, that the loop - imho - is much easier to understand than what is > going on here with all the masking, and aligning etc. that we have here. > But I'm not going to fight it. > > > +{ > > + u64 val; > > + uintptr_t align = (uintptr_t) from % 8; > > + > > + if (unlikely(!size)) > > + return true; > > Nit: I'd prefer int variables be checked with if (size != 0) :) > > > + > > + from -= align; > > + size += align; > > + > > + if (!user_access_begin(from, size)) > > + return -EFAULT; > > + > > + while (size >= 8) { > > + unsafe_get_user(val, (u64 __user *) from, err_fault); > > + if (align) { > > + /* @from is unaligned. */ > > + val &= ~aligned_byte_mask(align); > > + align = 0; > > + } > > + if (val) > > + goto done; > > + from += 8; > > + size -= 8; > > + } > > + if (size) { > > + /* (@from + @size) is unaligned. */ > > + unsafe_get_user(val, (u64 __user *) from, err_fault); > > + val &= aligned_byte_mask(size); > > + } > > + > > +done: > > + user_access_end(); > > + return (val == 0); > > +err_fault: > > + user_access_end(); > > + return -EFAULT; > > +} > > diff --git a/lib/struct_user.c b/lib/struct_user.c > > new file mode 100644 > > index 000000000000..57d79eb53bfa > > --- /dev/null > > +++ b/lib/struct_user.c > > @@ -0,0 +1,73 @@ > > +// SPDX-License-Identifier: GPL-2.0-or-later > > +/* > > + * Copyright (C) 2019 SUSE LLC > > + * Copyright (C) 2019 Aleksa Sarai > > + */ > > + > > +#include > > +#include > > +#include > > +#include > > +#include > > + > > +/** > > + * copy_struct_from_user: copy a struct from userspace > > + * @dst: Destination address, in kernel space. This buffer must be @ksize > > + * bytes long. > > + * @ksize: Size of @dst struct. > > + * @src: Source address, in userspace. > > + * @usize: (Alleged) size of @src struct. > > + * > > + * Copies a struct from userspace to kernel space, in a way that guarantees > > + * backwards-compatibility for struct syscall arguments (as long as future > > + * struct extensions are made such that all new fields are *appended* to the > > + * old struct, and zeroed-out new fields have the same meaning as the old > > + * struct). > > + * > > + * @ksize is just sizeof(*dst), and @usize should've been passed by userspace. > > + * The recommended usage is something like the following: > > + * > > + * SYSCALL_DEFINE2(foobar, const struct foo __user *, uarg, size_t, usize) > > + * { > > + * int err; > > + * struct foo karg = {}; > > + * > > + * err = copy_struct_from_user(&karg, sizeof(karg), uarg, size); > > + * if (err) > > + * return err; > > + * > > + * // ... > > + * } > > + * > > + * There are three cases to consider: > > + * * If @usize == @ksize, then it's copied verbatim. > > + * * If @usize < @ksize, then the userspace has passed an old struct to a > > + * newer kernel. The rest of the trailing bytes in @dst (@ksize - @usize) > > + * are to be zero-filled. > > + * * If @usize > @ksize, then the userspace has passed a new struct to an > > + * older kernel. The trailing bytes unknown to the kernel (@usize - @ksize) > > + * are checked to ensure they are zeroed, otherwise -E2BIG is returned. > > + * > > + * Returns (in all cases, some data may have been copied): > > + * * -E2BIG: (@usize > @ksize) and there are non-zero trailing bytes in @src. > > + * * -EFAULT: access to userspace failed. > > + */ > > +int copy_struct_from_user(void *dst, size_t ksize, > > + const void __user *src, size_t usize) Hm, and should that get tests in test_usercopy.c? Christian