ima: addressing mmap/mprotect concerns
Subsystems (4)
Mimi Zohar
maintainercover ima: addressing mmap/mprotect concerns
Igor Zhbanov's "Should mprotect(..., PROT_EXEC) be checked by IMA?" thread raised concerns about IMA's handling of mmap/mprotect. [1] The kernel calls deny_write_access() to prevent a file already opened for write from being executed and also prevents files being executed from being opened for write. For some reason this does not extend to files being mmap'ed execute. This is a known, well described problem.[2] Jordan Glover commented that the proposed minor LSM "SARA" addresses this issue.[3]
This patch set attempts to address some the IMA mmap/mprotect concerns without locking the mmap'ed files.
Mimi
[1] https://lore.kernel.org/linux-integrity/[email protected]/
[2] ]https://pax.grsecurity.net/docs/mprotect.txt
[3] https://sara.smeso.it/en/latest/
Mimi Zohar (3):
ima: verify mprotect change is consistent with mmap policy
ima: prevent a file already mmap'ed write to be mmap'ed execute
ima: prevent a file already mmap'ed read|execute to be mmap'ed write
include/linux/ima.h | 6 +++-- security/integrity/ima/ima_main.c | 53 ++++++++++++++++++++++++++++++++++++--- security/security.c | 9 +++++-- 3 files changed, 61 insertions(+), 7 deletions(-)
Signature · 1 line
2.7.5
Mimi Zohar
maintainerPATCH 1/3 ima: verify mprotect change is consistent with mmap policy
IMA can be configured to measure and appraise a file's integrity being mmap'ed execute. Files can be mmap'ed read/write and later changed to execute to circumvent IMA's mmap measurement and appraisal policy rules.
To prevent this from happening, this patch similarly calls ima_file_mmap() for mprotect changes.
--- security/security.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
security/security.c +6 -1
@@ -1411,7 +1411,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_mmap (vma->vm_file, prot); }int security_file_lock (struct file *file,unsigned int cmd)
Signature · 1 line
2.7.5
Mimi Zohar
maintainerPATCH 2/3 ima: prevent a file already mmap'ed write to be mmap'ed execute
In reply to Mimi ZoharThe kernel calls deny_write_access() to prevent a file already opened for write from being executed and also prevents files being executed from being opened for write. For some reason this does not extend to files being mmap'ed execute.
From an IMA perspective, measuring/appraising the integrity of a file being mmap'ed execute, without first making sure the file cannot be modified, makes no sense. This patch prevents files, in policy, already mmap'ed write, from being mmap'ed execute.
--- security/integrity/ima/ima_main.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-)
security/integrity/ima/ima_main.c +30 -2
@@ -72,6 +72,27 @@ static int __init hash_setup(char *str) }__setup ("ima_hash=" , hash_setup); +/* Prevent mmap'ing a file execute that is already mmap'ed write */ +static int mmap_violation_check (enum ima_hooks func,struct file *file, +char **pathbuf,const char **pathname, +char *filename) +{ +struct inode *inode; +int rc =0 ; + +if ((func ==MMAP_CHECK ) &&mapping_writably_mapped (file->f_mapping)) { + rc = -ETXTBSY ; + inode =file_inode (file); + +if (!*pathbuf)/* ima_rdwr_violation possibly pre-fetched */ + *pathname =ima_d_path (&file->f_path, pathbuf, + filename); +integrity_audit_msg (AUDIT_INTEGRITY_DATA , inode, *pathname, +"mmap_file" ,"mmapped_writers" , rc,0 ); + } +return rc; +} +/* * ima_rdwr_violation_check *
@@ -270,8 +291,12 @@ static int process_measurement(struct file *file, const struct cred *cred,/* Nothing to do, just return existing appraised status */ if (!action) { -if (must_appraise) - rc =ima_get_cache_status (iint, func); +if (must_appraise) { + rc =mmap_violation_check (func, file, &pathbuf, + &pathname, filename); +if (!rc) + rc =ima_get_cache_status (iint, func); + } goto out_locked; }
@@ -298,6 +323,9 @@ static int process_measurement(struct file *file, const struct cred *cred, rc =ima_appraise_measurement (func, iint, file, pathname, xattr_value, xattr_len);inode_unlock (inode); + + rc =mmap_violation_check (func, file, &pathbuf, &pathname, + filename); }if (action &IMA_AUDIT )ima_audit_measurement (iint, pathname);
Signature · 1 line
2.7.5
Mimi Zohar
maintainerPATCH 3/3 ima: prevent a file already mmap'ed read|execute to be mmap'ed write
In reply to Mimi ZoharThe kernel calls deny_write_access() to prevent a file already opened for write from being executed and also prevents files being executed from being opened for write. For some reason this does not extend to files being mmap'ed execute.
This patch prevents allowing a file in policy, already mmap'ed read|execute or read, from being mmap'ed shared write. It should differentiate between read|execute and read.
--- include/linux/ima.h | 6 ++++-- security/integrity/ima/ima_main.c | 21 ++++++++++++++++++++- security/security.c | 4 ++-- 3 files changed, 26 insertions(+), 5 deletions(-)
include/linux/ima.h +4 -2
@@ -20,7 +20,8 @@ extern int ima_bprm_check(struct linux_binprm *bprm);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_mmap (struct file *file,unsigned long prot, +unsigned long flags);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,
@@ -66,7 +67,8 @@ static inline void ima_file_free(struct file *file)return ; } -static inline int ima_file_mmap (struct file *file,unsigned long prot) +static inline int ima_file_mmap (struct file *file,unsigned long prot, +unsigned long flags) {return 0 ; }
security/integrity/ima/ima_main.c +20 -1
@@ -354,6 +354,7 @@ static int process_measurement(struct file *file, const struct cred *cred,* ima_file_mmap - based on policy, collect/store measurement. * @file: pointer to the file to be measured (May be NULL) * @prot: contains the protection that will be applied by the kernel. +* @flags: * * Measure files being mmapped executable based on the ima_must_measure() * policy decision.
@@ -361,8 +362,9 @@ static int process_measurement(struct file *file, const struct cred *cred,* On success return 0. On integrity appraisal error, assuming the file * is in policy and IMA-appraisal is in enforcing mode, return -EACCES. */ -int ima_file_mmap (struct file *file,unsigned long prot) +int ima_file_mmap (struct file *file,unsigned long prot,unsigned long flags) { +struct inode *inode;u32 secid;if (file && (prot &PROT_EXEC )) {
@@ -371,6 +373,23 @@ int ima_file_mmap(struct file *file, unsigned long prot) 0, MAY_EXEC, MMAP_CHECK); } +/* +* Prevent a file, in policy, mapped read|execute, from being mapped +* write shared. (Should differentiate between read and read|execute.) +*/ +if (file && (prot &PROT_WRITE ) && ((flags &MAP_TYPE ) ==MAP_SHARED ) && +mapping_mapped (file->f_mapping) && + !mapping_writably_mapped (file->f_mapping)) { + inode =file_inode (file); + +if (!ima_must_appraise (inode,MAY_ACCESS ,MMAP_CHECK )) +return 0 ; + +integrity_audit_msg (AUDIT_INTEGRITY_DATA , inode, +file_dentry (file)->d_iname, +"mmap_file" ,"mmapped_readers" , -EACCES ,0 ); +return -EACCES ; + }return 0 ; }
security/security.c +2 -2
@@ -1400,7 +1400,7 @@ int security_mmap_file(struct file *file, unsigned long prot, mmap_prot(file, prot), flags);if (ret)return ret; -return ima_file_mmap (file, prot); +return ima_file_mmap (file, prot, flags); }int security_mmap_addr (unsigned long addr)
@@ -1416,7 +1416,7 @@ int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot, ret =call_int_hook (file_mprotect,0 , vma, reqprot, prot);if (ret)return ret; -return ima_file_mmap (vma->vm_file, prot); +return ima_file_mmap (vma->vm_file, prot,0 ); }int security_file_lock (struct file *file,unsigned int cmd)
Signature · 1 line
2.7.5