Skip to content

Commit 6bd344e

Browse files
committed
Merge tag 'selinux-pr-20210629' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux
Pull SELinux updates from Paul Moore: - The slow_avc_audit() function is now non-blocking so we can remove the AVC_NONBLOCKING tricks; this also includes the 'flags' variant of avc_has_perm(). - Use kmemdup() instead of kcalloc()+copy when copying parts of the SELinux policydb. - The InfiniBand device name is now passed by reference when possible in the SELinux code, removing a strncpy(). - Minor cleanups including: constification of avtab function args, removal of useless LSM/XFRM function args, SELinux kdoc fixes, and removal of redundant assignments. * tag 'selinux-pr-20210629' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux: selinux: kill 'flags' argument in avc_has_perm_flags() and avc_audit() selinux: slow_avc_audit has become non-blocking selinux: Fix kernel-doc selinux: use __GFP_NOWARN with GFP_NOWAIT in the AVC lsm_audit,selinux: pass IB device name by reference selinux: Remove redundant assignment to rc selinux: Corrected comment to match kernel-doc comment selinux: delete selinux_xfrm_policy_lookup() useless argument selinux: constify some avtab function arguments selinux: simplify duplicate_policydb_cond_list() by using kmemdup()
2 parents 44b6ed4 + d99cf13 commit 6bd344e

File tree

15 files changed

+90
-121
lines changed

15 files changed

+90
-121
lines changed

include/linux/lsm_audit.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ struct lsm_ioctlop_audit {
4848
};
4949

5050
struct lsm_ibpkey_audit {
51-
u64 subnet_prefix;
52-
u16 pkey;
51+
u64 subnet_prefix;
52+
u16 pkey;
5353
};
5454

5555
struct lsm_ibendport_audit {
56-
char dev_name[IB_DEVICE_NAME_MAX];
57-
u8 port;
56+
const char *dev_name;
57+
u8 port;
5858
};
5959

6060
/* Auxiliary data to use in generating the audit record. */

include/linux/lsm_hook_defs.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,7 @@ LSM_HOOK(int, 0, xfrm_state_alloc_acquire, struct xfrm_state *x,
358358
struct xfrm_sec_ctx *polsec, u32 secid)
359359
LSM_HOOK(void, LSM_RET_VOID, xfrm_state_free_security, struct xfrm_state *x)
360360
LSM_HOOK(int, 0, xfrm_state_delete_security, struct xfrm_state *x)
361-
LSM_HOOK(int, 0, xfrm_policy_lookup, struct xfrm_sec_ctx *ctx, u32 fl_secid,
362-
u8 dir)
361+
LSM_HOOK(int, 0, xfrm_policy_lookup, struct xfrm_sec_ctx *ctx, u32 fl_secid)
363362
LSM_HOOK(int, 1, xfrm_state_pol_flow_match, struct xfrm_state *x,
364363
struct xfrm_policy *xp, const struct flowi_common *flic)
365364
LSM_HOOK(int, 0, xfrm_decode_session, struct sk_buff *skb, u32 *secid,

include/linux/security.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,7 @@ int security_xfrm_state_alloc_acquire(struct xfrm_state *x,
16811681
struct xfrm_sec_ctx *polsec, u32 secid);
16821682
int security_xfrm_state_delete(struct xfrm_state *x);
16831683
void security_xfrm_state_free(struct xfrm_state *x);
1684-
int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir);
1684+
int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid);
16851685
int security_xfrm_state_pol_flow_match(struct xfrm_state *x,
16861686
struct xfrm_policy *xp,
16871687
const struct flowi_common *flic);
@@ -1732,7 +1732,7 @@ static inline int security_xfrm_state_delete(struct xfrm_state *x)
17321732
return 0;
17331733
}
17341734

1735-
static inline int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir)
1735+
static inline int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid)
17361736
{
17371737
return 0;
17381738
}

net/xfrm/xfrm_policy.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,8 +1902,7 @@ static int xfrm_policy_match(const struct xfrm_policy *pol,
19021902

19031903
match = xfrm_selector_match(sel, fl, family);
19041904
if (match)
1905-
ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
1906-
dir);
1905+
ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid);
19071906
return ret;
19081907
}
19091908

