diff mbox series

[RFC,v1,3/8] mseal: add can_modify_mm and can_modify_vma

Message ID 20231016143828.647848-4-jeffxu@chromium.org (mailing list archive)
State New
Headers show
Series Introduce mseal() syscall | expand

Commit Message

Jeff Xu Oct. 16, 2023, 2:38 p.m. UTC
From: Jeff Xu <jeffxu@google.com>

can_modify_mm:
 checks sealing flags for given memory range.

can_modify_vma:
  checks sealing flags for given vma.

Signed-off-by: Jeff Xu <jeffxu@google.com>
---
 include/linux/mm.h | 34 ++++++++++++++++++++++++++
 mm/mseal.c         | 60 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 94 insertions(+)
diff mbox series

Patch

diff --git a/include/linux/mm.h b/include/linux/mm.h
index e790b91a0cd4..aafdb68950f8 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -257,6 +257,18 @@  extern struct rw_semaphore nommu_region_sem;
 extern unsigned int kobjsize(const void *objp);
 #endif
 
+enum caller_origin {
+	ON_BEHALF_OF_KERNEL = 0,
+	ON_BEHALF_OF_USERSPACE,
+};
+
+enum mm_action {
+	MM_ACTION_MPROTECT,
+	MM_ACTION_MUNMAP,
+	MM_ACTION_MREMAP,
+	MM_ACTION_MMAP,
+};
+
 /*
  * vm_seals in vm_area_struct, see mm_types.h.
  */
@@ -3302,6 +3314,28 @@  static inline void mm_populate(unsigned long addr, unsigned long len)
 static inline void mm_populate(unsigned long addr, unsigned long len) {}
 #endif
 
+#ifdef CONFIG_MSEAL
+extern bool can_modify_mm(struct mm_struct *mm, unsigned long start,
+			  unsigned long end, enum mm_action action,
+			  enum caller_origin called);
+
+extern bool can_modify_vma(struct vm_area_struct *vma, enum mm_action action,
+		    enum caller_origin called);
+#else
+static inline bool can_modify_mm(struct mm_struct *mm, unsigned long start,
+			  unsigned long end, enum mm_action action,
+			  enum caller_origin called)
+{
+	return true;
+}
+
+static inline bool can_modify_vma(struct vm_area_struct *vma, enum mm_action action,
+		    enum caller_origin called)
+{
+	return true;
+}
+#endif
+
 /* These take the mm semaphore themselves */
 extern int __must_check vm_brk(unsigned long, unsigned long);
 extern int __must_check vm_brk_flags(unsigned long, unsigned long, unsigned long);
diff --git a/mm/mseal.c b/mm/mseal.c
index 615b6e06ab44..3285ef6b95a6 100644
--- a/mm/mseal.c
+++ b/mm/mseal.c
@@ -36,6 +36,66 @@  static bool can_do_mseal(unsigned int types, unsigned int flags)
 	return true;
 }
 
+/*
+ * check if a vma is sealed for modification.
+ * return true, if modification is allowed.
+ */
+bool can_modify_vma(struct vm_area_struct *vma, enum mm_action action,
+		    enum caller_origin called)
+{
+	if (called == ON_BEHALF_OF_KERNEL)
+		return true;
+
+	switch (action) {
+	case MM_ACTION_MPROTECT:
+		if (vma->vm_seals & VM_SEAL_MPROTECT)
+			return false;
+		break;
+
+	case MM_ACTION_MUNMAP:
+		if (vma->vm_seals & VM_SEAL_MUNMAP)
+			return false;
+		break;
+
+	case MM_ACTION_MREMAP:
+		if (vma->vm_seals & VM_SEAL_MREMAP)
+			return false;
+		break;
+
+	case MM_ACTION_MMAP:
+		if (vma->vm_seals & VM_SEAL_MMAP)
+			return false;
+		break;
+	}
+
+	return true;
+}
+
+/*
+ * Check if the vmas of a memory range are allowed to be modified.
+ * the memory ranger can have a gap (unallocated memory).
+ * return true, if it is allowed.
+ */
+bool can_modify_mm(struct mm_struct *mm, unsigned long start, unsigned long end,
+		   enum mm_action action, enum caller_origin called)
+{
+	struct vm_area_struct *vma;
+
+	VMA_ITERATOR(vmi, mm, start);
+
+	if (called == ON_BEHALF_OF_KERNEL)
+		return true;
+
+	/* going through each vma to check */
+	for_each_vma_range(vmi, vma, end) {
+		if (!can_modify_vma(vma, action, called))
+			return false;
+	}
+
+	/* Allow by default. */
+	return true;
+}
+
 /*
  * Check if a seal type can be added to VMA.
  */