selinux: Clean up GFP flag usage
v2 cover selinux: Clean up GFP flag usage
This series corrects a few places where GFP_ATOMIC was used instead of GFP_KERNEL for no reason and removes the no longer needed GFP flag argument from str_read().
v2: convert a few more occurences found in services.c
Ondrej Mosnacek (2):
selinux: switch unnecessary GFP_ATOMIC allocs to GFP_KERNEL
selinux: drop the gfp_t argument from str_read()
security/selinux/hooks.c | 6 ++--- security/selinux/ss/policydb.c | 42 +++++++++++++++++----------------- security/selinux/ss/services.c | 4 ++-- 3 files changed, 26 insertions(+), 26 deletions(-)
Signature · 1 line
2.26.2
PATCH v2 1/2 selinux: switch unnecessary GFP_ATOMIC allocs to GFP_KERNEL
There seems to be no reason to use GFP_ATOMIC in these cases.
--- security/selinux/hooks.c | 6 +++--- security/selinux/ss/policydb.c | 10 +++++----- security/selinux/ss/services.c | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-)
security/selinux/hooks.c +3 -3
@@ -3171,7 +3171,7 @@ static int selinux_inode_setxattr(struct dentry *dentry, const char *name, audit_size =0 ; } ab =audit_log_start (audit_context (), -GFP_ATOMIC ,AUDIT_SELINUX_ERR ); +GFP_KERNEL ,AUDIT_SELINUX_ERR );audit_log_format (ab,"op=setxattr invalid_context=" );audit_log_n_untrustedstring (ab, value, audit_size);audit_log_end (ab);
@@ -6388,7 +6388,7 @@ static int selinux_setprocattr(const char *name, void *value, size_t size)else audit_size = size; ab =audit_log_start (audit_context (), -GFP_ATOMIC , +GFP_KERNEL ,AUDIT_SELINUX_ERR );audit_log_format (ab,"op=fscreate invalid_context=" );audit_log_n_untrustedstring (ab, value, audit_size);
@@ -6854,7 +6854,7 @@ static int selinux_lockdown(enum lockdown_reason what)if (WARN (invalid_reason,"Invalid lockdown reason" )) {audit_log (audit_context (), -GFP_ATOMIC ,AUDIT_SELINUX_ERR , +GFP_KERNEL ,AUDIT_SELINUX_ERR ,"lockdown_reason=invalid" );return -EINVAL ; }
security/selinux/ss/policydb.c +5 -5
@@ -1577,7 +1577,7 @@ static int sens_read(struct policydb *p, struct symtab *s, void *fp)__le32 buf[2 ];u32 len; - levdatum =kzalloc (sizeof (*levdatum),GFP_ATOMIC ); + levdatum =kzalloc (sizeof (*levdatum),GFP_KERNEL );if (!levdatum)return -ENOMEM ;
@@ -1588,12 +1588,12 @@ static int sens_read(struct policydb *p, struct symtab *s, void *fp) len =le32_to_cpu (buf[0 ]); levdatum->isalias =le32_to_cpu (buf[1 ]); - rc =str_read (&key,GFP_ATOMIC , fp, len); + rc =str_read (&key,GFP_KERNEL , fp, len);if (rc) goto bad; rc = -ENOMEM ; - levdatum->level =kmalloc (sizeof (*levdatum->level),GFP_ATOMIC ); + levdatum->level =kmalloc (sizeof (*levdatum->level),GFP_KERNEL );if (!levdatum->level) goto bad;
@@ -1618,7 +1618,7 @@ static int cat_read(struct policydb *p, struct symtab *s, void *fp)__le32 buf[3 ];u32 len; - catdatum =kzalloc (sizeof (*catdatum),GFP_ATOMIC ); + catdatum =kzalloc (sizeof (*catdatum),GFP_KERNEL );if (!catdatum)return -ENOMEM ;
@@ -1630,7 +1630,7 @@ static int cat_read(struct policydb *p, struct symtab *s, void *fp) catdatum->value =le32_to_cpu (buf[1 ]); catdatum->isalias =le32_to_cpu (buf[2 ]); - rc =str_read (&key,GFP_ATOMIC , fp, len); + rc =str_read (&key,GFP_KERNEL , fp, len);if (rc) goto bad;
security/selinux/ss/services.c +2 -2
@@ -109,7 +109,7 @@ static int selinux_set_mapping(struct policydb *pol, i++;/* Allocate space for the class records, plus one for class zero */ - out_map->mapping =kcalloc (++i,sizeof (*out_map->mapping),GFP_ATOMIC ); + out_map->mapping =kcalloc (++i,sizeof (*out_map->mapping),GFP_KERNEL );if (!out_map->mapping)return -ENOMEM ;
@@ -2982,7 +2982,7 @@ int security_set_bools(struct selinux_state *state, u32 len, int *values)int old_state = newpolicy->policydb.bool_val_to_struct[i]->state;if (new_state != old_state) { - audit_log(audit_context(), GFP_ATOMIC, + audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_CONFIG_CHANGE, "bool=%s val=%d old_val=%d auid=%u ses=%u", sym_name(&newpolicy->policydb, SYM_BOOLS, i),
Signature · 1 line
2.26.2
Stephen Smalley
maintainerRe: [PATCH v2 1/2] selinux: switch unnecessary GFP_ATOMIC allocs to GFP_KERNEL
Quoted 15 lines · Ondrej Mosnacek
There seems to be no reason to use
GFP_ATOMICin these cases.Signed-off-by Ondrej Mosnacek <[email protected]>--- security/selinux/hooks.c | 6 +++--- security/selinux/ss/policydb.c | 10 +++++----- security/selinux/ss/services.c | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-)security/selinux/hooks.c +0
-0
security/selinux/hooks.c static int selinux_lockdown(enum lockdown_reason what) · line 6860
Quoted 8 lines
security/selinux/hooks.c +1
-1@@ -6854,7 +6854,7 @@ static int selinux_lockdown(enum lockdown_reason what)if (WARN (invalid_reason,"Invalid lockdown reason" )) {audit_log (audit_context (), -GFP_ATOMIC ,AUDIT_SELINUX_ERR , +GFP_KERNEL ,AUDIT_SELINUX_ERR ,"lockdown_reason=invalid" );return -EINVAL ; }
Have you audited all callers of security_locked_down() to ensure that they are never holding any locks around the call? That's the only one I saw that might be a problem now or in the future.
Quoted 6 lines
security/selinux/ss/services.c +0
-0@@ -109,7 +109,7 @@ static int selinux_set_mapping(struct policydb *pol,
security/selinux/ss/services.c int security_set_bools(struct selinux_state *state, u32 len, int *values) · line 2988
Quoted 8 lines
security/selinux/ss/services.c +1
-1@@ -2982,7 +2982,7 @@ int security_set_bools(struct selinux_state *state, u32 len, int *values)int old_state = newpolicy->policydb.bool_val_to_struct[i]->state;if (new_state != old_state) { - audit_log(audit_context(), GFP_ATOMIC, + audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_CONFIG_CHANGE, "bool=%s val=%d old_val=%d auid=%u ses=%u", sym_name(&newpolicy->policydb, SYM_BOOLS, i),
Note that this one shouldn't be back-ported prior to my refactoring patches because previously this was called while holding the policy rwlock.
Re: [PATCH v2 1/2] selinux: switch unnecessary GFP_ATOMIC allocs to GFP_KERNEL
Quoted 17 lines · Stephen Smalley
Quoted 16 lines · Ondrej Mosnacek
There seems to be no reason to use
GFP_ATOMICin these cases.Signed-off-by Ondrej Mosnacek <[email protected]>--- security/selinux/hooks.c | 6 +++--- security/selinux/ss/policydb.c | 10 +++++----- security/selinux/ss/services.c | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-)security/selinux/hooks.c +0
-0@@ -6854,7 +6854,7 @@ static int selinux_lockdown(enum lockdown_reason what)
Quoted 11 lines
security/selinux/hooks.c static int selinux_lockdown(enum lockdown_reason what) · line 6860
Quoted 7 lines
security/selinux/hooks.c +1
-1if (WARN (invalid_reason,"Invalid lockdown reason" )) {audit_log (audit_context (), -GFP_ATOMIC ,AUDIT_SELINUX_ERR , +GFP_KERNEL ,AUDIT_SELINUX_ERR ,"lockdown_reason=invalid" );return -EINVAL ; }Have you audited all callers of
security_locked_down()to ensure that they are never holding any locks around the call? That's the only one I saw that might be a problem now or in the future.
Hm... didn't realize there were so many :) I'll try to go over them.
As a side note, it would be nice if we had the may (not) sleep requirements documented somehow for each hook at the LSM level, ideally with a might_sleep() check at the beginning of each secuity_*() function that is expected to be called only from a non-atomic context... Some hooks already have arguments that specify this (inode_permission, inode_follow_link), but for others it is just implicit. I'll put this on my long-term TODO list...
--
Ondrej Mosnacek
Software Engineer, Platform Security - SELinux kernel Red Hat, Inc.
Stephen Smalley
maintainerRe: [PATCH v2 1/2] selinux: switch unnecessary GFP_ATOMIC allocs to GFP_KERNEL
Quoted 34 lines · Ondrej Mosnacek
Quoted 17 lines · Stephen Smalley
Quoted 16 lines · Ondrej Mosnacek
There seems to be no reason to use
GFP_ATOMICin these cases.Signed-off-by Ondrej Mosnacek <[email protected]>--- security/selinux/hooks.c | 6 +++--- security/selinux/ss/policydb.c | 10 +++++----- security/selinux/ss/services.c | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-)security/selinux/hooks.c +0
-0@@ -6854,7 +6854,7 @@ static int selinux_lockdown(enum lockdown_reason what)Quoted 11 lines
security/selinux/hooks.c static int selinux_lockdown(enum lockdown_reason what) · line 6860
Quoted 7 lines
security/selinux/hooks.c +1
-1if (WARN (invalid_reason,"Invalid lockdown reason" )) {audit_log (audit_context (), -GFP_ATOMIC ,AUDIT_SELINUX_ERR , +GFP_KERNEL ,AUDIT_SELINUX_ERR ,"lockdown_reason=invalid" );return -EINVAL ; }Have you audited all callers of
security_locked_down()to ensure that they are never holding any locks around the call? That's the only one I saw that might be a problem now or in the future.Hm... didn't realize there were so many :) I'll try to go over them.
Quoted 7 lines
As a side note, it would be nice if we had the may (not) sleep requirements documented somehow for each hook at the LSM level, ideally with a
might_sleep()check at the beginning of each secuity_*() function that is expected to be called only from a non-atomic context... Some hooks already have arguments that specify this (inode_permission,inode_follow_link), but for others it is just implicit. I'll put this on my long-term TODO list...
Generally the core SELinux permission checking code assumes it can be called from any context (including hardirq) and under any locking conditions, and hooks that are for permission checking (not security blob allocation/management) do the same. This allows permission checks to be performed while locks are held by the caller. and from arbitrary contexts I'd be inclined to just leave selinux_lockdown unchanged given that it might be called anywhere much like capable().
Paul Moore
maintainerRe: [PATCH v2 1/2] selinux: switch unnecessary GFP_ATOMIC allocs to GFP_KERNEL
Quoted 7 lines · Stephen Smalley
Generally the core SELinux permission checking code assumes it can be called from any context (including hardirq) and under any locking conditions, and hooks that are for permission checking (not security blob allocation/management) do the same. This allows permission checks to be performed while locks are held by the caller. and from arbitrary contexts I'd be inclined to just leave
selinux_lockdownunchanged given that it might be called anywhere much likecapable().
Agreed. The code paths relating to policy load, etc. are good candidates for an ATOMIC->KERNEL conversion, assuming no other constraints, but I'm somewhat nervous of converting stuff in hook.c that doesn't have a hard sleep-ability defined.
The other question about this patchset is motivation: were you seeing problem reports relating to SELinux memory failures when the system was under pressure, or is this preemptive?
--
paul moore
www.paul-moore.com
Re: [PATCH v2 1/2] selinux: switch unnecessary GFP_ATOMIC allocs to GFP_KERNEL
Quoted 10 lines · Paul Moore
Quoted 7 lines · Stephen Smalley
Generally the core SELinux permission checking code assumes it can be called from any context (including hardirq) and under any locking conditions, and hooks that are for permission checking (not security blob allocation/management) do the same. This allows permission checks to be performed while locks are held by the caller. and from arbitrary contexts I'd be inclined to just leave
selinux_lockdownunchanged given that it might be called anywhere much likecapable().
Quoted 4 lines
Agreed. The code paths relating to policy load, etc. are good candidates for an
ATOMIC->KERNELconversion, assuming no other constraints, but I'm somewhat nervous of converting stuff inhook.cthat doesn't have a hard sleep-ability defined.
OK, I can drop the selinux_lockdown() hunk then. The other hooks.c hunks both have an existing GFP_KERNEL allocation in the same function, so I'd tend to keep them for consistency. Or do you want me to rather convert the GFP_KERNELs to GFP_ATOMICs there?
Quoted 4 lines
The other question about this patchset is motivation: were you seeing problem reports relating to SELinux memory failures when the system was under pressure, or is this preemptive?
No, I just went over the GFP_* usage in security/selinux/ when scoping how many GFP_ATOMIC ones could be changed to GFP_KERNEL after the refcount patch and found these that could be switched right away.
--
Ondrej Mosnacek
Software Engineer, Platform Security - SELinux kernel Red Hat, Inc.
Stephen Smalley
maintainerRe: [PATCH v2 1/2] selinux: switch unnecessary GFP_ATOMIC allocs to GFP_KERNEL
Quoted 17 lines · Ondrej Mosnacek
Quoted 10 lines · Paul Moore
Quoted 7 lines · Stephen Smalley
Generally the core SELinux permission checking code assumes it can be called from any context (including hardirq) and under any locking conditions, and hooks that are for permission checking (not security blob allocation/management) do the same. This allows permission checks to be performed while locks are held by the caller. and from arbitrary contexts I'd be inclined to just leave
selinux_lockdownunchanged given that it might be called anywhere much likecapable().Quoted 4 lines
Agreed. The code paths relating to policy load, etc. are good candidates for an
ATOMIC->KERNELconversion, assuming no other constraints, but I'm somewhat nervous of converting stuff inhook.cthat doesn't have a hard sleep-ability defined.
Quoted 4 lines
OK, I can drop the
selinux_lockdown()hunk then. The otherhooks.chunks both have an existingGFP_KERNELallocation in the same function, so I'd tend to keep them for consistency. Or do you want me to rather convert theGFP_KERNELstoGFP_ATOMICsthere?
No, I was fine with the other parts of the patch just not the lockdown one, because it can be called from many places like capable().
Paul Moore
maintainerRe: [PATCH v2 1/2] selinux: switch unnecessary GFP_ATOMIC allocs to GFP_KERNEL
In reply to Ondrej MosnacekQuoted 16 lines · Ondrej Mosnacek
Quoted 10 lines · Paul Moore
Quoted 7 lines · Stephen Smalley
Generally the core SELinux permission checking code assumes it can be called from any context (including hardirq) and under any locking conditions, and hooks that are for permission checking (not security blob allocation/management) do the same. This allows permission checks to be performed while locks are held by the caller. and from arbitrary contexts I'd be inclined to just leave
selinux_lockdownunchanged given that it might be called anywhere much likecapable().Quoted 4 lines
Agreed. The code paths relating to policy load, etc. are good candidates for an
ATOMIC->KERNELconversion, assuming no other constraints, but I'm somewhat nervous of converting stuff inhook.cthat doesn't have a hard sleep-ability defined.
Quoted 4 lines
OK, I can drop the
selinux_lockdown()hunk then. The otherhooks.chunks both have an existingGFP_KERNELallocation in the same function, so I'd tend to keep them for consistency. Or do you want me to rather convert theGFP_KERNELstoGFP_ATOMICsthere?
I was speaking generically in that I'm not comfortable converting ATOMIC allocations to KERNEL allocations in the hooks.c layer without some guarantee/constraint. In the case of both _setxattr and _setprocattr we are already doing KERNEL allocations in those functions so switching the audit allocations to KERNEL isn't necessarily changing the behavior of the SELinux hook, it can sleep now regardless.
Quoted 7 lines
Quoted 3 lines
The other question about this patchset is motivation: were you seeing problem reports relating to SELinux memory failures when the system was under pressure, or is this preemptive?
No, I just went over the GFP_* usage in
security/selinux/when scoping how manyGFP_ATOMICones could be changed toGFP_KERNELafter the refcount patch and found these that could be switched right away.
Okay, thanks.
Signature · 2 lines
paul moore www.paul-moore.com
PATCH v2 2/2 selinux: drop the gfp_t argument from str_read()
In reply to Ondrej MosnacekNow that all callers specify GFP_KERNEL here, the argument is no longer necessary and the function can just use GFP_KERNEL directly.
--- security/selinux/ss/policydb.c | 36 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 18 deletions(-)
security/selinux/ss/policydb.c +18 -18
@@ -1074,7 +1074,7 @@ out:* binary representation file. */ -static int str_read (char **strp,gfp_t flags,void *fp,u32 len) +static int str_read (char **strp,void *fp,u32 len) {int rc;char *str;
@@ -1082,7 +1082,7 @@ static int str_read(char **strp, gfp_t flags, void *fp, u32 len)if ((len ==0 ) || (len == (u32)-1 ))return -EINVAL ; - str =kmalloc (len +1 , flags | __GFP_NOWARN); + str =kmalloc (len +1 ,GFP_KERNEL | __GFP_NOWARN);if (!str)return -ENOMEM ;
@@ -1116,7 +1116,7 @@ static int perm_read(struct policydb *p, struct symtab *s, void *fp) len =le32_to_cpu (buf[0 ]); perdatum->value =le32_to_cpu (buf[1 ]); - rc =str_read (&key,GFP_KERNEL , fp, len); + rc =str_read (&key, fp, len);if (rc) goto bad;
@@ -1155,7 +1155,7 @@ static int common_read(struct policydb *p, struct symtab *s, void *fp) goto bad; comdatum->permissions.nprim =le32_to_cpu (buf[2 ]); - rc =str_read (&key,GFP_KERNEL , fp, len); + rc =str_read (&key, fp, len);if (rc) goto bad;
@@ -1322,12 +1322,12 @@ static int class_read(struct policydb *p, struct symtab *s, void *fp) ncons =le32_to_cpu (buf[5 ]); - rc =str_read (&key,GFP_KERNEL , fp, len); + rc =str_read (&key, fp, len);if (rc) goto bad;if (len2) { - rc =str_read (&cladatum->comkey,GFP_KERNEL , fp, len2); + rc =str_read (&cladatum->comkey, fp, len2);if (rc) goto bad;
@@ -1413,7 +1413,7 @@ static int role_read(struct policydb *p, struct symtab *s, void *fp)if (p->policyvers >=POLICYDB_VERSION_BOUNDARY ) role->bounds =le32_to_cpu (buf[2 ]); - rc =str_read (&key,GFP_KERNEL , fp, len); + rc =str_read (&key, fp, len);if (rc) goto bad;
@@ -1479,7 +1479,7 @@ static int type_read(struct policydb *p, struct symtab *s, void *fp) typdatum->primary =le32_to_cpu (buf[2 ]); } - rc =str_read (&key,GFP_KERNEL , fp, len); + rc =str_read (&key, fp, len);if (rc) goto bad;
@@ -1543,7 +1543,7 @@ static int user_read(struct policydb *p, struct symtab *s, void *fp)if (p->policyvers >=POLICYDB_VERSION_BOUNDARY ) usrdatum->bounds =le32_to_cpu (buf[2 ]); - rc =str_read (&key,GFP_KERNEL , fp, len); + rc =str_read (&key, fp, len);if (rc) goto bad;
@@ -1588,7 +1588,7 @@ static int sens_read(struct policydb *p, struct symtab *s, void *fp) len =le32_to_cpu (buf[0 ]); levdatum->isalias =le32_to_cpu (buf[1 ]); - rc =str_read (&key,GFP_KERNEL , fp, len); + rc =str_read (&key, fp, len);if (rc) goto bad;
@@ -1630,7 +1630,7 @@ static int cat_read(struct policydb *p, struct symtab *s, void *fp) catdatum->value =le32_to_cpu (buf[1 ]); catdatum->isalias =le32_to_cpu (buf[2 ]); - rc =str_read (&key,GFP_KERNEL , fp, len); + rc =str_read (&key, fp, len);if (rc) goto bad;
@@ -1904,7 +1904,7 @@ static int filename_trans_read_helper_compat(struct policydb *p, void *fp) len =le32_to_cpu (buf[0 ]);/* path component string */ - rc =str_read (&name,GFP_KERNEL , fp, len); + rc =str_read (&name, fp, len);if (rc)return rc;
@@ -1988,7 +1988,7 @@ static int filename_trans_read_helper(struct policydb *p, void *fp) len =le32_to_cpu (buf[0 ]);/* path component string */ - rc =str_read (&name,GFP_KERNEL , fp, len); + rc =str_read (&name, fp, len);if (rc)return rc;
@@ -2128,7 +2128,7 @@ static int genfs_read(struct policydb *p, void *fp)if (!newgenfs) goto out; - rc =str_read (&newgenfs->fstype,GFP_KERNEL , fp, len); + rc =str_read (&newgenfs->fstype, fp, len);if (rc) goto out;
@@ -2167,7 +2167,7 @@ static int genfs_read(struct policydb *p, void *fp)if (!newc) goto out; - rc =str_read (&newc->u.name,GFP_KERNEL , fp, len); + rc =str_read (&newc->u.name, fp, len);if (rc) goto out;
@@ -2261,7 +2261,7 @@ static int ocontext_read(struct policydb *p, struct policydb_compat_info *info, goto out; len =le32_to_cpu (buf[0 ]); - rc =str_read (&c->u.name,GFP_KERNEL , fp, len); + rc =str_read (&c->u.name, fp, len);if (rc) goto out;
@@ -2307,7 +2307,7 @@ static int ocontext_read(struct policydb *p, struct policydb_compat_info *info, goto out; len =le32_to_cpu (buf[1 ]); - rc =str_read (&c->u.name,GFP_KERNEL , fp, len); + rc =str_read (&c->u.name, fp, len);if (rc) goto out;
@@ -2370,7 +2370,7 @@ static int ocontext_read(struct policydb *p, struct policydb_compat_info *info, goto out; len =le32_to_cpu (buf[0 ]); - rc =str_read (&c->u.ibendport.dev_name,GFP_KERNEL , fp, len); + rc =str_read (&c->u.ibendport.dev_name, fp, len);if (rc) goto out;
Signature · 1 line
2.26.2