@@ -2181,8 +2180,7 @@ static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
21812180
goto out;
21822181
}
21832182
err = security_xfrm_policy_lookup(pol->security,
2184-
fl->flowi_secid,
2185-
dir);
2183+
fl->flowi_secid);
21862184
if (!err) {
21872185
if (!xfrm_pol_hold_rcu(pol))
21882186
goto again;

security/security.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2466,9 +2466,9 @@ void security_xfrm_state_free(struct xfrm_state *x)
24662466
call_void_hook(xfrm_state_free_security, x);
24672467
}
24682468

2469-
int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir)
2469+
int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid)
24702470
{
2471-
return call_int_hook(xfrm_policy_lookup, 0, ctx, fl_secid, dir);
2471+
return call_int_hook(xfrm_policy_lookup, 0, ctx, fl_secid);
24722472
}
24732473

24742474
int security_xfrm_state_pol_flow_match(struct xfrm_state *x,

security/selinux/avc.c

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -297,26 +297,27 @@ static struct avc_xperms_decision_node
297297
struct avc_xperms_decision_node *xpd_node;
298298
struct extended_perms_decision *xpd;
299299

300-
xpd_node = kmem_cache_zalloc(avc_xperms_decision_cachep, GFP_NOWAIT);
300+
xpd_node = kmem_cache_zalloc(avc_xperms_decision_cachep,
301+
GFP_NOWAIT | __GFP_NOWARN);
301302
if (!xpd_node)
302303
return NULL;
303304

304305
xpd = &xpd_node->xpd;
305306
if (which & XPERMS_ALLOWED) {
306307
xpd->allowed = kmem_cache_zalloc(avc_xperms_data_cachep,
307-
GFP_NOWAIT);
308+
GFP_NOWAIT | __GFP_NOWARN);
308309
if (!xpd->allowed)
309310
goto error;
310311
}
311312
if (which & XPERMS_AUDITALLOW) {
312313
xpd->auditallow = kmem_cache_zalloc(avc_xperms_data_cachep,
313-
GFP_NOWAIT);
314+
GFP_NOWAIT | __GFP_NOWARN);
314315
if (!xpd->auditallow)
315316
goto error;
316317
}
317318
if (which & XPERMS_DONTAUDIT) {
318319
xpd->dontaudit = kmem_cache_zalloc(avc_xperms_data_cachep,
319-
GFP_NOWAIT);
320+
GFP_NOWAIT | __GFP_NOWARN);
320321
if (!xpd->dontaudit)
321322
goto error;
322323
}
@@ -344,7 +345,7 @@ static struct avc_xperms_node *avc_xperms_alloc(void)
344345
{
345346
struct avc_xperms_node *xp_node;
346347

347-
xp_node = kmem_cache_zalloc(avc_xperms_cachep, GFP_NOWAIT);
348+
xp_node = kmem_cache_zalloc(avc_xperms_cachep, GFP_NOWAIT | __GFP_NOWARN);
348349
if (!xp_node)
349350
return xp_node;
350351
INIT_LIST_HEAD(&xp_node->xpd_head);
@@ -500,7 +501,7 @@ static struct avc_node *avc_alloc_node(struct selinux_avc *avc)
500501
{
501502
struct avc_node *node;
502503

503-
node = kmem_cache_zalloc(avc_node_cachep, GFP_NOWAIT);
504+
node = kmem_cache_zalloc(avc_node_cachep, GFP_NOWAIT | __GFP_NOWARN);
504505
if (!node)
505506
goto out;
506507

@@ -758,7 +759,11 @@ static void avc_audit_post_callback(struct audit_buffer *ab, void *a)
758759
}
759760
}
760761

761-
/* This is the slow part of avc audit with big stack footprint */
762+
/*
763+
* This is the slow part of avc audit with big stack footprint.
764+
* Note that it is non-blocking and can be called from under
765+
* rcu_read_lock().
766+
*/
762767
noinline int slow_avc_audit(struct selinux_state *state,
763768
u32 ssid, u32 tsid, u16 tclass,
764769
u32 requested, u32 audited, u32 denied, int result,
@@ -819,13 +824,13 @@ int __init avc_add_callback(int (*callback)(u32 event), u32 events)
819824
}
820825

