Skip to content

Commit 15bf323

Browse files
rhvgoyalpcmoore
authored andcommitted
security: Return xattr name from security_dentry_init_security()
Right now security_dentry_init_security() only supports single security label and is used by SELinux only. There are two users of this hook, namely ceph and nfs. NFS does not care about xattr name. Ceph hardcodes the xattr name to security.selinux (XATTR_NAME_SELINUX). I am making changes to fuse/virtiofs to send security label to virtiofsd and I need to send xattr name as well. I also hardcoded the name of xattr to security.selinux. Stephen Smalley suggested that it probably is a good idea to modify security_dentry_init_security() to also return name of xattr so that we can avoid this hardcoding in the callers. This patch adds a new parameter "const char **xattr_name" to security_dentry_init_security() and LSM puts the name of xattr too if caller asked for it (xattr_name != NULL). Signed-off-by: Vivek Goyal <[email protected]> Reviewed-by: Jeff Layton <[email protected]> Reviewed-by: Christian Brauner <[email protected]> Acked-by: James Morris <[email protected]> [PM: fixed typos in the commit description] Signed-off-by: Paul Moore <[email protected]>
1 parent 1c73213 commit 15bf323

File tree

7 files changed

+21
-10
lines changed

7 files changed

+21
-10
lines changed

fs/ceph/xattr.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,7 @@ int ceph_security_init_secctx(struct dentry *dentry, umode_t mode,
13111311
int err;
13121312

13131313
err = security_dentry_init_security(dentry, mode, &dentry->d_name,
1314-
&as_ctx->sec_ctx,
1314+
&name, &as_ctx->sec_ctx,
13151315
&as_ctx->sec_ctxlen);
13161316
if (err < 0) {
13171317
WARN_ON_ONCE(err != -EOPNOTSUPP);
@@ -1335,7 +1335,6 @@ int ceph_security_init_secctx(struct dentry *dentry, umode_t mode,
13351335
* It only supports single security module and only selinux has
13361336
* dentry_init_security hook.
13371337
*/
1338-
name = XATTR_NAME_SELINUX;
13391338
name_len = strlen(name);
13401339
err = ceph_pagelist_reserve(pagelist,
13411340
4 * 2 + name_len + as_ctx->sec_ctxlen);

fs/nfs/nfs4proc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
127127
return NULL;
128128

129129
err = security_dentry_init_security(dentry, sattr->ia_mode,
130-
&dentry->d_name, (void **)&label->label, &label->len);
130+
&dentry->d_name, NULL,
131+
(void **)&label->label, &label->len);
131132
if (err == 0)
132133
return label;
133134

include/linux/lsm_hook_defs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ LSM_HOOK(int, 0, sb_add_mnt_opt, const char *option, const char *val,
8383
LSM_HOOK(int, 0, move_mount, const struct path *from_path,
8484
const struct path *to_path)
8585
LSM_HOOK(int, 0, dentry_init_security, struct dentry *dentry,
86-
int mode, const struct qstr *name, void **ctx, u32 *ctxlen)
86+
int mode, const struct qstr *name, const char **xattr_name,
87+
void **ctx, u32 *ctxlen)
8788
LSM_HOOK(int, 0, dentry_create_files_as, struct dentry *dentry, int mode,
8889
struct qstr *name, const struct cred *old, struct cred *new)
8990

include/linux/lsm_hooks.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@
196196
* @dentry dentry to use in calculating the context.
197197
* @mode mode used to determine resource type.
198198
* @name name of the last path component used to create file
199+
* @xattr_name pointer to place the pointer to security xattr name.
200+
* Caller does not have to free the resulting pointer. Its
201+
* a pointer to static string.
199202
* @ctx pointer to place the pointer to the resulting context in.
200203
* @ctxlen point to place the length of the resulting context.
201204
* @dentry_create_files_as:

include/linux/security.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,9 @@ int security_add_mnt_opt(const char *option, const char *val,
317317
int len, void **mnt_opts);
318318
int security_move_mount(const struct path *from_path, const struct path *to_path);
319319
int security_dentry_init_security(struct dentry *dentry, int mode,
320-
const struct qstr *name, void **ctx,
321-
u32 *ctxlen);
320+
const struct qstr *name,
321+
const char **xattr_name, void **ctx,
322+
u32 *ctxlen);
322323
int security_dentry_create_files_as(struct dentry *dentry, int mode,
323324
struct qstr *name,
324325
const struct cred *old,
@@ -739,6 +740,7 @@ static inline void security_inode_free(struct inode *inode)
739740
static inline int security_dentry_init_security(struct dentry *dentry,
740741
int mode,
741742
const struct qstr *name,
743+
const char **xattr_name,
742744
void **ctx,
743745
u32 *ctxlen)
744746
{

security/security.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,11 +1052,12 @@ void security_inode_free(struct inode *inode)
10521052
}
10531053

10541054
int security_dentry_init_security(struct dentry *dentry, int mode,
1055-
const struct qstr *name, void **ctx,
1056-
u32 *ctxlen)
1055+
const struct qstr *name,
1056+
const char **xattr_name, void **ctx,
1057+
u32 *ctxlen)
10571058
{
10581059
return call_int_hook(dentry_init_security, -EOPNOTSUPP, dentry, mode,
1059-
name, ctx, ctxlen);
1060+
name, xattr_name, ctx, ctxlen);
10601061
}
10611062
EXPORT_SYMBOL(security_dentry_init_security);
10621063

security/selinux/hooks.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2927,7 +2927,8 @@ static void selinux_inode_free_security(struct inode *inode)
29272927
}
29282928

29292929
static int selinux_dentry_init_security(struct dentry *dentry, int mode,
2930-
const struct qstr *name, void **ctx,
2930+
const struct qstr *name,
2931+
const char **xattr_name, void **ctx,
29312932
u32 *ctxlen)
29322933
{
29332934
u32 newsid;
@@ -2940,6 +2941,9 @@ static int selinux_dentry_init_security(struct dentry *dentry, int mode,
29402941
if (rc)
29412942
return rc;
29422943

2944+
if (xattr_name)
2945+
*xattr_name = XATTR_NAME_SELINUX;
2946+
29432947
return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
29442948
ctxlen);
29452949
}

0 commit comments

Comments
 (0)