Skip to content

Commit be72a57

Browse files
Xu Kuohaipcmoore
authored andcommitted
lsm: Refactor return value of LSM hook vm_enough_memory
To be consistent with most LSM hooks, convert the return value of hook vm_enough_memory to 0 or a negative error code. Before: - Hook vm_enough_memory returns 1 if permission is granted, 0 if not. - LSM_RET_DEFAULT(vm_enough_memory_mm) is 1. After: - Hook vm_enough_memory reutrns 0 if permission is granted, negative error code if not. - LSM_RET_DEFAULT(vm_enough_memory_mm) is 0. Signed-off-by: Xu Kuohai <[email protected]> Reviewed-by: Casey Schaufler <[email protected]> Signed-off-by: Paul Moore <[email protected]>
1 parent 61a1dcd commit be72a57

File tree

5 files changed

+14
-27
lines changed

5 files changed

+14
-27
lines changed

include/linux/lsm_hook_defs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ LSM_HOOK(int, 0, quota_on, struct dentry *dentry)
4848
LSM_HOOK(int, 0, syslog, int type)
4949
LSM_HOOK(int, 0, settime, const struct timespec64 *ts,
5050
const struct timezone *tz)
51-
LSM_HOOK(int, 1, vm_enough_memory, struct mm_struct *mm, long pages)
51+
LSM_HOOK(int, 0, vm_enough_memory, struct mm_struct *mm, long pages)
5252
LSM_HOOK(int, 0, bprm_creds_for_exec, struct linux_binprm *bprm)
5353
LSM_HOOK(int, 0, bprm_creds_from_file, struct linux_binprm *bprm, const struct file *file)
5454
LSM_HOOK(int, 0, bprm_check_security, struct linux_binprm *bprm)

include/linux/security.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ static inline int security_settime64(const struct timespec64 *ts,
634634

635635
static inline int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
636636
{
637-
return __vm_enough_memory(mm, pages, cap_vm_enough_memory(mm, pages));
637+
return __vm_enough_memory(mm, pages, !cap_vm_enough_memory(mm, pages));
638638
}
639639

640640
static inline int security_bprm_creds_for_exec(struct linux_binprm *bprm)

security/commoncap.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,17 +1396,12 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
13961396
* Determine whether the allocation of a new virtual mapping by the current
13971397
* task is permitted.
13981398
*
1399-
* Return: 1 if permission is granted, 0 if not.
1399+
* Return: 0 if permission granted, negative error code if not.
14001400
*/
14011401
int cap_vm_enough_memory(struct mm_struct *mm, long pages)
14021402
{
1403-
int cap_sys_admin = 0;
1404-
1405-
if (cap_capable(current_cred(), &init_user_ns,
1406-
CAP_SYS_ADMIN, CAP_OPT_NOAUDIT) == 0)
1407-
cap_sys_admin = 1;
1408-
1409-
return cap_sys_admin;
1403+
return cap_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
1404+
CAP_OPT_NOAUDIT);
14101405
}
14111406

14121407
/**

security/security.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,15 +1129,14 @@ int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
11291129
int rc;
11301130

11311131
/*
1132-
* The module will respond with a positive value if
1133-
* it thinks the __vm_enough_memory() call should be
1134-
* made with the cap_sys_admin set. If all of the modules
1135-
* agree that it should be set it will. If any module
1136-
* thinks it should not be set it won't.
1132+
* The module will respond with 0 if it thinks the __vm_enough_memory()
1133+
* call should be made with the cap_sys_admin set. If all of the modules
1134+
* agree that it should be set it will. If any module thinks it should
1135+
* not be set it won't.
11371136
*/
11381137
hlist_for_each_entry(hp, &security_hook_heads.vm_enough_memory, list) {
11391138
rc = hp->hook.vm_enough_memory(mm, pages);
1140-
if (rc <= 0) {
1139+
if (rc < 0) {
11411140
cap_sys_admin = 0;
11421141
break;
11431142
}

security/selinux/hooks.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,23 +2202,16 @@ static int selinux_syslog(int type)
22022202
}
22032203

22042204
/*
2205-
* Check that a process has enough memory to allocate a new virtual
2206-
* mapping. 0 means there is enough memory for the allocation to
2207-
* succeed and -ENOMEM implies there is not.
2205+
* Check permission for allocating a new virtual mapping. Returns
2206+
* 0 if permission is granted, negative error code if not.
22082207
*
22092208
* Do not audit the selinux permission check, as this is applied to all
22102209
* processes that allocate mappings.
22112210
*/
22122211
static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
22132212
{
2214-
int rc, cap_sys_admin = 0;
2215-
2216-
rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
2217-
CAP_OPT_NOAUDIT, true);
2218-
if (rc == 0)
2219-
cap_sys_admin = 1;
2220-
2221-
return cap_sys_admin;
2213+
return cred_has_capability(current_cred(), CAP_SYS_ADMIN,
2214+
CAP_OPT_NOAUDIT, true);
22222215
}
22232216

22242217
/* binprm security operations */

0 commit comments

Comments
 (0)