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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 69966C433F5 for ; Fri, 30 Sep 2022 23:01:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232058AbiI3XBc (ORCPT ); Fri, 30 Sep 2022 19:01:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232034AbiI3XB1 (ORCPT ); Fri, 30 Sep 2022 19:01:27 -0400 Received: from mail-io1-xd31.google.com (mail-io1-xd31.google.com [IPv6:2607:f8b0:4864:20::d31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3EDCE12F764 for ; Fri, 30 Sep 2022 16:01:25 -0700 (PDT) Received: by mail-io1-xd31.google.com with SMTP id g8so4411753iob.0 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=jIALlHYadHIqqqBbf7YUAKg/owmOGnkK3QtCCFkgo1F4YbDxhQMG1U3ZLB4vetl4hK QfLlFDhwRPrVt2MdzA6JveKcuat1f57XRdPrFIIxXwziaXBik56wdrKmJrGdGCtYvNOr 4wLYCb+9LJrKWsjLOYF31AB7K0VtZX9Bp/450/5HhLcHeK4A3E/jUYckPXxnaC22lOJ3 QVYvd01lg7hLeKAPRwZ+XheYR0YtYBlAR3X190g+mWgKTIoo+/meOKImV4hLqTSGrgN3 eqiyQqU+bOun3XQykf1W0dlmjQdAzP34ku6JeZcridpXUexEv9ZuuP3ajcEdJcpEhY9q xflQ== X-Gm-Message-State: ACrzQf3KvNzY+vk9WYaB0qZq+hUr6lzIQGmV6Xc6H3h19o4Paqi4uNLp OJG2/CqjQ2MKW1jdnwhKbzizp0mdadft138gNBNgZA== 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" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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?