From patchwork Mon May 15 13:05:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13241401 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 38FF8C7EE22 for ; Mon, 15 May 2023 13:07:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 91AD7900008; Mon, 15 May 2023 09:07:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8A280900002; Mon, 15 May 2023 09:07:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 74323900008; Mon, 15 May 2023 09:07:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 5C395900002 for ; Mon, 15 May 2023 09:07:25 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id D02A28125A for ; Mon, 15 May 2023 13:07:24 +0000 (UTC) X-FDA: 80792515608.24.B6E41CF Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) by imf25.hostedemail.com (Postfix) with ESMTP id 68A0FA02C1 for ; Mon, 15 May 2023 13:06:03 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=lVLtpSyn; spf=pass (imf25.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.214.169 as permitted sender) smtp.mailfrom=jeffxu@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684155963; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=TQS98FLuUxt8kfip9Nk8O1hVfeuWAt0sl7+06c/6tso=; b=aL/3YfKOlRjJnbmAA48Z1IMGQdoTNyChHCzP6UszQnOCEo+sPfT8xXqWaehD8RI8clXk0c Vd0TFB3Hv0fOMfGPfduI0iNMlytB7X82XLWHZzJu+9tMYwzRA5h37uYiyCE/aoHxiRhg17 fJQFG6Po8/Pit83ozxfFj1bXxC3xC1I= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=lVLtpSyn; spf=pass (imf25.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.214.169 as permitted sender) smtp.mailfrom=jeffxu@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684155963; a=rsa-sha256; cv=none; b=Uxyl5wUnrIyCP5/RhRCbemlVD/0IrrivdpPuBAHDacuz02pVmZpuUdy8NtTjRg8zXZFYyp qQQ6+murfoQmIRIJCte6rSYjeGG8bqoUg6gsmsqqoJQ/mT2DoYDbTMwbQT75+JewIVsDCQ MZCylXj6pNeSE2To75kH/oc6V/5sSCM= Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-1ab0c697c2bso117565585ad.1 for ; Mon, 15 May 2023 06:06:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684155962; x=1686747962; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TQS98FLuUxt8kfip9Nk8O1hVfeuWAt0sl7+06c/6tso=; b=lVLtpSynI8nMT2aDpFG0AwTYGHG10mCOzf2uMVGaztPOf63EnxTkd4eJJVNqJlr8C5 g4595V6nKOPML2z8/lcW1UhNkQhkCyXDVgAutORAhmXktQaBwlD2yr8BPgV8UP/5LyyG byJlw9BmRMYbGWCADO6h/aSqv0KiIADjBwCC4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684155962; x=1686747962; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TQS98FLuUxt8kfip9Nk8O1hVfeuWAt0sl7+06c/6tso=; b=OBvdI6cYWST8SI6yeE3oyo22t3tW8dDx8d4q9RaD/C/5+NBEguQoWvW2BNX08phrHu 79DQyet4znWePhVBZIVbu6fCzBB+ACIwTk+d1CXvriH18R121iPEmpnKQkFlqXYuZPao sU6aUaeD1uoI65iH/NcseKNXZ2KLFbEZ5Cy183WMI0PbSMWdeHPf5wPZszedbnsn1GNZ mOmocpX/ItgJot+VQ82CKUmWeRu96nizus/SESaNIvuLy/2O49azwWlMYBHhhivViBj1 NgRu7xNU+068vA3Xop+i/qvQwENVNHnfAe7TKRpp8Rf+LJc8Ys4QQeiuUX6m2f5eVWMR 0CMQ== X-Gm-Message-State: AC+VfDxvBUDNu9I1S71Hsmjll2Qmx/tjbSS6fEvLYzM7Op34UVbTg+sf GwwmX0POucgaKea4tvrYsECY1A== X-Google-Smtp-Source: ACHHUZ5LvI01D49NaxlHJHiG9oq5TunRXuf9uX9R4zcylWjAPhk6R7RxfjFf6bkyuvAy6sflWATEYg== X-Received: by 2002:a17:902:788d:b0:1ac:8215:623d with SMTP id q13-20020a170902788d00b001ac8215623dmr24516585pll.0.1684155961805; Mon, 15 May 2023 06:06:01 -0700 (PDT) Received: from localhost (183.43.230.35.bc.googleusercontent.com. [35.230.43.183]) by smtp.gmail.com with UTF8SMTPSA id nn4-20020a17090b38c400b0024df7d7c35esm1095703pjb.43.2023.05.15.06.06.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 15 May 2023 06:06:01 -0700 (PDT) From: jeffxu@chromium.org To: dave.hansen@intel.com, luto@kernel.org, jorgelo@chromium.org, keescook@chromium.org, groeck@chromium.org, jannh@google.com, sroettger@google.com Cc: akpm@linux-foundation.org, jeffxu@google.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org Subject: [PATCH 2/6] PKEY: Add arch_check_pkey_enforce_api() Date: Mon, 15 May 2023 13:05:48 +0000 Message-ID: <20230515130553.2311248-3-jeffxu@chromium.org> X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog In-Reply-To: <20230515130553.2311248-1-jeffxu@chromium.org> References: <20230515130553.2311248-1-jeffxu@chromium.org> MIME-Version: 1.0 X-Stat-Signature: orui74jq3jfrkuzh86f75i6wxdsacm9e X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 68A0FA02C1 X-Rspam-User: X-HE-Tag: 1684155963-240352 X-HE-Meta: U2FsdGVkX18vcPonre/GGCfF3wLm4vds4ktD36qhoc2hkHM4U5Olza0p2+Lgr3MnlMvzgj/SPvf3m2bKxLpSk15C3FxgZTbtJli+IdRQ8H+DpEDq7cs/NRoBn5URSh7TOJlHP1LjaCa3jQu5dNQ8qAhaI0Uk0u4IxLJZQouPbdZPFpOqVVfisfLy5+8WkD7frB450WCZEqZHQ11heIaZlrIxOdLTZB4/DDy18zWQNijyO5L3hphnBE3twKyCRDQmEYL6RmV/FZm2lO+BtNE6H1eHGWNOgDItanIt8Wi53SM2vjEmxWJqGjWOVetvNOTioXZgLziy/P4WReHHxOPQyCUdN3D8ZFOgiyBgAIQXDB+40Pzz/wCTMj5DcTYN2Bw/gevHDCDSq7oy0sQJjD+rP8RINwzJOmPHLprKVCpvYkF9us3QomX1R6trA2Cu9JGz6aOiWc7aC3Dekz2oPb3q+brBxHnpGUolVTY+uu2DSHmQG3gwOZb7Qd9uOJ2HD+2ibWHLv+XPJWj3pciPTrRc+AHFvJvxe5nMHzMX7JAdKdCUs5V2kIEHF5YJ61rWbOD4QqahOyV3iMAPBwnbWW+Kw33K4XLIY3G60jm9CgSJbkxNHqBYGyewFHKudcSRw47N5DLUjTqkLQrSc1Pr/0VkMMeSAvLrMdGE30FLSWUzsq91RNq9u86smV0yXdZ4vEEJPfcdUqK/cPiktcEs+oDdnz0wSYL3brg4jSv4EAfsHbEB+nm9Hlw7CVmz9/aZYqwUqPhiwfDlxW0qPgzER0PCw7z2J8OoF53VAQoJd7SQsb0oK4EARcdwpRzTqED8rE7E+iuHxDZSObn31m0+v7AhKwU4A0JYeI8b9yrS1sSBPVSlcGsYheum/TCw2YI1QF9YyOgPeCxfUuzDkryGGiybv1LIBcEmkA/c4rhYlBFNvLa9hq+LCgImIzrr7trAPzxz8SDEkRfEyiFGWRsJWW5 GRXPACUD p99wdZyaYOx6nCK2/jyTrMBFriryQP6WcwpaT6b6L8gMRONuT6/vIVhWZzQv0JRr+FdZ7OSyeBvKb34XORTW+kj6V6NUUeJQVlcAUoQBGRA6sRB/ofGVlwBCMf5p6bGwOOVXjEVPT/UCqmk3kF3ufO2wzRl7q0FCu1NbD4nVAl8wT1ZvY1I51nIC+7kLwS5bQJTmKfwBDXgV24LCM/wqRH2CLV64TFJP7SBAwSzFZhhghDsFO18XiiloADnSAYMSC8EycF/7gpjOYsMw7qIMTny7Gbec76EHT/d2DaGj4jAKjT1f27DxyE/AdNFi860gw93F1YDC+F/Q3BukcBsgsg6vH4KaqlCCHppts1JnKlXVSHKGdzL6UJQ87iw== 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: From: Jeff Xu This patch adds an architecture-independent function, arch_check_pkey_enforce_api(), that checks whether the calling thread has write access to the PKRU for a given range of memory. If the memory range is protected by PKEY_ENFORCE_API, then the thread must have write access to the PKRU in order to make changes to the memory mapping (such as mprotect/munmap). This function is used by the kernel to enforce the PKEY_ENFORCE_API flag. Signed-off-by: Jeff Xu --- arch/powerpc/include/asm/pkeys.h | 8 +++++ arch/x86/include/asm/pkeys.h | 50 ++++++++++++++++++++++++++++++++ include/linux/pkeys.h | 9 ++++++ 3 files changed, 67 insertions(+) diff --git a/arch/powerpc/include/asm/pkeys.h b/arch/powerpc/include/asm/pkeys.h index 943333ac0fee..24c481e5e95b 100644 --- a/arch/powerpc/include/asm/pkeys.h +++ b/arch/powerpc/include/asm/pkeys.h @@ -177,5 +177,13 @@ static inline bool arch_check_pkey_alloc_flags(unsigned long flags) return true; } +static inline int arch_check_pkey_enforce_api(struct mm_struct *mm, + unsigned long start, + unsigned long end) +{ + /* Allow by default */ + return 0; +} + extern void pkey_mm_init(struct mm_struct *mm); #endif /*_ASM_POWERPC_KEYS_H */ diff --git a/arch/x86/include/asm/pkeys.h b/arch/x86/include/asm/pkeys.h index ecadf04a8251..8b94ffc4ca32 100644 --- a/arch/x86/include/asm/pkeys.h +++ b/arch/x86/include/asm/pkeys.h @@ -161,4 +161,54 @@ static inline bool arch_check_pkey_alloc_flags(unsigned long flags) return true; } + +static inline int __arch_check_vma_pkey_for_write(struct vm_area_struct *vma) +{ + int pkey = vma_pkey(vma); + + if (mm_pkey_enforce_api(vma->vm_mm, pkey)) { + if (!__pkru_allows_write(read_pkru(), pkey)) + return -EACCES; + } + + return 0; +} + +/* + * arch_check_pkey_enforce_api is used by the kernel to enforce + * PKEY_ENFORCE_API flag. + * It checks whether the calling thread has write access to the PKRU + * for a given range of memory. If the memory range is protected by + * PKEY_ENFORCE_API, then the thread must have write access to the + * PKRU in order to make changes to the memory mapping, such as + * mprotect/munmap. + */ +static inline int arch_check_pkey_enforce_api(struct mm_struct *mm, + unsigned long start, + unsigned long end) +{ + int error; + struct vm_area_struct *vma; + + if (!arch_pkeys_enabled()) + return 0; + + while (true) { + vma = find_vma_intersection(mm, start, end); + if (!vma) + break; + + error = __arch_check_vma_pkey_for_write(vma); + if (error) + return error; + + if (vma->vm_end >= end) + break; + + start = vma->vm_end; + } + + return 0; +} + #endif /*_ASM_X86_PKEYS_H */ diff --git a/include/linux/pkeys.h b/include/linux/pkeys.h index 81a482c3e051..7b00689e1c24 100644 --- a/include/linux/pkeys.h +++ b/include/linux/pkeys.h @@ -53,6 +53,15 @@ static inline bool arch_check_pkey_alloc_flags(unsigned long flags) return false; return true; } + +static inline int arch_check_pkey_enforce_api(struct mm_struct *mm, + unsigned long start, + unsigned long end) +{ + // Allow by default. + return 0; +} + #endif /* ! CONFIG_ARCH_HAS_PKEYS */ #endif /* _LINUX_PKEYS_H */