Skip to contentSkip to conversation

ima: verify mprotect change is consistent with mmap policy

RFC PATCH In review Mimi Zohar 2 participants 2 messages May 5, 2020

Subsystems (4)
lore
Share & tools
lore

Mimi Zohar

author maintainer
#0 lore raw

RFC PATCH 1/1 ima: verify mprotect change is consistent with mmap policy

Files can be mmap'ed read/write and later changed to execute to circumvent IMA's mmap appraise policy rules. Due to locking issues (mmap semaphore would be taken prior to i_mutex), files can not be measured or appraised at this point. Eliminate this integrity gap, by denying the mprotect PROT_EXECUTE change, if an mmap appraise policy rule exists.

On mprotect change success, return 0. On failure, return -EACESS.

---
Changelog v1:
- Reverse tests to remove code indentation. (Lakshmi Ramasubramanian)
- General code cleanup, including adding comments.

 include/linux/ima.h               |  7 ++++++
 security/integrity/ima/ima_main.c | 51 +++++++++++++++++++++++++++++++++++++++
 security/security.c               |  7 +++++-
 3 files changed, 64 insertions(+), 1 deletion(-)
include/linux/ima.h +7 -0
index aefe758f4466..9164e1534ec9 100644
@@ -18,6 +18,7 @@ extern int ima_file_check(struct file *file, int mask);
 extern void ima_post_create_tmpfile(struct inode *inode);
 extern void ima_file_free(struct file *file);
 extern int ima_file_mmap(struct file *file, unsigned long prot);
+extern int ima_file_mprotect(struct vm_area_struct *vma, unsigned long prot);
 extern int ima_load_data(enum kernel_load_data_id id);
 extern int ima_read_file(struct file *file, enum kernel_read_file_id id);
 extern int ima_post_read_file(struct file *file, void *buf, loff_t size,
@@ -70,6 +71,12 @@ static inline int ima_file_mmap(struct file *file, unsigned long prot)
 	return 0;
 }
 
+static inline int ima_file_mprotect(struct vm_area_struct *vma,
+				    unsigned long prot)
+{
+	return 0;
+}
+
 static inline int ima_load_data(enum kernel_load_data_id id)
 {
 	return 0;
security/integrity/ima/ima_main.c +51 -0

57 lines not on this page · raw message

security/security.c +6 -1
index 7fed24b9d57e..dd0917c5bfe9 100644
@@ -1512,7 +1512,12 @@ int security_mmap_addr(unsigned long addr)
 int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
 			    unsigned long prot)
 {
-	return call_int_hook(file_mprotect, 0, vma, reqprot, prot);
+	int ret;
+
+	ret = call_int_hook(file_mprotect, 0, vma, reqprot, prot);
+	if (ret)
+		return ret;
+	return ima_file_mprotect(vma, prot);
 }
 
 int security_file_lock(struct file *file, unsigned int cmd)
Signature · 1 line
2.7.5
Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Mimi Zohar

Files can be mmap'ed read/write and later changed to execute to circumvent IMA's mmap appraise policy rules. Due to locking issues (mmap semaphore would be taken prior to i_mutex), files can not be measured or appraised at this point. Eliminate this integrity gap, by denying the mprotect PROT_EXECUTE change, if an mmap appraise policy rule exists.

On mprotect change success, return 0. On failure, return -EACESS.

---
Changelog v1:
- Reverse tests to remove code indentation. (Lakshmi Ramasubramanian)
- General code cleanup, including adding comments.

Quoted 4 lines
  include/linux/ima.h               |  7 ++++++
  security/integrity/ima/ima_main.c | 51 +++++++++++++++++++++++++++++++++++++++
  security/security.c               |  7 +++++-
  3 files changed, 64 insertions(+), 1 deletion(-)