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=-2.4 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_SANE_1 autolearn=no 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 86ABACA9ECF for ; Fri, 1 Nov 2019 12:34:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 51BA52067D for ; Fri, 1 Nov 2019 12:34:36 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="MRvHRHn8" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729960AbfKAMef (ORCPT ); Fri, 1 Nov 2019 08:34:35 -0400 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:49890 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726229AbfKAMef (ORCPT ); Fri, 1 Nov 2019 08:34:35 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1572611673; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Blgz1QDlbrX4CFcoZXAgNrnij3Ts9PT2qjsZuUJOKd4=; b=MRvHRHn8eBtStDNvADnO8LyqVs1hHvYfshdBusObc4qkB6WwppDdZSkAZJtSkKGrBldq7u muIdljDhcO/5sixyWSK+E+G2KP4QKnVEWWlM1quNYt1zKfkSPn50k3ptP8Iqn00RDC1LS/ qCgdG37LO3M/97jY6dA2KkqMe3H1+9k= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-155-QADGKEzYOKOPHVxH7fQt-A-1; Fri, 01 Nov 2019 08:33:05 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 86BE9800683; Fri, 1 Nov 2019 12:33:02 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.43.17.44]) by smtp.corp.redhat.com (Postfix) with SMTP id 113F75D9CD; Fri, 1 Nov 2019 12:32:59 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Fri, 1 Nov 2019 13:33:00 +0100 (CET) Date: Fri, 1 Nov 2019 13:32:57 +0100 From: Oleg Nesterov To: Christian Brauner 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: <20191101123257.GA508@redhat.com> References: <20191031113608.20713-1-christian.brauner@ubuntu.com> <20191031164653.GA24629@redhat.com> <20191101110639.icbfihw3fk2nzz4o@wittgenstein> MIME-Version: 1.0 In-Reply-To: <20191101110639.icbfihw3fk2nzz4o@wittgenstein> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: QADGKEzYOKOPHVxH7fQt-A-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=WINDOWS-1252 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/01, Christian Brauner wrote: > > 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. Thanks... > (Probably most of > that is well-know, Certainly not to me ;) Thanks. > > > +static inline bool clone3_stack_valid(struct kernel_clone_args *karg= s) > > > +{ > > > +=09if (kargs->stack =3D=3D 0) { > > > +=09=09if (kargs->stack_size > 0) > > > +=09=09=09return false; > > > +=09} else { > > > +=09=09if (kargs->stack_size =3D=3D 0) > > > +=09=09=09return false; > > > > So to implement clone3_wrapper(void *bottom_of_stack) you need to do > > > > =09clone3_wrapper(void *bottom_of_stack) > > =09{ > > =09=09struct clone_args args =3D { > > =09=09=09... > > =09=09=09// make clone3_stack_valid() happy > > =09=09=09.stack =3D bottom_of_stack - 1, > > =09=09=09.stack_size =3D 1, > > =09=09}; > > =09} > > > > looks a bit strange. OK, I agree, this example is very artificial. > > But why do you think clone3() should nack stack_size =3D=3D 0 ? > > In short, consistency. And in my opinion this stack_size =3D=3D 0 check destroys the consistency, see below. But just in case, let me say that overall I personally like this change. > 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 Agreed. But the kernel can't verify that "stack" actually points to the lowest address and stack_size is actually the stack size. Consider another artificial =09clone3_wrapper(void *bottom_of_stack, unsigned long offs) =09{ =09=09struct clone_args args =3D { =09=09=09... =09=09=09// make clone3_stack_valid() happy =09=09=09.stack =3D bottom_of_stack - offs, =09=09=09.stack_size =3D offs, =09=09}; =09=09sys_clone3(args); =09} =09 Now, =09clone3_wrapper(bottom_of_stack, offs); is same thing for _any_ offs except offs =3D=3D 0 will fail. Why? To me thi= s is not consistent, I think the "stack_size =3D=3D 0" check buys nothing and only adds some confusion. Say, stack_size =3D=3D 1 is "obviously wrong" too, this certainly means tha= t "stack" doesn't point to the lowest address (or the child will corrupt the memory), but it works. OK, I won't insist. Perhaps it can help to detect the case when a user forgets to pass the correct stack size. > > > +=09=09if (!access_ok((void __user *)kargs->stack, kargs->stack_size)= ) > > > +=09=09=09return 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. Heh. Then why this code doesn't verify that at least stack + stack_size is properly mmaped with PROT_READ|WRITE? Oleg.