821826
/**
822-
* avc_update_node Update an AVC entry
827+
* avc_update_node - Update an AVC entry
823828
* @event : Updating event
824829
* @perms : Permission mask bits
825830
* @ssid,@tsid,@tclass : identifier of an AVC entry
826831
* @seqno : sequence number when decision was made
827832
* @xpd: extended_perms_decision to be added to the node
828-
* @flags: the AVC_* flags, e.g. AVC_NONBLOCKING, AVC_EXTENDED_PERMS, or 0.
833+
* @flags: the AVC_* flags, e.g. AVC_EXTENDED_PERMS, or 0.
829834
*
830835
* if a valid AVC entry doesn't exist,this function returns -ENOENT.
831836
* if kmalloc() called internal returns NULL, this function returns -ENOMEM.
@@ -844,21 +849,6 @@ static int avc_update_node(struct selinux_avc *avc,
844849
struct hlist_head *head;
845850
spinlock_t *lock;
846851

847-
/*
848-
* If we are in a non-blocking code path, e.g. VFS RCU walk,
849-
* then we must not add permissions to a cache entry
850-
* because we will not audit the denial. Otherwise,
851-
* during the subsequent blocking retry (e.g. VFS ref walk), we
852-
* will find the permissions already granted in the cache entry
853-
* and won't audit anything at all, leading to silent denials in
854-
* permissive mode that only appear when in enforcing mode.
855-
*
856-
* See the corresponding handling of MAY_NOT_BLOCK in avc_audit()
857-
* and selinux_inode_permission().
858-
*/
859-
if (flags & AVC_NONBLOCKING)
860-
return 0;
861-
862852
node = avc_alloc_node(avc);
863853
if (!node) {
864854
rc = -ENOMEM;
@@ -1119,7 +1109,7 @@ int avc_has_extended_perms(struct selinux_state *state,
11191109
* @tsid: target security identifier
11201110
* @tclass: target security class
11211111
* @requested: requested permissions, interpreted based on @tclass
1122-
* @flags: AVC_STRICT, AVC_NONBLOCKING, or 0
1112+
* @flags: AVC_STRICT or 0
11231113
* @avd: access vector decisions
11241114
*
11251115
* Check the AVC to determine whether the @requested permissions are granted
@@ -1190,26 +1180,7 @@ int avc_has_perm(struct selinux_state *state, u32 ssid, u32 tsid, u16 tclass,
11901180
&avd);
11911181

11921182
rc2 = avc_audit(state, ssid, tsid, tclass, requested, &avd, rc,
1193-
auditdata, 0);
1194-
if (rc2)
1195-
return rc2;
1196-
return rc;
1197-
}
1198-
1199-
int avc_has_perm_flags(struct selinux_state *state,
1200-
u32 ssid, u32 tsid, u16 tclass, u32 requested,
1201-
struct common_audit_data *auditdata,
1202-
int flags)
1203-
{
1204-
struct av_decision avd;
1205-
int rc, rc2;
1206-
1207-
rc = avc_has_perm_noaudit(state, ssid, tsid, tclass, requested,
1208-
(flags & MAY_NOT_BLOCK) ? AVC_NONBLOCKING : 0,
1209-
&avd);
1210-
1211-
rc2 = avc_audit(state, ssid, tsid, tclass, requested, &avd, rc,
1212-
auditdata, flags);
1183+
auditdata);
12131184
if (rc2)
12141185
return rc2;
12151186
return rc;

security/selinux/hooks.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,7 +1676,7 @@ static int cred_has_capability(const struct cred *cred,
16761676
sid, sid, sclass, av, 0, &avd);
16771677
if (!(opts & CAP_OPT_NOAUDIT)) {
16781678
int rc2 = avc_audit(&selinux_state,
1679-
sid, sid, sclass, av, &avd, rc, &ad, 0);
1679+
sid, sid, sclass, av, &avd, rc, &ad);
16801680
if (rc2)
16811681
return rc2;
16821682
}
@@ -3153,9 +3153,8 @@ static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
31533153
if (IS_ERR(isec))
31543154
return PTR_ERR(isec);
31553155

3156-
return avc_has_perm_flags(&selinux_state,
3157-
sid, isec->sid, isec->sclass, FILE__READ, &ad,
3158-
rcu ? MAY_NOT_BLOCK : 0);
3156+
return avc_has_perm(&selinux_state,
3157+
sid, isec->sid, isec->sclass, FILE__READ, &ad);
31593158
}
31603159

