Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp344585ybx; Fri, 1 Nov 2019 04:31:04 -0700 (PDT) X-Google-Smtp-Source: APXvYqxMwIDaAgs3SOtlwm+SBNwjM4Bl4G7YtZEgTCAALyy9dGpDebH+MFtYJIYKO1eT0GgMkmsa X-Received: by 2002:a17:906:f249:: with SMTP id gy9mr8903022ejb.216.1572607864707; Fri, 01 Nov 2019 04:31:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572607864; cv=none; d=google.com; s=arc-20160816; b=hDd7P8++fJpP9DbyZKGdlR53QNYuKrChSbnFHAAzZ9wKxc/LYcVQ1Lm4nt1eyS6wRI B2cuvbyWqGLyLX8vXswiVxLVxCgZBlcHsPVR8kVqY4BoMZm7KSsmM2bH48giqBXpww3O HugVkZdux+TX+obG0/fYPKc0wpMHFDly8d1d6QynXnqoJx/UBqJ5r1+0y62Js7xKtlYv +OsW68/qjg9q+AypqO+dwkWbOKYdELrNGvU9ugiAPixqSjzzRBq7JfXS52zzbXHHbVf3 qvEhfpLiBf1tseqynCeLb1hjsFAywleXqI4i62rY5qTUSwU7uoG8IF9CCZAyyll8WHZV GQ6w== 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=oXHTjaipYmIhhMlB8nkYMn4mgvo9sK+LjeQgFeXExz4=; b=F97L+HH/4NZLN6qrm584amZ9IAL9zk+k+bjeEqy2f9qVvqdJ1ckwauK13azUHr+dcQ y+48OY8UwzkujLSyjAn0uKKB/mH3u+qvubiQwHNNCL4ZZXRAdIsuh3/E0Hnduh+MVGtF CW2BLqbEsa4fasjH45GwRmAuapJYru8LWQXUEVwwZu7UozwcaefhUFqYa3vd0cvIsbYs QegundjuJ6MOojvzdpMqOXZyQlkkIHb2gRgKJauWmlm5+OphrDSxhf/qrHa5ERvfYeio W+ZCDE4sE5Xe/tLoBzNuwQEtMzvICNaimVmiGMY8PwOATOZlXrEFgSxPEWdxxLG1Sk8y bCVQ== 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 j15si5736393ejc.369.2019.11.01.04.30.41; Fri, 01 Nov 2019 04:31:04 -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 S1730278AbfKALGo (ORCPT + 99 others); Fri, 1 Nov 2019 07:06:44 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:54392 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727561AbfKALGn (ORCPT ); Fri, 1 Nov 2019 07:06:43 -0400 Received: from [91.217.168.176] (helo=wittgenstein) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1iQUlN-0007Zu-7r; Fri, 01 Nov 2019 11:06:41 +0000 Date: Fri, 1 Nov 2019 12:06:40 +0100 From: Christian Brauner To: Oleg Nesterov Cc: linux-kernel@vger.kernel.org, Florian Weimer , GNU C Library , Arnd Bergmann , Kees Cook , Jann Horn , David Howells , Ingo Molnar , Linus Torvalds , Peter Zijlstra , linux-api@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH] clone3: validate stack arguments Message-ID: <20191101110639.icbfihw3fk2nzz4o@wittgenstein> References: <20191031113608.20713-1-christian.brauner@ubuntu.com> <20191031164653.GA24629@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20191031164653.GA24629@redhat.com> User-Agent: NeoMutt/20180716 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 31, 2019 at 05:46:53PM +0100, Oleg Nesterov wrote: > On 10/31, Christian Brauner wrote: > > > > --- a/include/uapi/linux/sched.h > > +++ b/include/uapi/linux/sched.h > > @@ -51,6 +51,10 @@ > > * sent when the child exits. > > * @stack: Specify the location of the stack for the > > * child process. > > + * Note, @stack is expected to point to the > > + * lowest address. The stack direction will be > > + * determined by the kernel and set up > > + * appropriately based on @stack_size. > > I can't review this patch, I have no idea what does stack_size mean > if !arch/x86. In short: nothing at all if it weren't for ia64 (and maybe parisc). But let me provide some (hopefully useful) context. (Probably most of that is well-know, so sorry for superflous info. :)) The stack and stack_size argument are used in copy_thread_tls() and in copy_thread(). What the arch will end up calling depends on CONFIG_HAVE_COPY_THREAD. Afaict, mips, powerpc, s390, and x86 call copy_thread_tls(). The other arches call copy_thread(). On all arches _except_ IA64 copy_thread{_tls}() just assigns "stack" to the right register and is done with it. On all arches _except_ parisc "stack" needs to point to the highest address. On parisc it needs to point to the lowest (CONFIG_STACK_GROWSUP). IA64 has a downwards growing stack like all the other architectures but it expects "stack" to poin to the _lowest_ address nonetheless. In contrast to all the other arches it does: child_ptregs->r12 = user_stack_base + user_stack_size - 16; so ia64 sets up the stack pointer itself. So now we have: parisc -> upwards growing stack, stack_size _unused_ for user stacks !parisc -> downwards growing stack, stack_size _unused_ for user stacks ia64 -> downwards growing stack, stack_size _used_ for user stacks Now it gets more confusing since the clone() syscall layout is arch dependent as well. Let's ignore the case of arches that have a clone syscall version with switched flags and stack argument and only focus on arches with an _additional_ stack_size argument: microblaze -> clone(stack, stack_size) Then there's clone2() for ia64 which is a _separate_ syscall with an additional stack_size argument: ia64 -> clone2(stack, stack_size) Now, contrary to what you'd expect, microblaze ignores the stack_size argument. So the stack_size argument _would_ be completely meaningless if it weren't for ia64 and parisc. > > x86 doesn't use stack_size unless a kthread does kernel_thread(), so > this change is probably fine... > > Hmm. Off-topic question, why did 7f192e3cd3 ("fork: add clone3") add > "& ~CSIGNAL" in kernel_thread() ? This looks pointless and confusing > to me... (Can we discuss this over a patch that removes this restriction if we think this is pointless?) > > > +static inline bool clone3_stack_valid(struct kernel_clone_args *kargs) > > +{ > > + if (kargs->stack == 0) { > > + if (kargs->stack_size > 0) > > + return false; > > + } else { > > + if (kargs->stack_size == 0) > > + return false; > > So to implement clone3_wrapper(void *bottom_of_stack) you need to do > > clone3_wrapper(void *bottom_of_stack) > { > struct clone_args args = { > ... > // make clone3_stack_valid() happy > .stack = bottom_of_stack - 1, > .stack_size = 1, > }; > } > > looks a bit strange. OK, I agree, this example is very artificial. > But why do you think clone3() should nack stack_size == 0 ? In short, consistency. I think prior clone() versions (on accident) have exposed the stack direction as an implementation detail to userspace. Userspace clone() code wrapping code is _wild_ and buggy partially because of that. The best thing imho, is to clearly communicate to userspace that stack needs to point to the lowest address and stack_size to the initial range of the stack pointer or both are 0. The alternative is to let userspace either give us a stack pointer that we expect to be setup correctly by userspace or a stack pointer to the lowest address and a stack_size argument. That's just an invitation for more confusion and we have proof with legacy clone that this is not a good idea. > > > + if (!access_ok((void __user *)kargs->stack, kargs->stack_size)) > > + return false; > > Why? It's nice of us to tell userspace _before_ we have created a thread that it messed up its parameters instead of starting a thread that then immediately crashes. Christian