selinux: fix memleak in security_read_state_kernel()
authorXiu Jianfeng <xiujianfeng@huawei.com>
Mon, 13 Jun 2022 13:59:53 +0000 (21:59 +0800)
committerPaul Moore <paul@paul-moore.com>
Mon, 13 Jun 2022 23:31:53 +0000 (19:31 -0400)
In this function, it directly returns the result of __security_read_policy
without freeing the allocated memory in *data, cause memory leak issue,
so free the memory if __security_read_policy failed.

Signed-off-by: Xiu Jianfeng <xiujianfeng@huawei.com>
[PM: subject line tweak]
Signed-off-by: Paul Moore <paul@paul-moore.com>
security/selinux/ss/services.c

index 69b2734311a69e7df415b1fd373aa4e84c430666..fe5fcf571c564dbe2b6cbfe56dfa6f5cfc809128 100644 (file)
@@ -4048,6 +4048,7 @@ int security_read_policy(struct selinux_state *state,
 int security_read_state_kernel(struct selinux_state *state,
                               void **data, size_t *len)
 {
+       int err;
        struct selinux_policy *policy;
 
        policy = rcu_dereference_protected(
@@ -4060,5 +4061,11 @@ int security_read_state_kernel(struct selinux_state *state,
        if (!*data)
                return -ENOMEM;
 
-       return __security_read_policy(policy, *data, len);
+       err = __security_read_policy(policy, *data, len);
+       if (err) {
+               vfree(*data);
+               *data = NULL;
+               *len = 0;
+       }
+       return err;
 }