From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BAE9BC32792 for ; Tue, 1 Oct 2019 01:58:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7BA562087F for ; Tue, 1 Oct 2019 01:58:43 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="CQkP+Pn4" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729202AbfJAB6n (ORCPT ); Mon, 30 Sep 2019 21:58:43 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:38730 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727810AbfJAB6m (ORCPT ); Mon, 30 Sep 2019 21:58:42 -0400 Received: by mail-pf1-f196.google.com with SMTP id h195so6765938pfe.5 for ; Mon, 30 Sep 2019 18:58:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=V13OWGdOgQZc0p8jcFcksNKGAg1h1vgIDhJUeYe6EjA=; b=CQkP+Pn4SrN43elxLPl3lRSSih8N/K+RJ8mCbFGae73ZgNBdriOIuOQVTRwHPqoBaw 2uSWTdQXM4T1vmiSq2j/950XSKLhl7SlHbdYlo8JF2bxpSy6vFSvqeRy94C2Q2QpI4N1 fLdAvgmAXRFaGfry1lmfc1QcesO48+B6XIk6A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=V13OWGdOgQZc0p8jcFcksNKGAg1h1vgIDhJUeYe6EjA=; b=qULg7S20t9w6pytr4e+Jk5VMRzstPxXMB8NCX+QmqekdLM9PsDhlEar2ueYpdrvVxT q5tt2gmNPxKqweUkfvypKrL+8Z+LwSxOS9RE9ET1OW/s7h1442M/L4RkxOZIpSJZrQyy 3qcF0H3K6CMXbYjbVzvY6GuFl2RXf2c+kB2o7zpT1j6u+par9LH7GL5sg0UG0YbFF0AC 2XUDpsV6T07t6mKOR9ngkqHipWwQpYPBzAy6TLAZNmKcsDiK7v6suAoHVS+Z0VsHD4h3 DaBe6rskiQVzEN47OrpObobgfQU2YhNOjf8tgBkXwHeFA7BXHRj4HOx3hEWfGbK8uKWo sLeg== X-Gm-Message-State: APjAAAXaCE79oPA0StLbxCeQcQLLbp9yX1upDcJh/urYJmlKmwOUV0dy 19OOJNVJxnrWXm6o2PK+pnxv3w== X-Google-Smtp-Source: APXvYqwfQ7ZP6YE7B7tN65nhMUqu79o93n2Y6H0GpdorOuxDSEdq01lxwsgs9lqlYCV4LblozdDPcw== X-Received: by 2002:a62:76d1:: with SMTP id r200mr25943545pfc.27.1569895121675; Mon, 30 Sep 2019 18:58:41 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id l12sm19539490pgs.44.2019.09.30.18.58.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Sep 2019 18:58:40 -0700 (PDT) Date: Mon, 30 Sep 2019 18:58:39 -0700 From: Kees Cook To: Aleksa Sarai Cc: Ingo Molnar , Peter Zijlstra , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Christian Brauner , Rasmus Villemoes , Al Viro , Linus Torvalds , libc-alpha@sourceware.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v4 1/4] lib: introduce copy_struct_from_user() helper Message-ID: <201909301856.01255535BD@keescook> References: <20191001011055.19283-1-cyphar@cyphar.com> <20191001011055.19283-2-cyphar@cyphar.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20191001011055.19283-2-cyphar@cyphar.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 01, 2019 at 11:10:52AM +1000, 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(). > > Some in-kernel selftests that insure that the handling of alignment and > various byte patterns are all handled identically to memchr_inv() usage. > > [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/bitops.h | 7 +++ > include/linux/uaccess.h | 70 +++++++++++++++++++++ > lib/strnlen_user.c | 8 +-- > lib/test_user_copy.c | 136 ++++++++++++++++++++++++++++++++++++++-- > lib/usercopy.c | 55 ++++++++++++++++ > 5 files changed, 263 insertions(+), 13 deletions(-) > > diff --git a/include/linux/bitops.h b/include/linux/bitops.h > index cf074bce3eb3..c94a9ff9f082 100644 > --- a/include/linux/bitops.h > +++ b/include/linux/bitops.h > @@ -4,6 +4,13 @@ > #include > #include > > +/* Set bits in the first 'n' bytes when loaded from memory */ > +#ifdef __LITTLE_ENDIAN > +# define aligned_byte_mask(n) ((1UL << 8*(n))-1) > +#else > +# define aligned_byte_mask(n) (~0xffUL << (BITS_PER_LONG - 8 - 8*(n))) > +#endif > + > #define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE) > #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(long)) > > diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h > index 70bbdc38dc37..8abbc713f7fb 100644 > --- a/include/linux/uaccess.h > +++ b/include/linux/uaccess.h > @@ -231,6 +231,76 @@ __copy_from_user_inatomic_nocache(void *to, const void __user *from, > > #endif /* ARCH_HAS_NOCACHE_UACCESS */ > > +extern int check_zeroed_user(const void __user *from, size_t size); > + > +/** > + * 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 = {}; > + * > + * if (usize > PAGE_SIZE) > + * return -E2BIG; > + * if (usize < FOO_SIZE_VER0) > + * return -EINVAL; > + * > + * err = copy_struct_from_user(&karg, sizeof(karg), uarg, usize); > + * 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. > + */ > +static __always_inline > +int copy_struct_from_user(void *dst, size_t ksize, > + const void __user *src, size_t usize) And of course I forgot to realize both this and check_zeroed_user() should also have the __must_check attribute. Sorry for forgetting that earlier! With that, please consider it: Reviewed-by: Kees Cook Thanks for working on this! -- Kees Cook