Skip to content

Commit bda0be7

Browse files
neilbrownAl Viro
authored andcommitted
security: make inode_follow_link RCU-walk aware
inode_follow_link now takes an inode and rcu flag as well as the dentry. inode is used in preference to d_backing_inode(dentry), particularly in RCU-walk mode. selinux_inode_follow_link() gets dentry_has_perm() and inode_has_perm() open-coded into it so that it can call avc_has_perm_flags() in way that is safe if LOOKUP_RCU is set. Calling avc_has_perm_flags() with rcu_read_lock() held means that when avc_has_perm_noaudit calls avc_compute_av(), the attempt to rcu_read_unlock() before calling security_compute_av() will not actually drop the RCU read-lock. However as security_compute_av() is completely in a read_lock()ed region, it should be safe with the RCU read-lock held. Signed-off-by: NeilBrown <neilb@suse.de> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 7b20ea2 commit bda0be7

5 files changed

Lines changed: 32 additions & 11 deletions

File tree

‎fs/namei.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -881,8 +881,9 @@ const char *get_link(struct nameidata *nd)
881881

882882
touch_atime(&last->link);
883883

884-
error = security_inode_follow_link(dentry);
885-
if (error)
884+
error = security_inode_follow_link(dentry, inode,
885+
nd->flags & LOOKUP_RCU);
886+
if (unlikely(error))
886887
return ERR_PTR(error);
887888

888889
nd->last_type = LAST_BIND;

‎include/linux/security.h‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,8 @@ static inline void security_free_mnt_opts(struct security_mnt_opts *opts)
476476
* @inode_follow_link:
477477
* Check permission to follow a symbolic link when looking up a pathname.
478478
* @dentry contains the dentry structure for the link.
479+
* @inode contains the inode, which itself is not stable in RCU-walk
480+
* @rcu indicates whether we are in RCU-walk mode.
479481
* Return 0 if permission is granted.
480482
* @inode_permission:
481483
* Check permission before accessing an inode. This hook is called by the
@@ -1551,7 +1553,8 @@ struct security_operations {
15511553
int (*inode_rename) (struct inode *old_dir, struct dentry *old_dentry,
15521554
struct inode *new_dir, struct dentry *new_dentry);
15531555
int (*inode_readlink) (struct dentry *dentry);
1554-
int (*inode_follow_link) (struct dentry *dentry);
1556+
int (*inode_follow_link) (struct dentry *dentry, struct inode *inode,
1557+
bool rcu);
15551558
int (*inode_permission) (struct inode *inode, int mask);
15561559
int (*inode_setattr) (struct dentry *dentry, struct iattr *attr);
15571560
int (*inode_getattr) (const struct path *path);
@@ -1837,7 +1840,8 @@ int security_inode_rename(struct inode *old_dir, struct dentry *old_dentry,
18371840
struct inode *new_dir, struct dentry *new_dentry,
18381841
unsigned int flags);
18391842
int security_inode_readlink(struct dentry *dentry);
1840-
int security_inode_follow_link(struct dentry *dentry);
1843+
int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
1844+
bool rcu);
18411845
int security_inode_permission(struct inode *inode, int mask);
18421846
int security_inode_setattr(struct dentry *dentry, struct iattr *attr);
18431847
int security_inode_getattr(const struct path *path);
@@ -2239,7 +2243,9 @@ static inline int security_inode_readlink(struct dentry *dentry)
22392243
return 0;
22402244
}
22412245

2242-
static inline int security_inode_follow_link(struct dentry *dentry)
2246+
static inline int security_inode_follow_link(struct dentry *dentry,
2247+
struct inode *inode,
2248+
bool rcu)
22432249
{
22442250
return 0;
22452251
}

‎security/capability.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ static int cap_inode_readlink(struct dentry *dentry)
209209
return 0;
210210
}
211211

212-
static int cap_inode_follow_link(struct dentry *dentry)
212+
static int cap_inode_follow_link(struct dentry *dentry, struct inode *inode,
213+
bool rcu)
213214
{
214215
return 0;
215216
}

‎security/security.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,11 +581,12 @@ int security_inode_readlink(struct dentry *dentry)
581581
return security_ops->inode_readlink(dentry);
582582
}
583583

584-
int security_inode_follow_link(struct dentry *dentry)
584+
int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
585+
bool rcu)
585586
{
586-
if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
587+
if (unlikely(IS_PRIVATE(inode)))
587588
return 0;
588-
return security_ops->inode_follow_link(dentry);
589+
return security_ops->inode_follow_link(dentry, inode, rcu);
589590
}
590591

591592
int security_inode_permission(struct inode *inode, int mask)

‎security/selinux/hooks.c‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2861,11 +2861,23 @@ static int selinux_inode_readlink(struct dentry *dentry)
28612861
return dentry_has_perm(cred, dentry, FILE__READ);
28622862
}
28632863

2864-
static int selinux_inode_follow_link(struct dentry *dentry)
2864+
static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2865+
bool rcu)
28652866
{
28662867
const struct cred *cred = current_cred();
2868+
struct common_audit_data ad;
2869+
struct inode_security_struct *isec;
2870+
u32 sid;
28672871

2868-
return dentry_has_perm(cred, dentry, FILE__READ);
2872+
validate_creds(cred);
2873+
2874+
ad.type = LSM_AUDIT_DATA_DENTRY;
2875+
ad.u.dentry = dentry;
2876+
sid = cred_sid(cred);
2877+
isec = inode->i_security;
2878+
2879+
return avc_has_perm_flags(sid, isec->sid, isec->sclass, FILE__READ, &ad,
2880+
rcu ? MAY_NOT_BLOCK : 0);
28692881
}
28702882

28712883
static noinline int audit_inode_permission(struct inode *inode,

0 commit comments

Comments
 (0)