Skip to content

Commit 0dc1ba2

Browse files
committed
SELINUX: Make selinux cache VFS RCU walks safe
Now that the security modules can decide whether they support the dcache RCU walk or not it's possible to make selinux a bit more RCU friendly. The SELinux AVC and security server access decision code is RCU safe. A specific piece of the LSM audit code may not be RCU safe. This patch makes the VFS RCU walk retry if it would hit the non RCU safe chunk of code. It will normally just work under RCU. This is done simply by passing the VFS RCU state as a flag down into the avc_audit() code and returning ECHILD there if it would have an issue. Based-on-patch-by: Andi Kleen <ak@linux.intel.com> Signed-off-by: Eric Paris <eparis@redhat.com>
1 parent 1c99042 commit 0dc1ba2

3 files changed

Lines changed: 55 additions & 25 deletions

File tree

‎security/selinux/avc.c‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ static void avc_audit_post_callback(struct audit_buffer *ab, void *a)
471471
* @avd: access vector decisions
472472
* @result: result from avc_has_perm_noaudit
473473
* @a: auxiliary audit data
474+
* @flags: VFS walk flags
474475
*
475476
* Audit the granting or denial of permissions in accordance
476477
* with the policy. This function is typically called by
@@ -481,9 +482,10 @@ static void avc_audit_post_callback(struct audit_buffer *ab, void *a)
481482
* be performed under a lock, to allow the lock to be released
482483
* before calling the auditing code.
483484
*/
484-
void avc_audit(u32 ssid, u32 tsid,
485+
int avc_audit(u32 ssid, u32 tsid,
485486
u16 tclass, u32 requested,
486-
struct av_decision *avd, int result, struct common_audit_data *a)
487+
struct av_decision *avd, int result, struct common_audit_data *a,
488+
unsigned flags)
487489
{
488490
struct common_audit_data stack_data;
489491
u32 denied, audited;
@@ -515,11 +517,24 @@ void avc_audit(u32 ssid, u32 tsid,
515517
else
516518
audited = requested & avd->auditallow;
517519
if (!audited)
518-
return;
520+
return 0;
521+
519522
if (!a) {
520523
a = &stack_data;
521524
COMMON_AUDIT_DATA_INIT(a, NONE);
522525
}
526+
527+
/*
528+
* When in a RCU walk do the audit on the RCU retry. This is because
529+
* the collection of the dname in an inode audit message is not RCU
530+
* safe. Note this may drop some audits when the situation changes
531+
* during retry. However this is logically just as if the operation
532+
* happened a little later.
533+
*/
534+
if ((a->type == LSM_AUDIT_DATA_FS) &&
535+
(flags & IPERM_FLAG_RCU))
536+
return -ECHILD;
537+
523538
a->selinux_audit_data.tclass = tclass;
524539
a->selinux_audit_data.requested = requested;
525540
a->selinux_audit_data.ssid = ssid;
@@ -529,6 +544,7 @@ void avc_audit(u32 ssid, u32 tsid,
529544
a->lsm_pre_audit = avc_audit_pre_callback;
530545
a->lsm_post_audit = avc_audit_post_callback;
531546
common_lsm_audit(a);
547+
return 0;
532548
}
533549

534550
/**
@@ -793,6 +809,7 @@ int avc_has_perm_noaudit(u32 ssid, u32 tsid,
793809
* @tclass: target security class
794810
* @requested: requested permissions, interpreted based on @tclass
795811
* @auditdata: auxiliary audit data
812+
* @flags: VFS walk flags
796813
*
797814
* Check the AVC to determine whether the @requested permissions are granted
798815
* for the SID pair (@ssid, @tsid), interpreting the permissions
@@ -802,14 +819,19 @@ int avc_has_perm_noaudit(u32 ssid, u32 tsid,
802819
* permissions are granted, -%EACCES if any permissions are denied, or
803820
* another -errno upon other errors.
804821
*/
805-
int avc_has_perm(u32 ssid, u32 tsid, u16 tclass,
806-
u32 requested, struct common_audit_data *auditdata)
822+
int avc_has_perm_flags(u32 ssid, u32 tsid, u16 tclass,
823+
u32 requested, struct common_audit_data *auditdata,
824+
unsigned flags)
807825
{
808826
struct av_decision avd;
809-
int rc;
827+
int rc, rc2;
810828

811829
rc = avc_has_perm_noaudit(ssid, tsid, tclass, requested, 0, &avd);
812-
avc_audit(ssid, tsid, tclass, requested, &avd, rc, auditdata);
830+
831+
rc2 = avc_audit(ssid, tsid, tclass, requested, &avd, rc, auditdata,
832+
flags);
833+
if (rc2)
834+
return rc2;
813835
return rc;
814836
}
815837

‎security/selinux/hooks.c‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,8 +1447,11 @@ static int task_has_capability(struct task_struct *tsk,
14471447
}
14481448

14491449
rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
1450-
if (audit == SECURITY_CAP_AUDIT)
1451-
avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
1450+
if (audit == SECURITY_CAP_AUDIT) {
1451+
int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0);
1452+
if (rc2)
1453+
return rc2;
1454+
}
14521455
return rc;
14531456
}
14541457

