[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20231016143828.647848-4-jeffxu@chromium.org>
Date: Mon, 16 Oct 2023 14:38:22 +0000
From: jeffxu@...omium.org
To: akpm@...ux-foundation.org,
keescook@...omium.org,
sroettger@...gle.com
Cc: jeffxu@...gle.com,
jorgelo@...omium.org,
groeck@...omium.org,
linux-kernel@...r.kernel.org,
linux-kselftest@...r.kernel.org,
linux-mm@...ck.org,
jannh@...gle.com,
surenb@...gle.com,
alex.sierra@....com,
apopple@...dia.com,
aneesh.kumar@...ux.ibm.com,
axelrasmussen@...gle.com,
ben@...adent.org.uk,
catalin.marinas@....com,
david@...hat.com,
dwmw@...zon.co.uk,
ying.huang@...el.com,
hughd@...gle.com,
joey.gouly@....com,
corbet@....net,
wangkefeng.wang@...wei.com,
Liam.Howlett@...cle.com,
torvalds@...ux-foundation.org,
lstoakes@...il.com,
willy@...radead.org,
mawupeng1@...wei.com,
linmiaohe@...wei.com,
namit@...are.com,
peterx@...hat.com,
peterz@...radead.org,
ryan.roberts@....com,
shr@...kernel.io,
vbabka@...e.cz,
xiujianfeng@...wei.com,
yu.ma@...el.com,
zhangpeng362@...wei.com,
dave.hansen@...el.com,
luto@...nel.org,
linux-hardening@...r.kernel.org
Subject: [RFC PATCH v1 3/8] mseal: add can_modify_mm and can_modify_vma
From: Jeff Xu <jeffxu@...gle.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@...gle.com>
---
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
Powered by blists - more mailing lists