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=-5.4 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY,USER_AGENT_SANE_1 autolearn=unavailable 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 B65EFC3F2CD for ; Tue, 3 Mar 2020 09:39:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 80B95214DB for ; Tue, 3 Mar 2020 09:39:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="LRDv2LFk" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728206AbgCCJjM (ORCPT ); Tue, 3 Mar 2020 04:39:12 -0500 Received: from aserp2120.oracle.com ([141.146.126.78]:60048 "EHLO aserp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727791AbgCCJjL (ORCPT ); Tue, 3 Mar 2020 04:39:11 -0500 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 0239Y00N124318; Tue, 3 Mar 2020 09:38:53 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=date : from : to : cc : subject : message-id : references : mime-version : content-type : in-reply-to; s=corp-2020-01-29; bh=MSC4Wtn4COvNsRCEoK0KFvQBDjOBfSLVQcEKZQ8ZTdg=; b=LRDv2LFkdT7WCfr+9xjf73IV/JlTN3jptGTipndciOQdpCVMP7EWMeE9e4b88WUmJeyk R/Zwo9JQkFXNZHjQhqMNwG93G1vIBc4ktNcpGGE5ktgN6Wm0BI0OhWOnd66z7MW1bCeE 5Vv3pd+ZgATHDhU9h25gMWfU6SCAFq9S6xnYYZX1qRJsHpPUQJeKRb6QMXOn2gqN1yKG sC+p5vKAv2iq56Z399oxI2nnU92nxWcDWFipLJsdO3Z77GaHv8W3f5UN4D1yVgRwiEKR HhsBVUBpobfztgQwQZYZxQiNtqGxH/xnPgjvW6QBPXpvCrveJxBiB84SSdim0ospP/Oh TQ== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by aserp2120.oracle.com with ESMTP id 2yffwqnrcg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 03 Mar 2020 09:38:53 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 0239WZvP182796; Tue, 3 Mar 2020 09:38:52 GMT Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by userp3030.oracle.com with ESMTP id 2yg1eka3kp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 03 Mar 2020 09:38:52 +0000 Received: from abhmp0006.oracle.com (abhmp0006.oracle.com [141.146.116.12]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id 0239cjrk013071; Tue, 3 Mar 2020 09:38:45 GMT Received: from kadam (/129.205.23.165) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 03 Mar 2020 01:38:44 -0800 Date: Tue, 3 Mar 2020 12:38:32 +0300 From: Dan Carpenter To: Alexander Potapenko Cc: Joe Perches , "open list:ANDROID DRIVERS" , Kees Cook , Jann Horn , Peter Zijlstra , Greg Kroah-Hartman , LKML , Arve =?iso-8859-1?B?SGr4bm5lduVn?= , Ingo Molnar , Dmitriy Vyukov , Todd Kjos Subject: Re: [PATCH v2 2/3] binder: do not initialize locals passed to copy_from_user() Message-ID: <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> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9548 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 suspectscore=0 spamscore=0 mlxlogscore=999 malwarescore=0 bulkscore=0 mlxscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2001150001 definitions=main-2003030074 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9548 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 lowpriorityscore=0 spamscore=0 impostorscore=0 malwarescore=0 mlxlogscore=999 mlxscore=0 suspectscore=0 phishscore=0 clxscore=1015 bulkscore=0 adultscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2001150001 definitions=main-2003030074 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. > > --- > > include/linux/uaccess.h | 3 ++- > > 1 file changed, 2 insertions(+), 1 deletion(-) > > > > diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h > > index 8a215c..3e034b5 100644 > > --- 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? > Or do we need to encode the length as well, as Jann suggests? > 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. 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. regards, dan carpenter