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 8FB28C433FE for ; Fri, 30 Sep 2022 23:01:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 75CDB6B0071; Fri, 30 Sep 2022 19:01:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 70B9F8D0002; Fri, 30 Sep 2022 19:01:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5D2828D0001; Fri, 30 Sep 2022 19:01:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 4E35B6B0071 for ; Fri, 30 Sep 2022 19:01:26 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2398581654 for ; Fri, 30 Sep 2022 23:01:26 +0000 (UTC) X-FDA: 79970274972.18.34EE6DD Received: from mail-io1-f53.google.com (mail-io1-f53.google.com [209.85.166.53]) by imf13.hostedemail.com (Postfix) with ESMTP id 7C84F2001F for ; Fri, 30 Sep 2022 23:01:25 +0000 (UTC) Received: by mail-io1-f53.google.com with SMTP id 138so4385648iou.9 for ; Fri, 30 Sep 2022 16:01:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=0zt2Q5NN+Rs2C2aEkp8f4auq2xya6N3MePx4qmypsFc=; b=K9MohiTFe/jI/NYUnjHZt0j/r3AEx4tWk8aMjFSTQ2wHuJqShRre3HIQaSYMPdsPqw CgkBdBtxLiPDFdLfCA6GxIeipwCNfggZQ/FlAtVWHhFws8AnMQjJKh9HcYFItYRxPO9o XZ79w2s0es/8qkrg9qrd9Y+BGBx/7JaqjYds66RbwHuYGYC6AyGz7S9dcfKQka/X/yaU +YrmO36GxfU598k6MBGgoGPwqLiEYmADAe79yYEjNpKUtT25cS3cvkUjS3O+TDumwRCl vdK2JwQniT4vSJ1+KjSMq/WF0h6hTdvi6Zr89DHV15jR6UYe5Wy/UrjSMIaJSTLdYkg9 OM0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=0zt2Q5NN+Rs2C2aEkp8f4auq2xya6N3MePx4qmypsFc=; b=a9O5xemksAFOXf2udkKGZcOOzT14qthaTJ1oblnaTZCMn7QGSXUuNaW7v4MkaaVHVO n1mwzh1QBGx/gnO2c5oJh68ZA5T+49lG5CuRZ7Rd0PtB9OUg6pHlXXrGHw5OnwgHyx2m BHm5JAWhHX2j3vzunHd5k0DFsLbQIs5nekJxNF4Inh9Ij3DOLuCpcN03uzm7+MqbuP6C Dv4/xv9OG7KeJjo+crJVE6Pib1vv1dkCBbW7T5Zh+xPJ4AAbyvUpXxRCWF85Txm9T9hL XeiwpNlpIvMxyEIbruAa5cY7MODDAmrhBJZcOhoHgITadTJD06aNECRQX1xgkp2l5aJy E//w== X-Gm-Message-State: ACrzQf34EFOgX9SdnGLgDWbbUKjNzKhoLTl8ZrTawTcTsZwHNcHzHZku GR19u/3IpbwPK1+dyTFzfim/GjZTP381/32NaVs19g== X-Google-Smtp-Source: AMsMyM4pyhAAdStKkBOdZk4WLOBE/ewn/Xk3NAXvmNiNkUkh/Xch5qpt+jkA7Ix5YZMuQ0NMUnkAdvv1yy96Z0gQfd8= X-Received: by 2002:a05:6602:2ccd:b0:6a1:c561:50ca with SMTP id j13-20020a0566022ccd00b006a1c56150camr4976341iow.154.1664578884508; Fri, 30 Sep 2022 16:01:24 -0700 (PDT) MIME-Version: 1.0 References: <20220929222936.14584-1-rick.p.edgecombe@intel.com> <20220929222936.14584-23-rick.p.edgecombe@intel.com> <9fed0342-2d02-aaf2-ed66-20ff08bdfd0b@intel.com> In-Reply-To: <9fed0342-2d02-aaf2-ed66-20ff08bdfd0b@intel.com> From: Jann Horn Date: Sat, 1 Oct 2022 01:00:48 +0200 Message-ID: Subject: Re: [PATCH v2 22/39] mm: Don't allow write GUPs to shadow stack memory To: Dave Hansen Cc: Rick Edgecombe , 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" , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V . Shankar" , Weijiang Yang , "Kirill A . Shutemov" , joao.moreira@intel.com, John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com Content-Type: text/plain; charset="UTF-8" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1664578885; a=rsa-sha256; cv=none; b=d/dUcxXaDE3Db6ZV4Q+VgQcwznBRumjwD1GuNczDMncr/Id5kISaVBmf1VoK3X/O7hynQ3 iEOL9dRLoCmPyeND34TI0xd6kjOhr5zGthSIiB4fUg0Q60rn1aYNhaqpZj9PoJQOk934zz n2/eNDbloHPO6wlJ9C10Dl4/ZZ7Eg+o= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=K9MohiTF; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of jannh@google.com designates 209.85.166.53 as permitted sender) smtp.mailfrom=jannh@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1664578885; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=0zt2Q5NN+Rs2C2aEkp8f4auq2xya6N3MePx4qmypsFc=; b=gEo10piF4zvgdgSO1P/YIl8vqJdjKiUKkWSb+36FbA68IWahFFwM2KEqg5EDU2zwYXQiSs sAYjji1JzOBLAFMCRPp6wS7kf6dEfO+wlOb8hHRWbddPOP3etaR4ajlFvQLQyN3vTYJTPa kVM6+Q2OEG21by/DSIXzX5qzUgGL6RI= X-Rspamd-Queue-Id: 7C84F2001F X-Rspam-User: Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=K9MohiTF; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of jannh@google.com designates 209.85.166.53 as permitted sender) smtp.mailfrom=jannh@google.com X-Rspamd-Server: rspam03 X-Stat-Signature: 5xyo9595jbyab8d5oc3ypzb8oa96czpx X-HE-Tag: 1664578885-312464 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Fri, Sep 30, 2022 at 9:16 PM Dave Hansen wrote: > On 9/29/22 15:29, Rick Edgecombe wrote: > > @@ -1633,6 +1633,9 @@ static inline bool __pte_access_permitted(unsigned long pteval, bool write) > > { > > unsigned long need_pte_bits = _PAGE_PRESENT|_PAGE_USER; > > > > + if (write && (pteval & (_PAGE_RW | _PAGE_DIRTY)) == _PAGE_DIRTY) > > + return 0; > > Do we not have a helper for this? Seems a bit messy to open-code these > shadow-stack permissions. Definitely at least needs a comment. FWIW, if you look at more context around this diff, the function looks like this: static inline bool __pte_access_permitted(unsigned long pteval, bool write) { unsigned long need_pte_bits = _PAGE_PRESENT|_PAGE_USER; + if (write && (pteval & (_PAGE_RW | _PAGE_DIRTY)) == _PAGE_DIRTY) + return 0; + if (write) need_pte_bits |= _PAGE_RW; if ((pteval & need_pte_bits) != need_pte_bits) return 0; return __pkru_allows_pkey(pte_flags_pkey(pteval), write); } So I think this change is actually a no-op - the only thing it does is to return 0 if write==1, !_PAGE_RW, and _PAGE_DIRTY. But the check below will always return 0 if !_PAGE_RW, unless I'm misreading it? And this is the only patch in the series that touches this function, so it's not like this becomes necessary with a later patch in the series either. Should this check go in anyway for clarity reasons, or should this instead be a comment explaining that __pte_access_permitted() behaves just like the hardware access check, which means shadow pages are treated as readonly?