31613160
static noinline int audit_inode_permission(struct inode *inode,
@@ -3164,17 +3163,13 @@ static noinline int audit_inode_permission(struct inode *inode,
31643163
{
31653164
struct common_audit_data ad;
31663165
struct inode_security_struct *isec = selinux_inode(inode);
3167-
int rc;
31683166

31693167
ad.type = LSM_AUDIT_DATA_INODE;
31703168
ad.u.inode = inode;
31713169

3172-
rc = slow_avc_audit(&selinux_state,
3170+
return slow_avc_audit(&selinux_state,
31733171
current_sid(), isec->sid, isec->sclass, perms,
31743172
audited, denied, result, &ad);
3175-
if (rc)
3176-
return rc;
3177-
return 0;
31783173
}
31793174

31803175
static int selinux_inode_permission(struct inode *inode, int mask)
@@ -3209,19 +3204,14 @@ static int selinux_inode_permission(struct inode *inode, int mask)
32093204
return PTR_ERR(isec);
32103205

32113206
rc = avc_has_perm_noaudit(&selinux_state,
3212-
sid, isec->sid, isec->sclass, perms,
3213-
no_block ? AVC_NONBLOCKING : 0,
3207+
sid, isec->sid, isec->sclass, perms, 0,
32143208
&avd);
32153209
audited = avc_audit_required(perms, &avd, rc,
32163210
from_access ? FILE__AUDIT_ACCESS : 0,
32173211
&denied);
32183212
if (likely(!audited))
32193213
return rc;
32203214

3221-
/* fall back to ref-walk if we have to generate audit */
3222-
if (no_block)
3223-
return -ECHILD;
3224-
32253215
rc2 = audit_inode_permission(inode, perms, audited, denied, rc);
32263216
if (rc2)
32273217
return rc2;
@@ -6850,7 +6840,7 @@ static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
68506840
return err;
68516841

68526842
ad.type = LSM_AUDIT_DATA_IBENDPORT;
6853-
strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6843+
ibendport.dev_name = dev_name;
68546844
ibendport.port = port_num;
68556845
ad.u.ibendport = &ibendport;
68566846
return avc_has_perm(&selinux_state,

security/selinux/include/avc.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ int slow_avc_audit(struct selinux_state *state,
111111
* @avd: access vector decisions
112112
* @result: result from avc_has_perm_noaudit
113113
* @a: auxiliary audit data
114-
* @flags: VFS walk flags
115114
*
116115
* Audit the granting or denial of permissions in accordance
117116
* with the policy. This function is typically called by
@@ -127,24 +126,19 @@ static inline int avc_audit(struct selinux_state *state,
127126
u16 tclass, u32 requested,
128127
struct av_decision *avd,
129128
int result,
130-
struct common_audit_data *a,
131-
int flags)
129+
struct common_audit_data *a)
132130
{
133131
u32 audited, denied;
134132
audited = avc_audit_required(requested, avd, result, 0, &denied);
135133
if (likely(!audited))
136134
return 0;
137-
/* fall back to ref-walk if we have to generate audit */
138-
if (flags & MAY_NOT_BLOCK)
139-
return -ECHILD;
140135
return slow_avc_audit(state, ssid, tsid, tclass,
141136
requested, audited, denied, result,
142137
a);
143138
}
144139

145140
#define AVC_STRICT 1 /* Ignore permissive mode. */
146141
#define AVC_EXTENDED_PERMS 2 /* update extended permissions */
147-
#define AVC_NONBLOCKING 4 /* non blocking */
148142
int avc_has_perm_noaudit(struct selinux_state *state,
149143
u32 ssid, u32 tsid,
150144
u16 tclass, u32 requested,
@@ -155,11 +149,6 @@ int avc_has_perm(struct selinux_state *state,
155149
u32 ssid, u32 tsid,
156150
u16 tclass, u32 requested,
157151
struct common_audit_data *auditdata);
158-
int avc_has_perm_flags(struct selinux_state *state,
159-
u32 ssid, u32 tsid,
160-
u16 tclass, u32 requested,
161-
struct common_audit_data *auditdata,
162-
int flags);
163152

164153
int avc_has_extended_perms(struct selinux_state *state,
165154
u32 ssid, u32 tsid, u16 tclass, u32 requested,

security/selinux/include/xfrm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ int selinux_xfrm_state_alloc_acquire(struct xfrm_state *x,
2323
struct xfrm_sec_ctx *polsec, u32 secid);
2424
void selinux_xfrm_state_free(struct xfrm_state *x);
2525
int selinux_xfrm_state_delete(struct xfrm_state *x);
26-
int selinux_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir);
26+
int selinux_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid);
2727
int selinux_xfrm_state_pol_flow_match(struct xfrm_state *x,
2828
struct xfrm_policy *xp,
2929
const struct flowi_common *flic);

0 commit comments

Comments
 (0)