Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1199973imm; Wed, 11 Jul 2018 19:58:52 -0700 (PDT) X-Google-Smtp-Source: AAOMgpc9OpQCz6pm12m3eL00i2HDCoYjx1V4cCBx7GJuPlLFEzrUo6ABkeW9pYy8joeice0prm8C X-Received: by 2002:a63:5025:: with SMTP id e37-v6mr409733pgb.341.1531364332518; Wed, 11 Jul 2018 19:58:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531364332; cv=none; d=google.com; s=arc-20160816; b=tfGAm0nqYUoUFEgvwJ+/Fo45ZXBsrWvARIgW6xnYRHbG0SUy/13P1AVOR0naiaPlLo yvvwrqIegNfUg8NRGdMnPNWSTSmwDkWc0uRoa4RC61AGCZyF3zCkoPK+S7jyeiSzo491 g1VcZREFfuoeNrhaE0Q0w4eF1QTHqexD8ta0/hFjlwfEykwZGLDULh+j8pkY1jlYLmSs PhqpQKrcwz3JY0Jm3ReBkaLdTdC64rXHeQ45Empbdatz6lhtsVNGE3/HZ1Zv0paW/rRO mmSOORt7wOjHOLEfy8kA1MJ8wktGAAIE6t9pDmO4dX2do8aaJlGG8HoColTEtXNt4Ztv v2hA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature :arc-authentication-results; bh=Jx87YraUcIHdT3OsK3qx7PdpgrM2z++7IeOUm35AtmI=; b=y5gKG2RZA7otYW3vjcFnDGByBqn3qhPNcJqoui8z1inNOnKnR7zq8DsouyzaNJveji DcVO/Ps3LwyXC6c70Y1ZKg60PAfwZwXZqwuqUiF8ubldWVKyPSwlpE8wtqBuS7HlPk3i 1byDD+IeyyxiUuF0TBJsD8JXRpQA7cY26vROY5OznEYtg7/uaIxdkkFPB23h+pdR8KcY txmY5d1N0z6hAvSf+riYeA+FMrkjzUc9IDdq4mqbpkyDRzor2HSyu+E0V4wV8+83M1+j EfPYc89yoc/nHGk7owCZGMwMuNO+15Hs/s9bji/ME6Aon8KN0N62noFZWjiRnmpxidCZ lb0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ZaUFoxLG; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s12-v6si19466194pgo.112.2018.07.11.19.58.37; Wed, 11 Jul 2018 19:58:52 -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=@google.com header.s=20161025 header.b=ZaUFoxLG; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389182AbeGKVRD (ORCPT + 99 others); Wed, 11 Jul 2018 17:17:03 -0400 Received: from mail-oi0-f67.google.com ([209.85.218.67]:43196 "EHLO mail-oi0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726463AbeGKVRC (ORCPT ); Wed, 11 Jul 2018 17:17:02 -0400 Received: by mail-oi0-f67.google.com with SMTP id b15-v6so51830898oib.10 for ; Wed, 11 Jul 2018 14:10:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Jx87YraUcIHdT3OsK3qx7PdpgrM2z++7IeOUm35AtmI=; b=ZaUFoxLGR53reri6A7z19pESQnLR3ST1C3eOXHHVNRgLWMxg+EzDC4mmrHJ7uT5ybO 325vx4Sd83SZeS8Pr2UjVLHuNKDDXSmVIOyPAcVRshv8VETSckPr3BU+8K3ucIEqGOF/ +laEHNpGsAfY1mFP8yXxsMNd02D0ypw9Je/YEaMufMhfdhQEnusPdFI1XuIUkmFYOY05 2yj5I8a7v+fz1k7uo35Vsts4ebFR05XHP4roTUt/9fZNVrSdybHSyIMPsdgm/3K54Reu /lzYjAqTejzNo72Dr2HcEjkpbWqAposgnerU0WGEw4/+Be8A3/X2e4LPHt0KrUQOu4F8 5g+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Jx87YraUcIHdT3OsK3qx7PdpgrM2z++7IeOUm35AtmI=; b=NTkAR/sRg1po1YOqFqDvPAiqeb4Xj7CybviXQoxEX/UEuiCJoPmQ0TFIfkICilnT8p xmekXyBxt8fnIDlv4CRribuOFNZ2kubvnKtD2OaGy4yt5fVTCL5pxEYK07HTi0aur/ze H6Whn7kQjtzzly0jyyGgO5F88uJOnU520nWUz4VqlAgD2wy6rAvutXpVv3WhZr6MzMhk TApkgxCzhvXxFeT2jRtZdXHvxUsCnBmSV77WskX7+WjD+InY+MHNOZg6aAFkUIDAtIh/ kasPhW5+EP3jZbviGijWazOj9zTRz8xTdaK17nZJbws9npdbU/+dc+S+thvxVJ149i76 X+bA== X-Gm-Message-State: AOUpUlGIxBef4Lnct8xFyNHeRW+URGrN1QNL/InHN2cM/UvXew/Fd6fq ScBANZMM01Smp9aayDIpDJtsMD+NkF0uUByaxGeHag== X-Received: by 2002:aca:c42:: with SMTP id i2-v6mr274522oiy.219.1531343449017; Wed, 11 Jul 2018 14:10:49 -0700 (PDT) MIME-Version: 1.0 References: <20180710222639.8241-1-yu-cheng.yu@intel.com> <20180710222639.8241-18-yu-cheng.yu@intel.com> In-Reply-To: <20180710222639.8241-18-yu-cheng.yu@intel.com> From: Jann Horn Date: Wed, 11 Jul 2018 14:10:22 -0700 Message-ID: Subject: Re: [RFC PATCH v2 17/27] x86/cet/shstk: User-mode shadow stack support To: yu-cheng.yu@intel.com Cc: "the arch/x86 maintainers" , "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , kernel list , linux-doc@vger.kernel.org, Linux-MM , linux-arch , Linux API , Arnd Bergmann , Andy Lutomirski , bsingharora@gmail.com, Cyrill Gorcunov , Dave Hansen , Florian Weimer , hjl.tools@gmail.com, Jonathan Corbet , keescook@chromiun.org, Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , ravi.v.shankar@intel.com, vedvyas.shanbhogue@intel.com Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jul 10, 2018 at 3:31 PM Yu-cheng Yu wrote: > > This patch adds basic shadow stack enabling/disabling routines. > A task's shadow stack is allocated from memory with VM_SHSTK > flag set and read-only protection. The shadow stack is > allocated to a fixed size. > > Signed-off-by: Yu-cheng Yu [...] > diff --git a/arch/x86/kernel/cet.c b/arch/x86/kernel/cet.c > new file mode 100644 > index 000000000000..96bf69db7da7 > --- /dev/null > +++ b/arch/x86/kernel/cet.c [...] > +static unsigned long shstk_mmap(unsigned long addr, unsigned long len) > +{ > + struct mm_struct *mm = current->mm; > + unsigned long populate; > + > + down_write(&mm->mmap_sem); > + addr = do_mmap(NULL, addr, len, PROT_READ, > + MAP_ANONYMOUS | MAP_PRIVATE, VM_SHSTK, > + 0, &populate, NULL); > + up_write(&mm->mmap_sem); > + > + if (populate) > + mm_populate(addr, populate); > + > + return addr; > +} How does this interact with UFFDIO_REGISTER? Is there an explicit design decision on whether FOLL_FORCE should be able to write to shadow stacks? I'm guessing the answer is "yes, FOLL_FORCE should be able to write to shadow stacks"? It might make sense to add documentation for this. Should the kernel enforce that two shadow stacks must have a guard page between them so that they can not be directly adjacent, so that if you have too much recursion, you can't end up corrupting an adjacent shadow stack? > +int cet_setup_shstk(void) > +{ > + unsigned long addr, size; > + > + if (!cpu_feature_enabled(X86_FEATURE_SHSTK)) > + return -EOPNOTSUPP; > + > + size = in_ia32_syscall() ? SHSTK_SIZE_32:SHSTK_SIZE_64; > + addr = shstk_mmap(0, size); > + > + /* > + * Return actual error from do_mmap(). > + */ > + if (addr >= TASK_SIZE_MAX) > + return addr; > + > + set_shstk_ptr(addr + size - sizeof(u64)); > + current->thread.cet.shstk_base = addr; > + current->thread.cet.shstk_size = size; > + current->thread.cet.shstk_enabled = 1; > + return 0; > +} [...] > +void cet_disable_free_shstk(struct task_struct *tsk) > +{ > + if (!cpu_feature_enabled(X86_FEATURE_SHSTK) || > + !tsk->thread.cet.shstk_enabled) > + return; > + > + if (tsk == current) > + cet_disable_shstk(); > + > + /* > + * Free only when tsk is current or shares mm > + * with current but has its own shstk. > + */ > + if (tsk->mm && (tsk->mm == current->mm) && > + (tsk->thread.cet.shstk_base)) { > + vm_munmap(tsk->thread.cet.shstk_base, > + tsk->thread.cet.shstk_size); > + tsk->thread.cet.shstk_base = 0; > + tsk->thread.cet.shstk_size = 0; > + } > + > + tsk->thread.cet.shstk_enabled = 0; > +}