[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230515130553.2311248-4-jeffxu@chromium.org>
Date: Mon, 15 May 2023 13:05:49 +0000
From: jeffxu@...omium.org
To: dave.hansen@...el.com, luto@...nel.org, jorgelo@...omium.org,
keescook@...omium.org, groeck@...omium.org, jannh@...gle.com,
sroettger@...gle.com
Cc: akpm@...ux-foundation.org, jeffxu@...gle.com,
linux-kernel@...r.kernel.org, linux-kselftest@...r.kernel.org,
linux-mm@...ck.org, linux-hardening@...r.kernel.org
Subject: [PATCH 3/6] PKEY: Apply PKEY_ENFORCE_API to mprotect
From: Jeff Xu <jeffxu@...gle.com>
This patch enables PKEY_ENFORCE_API for the mprotect and
mprotect_pkey syscalls.
Signed-off-by: Jeff Xu<jeffxu@...gle.com>
---
mm/mprotect.c | 25 ++++++++++++++++++++++---
1 file changed, 22 insertions(+), 3 deletions(-)
diff --git a/mm/mprotect.c b/mm/mprotect.c
index 8a68fdca8487..1378be50567d 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -727,9 +727,13 @@ mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb,
/*
* pkey==-1 when doing a legacy mprotect()
+ * syscall==true if this is called by syscall from userspace.
+ * Note: this is always true for now, added as a reminder in case that
+ * do_mprotect_pkey is called directly by kernel in the future.
+ * Also it is consistent with __do_munmap().
*/
static int do_mprotect_pkey(unsigned long start, size_t len,
- unsigned long prot, int pkey)
+ unsigned long prot, int pkey, bool syscall)
{
unsigned long nstart, end, tmp, reqprot;
struct vm_area_struct *vma, *prev;
@@ -794,6 +798,21 @@ static int do_mprotect_pkey(unsigned long start, size_t len,
}
}
+ /*
+ * When called by syscall from userspace, check if the calling
+ * thread has the PKEY permission to modify the memory mapping.
+ */
+ if (syscall &&
+ arch_check_pkey_enforce_api(current->mm, start, end) < 0) {
+ char comm[TASK_COMM_LEN];
+
+ pr_warn_ratelimited(
+ "munmap was denied on PKEY_ENFORCE_API memory, pid=%d '%s'\n",
+ task_pid_nr(current), get_task_comm(comm, current));
+ error = -EACCES;
+ goto out;
+ }
+
prev = vma_prev(&vmi);
if (start > vma->vm_start)
prev = vma;
@@ -878,7 +897,7 @@ static int do_mprotect_pkey(unsigned long start, size_t len,
SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
unsigned long, prot)
{
- return do_mprotect_pkey(start, len, prot, -1);
+ return do_mprotect_pkey(start, len, prot, -1, true);
}
#ifdef CONFIG_ARCH_HAS_PKEYS
@@ -886,7 +905,7 @@ SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
SYSCALL_DEFINE4(pkey_mprotect, unsigned long, start, size_t, len,
unsigned long, prot, int, pkey)
{
- return do_mprotect_pkey(start, len, prot, pkey);
+ return do_mprotect_pkey(start, len, prot, pkey, true);
}
SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val)
--
2.40.1.606.ga4b1b128d6-goog
Powered by blists - more mailing lists