Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp422739ybx; Fri, 1 Nov 2019 05:43:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqys+HPvZZEpotwTAeHzcVdnosDUiotej4NfuFYCwaSoVh6y36uxzC3LryThNjAiFIz2k8B9 X-Received: by 2002:a17:906:e20d:: with SMTP id gf13mr9727382ejb.176.1572612222475; Fri, 01 Nov 2019 05:43:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572612222; cv=none; d=google.com; s=arc-20160816; b=KzfMzPwi7iQ+vFUjGVlbiU5ij6o7GOBV8oAR+jIvIOUaNozdFQ44YQCPn92PF4TjSd NduGah6jPcknWO6AYjxpmlQLPWBw3iLYFnzEdSB5ptDVAy5xQ3tGdVoX+MAD9ZPCMCBo 3mMkHb82IhPZod3UO8IooEo/6FMJW78TyqDzKuE6TSaWMlznw999N+HMrr1AV/AfuSpB 7t3wYHPskFi4+UJ3RWtl2+XK5fzsBVGxhloqx/3Zn4+kJMoALlLg5JWyU6D134swn5j0 VCTaj2FSEQ3bypI5WejdPyqVl0qIBjOjC2WbPXCcGteN2Nx33JE0nUjQJkR4l2vH9Wga KltA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:user-agent:in-reply-to:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=Blgz1QDlbrX4CFcoZXAgNrnij3Ts9PT2qjsZuUJOKd4=; b=K5LhD0dMRCzZJHsdatOAMoSt1aBIHQQxkZI1OGKoRkinjX10y+K8IDiv/S06D2er1u yrOAAjpE1tLCafaHyE/p6jkjovQp5K7AD1kOfOKbsOrA9M7dRe8is/bLbbV353AAUMK/ 4NSFPT3/VWSSKvMIM+uvaAhHbWtHQOjIT94IPG9kBsUhPaMKzov6q+zafL2ZPO0QRPfZ wSafOrx2NBzFYnLS6wCEfPwIo9ZQsjiZczAezMUVgna9y10khtl2ecM2tOhZkLmd7AXo /e6fqJdmx0FDu5NC4adqdzUBHgZKfXS13bbaTSv7BOeg01TmSgTPT07FbBPv/4aPzzY7 H2Jw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MRvHRHn8; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y11si5684914ejw.201.2019.11.01.05.43.18; Fri, 01 Nov 2019 05:43:42 -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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MRvHRHn8; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729852AbfKAMef (ORCPT + 99 others); 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.