Skip to contentSkip to conversation

mm: move the check of READ_IMPLIES_EXEC out of do_mmap()

RFC PATCH Superseded Shu Han 1 participant 1 message Sep 25, 2024

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Shu Han

author
#0 lore raw

RFC PATCH v2 1/1 mm: move the check of READ_IMPLIES_EXEC out of do_mmap()

This patch moves the check of READ_IMPLIES_EXEC out of do_mmap(), and calls the LSM hooks at the same time. Below is the reason.

Some logic may call do_mmap() which adding PROT_EXEC internally when the user controlled READ_IMPLIES_EXEC flag is set, without being aware of the harm this poses to LSM.
For example, CVE-2016-10044[1] was reported many years ago, but the remap_file_pages() can still bypass the W^X policy enforced by SELinux[2] for a long time.

Adding checks is easy(and has been done in all required call sites for the current version), but there may have more calls to do_mmap() in the future. And moving the security check in do_mmap() will let it in the mmap_write_lock, which slows down the performance and even have deadlocks if someone depends on it.

Moving the check of READ_IMPLIES_EXEC out of do_mmap() can keep the LSM hook is called out of the mmap_write_lock, and avoid repeated logic for whether READ_IMPLIES_EXEC should add PROT_EXEC for the mapping or not(In current, the !MMU case won't implies exec if file's mmap_capabilities is not exist, but the security check logic is different). And all known cases that call do_mmap() become secure and anyone who want to apply the READ_IMPLIES_EXEC logic will call the LSM hook correctly.

Some minor behavioral changes to call sites to do_mmap() and why they should clearly be fine:
mm/util.c and ipc/shm.c: the common case, no changes in behavior. fs/aio.c: have ensured never have PROT_EXEC in the fix of CVE-2016-10044, so no changes in behavior.
arch/x86/kernel/shstk.c: must not want to PROT_EXEC in any case. since shadow stack is a stack only store return addresses, execute it is never required.
mm/mmap.c: in the history, remap_file_pages won't care about the READ_IMPLIES_EXEC. this side effect is introduced in the emulation version after the deprecated mark exists. The patch only remove the side effect introduced.

---
V2: Add RFC tag as [email protected] suggested, and refine the comment in patch.
V1: https://lore.kernel.org/all/[email protected]/
Alternatives:
1. mm: move security_mmap_file() back into do_mmap()
https://lore.kernel.org/all/[email protected]/
2. Add sufficient comments for do_mmap()

---
 include/linux/mm.h       |  2 ++
 include/linux/security.h |  8 +++----
 ipc/shm.c                |  2 +-
 mm/mmap.c                | 42 ++++++++++++++++++++++------------
 mm/nommu.c               | 49 +++++++++++++++++++++++++++++++---------
 mm/util.c                |  2 +-
 security/security.c      | 41 ++++-----------------------------
 7 files changed, 78 insertions(+), 68 deletions(-)
include/linux/mm.h +2 -0
index c4b238a20b76..83f334590b06 100644
@@ -3392,6 +3392,8 @@ get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
 extern unsigned long mmap_region(struct file *file, unsigned long addr,
 	unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
 	struct list_head *uf);
+extern int mmap_check_prot(struct file *file, unsigned long *prot,
+	unsigned long flags);
 extern unsigned long do_mmap(struct file *file, unsigned long addr,
 	unsigned long len, unsigned long prot, unsigned long flags,
 	vm_flags_t vm_flags, unsigned long pgoff, unsigned long *populate,
include/linux/security.h +4 -4
index c37c32ebbdcd..e061bc9a0331 100644
@@ -423,8 +423,8 @@ void security_file_free(struct file *file);
 int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
 int security_file_ioctl_compat(struct file *file, unsigned int cmd,
 			       unsigned long arg);
-int security_mmap_file(struct file *file, unsigned long prot,
-			unsigned long flags);
+int security_mmap_file(struct file *file, unsigned long reqprot,
+		       unsigned long prot, unsigned long flags);
 int security_mmap_addr(unsigned long addr);
 int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
 			   unsigned long prot);
@@ -1077,8 +1077,8 @@ static inline int security_file_ioctl_compat(struct file *file,
 	return 0;
 }
 
-static inline int security_mmap_file(struct file *file, unsigned long prot,
-				     unsigned long flags)
+static inline int security_mmap_file(struct file *file, unsigned long reqprot,
+				     unsigned long prot, unsigned long flags)
 {
 	return 0;
 }
ipc/shm.c +1 -1
index 3e3071252dac..f1095ee3796d 100644
@@ -1636,7 +1636,7 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg,
 	sfd->vm_ops = NULL;
 	file->private_data = sfd;
 
-	err = security_mmap_file(file, prot, flags);
+	err = mmap_check_prot(file, &prot, flags);
 	if (err)
 		goto out_fput;
 
mm/mmap.c +28 -14

64 lines not on this page · raw message

mm/nommu.c +38 -11

67 lines not on this page · raw message

mm/util.c +1 -1
index bd283e2132e0..2eb4d6037610 100644
@@ -581,7 +581,7 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
 	unsigned long populate;
 	LIST_HEAD(uf);
 
-	ret = security_mmap_file(file, prot, flag);
+	ret = mmap_check_prot(file, &prot, flag);
 	if (!ret) {
 		if (mmap_write_lock_killable(mm))
 			return -EINTR;
security/security.c +4 -37

57 lines not on this page · raw message

base-commit: f89722faa31466ff41aed21bdeb9cf34c2312858

Signature · 1 line
2.34.1