Skip to content

Commit d47be3d

Browse files
dpquiglTrond Myklebust
authored andcommitted
Security: Add hook to calculate context based on a negative dentry.
There is a time where we need to calculate a context without the inode having been created yet. To do this we take the negative dentry and calculate a context based on the process and the parent directory contexts. Acked-by: Eric Paris <eparis@redhat.com> Acked-by: James Morris <james.l.morris@oracle.com> Signed-off-by: Matthew N. Dodd <Matthew.Dodd@sparta.com> Signed-off-by: Miguel Rodel Felipe <Rodel_FM@dsi.a-star.edu.sg> Signed-off-by: Phua Eu Gene <PHUA_Eu_Gene@dsi.a-star.edu.sg> Signed-off-by: Khin Mi Mi Aung <Mi_Mi_AUNG@dsi.a-star.edu.sg> Signed-off-by: Steve Dickson <steved@redhat.com> Signed-off-by: Trond Myklebust <Trond.Myklebust@netapp.com>
1 parent 4488cc9 commit d47be3d

4 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎include/linux/security.h‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/capability.h>
2727
#include <linux/slab.h>
2828
#include <linux/err.h>
29+
#include <linux/string.h>
2930

3031
struct linux_binprm;
3132
struct cred;
@@ -306,6 +307,15 @@ static inline void security_free_mnt_opts(struct security_mnt_opts *opts)
306307
* Parse a string of security data filling in the opts structure
307308
* @options string containing all mount options known by the LSM
308309
* @opts binary data structure usable by the LSM
310+
* @dentry_init_security:
311+
* Compute a context for a dentry as the inode is not yet available
312+
* since NFSv4 has no label backed by an EA anyway.
313+
* @dentry dentry to use in calculating the context.
314+
* @mode mode used to determine resource type.
315+
* @name name of the last path component used to create file
316+
* @ctx pointer to place the pointer to the resulting context in.
317+
* @ctxlen point to place the length of the resulting context.
318+
*
309319
*
310320
* Security hooks for inode operations.
311321
*
@@ -1443,6 +1453,10 @@ struct security_operations {
14431453
int (*sb_clone_mnt_opts) (const struct super_block *oldsb,
14441454
struct super_block *newsb);
14451455
int (*sb_parse_opts_str) (char *options, struct security_mnt_opts *opts);
1456+
int (*dentry_init_security) (struct dentry *dentry, int mode,
1457+
struct qstr *name, void **ctx,
1458+
u32 *ctxlen);
1459+
14461460

14471461
#ifdef CONFIG_SECURITY_PATH
14481462
int (*path_unlink) (struct path *dir, struct dentry *dentry);
@@ -1729,6 +1743,9 @@ int security_sb_set_mnt_opts(struct super_block *sb, struct security_mnt_opts *o
17291743
int security_sb_clone_mnt_opts(const struct super_block *oldsb,
17301744
struct super_block *newsb);
17311745
int security_sb_parse_opts_str(char *options, struct security_mnt_opts *opts);
1746+
int security_dentry_init_security(struct dentry *dentry, int mode,
1747+
struct qstr *name, void **ctx,
1748+
u32 *ctxlen);
17321749

17331750
int security_inode_alloc(struct inode *inode);
17341751
void security_inode_free(struct inode *inode);
@@ -2035,6 +2052,16 @@ static inline int security_inode_alloc(struct inode *inode)
20352052
static inline void security_inode_free(struct inode *inode)
20362053
{ }
20372054

2055+
static inline int security_dentry_init_security(struct dentry *dentry,
2056+
int mode,
2057+
struct qstr *name,
2058+
void **ctx,
2059+
u32 *ctxlen)
2060+
{
2061+
return -EOPNOTSUPP;
2062+
}
2063+
2064+
20382065
static inline int security_inode_init_security(struct inode *inode,
20392066
struct inode *dir,
20402067
const struct qstr *qstr,

‎security/capability.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ static int cap_sb_parse_opts_str(char *options, struct security_mnt_opts *opts)
109109
return 0;
110110
}
111111

112+
static int cap_dentry_init_security(struct dentry *dentry, int mode,
113+
struct qstr *name, void **ctx,
114+
u32 *ctxlen)
115+
{
116+
return 0;
117+
}
118+
112119
static int cap_inode_alloc_security(struct inode *inode)
113120
{
114121
return 0;
@@ -931,6 +938,7 @@ void __init security_fixup_ops(struct security_operations *ops)
931938
set_to_cap_if_null(ops, sb_set_mnt_opts);
932939
set_to_cap_if_null(ops, sb_clone_mnt_opts);
933940
set_to_cap_if_null(ops, sb_parse_opts_str);
941+
set_to_cap_if_null(ops, dentry_init_security);
934942
set_to_cap_if_null(ops, inode_alloc_security);
935943
set_to_cap_if_null(ops, inode_free_security);
936944
set_to_cap_if_null(ops, inode_init_security);

‎security/security.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
#include <linux/capability.h>
15+
#include <linux/dcache.h>
1516
#include <linux/module.h>
1617
#include <linux/init.h>
1718
#include <linux/kernel.h>
@@ -324,6 +325,15 @@ void security_inode_free(struct inode *inode)
324325
security_ops->inode_free_security(inode);
325326
}
326327

328+
int security_dentry_init_security(struct dentry *dentry, int mode,
329+
struct qstr *name, void **ctx,
330+
u32 *ctxlen)
331+
{
332+
return security_ops->dentry_init_security(dentry, mode, name,
333+
ctx, ctxlen);
334+
}
335+
EXPORT_SYMBOL(security_dentry_init_security);
336+
327337
int security_inode_init_security(struct inode *inode, struct inode *dir,
328338
const struct qstr *qstr,
329339
const initxattrs initxattrs, void *fs_data)

‎security/selinux/hooks.c‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2515,6 +2515,40 @@ static void selinux_inode_free_security(struct inode *inode)
25152515
inode_free_security(inode);
25162516
}
25172517

2518+
static int selinux_dentry_init_security(struct dentry *dentry, int mode,
2519+
struct qstr *name, void **ctx,
2520+
u32 *ctxlen)
2521+
{
2522+
const struct cred *cred = current_cred();
2523+
struct task_security_struct *tsec;
2524+
struct inode_security_struct *dsec;
2525+
struct superblock_security_struct *sbsec;
2526+
struct inode *dir = dentry->d_parent->d_inode;
2527+
u32 newsid;
2528+
int rc;
2529+
2530+
tsec = cred->security;
2531+
dsec = dir->i_security;
2532+
sbsec = dir->i_sb->s_security;
2533+
2534+
if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) {
2535+
newsid = tsec->create_sid;
2536+
} else {
2537+
rc = security_transition_sid(tsec->sid, dsec->sid,
2538+
inode_mode_to_security_class(mode),
2539+
name,
2540+
&newsid);
2541+
if (rc) {
2542+
printk(KERN_WARNING
2543+
"%s: security_transition_sid failed, rc=%d\n",
2544+
__func__, -rc);
2545+
return rc;
2546+
}
2547+
}
2548+
2549+
return security_sid_to_context(newsid, (char **)ctx, ctxlen);
2550+
}
2551+
25182552
static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
25192553
const struct qstr *qstr, char **name,
25202554
void **value, size_t *len)
@@ -5562,6 +5596,7 @@ static struct security_operations selinux_ops = {
55625596
.sb_clone_mnt_opts = selinux_sb_clone_mnt_opts,
55635597
.sb_parse_opts_str = selinux_parse_opts_str,
55645598

5599+
.dentry_init_security = selinux_dentry_init_security,
55655600

55665601
.inode_alloc_security = selinux_inode_alloc_security,
55675602
.inode_free_security = selinux_inode_free_security,

0 commit comments

Comments
 (0)