lsm: Refactor return value of LSM hook vm_enough_memory
authorXu Kuohai <xukuohai@huawei.com>
Wed, 24 Jul 2024 02:06:58 +0000 (10:06 +0800)
committerPaul Moore <paul@paul-moore.com>
Wed, 31 Jul 2024 18:46:51 +0000 (14:46 -0400)
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 <xukuohai@huawei.com>
Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
Signed-off-by: Paul Moore <paul@paul-moore.com>
include/linux/lsm_hook_defs.h
include/linux/security.h
security/commoncap.c
security/security.c
security/selinux/hooks.c

index 12c81be78eb947ba2cd5da50ed3f4d8aad250823..63e2656d1d5679e4ce28a3408777d4a059cff382 100644 (file)
@@ -48,7 +48,7 @@ LSM_HOOK(int, 0, quota_on, struct dentry *dentry)
 LSM_HOOK(int, 0, syslog, int type)
 LSM_HOOK(int, 0, settime, const struct timespec64 *ts,
         const struct timezone *tz)
-LSM_HOOK(int, 1, vm_enough_memory, struct mm_struct *mm, long pages)
+LSM_HOOK(int, 0, vm_enough_memory, struct mm_struct *mm, long pages)
 LSM_HOOK(int, 0, bprm_creds_for_exec, struct linux_binprm *bprm)
 LSM_HOOK(int, 0, bprm_creds_from_file, struct linux_binprm *bprm, const struct file *file)
 LSM_HOOK(int, 0, bprm_check_security, struct linux_binprm *bprm)
index 1390f1efb4f0753c646ac2ce84ada9fa1090d9f1..62233fec8ead77f208b1cce2ef0dd6cbfbc86719 100644 (file)
@@ -634,7 +634,7 @@ static inline int security_settime64(const struct timespec64 *ts,
 
 static inline int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
 {
-       return __vm_enough_memory(mm, pages, cap_vm_enough_memory(mm, pages));
+       return __vm_enough_memory(mm, pages, !cap_vm_enough_memory(mm, pages));
 }
 
 static inline int security_bprm_creds_for_exec(struct linux_binprm *bprm)
index 162d96b3a6768bc66c235c4233ef9f274ddf0c49..cefad323a0b1b76908d28d1d4335471db0c796ea 100644 (file)
@@ -1396,17 +1396,12 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
  * Determine whether the allocation of a new virtual mapping by the current
  * task is permitted.
  *
- * Return: 1 if permission is granted, 0 if not.
+ * Return: 0 if permission granted, negative error code if not.
  */
 int cap_vm_enough_memory(struct mm_struct *mm, long pages)
 {
-       int cap_sys_admin = 0;
-
-       if (cap_capable(current_cred(), &init_user_ns,
-                               CAP_SYS_ADMIN, CAP_OPT_NOAUDIT) == 0)
-               cap_sys_admin = 1;
-
-       return cap_sys_admin;
+       return cap_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
+                          CAP_OPT_NOAUDIT);
 }
 
 /**
index 93ed7670fbc9fa7951a587e47e7284c8066ee250..b2f0e9a5786406836d7c7409a195a2b92f96f445 100644 (file)
@@ -1129,15 +1129,14 @@ int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
        int rc;
 
        /*
-        * The module will respond with a positive value if
-        * it thinks the __vm_enough_memory() call should be
-        * made with the cap_sys_admin set. If all of the modules
-        * agree that it should be set it will. If any module
-        * thinks it should not be set it won't.
+        * The module will respond with 0 if it thinks the __vm_enough_memory()
+        * call should be made with the cap_sys_admin set. If all of the modules
+        * agree that it should be set it will. If any module thinks it should
+        * not be set it won't.
         */
        hlist_for_each_entry(hp, &security_hook_heads.vm_enough_memory, list) {
                rc = hp->hook.vm_enough_memory(mm, pages);
-               if (rc <= 0) {
+               if (rc < 0) {
                        cap_sys_admin = 0;
                        break;
                }
index 0939816e967120969223053e21ead9aa1e1d7691..af7467cdd181ffc9f6f5381848c719fccfbd9ad3 100644 (file)
@@ -2202,23 +2202,16 @@ static int selinux_syslog(int type)
 }
 
 /*
- * Check that a process has enough memory to allocate a new virtual
- * mapping. 0 means there is enough memory for the allocation to
- * succeed and -ENOMEM implies there is not.
+ * Check permission for allocating a new virtual mapping. Returns
+ * 0 if permission is granted, negative error code if not.
  *
  * Do not audit the selinux permission check, as this is applied to all
  * processes that allocate mappings.
  */
 static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
 {
-       int rc, cap_sys_admin = 0;
-
-       rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
-                                CAP_OPT_NOAUDIT, true);
-       if (rc == 0)
-               cap_sys_admin = 1;
-
-       return cap_sys_admin;
+       return cred_has_capability(current_cred(), CAP_SYS_ADMIN,
+                                  CAP_OPT_NOAUDIT, true);
 }
 
 /* binprm security operations */