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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 31CD8C433EF for ; Fri, 25 Feb 2022 04:47:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A44748D0002; Thu, 24 Feb 2022 23:47:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9F3978D0001; Thu, 24 Feb 2022 23:47:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8E2698D0002; Thu, 24 Feb 2022 23:47:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0089.hostedemail.com [216.40.44.89]) by kanga.kvack.org (Postfix) with ESMTP id 7EBF48D0001 for ; Thu, 24 Feb 2022 23:47:05 -0500 (EST) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 2FF09998AF for ; Fri, 25 Feb 2022 04:47:05 +0000 (UTC) X-FDA: 79180067610.19.984D9A0 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) by imf17.hostedemail.com (Postfix) with ESMTP id A200C4000A for ; Fri, 25 Feb 2022 04:47:04 +0000 (UTC) Received: by mail-pj1-f47.google.com with SMTP id j10-20020a17090a94ca00b001bc2a9596f6so3802978pjw.5 for ; Thu, 24 Feb 2022 20:47:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=SyanVPTDj26hoM0+/34vC3ojlwHE6zX0sye59Y+Rtjs=; b=ez/pn28OrXoTJLAydt7fC3l/1BRKMczx0EuxgJzCsoJxTes/Q7y60tcVMO7wzlZseu 9ifY3jRNBn24/NGKwkqqOVQOACDLdGJKM8O1juUN8Vb5C+HPbiKG5WnRq7DsniVf7u8e fw6ieiJnZPOJ/xg6orBYtG5jEZmuIWIwk6hF4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=SyanVPTDj26hoM0+/34vC3ojlwHE6zX0sye59Y+Rtjs=; b=QAULYjWrhkpM4T7KcOsr9ZusemNSE1xw/zU2mWvabgIvAwclweiRSt4yOFaXufCfOS 3AuKEmmDewXHltCzbfROfy0UD+cLq0ndwfEcDxCZcKa3kM38d5T3OdjjvVWZSTdIgzcZ O9PfCCHHQuRMh2SNvWwcmw/x1U8g8t8qfoJi3BFfb902nXPkYK2JJcfjcDwTpTu7CVQw mwhip0AQjzHZ/6jhIwoQqdCX36Z42Wi9iwV6dBB0hsiGCoPOUZwisCMm2qcTeerbd7+8 7XRmGUxmQHjw9X9TDwdq5xE2hfgJR0wag33MgOmNbUmBdLGUwoO9fBFp1kEEftzyh799 3xWA== X-Gm-Message-State: AOAM530zWn3/Gu0cvEymHj72YODdLM1uBEdhKQKZQUsQGrUtmIe7EOrS Mgk0SAXWo4nLa9zoBiL3DGfnOQ== X-Google-Smtp-Source: ABdhPJzy/9ym4pKvdp7gXGiiTJRBM16FFuwlMDSrJbIDB/E//S1S2eCPnNgmXzRLoOrLqxAOu4UIKw== X-Received: by 2002:a17:902:f64e:b0:14d:20db:8478 with SMTP id m14-20020a170902f64e00b0014d20db8478mr5755429plg.158.1645764423449; Thu, 24 Feb 2022 20:47:03 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id f16-20020a623810000000b004e1b132bc9esm1254834pfa.149.2022.02.24.20.47.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Feb 2022 20:47:03 -0800 (PST) Date: Thu, 24 Feb 2022 20:47:02 -0800 From: Kees Cook To: David Laight Cc: Matthew Wilcox , Josh Poimboeuf , Andrew Morton , "linux-mm@kvack.org" , Muhammad Usama Anjum , "linux-kernel@vger.kernel.org" , "x86@kernel.org" , "linux-arm-kernel@lists.infradead.org" , "linuxppc-dev@lists.ozlabs.org" , "linux-s390@vger.kernel.org" , "linux-sh@vger.kernel.org" , "linux-hardening@vger.kernel.org" Subject: Re: [PATCH v2] usercopy: Check valid lifetime via stack depth Message-ID: <202202242046.33FF8372@keescook> References: <20220224060342.1855457-1-keescook@chromium.org> <85d42900efaa4fdb8c20de2147d938c7@AcuMS.aculab.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <85d42900efaa4fdb8c20de2147d938c7@AcuMS.aculab.com> X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: A200C4000A X-Stat-Signature: 17wkunnzipmssto5yp5fnjzmsya3uwmk Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b="ez/pn28O"; spf=pass (imf17.hostedemail.com: domain of keescook@chromium.org designates 209.85.216.47 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org X-HE-Tag: 1645764424-15591 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000007, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, Feb 24, 2022 at 08:58:20AM +0000, David Laight wrote: > From: Kees Cook > > Sent: 24 February 2022 06:04 > > > > Under CONFIG_HARDENED_USERCOPY=y, when exact stack frame boundary checking > > is not available (i.e. everything except x86 with FRAME_POINTER), check > > a stack object as being at least "current depth valid", in the sense > > that any object within the stack region but not between start-of-stack > > and current_stack_pointer should be considered unavailable (i.e. its > > lifetime is from a call no longer present on the stack). > > > ... > > diff --git a/mm/usercopy.c b/mm/usercopy.c > > index d0d268135d96..5d28725af95f 100644 > > --- a/mm/usercopy.c > > +++ b/mm/usercopy.c > > @@ -22,6 +22,30 @@ > > #include > > #include "slab.h" > > > > +/* > > + * Only called if obj is within stack/stackend bounds. Determine if within > > + * current stack depth. > > + */ > > +static inline int check_stack_object_depth(const void *obj, > > + unsigned long len) > > +{ > > +#ifdef CONFIG_ARCH_HAS_CURRENT_STACK_POINTER > > +#ifndef CONFIG_STACK_GROWSUP > > Pointless negation > > > + const void * const high = stackend; > > + const void * const low = (void *)current_stack_pointer; > > +#else > > + const void * const high = (void *)current_stack_pointer; > > + const void * const low = stack; > > +#endif > > + > > + /* Reject: object not within current stack depth. */ > > + if (obj < low || high < obj + len) > > + return BAD_STACK; > > + > > +#endif > > + return GOOD_STACK; > > +} > > If the comment at the top of the function is correct then > only a single test for the correct end of the buffer against > the current stack pointer is needed. > Something like: > #ifdef CONFIG_STACK_GROWSUP > if ((void *)current_stack_pointer < obj + len) > return BAD_STACK; > #else > if (obj < (void *)current_stack_pointer) > return BAD_STACK; > #endif > return GOOD_STACK; Oh, yeah, excellent point. I suspect the compiler would probably optimize it all away, but yes, this is, in fact, easier to read, and short enough I should probably just not bother with a separate function. Thanks! -Kees > > Although it may depend on exactly where the stack pointer > points to - especially for GROWSUP. > > David > > - > Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK > Registration No: 1397386 (Wales) > -- Kees Cook