Skip to contentSkip to conversation

mm: move security_mmap_file() back into do_mmap()

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

Subsystems (4)
lore
Share & tools
lore

Shu Han

author
#0 lore raw

RFC PATCH v2 1/1 mm: move security_mmap_file() back into do_mmap()

This patch moves the security_mmap_file() back into do_mmap(), which revert(It is conceptually a revert of the commit(revert the feature change in the commit), but not in the git sense(revert lines)) the commit 8b3ec6814c83d76b85bd13badc48552836c24839
("take security_mmap_file() outside of ->mmap_sem"). Below is the reason.

Some logic may call do_mmap() without calling security_mmap_file(), 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. Moving security_mmap_file() back into do_mmap() can avoid forgetting, 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 imply exec if the file's mmap_capabilities is not exist, but the security check logic is different).

It is noteworthy that 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(Since security_file_mprotect() is already in the lock, this possibility is tiny), which requires LSM modules to check.

---
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 the check of READ_IMPLIES_EXEC out of do_mmap()
2. Add sufficient comments for do_mmap()

---
 include/linux/security.h |  8 ++++----
 ipc/shm.c                |  4 ----
 mm/mmap.c                |  9 +++++----
 mm/nommu.c               |  5 ++++-
 mm/util.c                | 19 ++++++++-----------
 security/security.c      | 41 ++++------------------------------------
 6 files changed, 25 insertions(+), 61 deletions(-)
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 +0 -4
index 3e3071252dac..ce02560b856f 100644
@@ -1636,10 +1636,6 @@ 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);
-	if (err)
-		goto out_fput;
-
 	if (mmap_write_lock_killable(current->mm)) {
 		err = -EINTR;
 		goto out_fput;
mm/mmap.c +5 -4
index 18fddcce03b8..56f9520f85ab 100644
@@ -1260,6 +1260,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
 {
 	struct mm_struct *mm = current->mm;
 	int pkey = 0;
+	unsigned long reqprot = prot, err;
 
 	*populate = 0;
 
@@ -1276,6 +1277,10 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
 		if (!(file && path_noexec(&file->f_path)))
 			prot |= PROT_EXEC;
 
+	err = security_mmap_file(file, reqprot, prot, flags);
+	if (err)
+		return err;
+
 	/* force arch specific MAP_FIXED handling in get_unmapped_area */
 	if (flags & MAP_FIXED_NOREPLACE)
 		flags |= MAP_FIXED;
@@ -3198,12 +3203,8 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
 		flags |= MAP_LOCKED;
 
 	file = get_file(vma->vm_file);
-	ret = security_mmap_file(vma->vm_file, prot, flags);
-	if (ret)
-		goto out_fput;
 	ret = do_mmap(vma->vm_file, start, size,
 			prot, flags, 0, pgoff, &populate, NULL);
-out_fput:
 	fput(file);
 out:
 	mmap_write_unlock(mm);
mm/nommu.c +4 -1
index 7296e775e04e..e632f3105a5a 100644
@@ -681,7 +681,7 @@ static int validate_mmap_request(struct file *file,
 				 unsigned long pgoff,
 				 unsigned long *_capabilities)
 {
-	unsigned long capabilities, rlen;
+	unsigned long capabilities, rlen, reqprot = prot;
 	int ret;
 
 	/* do the simple checks first */
@@ -818,6 +818,9 @@ static int validate_mmap_request(struct file *file,
 	}
 
 	/* allow the security API to have its say */
+	ret = security_mmap_file(file, reqprot, prot, flags);
+	if (ret < 0)
+		return ret;
 	ret = security_mmap_addr(addr);
 	if (ret < 0)
 		return ret;
mm/util.c +8 -11
index bd283e2132e0..47345e927a8f 100644
@@ -581,17 +581,14 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
 	unsigned long populate;
 	LIST_HEAD(uf);
 
-	ret = security_mmap_file(file, prot, flag);
-	if (!ret) {
-		if (mmap_write_lock_killable(mm))
-			return -EINTR;
-		ret = do_mmap(file, addr, len, prot, flag, 0, pgoff, &populate,
-			      &uf);
-		mmap_write_unlock(mm);
-		userfaultfd_unmap_complete(mm, &uf);
-		if (populate)
-			mm_populate(ret, populate);
-	}
+	if (mmap_write_lock_killable(mm))
+		return -EINTR;
+	ret = do_mmap(file, addr, len, prot, flag, 0, pgoff, &populate,
+		      &uf);
+	mmap_write_unlock(mm);
+	userfaultfd_unmap_complete(mm, &uf);
+	if (populate)
+		mm_populate(ret, populate);
 	return ret;
 }
 
security/security.c +4 -37

57 lines not on this page · raw message

base-commit: f89722faa31466ff41aed21bdeb9cf34c2312858

Signature · 1 line
2.34.1