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=-6.9 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS autolearn=ham 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 B3CDBC433DF for ; Thu, 21 May 2020 22:43:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 84925207D8 for ; Thu, 21 May 2020 22:43:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="eepgmPO9" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730705AbgEUWnr (ORCPT ); Thu, 21 May 2020 18:43:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730525AbgEUWnr (ORCPT ); Thu, 21 May 2020 18:43:47 -0400 Received: from mail-pg1-x544.google.com (mail-pg1-x544.google.com [IPv6:2607:f8b0:4864:20::544]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F94BC08C5C0 for ; Thu, 21 May 2020 15:43:47 -0700 (PDT) Received: by mail-pg1-x544.google.com with SMTP id u35so4017766pgk.6 for ; Thu, 21 May 2020 15:43:47 -0700 (PDT) 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=B63jtZppCxME57GNYlFpnJWpRDsZGhB+gP+ncDnuMt8=; b=eepgmPO9ioxUzOWMCrJPo/zU5f5QS4Xx+r4Hholy74x9HqMRiWQOONy2sfaiNQXtNn h5FNQRW5pfmK+w7kmw31o+u/yF49Sdrvoo6xIYXc0n9pPiLFFEQslBg1NJNH3ZBbBmAK fD9TG93TFPzuZItcLl3SozEamPUbqj4iPQy9M= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=B63jtZppCxME57GNYlFpnJWpRDsZGhB+gP+ncDnuMt8=; b=DXGkav7aFLtpQNvmGTzePqwRl0wYU94/mhbqj1ZLADNiJaYvnDiTi7bYVz4Uleu/Rd Zd6QJqlWeS3tyeMRog0GAFYsRYK1+kQkv+LW350NSG+eDUxQUtNJzMZrsf9u9yQXrvwI CrDYcJPF/QjLHJ4xV4bc8rx2YVwsx/A6vAaFQyvLSvoGpRFFTBVD3x80YjASxu5NaPpD Oj/yk7Wwh8oju/iGWxn45sMOcZqqIV39GvBOsHWtqYxLuMhvCql7f6CSE3LJ5XmKrkSU dGMzxJ0FYTTrr8qgHVmgn8bzvZjYHmkhARdZzb71jFdRUHHZ3HCxxp2wf3KRK3TrHeei fICA== X-Gm-Message-State: AOAM531b9n5v31JT3EgbJZwsnA8fzTQo9N3iA87f/DHBWlkZqKej2Jwf mDWtxySDNWUpNeofW8bguROxBQ== X-Google-Smtp-Source: ABdhPJwlOWgV2/IRVciIy5dDDOHU1RQaZOZ0x3AhQUpQtOtVgPfb7eiOoEtcyYapVJxob6sP9epa2g== X-Received: by 2002:a63:8f03:: with SMTP id n3mr10735041pgd.352.1590101026773; Thu, 21 May 2020 15:43:46 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id q18sm4799493pgt.74.2020.05.21.15.43.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 May 2020 15:43:45 -0700 (PDT) Date: Thu, 21 May 2020 15:43:44 -0700 From: Kees Cook To: Yu-cheng Yu Cc: x86@kernel.org, "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H.J. Lu" , Jann Horn , Jonathan Corbet , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V. Shankar" , Vedvyas Shanbhogue , Dave Martin , Weijiang Yang Subject: Re: [RFC PATCH 1/5] x86/cet/shstk: Modify ARCH_X86_CET_ALLOC_SHSTK for 32-bit address range Message-ID: <202005211542.35AB0A71C7@keescook> References: <20200521211720.20236-1-yu-cheng.yu@intel.com> <20200521211720.20236-2-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200521211720.20236-2-yu-cheng.yu@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 21, 2020 at 02:17:16PM -0700, Yu-cheng Yu wrote: > Sometimes a 64-bit task might need to have a shadow stack allocated from > within 32-bit address range. One example is selftests/x86/sigreturn. > > Currently arch_prctl(ARCH_X86_CET_ALLOC_SHSTK) takes a input parameter for > the desired shadow stack size. Modify it and use bit[0] of the parameter > to indicate the desire to allocate from 32-bit address range. > > Signed-off-by: Yu-cheng Yu > --- > arch/x86/include/asm/cet.h | 2 +- > arch/x86/include/uapi/asm/prctl.h | 2 ++ > arch/x86/kernel/cet.c | 19 ++++++++++++------- > arch/x86/kernel/cet_prctl.c | 6 +++++- > 4 files changed, 20 insertions(+), 9 deletions(-) > > diff --git a/arch/x86/include/asm/cet.h b/arch/x86/include/asm/cet.h > index f163c805a559..534b02785a39 100644 > --- a/arch/x86/include/asm/cet.h > +++ b/arch/x86/include/asm/cet.h > @@ -22,7 +22,7 @@ struct cet_status { > int prctl_cet(int option, u64 arg2); > int cet_setup_shstk(void); > int cet_setup_thread_shstk(struct task_struct *p); > -int cet_alloc_shstk(unsigned long *arg); > +int cet_alloc_shstk(unsigned long *arg, int map_32bit); > void cet_disable_free_shstk(struct task_struct *p); > int cet_verify_rstor_token(bool ia32, unsigned long ssp, unsigned long *new_ssp); > void cet_restore_signal(struct sc_ext *sc); > diff --git a/arch/x86/include/uapi/asm/prctl.h b/arch/x86/include/uapi/asm/prctl.h > index d962f0ec9ccf..e254c6a21475 100644 > --- a/arch/x86/include/uapi/asm/prctl.h > +++ b/arch/x86/include/uapi/asm/prctl.h > @@ -19,4 +19,6 @@ > #define ARCH_X86_CET_LOCK 0x3003 > #define ARCH_X86_CET_ALLOC_SHSTK 0x3004 > > +#define ARCH_X86_CET_ALLOC_SHSTK_32BIT 0x1UL Perhaps declare a set of bits here to verify that they are zero into the future? > + > #endif /* _ASM_X86_PRCTL_H */ > diff --git a/arch/x86/kernel/cet.c b/arch/x86/kernel/cet.c > index 92b8730c0b08..d6f93e1864b2 100644 > --- a/arch/x86/kernel/cet.c > +++ b/arch/x86/kernel/cet.c > @@ -57,14 +57,19 @@ static unsigned long cet_get_shstk_addr(void) > return ssp; > } > > -static unsigned long alloc_shstk(unsigned long size) > +static unsigned long alloc_shstk(unsigned long size, int map_32bit) > { > struct mm_struct *mm = current->mm; > unsigned long addr, populate; > + unsigned long map_flags; > + > + map_flags = MAP_ANONYMOUS | MAP_PRIVATE; > + if (map_32bit) > + map_flags |= MAP_32BIT; > > down_write(&mm->mmap_sem); > - addr = do_mmap(NULL, 0, size, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, > - VM_SHSTK, 0, &populate, NULL); > + addr = do_mmap(NULL, 0, size, PROT_READ, map_flags, VM_SHSTK, 0, > + &populate, NULL); > up_write(&mm->mmap_sem); > > if (populate) > @@ -147,14 +152,14 @@ static int create_rstor_token(bool ia32, unsigned long ssp, > return 0; > } > > -int cet_alloc_shstk(unsigned long *arg) > +int cet_alloc_shstk(unsigned long *arg, int map_32bit) > { > unsigned long len = *arg; > unsigned long addr; > unsigned long token; > unsigned long ssp; > > - addr = alloc_shstk(round_up(len, PAGE_SIZE)); > + addr = alloc_shstk(round_up(len, PAGE_SIZE), map_32bit); > > if (IS_ERR((void *)addr)) > return PTR_ERR((void *)addr); > @@ -185,7 +190,7 @@ int cet_setup_shstk(void) > return -EOPNOTSUPP; > > size = round_up(min(rlimit(RLIMIT_STACK), 1UL << 32), PAGE_SIZE); > - addr = alloc_shstk(size); > + addr = alloc_shstk(size, 0); > > if (IS_ERR((void *)addr)) > return PTR_ERR((void *)addr); > @@ -226,7 +231,7 @@ int cet_setup_thread_shstk(struct task_struct *tsk) > if (in_compat_syscall()) > size /= 4; > size = round_up(size, PAGE_SIZE); > - addr = alloc_shstk(size); > + addr = alloc_shstk(size, 0); > > if (IS_ERR((void *)addr)) { > cet->shstk_base = 0; > diff --git a/arch/x86/kernel/cet_prctl.c b/arch/x86/kernel/cet_prctl.c > index a8e68fefd524..364ed2420202 100644 > --- a/arch/x86/kernel/cet_prctl.c > +++ b/arch/x86/kernel/cet_prctl.c > @@ -35,12 +35,16 @@ static int handle_alloc_shstk(u64 arg2) > unsigned long arg; > unsigned long addr = 0; > unsigned long size = 0; > + int map_32bit; > > if (get_user(arg, (unsigned long __user *)arg2)) > return -EFAULT; > i.e. reject arg if any bits besides ARCH_X86_CET_ALLOC_SHSTK_32BIT are set in the mask you pick. > + map_32bit = (arg & ARCH_X86_CET_ALLOC_SHSTK_32BIT) ? 1 : 0; > + arg &= ~(ARCH_X86_CET_ALLOC_SHSTK_32BIT); And then clear the whole mask here. -Kees > + > size = arg; > - err = cet_alloc_shstk(&arg); > + err = cet_alloc_shstk(&arg, map_32bit); > if (err) > return err; > > -- > 2.21.0 > -- Kees Cook