@@ -1468,7 +1471,8 @@ static int task_has_system(struct task_struct *tsk,
14681471
static int inode_has_perm(const struct cred *cred,
14691472
struct inode *inode,
14701473
u32 perms,
1471-
struct common_audit_data *adp)
1474+
struct common_audit_data *adp,
1475+
unsigned flags)
14721476
{
14731477
struct inode_security_struct *isec;
14741478
struct common_audit_data ad;
@@ -1488,7 +1492,7 @@ static int inode_has_perm(const struct cred *cred,
14881492
ad.u.fs.inode = inode;
14891493
}
14901494

1491-
return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp);
1495+
return avc_has_perm_flags(sid, isec->sid, isec->sclass, perms, adp, flags);
14921496
}
14931497

14941498
/* Same as inode_has_perm, but pass explicit audit data containing
@@ -1505,7 +1509,7 @@ static inline int dentry_has_perm(const struct cred *cred,
15051509
COMMON_AUDIT_DATA_INIT(&ad, FS);
15061510
ad.u.fs.path.mnt = mnt;
15071511
ad.u.fs.path.dentry = dentry;
1508-
return inode_has_perm(cred, inode, av, &ad);
1512+
return inode_has_perm(cred, inode, av, &ad, 0);
15091513
}
15101514

15111515
/* Check whether a task can use an open file descriptor to
@@ -1541,7 +1545,7 @@ static int file_has_perm(const struct cred *cred,
15411545
/* av is zero if only checking access to the descriptor. */
15421546
rc = 0;
15431547
if (av)
1544-
rc = inode_has_perm(cred, inode, av, &ad);
1548+
rc = inode_has_perm(cred, inode, av, &ad, 0);
15451549

15461550
out:
15471551
return rc;
@@ -2103,7 +2107,7 @@ static inline void flush_unauthorized_files(const struct cred *cred,
21032107
file = file_priv->file;
21042108
inode = file->f_path.dentry->d_inode;
21052109
if (inode_has_perm(cred, inode,
2106-
FILE__READ | FILE__WRITE, NULL)) {
2110+
FILE__READ | FILE__WRITE, NULL, 0)) {
21072111
drop_tty = 1;
21082112
}
21092113
}
@@ -2649,10 +2653,6 @@ static int selinux_inode_permission(struct inode *inode, int mask, unsigned flag
26492653
if (!mask)
26502654
return 0;
26512655

2652-
/* May be droppable after audit */
2653-
if (flags & IPERM_FLAG_RCU)
2654-
return -ECHILD;
2655-
26562656
COMMON_AUDIT_DATA_INIT(&ad, FS);
26572657
ad.u.fs.inode = inode;
26582658

@@ -2661,7 +2661,7 @@ static int selinux_inode_permission(struct inode *inode, int mask, unsigned flag
26612661

26622662
perms = file_mask_to_av(inode->i_mode, mask);
26632663

2664-
return inode_has_perm(cred, inode, perms, &ad);
2664+
return inode_has_perm(cred, inode, perms, &ad, flags);
26652665
}
26662666

26672667
static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
@@ -3208,7 +3208,7 @@ static int selinux_dentry_open(struct file *file, const struct cred *cred)
32083208
* new inode label or new policy.
32093209
* This check is not redundant - do not remove.
32103210
*/
3211-
return inode_has_perm(cred, inode, open_file_to_av(file), NULL);
3211+
return inode_has_perm(cred, inode, open_file_to_av(file), NULL, 0);
32123212
}
32133213

32143214
/* task security operations */

‎security/selinux/include/avc.h‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,29 @@ struct avc_cache_stats {
5454

5555
void __init avc_init(void);
5656

57-
void avc_audit(u32 ssid, u32 tsid,
57+
int avc_audit(u32 ssid, u32 tsid,
5858
u16 tclass, u32 requested,
5959
struct av_decision *avd,
6060
int result,
61-
struct common_audit_data *a);
61+
struct common_audit_data *a, unsigned flags);
6262

6363
#define AVC_STRICT 1 /* Ignore permissive mode. */
6464
int avc_has_perm_noaudit(u32 ssid, u32 tsid,
6565
u16 tclass, u32 requested,
6666
unsigned flags,
6767
struct av_decision *avd);
6868

69-
int avc_has_perm(u32 ssid, u32 tsid,
70-
u16 tclass, u32 requested,
71-
struct common_audit_data *auditdata);
69+
int avc_has_perm_flags(u32 ssid, u32 tsid,
70+
u16 tclass, u32 requested,
71+
struct common_audit_data *auditdata,
72+
unsigned);
73+
74+
static inline int avc_has_perm(u32 ssid, u32 tsid,
75+
u16 tclass, u32 requested,
76+
struct common_audit_data *auditdata)
77+
{
78+
return avc_has_perm_flags(ssid, tsid, tclass, requested, auditdata, 0);
79+
}
7280

7381
u32 avc_policy_seqno(void);
7482

0 commit comments

Comments
 (0)