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

From: jeffxu
Date: Mon Oct 16 2023 - 10:39:14 EST


From: Jeff Xu <jeffxu@xxxxxxxxxx>

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@xxxxxxxxxx>
---
include/linux/mm.h | 34 ++++++++++++++++++++++++++
mm/mseal.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 94 insertions(+)

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.
*/
--
2.42.0.609.gbb76f46606-goog