Skip to content

Commit 98de59b

Browse files
author
Al Viro
committed
take calculation of final prot in security_mmap_file() into a helper
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 9ac4ed4 commit 98de59b

1 file changed

Lines changed: 28 additions & 18 deletions

File tree

‎security/security.c‎

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -660,36 +660,46 @@ int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
660660
return security_ops->file_ioctl(file, cmd, arg);
661661
}
662662

663-
int security_mmap_file(struct file *file, unsigned long prot,
664-
unsigned long flags)
663+
static inline unsigned long mmap_prot(struct file *file, unsigned long prot)
665664
{
666-
unsigned long reqprot = prot;
667-
int ret;
668665
/*
669-
* Does the application expect PROT_READ to imply PROT_EXEC?
670-
*
671-
* (the exception is when the underlying filesystem is noexec
672-
* mounted, in which case we dont add PROT_EXEC.)
666+
* Does we have PROT_READ and does the application expect
667+
* it to imply PROT_EXEC? If not, nothing to talk about...
673668
*/
674-
if (!(reqprot & PROT_READ))
675-
goto out;
669+
if ((prot & (PROT_READ | PROT_EXEC)) != PROT_READ)
670+
return prot;
676671
if (!(current->personality & READ_IMPLIES_EXEC))
677-
goto out;
678-
if (!file) {
679-
prot |= PROT_EXEC;
680-
} else if (!(file->f_path.mnt->mnt_flags & MNT_NOEXEC)) {
672+
return prot;
673+
/*
674+
* if that's an anonymous mapping, let it.
675+
*/
676+
if (!file)
677+
return prot | PROT_EXEC;
678+
/*
679+
* ditto if it's not on noexec mount, except that on !MMU we need
680+
* BDI_CAP_EXEC_MMAP (== VM_MAYEXEC) in this case
681+
*/
682+
if (!(file->f_path.mnt->mnt_flags & MNT_NOEXEC)) {
681683
#ifndef CONFIG_MMU
682684
unsigned long caps = 0;
683685
struct address_space *mapping = file->f_mapping;
684686
if (mapping && mapping->backing_dev_info)
685687
caps = mapping->backing_dev_info->capabilities;
686688
if (!(caps & BDI_CAP_EXEC_MAP))
687-
goto out;
689+
return prot;
688690
#endif
689-
prot |= PROT_EXEC;
691+
return prot | PROT_EXEC;
690692
}
691-
out:
692-
ret = security_ops->mmap_file(file, reqprot, prot, flags);
693+
/* anything on noexec mount won't get PROT_EXEC */
694+
return prot;
695+
}
696+
697+
int security_mmap_file(struct file *file, unsigned long prot,
698+
unsigned long flags)
699+
{
700+
int ret;
701+
ret = security_ops->mmap_file(file, prot,
702+
mmap_prot(file, prot), flags);
693703
if (ret)
694704
return ret;
695705
return ima_file_mmap(file, prot);

0 commit comments

Comments
 (0)