Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp3445197ybf; Tue, 3 Mar 2020 06:12:58 -0800 (PST) X-Google-Smtp-Source: ADFU+vudJTbG8Z3tidawQjJWV6CxFXwR589B7v8jwRvscbtWAXaI89PX+mgNYZWfXQd0OJ8XxOLa X-Received: by 2002:aca:4843:: with SMTP id v64mr2466269oia.13.1583244778367; Tue, 03 Mar 2020 06:12:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583244778; cv=none; d=google.com; s=arc-20160816; b=BlFn3G3AKEKciEd4yNUCBPaeSR3AYhmjvqvHPJMpST4PU4ymxJRQf1F8h8teJBabuT g2v8gbxAOB9tFwPvLJ7Hs7j4oK4XyTKAUhNznltw566nUJ/gb0cLfno1UnFuIeQ0lyyo EIsBNrTqP6qToRJQeLQlJZPmxHyYi5MbMOxfZVa0QTMRv4YT4tzJc+SspgVRAP5nyl1a hkJDVaA78y1/0VUGdSesrrb9cSwkZnYX+4m2wDq+iL1JaBww2RhkSnkF1VAHEIMssBBB z7scqJhl4Epg08ZAfKdnor3f9wl6EV1cZHp9yf6Ef5Zxmz61vyIonlubS14f02HVEN/5 TFhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id; bh=gaG0WJsPhPo+ua0MCmaWaKCqmVfaBAeBFQktXsuhO7I=; b=Ldqf8MyX2WxrhtQaGqi2KxPPZLZEyJXMCZHHNkA9qSuHxIy6NRZacgTGajyf+CDr/i cQyLvlHm9X6eSg0vFdt6rEOF98wSogfiiF8e0MbhgccIUi/2gIBuCoXMLCn8zqozKbhs ddHusPBBNDIQb9eFfH9hGYbfJzxSc5Q1dNAEXhfM595/kCETHBN749NTGlNu4q9m/ehp p+tLqzOj3NVlWDBOhoZzJwdriMEvqsoBml2gfMaBCmF9FavonQ5AUuo23piaCeFl5PL0 YN9lDEx8AYr960Hyun/BHfHGQwzBzV5xWi4bVi1fYffoNLTNxhXbGLOfOt5DtooLcQah PbTg== 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 d10si8488693oti.226.2020.03.03.06.12.31; Tue, 03 Mar 2020 06:12:58 -0800 (PST) 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 S1729211AbgCCN62 (ORCPT + 99 others); Tue, 3 Mar 2020 08:58:28 -0500 Received: from smtprelay0155.hostedemail.com ([216.40.44.155]:58754 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725796AbgCCN62 (ORCPT ); Tue, 3 Mar 2020 08:58:28 -0500 Received: from filter.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay06.hostedemail.com (Postfix) with ESMTP id 866DE18225DF6; Tue, 3 Mar 2020 13:58:26 +0000 (UTC) X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 50,0,0,,d41d8cd98f00b204,joe@perches.com,,RULES_HIT:41:355:379:599:800:960:967:973:988:989:1260:1263:1277:1311:1313:1314:1345:1359:1437:1515:1516:1518:1535:1544:1593:1594:1605:1711:1730:1747:1777:1792:1801:2194:2198:2199:2200:2393:2525:2553:2560:2563:2682:2685:2692:2693:2828:2859:2918:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3622:3865:3866:3867:3868:3870:3871:3872:3873:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4117:4321:4470:4605:5007:6119:6691:7875:7903:7904:9025:9388:10004:10049:10848:11026:11232:11233:11473:11658:11914:12043:12295:12296:12297:12438:12555:12740:12760:12776:12895:13160:13161:13229:13439:14096:14097:14106:14107:14181:14659:14721:14849:21080:21220:21433:21611:21627:21740:21939:21990:30012:30051:30054:30090:30091,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:2,LUA_SUMMARY:none X-HE-Tag: yard67_83a1f2d77a221 X-Filterd-Recvd-Size: 6456 Received: from XPS-9350.home (unknown [47.151.143.254]) (Authenticated sender: joe@perches.com) by omf07.hostedemail.com (Postfix) with ESMTPA; Tue, 3 Mar 2020 13:58:24 +0000 (UTC) Message-ID: <58c1f1bf6b30bd5c39184cd9c09f25a9b9d67a68.camel@perches.com> Subject: Re: [PATCH v2 2/3] binder: do not initialize locals passed to copy_from_user() From: Joe Perches To: Dan Carpenter , Alexander Potapenko Cc: "open list:ANDROID DRIVERS" , Kees Cook , Jann Horn , Peter Zijlstra , Greg Kroah-Hartman , LKML , Arve =?ISO-8859-1?Q?Hj=F8nnev=E5g?= , Ingo Molnar , Dmitriy Vyukov , Todd Kjos Date: Tue, 03 Mar 2020 05:56:51 -0800 In-Reply-To: <20200303093832.GD24372@kadam> References: <20200302130430.201037-1-glider@google.com> <20200302130430.201037-2-glider@google.com> <0eaac427354844a4fcfb0d9843cf3024c6af21df.camel@perches.com> <4cac10d3e2c03e4f21f1104405a0a62a853efb4e.camel@perches.com> <18b0d6ea5619c34ca4120a6151103dbe9bfa0cbe.camel@perches.com> <20200303093832.GD24372@kadam> Content-Type: text/plain; charset="ISO-8859-1" User-Agent: Evolution 3.34.1-2 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 2020-03-03 at 12:38 +0300, Dan Carpenter wrote: > On Tue, Mar 03, 2020 at 10:14:18AM +0100, Alexander Potapenko wrote: > > On Mon, Mar 2, 2020 at 7:51 PM Joe Perches wrote: > > > On Mon, 2020-03-02 at 19:17 +0100, Alexander Potapenko wrote: > > > > On Mon, Mar 2, 2020 at 3:00 PM Joe Perches wrote: > > > > > On Mon, 2020-03-02 at 14:25 +0100, Alexander Potapenko wrote: > > > > > > On Mon, Mar 2, 2020 at 2:11 PM Joe Perches wrote: > > > > > > > On Mon, 2020-03-02 at 14:04 +0100, glider@google.com wrote: > > > > > > > > Certain copy_from_user() invocations in binder.c are known to > > > > > > > > unconditionally initialize locals before their first use, like e.g. in > > > > > > > > the following case: > > > > > > > [] > > > > > > > > diff --git a/drivers/android/binder.c b/drivers/android/binder.c > > > > > > > [] > > > > > > > > @@ -3788,7 +3788,7 @@ static int binder_thread_write(struct binder_proc *proc, > > > > > > > > > > > > > > > > case BC_TRANSACTION_SG: > > > > > > > > case BC_REPLY_SG: { > > > > > > > > - struct binder_transaction_data_sg tr; > > > > > > > > + struct binder_transaction_data_sg tr __no_initialize; > > > > > > > > > > > > > > > > if (copy_from_user(&tr, ptr, sizeof(tr))) > > > > > > > > > > > > > > I fail to see any value in marking tr with __no_initialize > > > > > > > when it's immediately written to by copy_from_user. > > > > > > > > > > > > This is being done exactly because it's immediately written to by copy_to_user() > > > > > > Clang is currently unable to figure out that copy_to_user() initializes memory. > > > > > > So building the kernel with CONFIG_INIT_STACK_ALL=y basically leads to > > > > > > the following code: > > > > > > > > > > > > struct binder_transaction_data_sg tr; > > > > > > memset(&tr, 0xAA, sizeof(tr)); > > > > > > if (copy_from_user(&tr, ptr, sizeof(tr))) {...} > > > > > > > > > > > > This unnecessarily slows the code down, so we add __no_initialize to > > > > > > prevent the compiler from emitting the redundant initialization. > > > > > > > > > > So? CONFIG_INIT_STACK_ALL by design slows down code. > > > > Correct. > > > > > > > > > This marking would likely need to be done for nearly all > > > > > 3000+ copy_from_user entries. > > > > Unfortunately, yes. I was just hoping to do so for a handful of hot > > > > cases that we encounter, but in the long-term a compiler solution must > > > > supersede them. > > > > > > > > > Why not try to get something done on the compiler side > > > > > to mark the function itself rather than the uses? > > > > This is being worked on in the meantime as well (see > > > > http://lists.llvm.org/pipermail/cfe-dev/2020-February/064633.html) > > > > Do you have any particular requisitions about how this should look on > > > > the source level? > > > > > > I presume something like the below when appropriate for > > > automatic variables when not already initialized or modified. > > > --- [] > > > diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h [] > > > @@ -138,7 +138,8 @@ _copy_to_user(void __user *, const void *, unsigned long); > > > #endif > > > > > > static __always_inline unsigned long __must_check > > > -copy_from_user(void *to, const void __user *from, unsigned long n) > > > +copy_from_user(void __no_initialize *to, const void __user *from, > > > + unsigned long n) > > > > Shall this __no_initialize attribute denote that the whole object > > passed to it is initialized? My presumption is the compiler could determine that only if the accessed variable is a local automatic, it does not need to be initialized. > > Or do we need to encode the length as well, as Jann suggests? I think not. > > It's also interesting what should happen if *to is pointing _inside_ a > > local object - presumably it's unsafe to disable initialization for > > the whole object. Are you asking if for example: struct foo { ...; }; struct bar { struct foo a; ...; }; void func(void) { struct bar b; ...; copy_from_user(&b.a, baz, len); ...; } that the containing struct b would not be initialized? I presume a compiler would initialized all of b, but if it manages to initialize all of b but b.a, good on the compiler writer. > The real fix is to initialize everything manually, the automated > initialization is a hardenning feature which many people will disable. > So I don't think the hardenning needs to be perfect, it needs to simple > and fast. Dan, perhaps I don't understand you. Can you clarify what you mean?