ima: verify mprotect change is consistent with mmap policy
Subsystems (4)
Mimi Zohar
maintainerRFC 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.
--- include/linux/ima.h | 7 ++++++ security/integrity/ima/ima_main.c | 50 +++++++++++++++++++++++++++++++++++++++ security/security.c | 7 +++++- 3 files changed, 63 insertions(+), 1 deletion(-)
include/linux/ima.h +7 -0
@@ -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 +50 -0
56 lines not on this page · raw message
security/security.c +6 -1
@@ -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
On 5/4/20 2:17 PM, Mimi Zohar wrote:
Hi Mimi,
security/integrity/ima/ima_main.c int ima_file_mmap(struct file *file, unsigned long prot) · line 417
Quoted 10 lines
security/integrity/ima/ima_main.c +10
-0+int ima_file_mprotect (struct vm_area_struct *vma,unsigned long prot) +{ +struct ima_template_desc *template; +struct inode *inode; +int result =0 ; +int action; +u32 secid; +int pcr; + +if (vma->vm_file && (prot &PROT_EXEC ) && !(vma->vm_flags &VM_EXEC )) {
Just a suggestion:
Maybe you could do the negative of the above check and return, so that the block within the if statement doesn't have to be indented.
security/integrity/ima/ima_main.c int ima_file_mmap(struct file *file, unsigned long prot) · line 427
Quoted 10 lines
security/integrity/ima/ima_main.c +10
-0+ inode =file_inode (vma->vm_file); + +security_task_getsecid (current, &secid); + action =ima_get_action (inode,current_cred (), secid,MAY_EXEC , +MMAP_CHECK , &pcr, &template,0 ); + +if (action &IMA_APPRAISE_SUBMASK ) + result = -EPERM ; + +if ((action &IMA_APPRAISE_SUBMASK ) || (action &IMA_MEASURE )) {
action is checked for IMA_APPRAISE_SUBMASK bits in the previous if statement. Does it need to be checked again in the above if statement?
Quoted 7 lines
security/integrity/ima/ima_main.c +7
-0+struct file *file = vma->vm_file; +char *pathbuf =NULL ; +const char *pathname; +char filename[NAME_MAX ]; + + pathname =ima_d_path (&file->f_path, &pathbuf, + filename);
security/integrity/ima/ima_main.c int ima_file_mmap(struct file *file, unsigned long prot) · line 444
Quoted 10 lines
security/integrity/ima/ima_main.c +10
-0+integrity_audit_msg (AUDIT_INTEGRITY_DATA , inode, + pathname,"collect_data" , +"failed-mprotect" , result,0 ); + +if (pathbuf) +__putname (pathbuf); + } + } +return result; +}
thanks,
-lakshmi
Mimi Zohar
maintainerQuoted 4 lines · Lakshmi Ramasubramanian
On 5/4/20 2:17 PM, Mimi Zohar wrote:
Hi Mimi,
Quoted 14 lines
security/integrity/ima/ima_main.c int ima_file_mmap(struct file *file, unsigned long prot) · line 417
Quoted 10 lines
security/integrity/ima/ima_main.c +10
-0+int ima_file_mprotect (struct vm_area_struct *vma,unsigned long prot) +{ +struct ima_template_desc *template; +struct inode *inode; +int result =0 ; +int action; +u32 secid; +int pcr; + +if (vma->vm_file && (prot &PROT_EXEC ) && !(vma->vm_flags &VM_EXEC )) {Just a suggestion:
Maybe you could do the negative of the above check and return, so that the block within the if statement doesn't have to be indented.
Good suggestion.
Quoted 14 lines
security/integrity/ima/ima_main.c int ima_file_mmap(struct file *file, unsigned long prot) · line 427
Quoted 11 lines
security/integrity/ima/ima_main.c +10
-0+ inode =file_inode (vma->vm_file); + +security_task_getsecid (current, &secid); + action =ima_get_action (inode,current_cred (), secid,MAY_EXEC , +MMAP_CHECK , &pcr, &template,0 ); + +if (action &IMA_APPRAISE_SUBMASK ) + result = -EPERM ; + +if ((action &IMA_APPRAISE_SUBMASK ) || (action &IMA_MEASURE )) {action is checked for
IMA_APPRAISE_SUBMASKbits in the previous if statement. Does it need to be checked again in the above if statement?
Agreed, the code should be cleaned up here too. In either the measurement or the appraisal case, mprotect modifying the execute mmap flag should be audited, but only in the appraisal case is the request denied.
Mimi
Quoted 22 lines
Quoted 8 lines
security/integrity/ima/ima_main.c +7
-0+struct file *file = vma->vm_file; +char *pathbuf =NULL ; +const char *pathname; +char filename[NAME_MAX ]; + + pathname =ima_d_path (&file->f_path, &pathbuf, + filename);security/integrity/ima/ima_main.c int ima_file_mmap(struct file *file, unsigned long prot) · line 444
Quoted 10 lines
security/integrity/ima/ima_main.c +10
-0+integrity_audit_msg (AUDIT_INTEGRITY_DATA , inode, + pathname,"collect_data" , +"failed-mprotect" , result,0 ); + +if (pathbuf) +__putname (pathbuf); + } + } +return result; +}thanks,
-lakshmi
Quoted 5 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 mprotectPROT_EXECUTEchange, if an mmap appraise policy rule exists.
Just keep in mind that there are other ways to create executable mappings containing controlled code; e.g. PROT_EXEC with MAP_ANONYMOUS, or writing to /proc/self/mem (which is a debugging API that works entirely without ever making the VMA writable - I had an old series to provide LSM hooks for that stuff at
<https://lore.kernel.org/lkml/[email protected]/>,
but I guess I must have forgotten about it or something...).
Mimi Zohar
maintainerHi Jann,
Quoted 7 lines · Jann Horn
Quoted 5 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 mprotectPROT_EXECUTEchange, if an mmap appraise policy rule exists.
Quoted 7 lines
Just keep in mind that there are other ways to create executable mappings containing controlled code; e.g.
PROT_EXECwithMAP_ANONYMOUS, or writing to/proc/self/mem(which is a debugging API that works entirely without ever making the VMA writable - I had an old series to provide LSM hooks for that stuff at
<https://lore.kernel.org/lkml/[email protected]/>,
but I guess I must have forgotten about it or something...).
Sure. These sound like memory attacks, which should be closed, but are probably out of scope for IMA.
thanks,
Mimi