Skip to content

Commit b530104

Browse files
cschauflerpcmoore
authored andcommitted
lsm: lsm_context in security_dentry_init_security
Replace the (secctx,seclen) pointer pair with a single lsm_context pointer to allow return of the LSM identifier along with the context and context length. This allows security_release_secctx() to know how to release the context. Callers have been modified to use or save the returned data from the new structure. Cc: [email protected] Cc: [email protected] Signed-off-by: Casey Schaufler <[email protected]> [PM: subject tweak] Signed-off-by: Paul Moore <[email protected]>
1 parent 76ecf30 commit b530104

File tree

8 files changed

+49
-70
lines changed

8 files changed

+49
-70
lines changed

fs/ceph/super.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,8 +1132,7 @@ struct ceph_acl_sec_ctx {
11321132
void *acl;
11331133
#endif
11341134
#ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1135-
void *sec_ctx;
1136-
u32 sec_ctxlen;
1135+
struct lsm_context lsmctx;
11371136
#endif
11381137
#ifdef CONFIG_FS_ENCRYPTION
11391138
struct ceph_fscrypt_auth *fscrypt_auth;

fs/ceph/xattr.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,8 +1383,7 @@ int ceph_security_init_secctx(struct dentry *dentry, umode_t mode,
13831383
int err;
13841384

13851385
err = security_dentry_init_security(dentry, mode, &dentry->d_name,
1386-
&name, &as_ctx->sec_ctx,
1387-
&as_ctx->sec_ctxlen);
1386+
&name, &as_ctx->lsmctx);
13881387
if (err < 0) {
13891388
WARN_ON_ONCE(err != -EOPNOTSUPP);
13901389
err = 0; /* do nothing */
@@ -1409,7 +1408,7 @@ int ceph_security_init_secctx(struct dentry *dentry, umode_t mode,
14091408
*/
14101409
name_len = strlen(name);
14111410
err = ceph_pagelist_reserve(pagelist,
1412-
4 * 2 + name_len + as_ctx->sec_ctxlen);
1411+
4 * 2 + name_len + as_ctx->lsmctx.len);
14131412
if (err)
14141413
goto out;
14151414

@@ -1432,8 +1431,9 @@ int ceph_security_init_secctx(struct dentry *dentry, umode_t mode,
14321431
ceph_pagelist_encode_32(pagelist, name_len);
14331432
ceph_pagelist_append(pagelist, name, name_len);
14341433

1435-
ceph_pagelist_encode_32(pagelist, as_ctx->sec_ctxlen);
1436-
ceph_pagelist_append(pagelist, as_ctx->sec_ctx, as_ctx->sec_ctxlen);
1434+
ceph_pagelist_encode_32(pagelist, as_ctx->lsmctx.len);
1435+
ceph_pagelist_append(pagelist, as_ctx->lsmctx.context,
1436+
as_ctx->lsmctx.len);
14371437

14381438
err = 0;
14391439
out:
@@ -1446,16 +1446,12 @@ int ceph_security_init_secctx(struct dentry *dentry, umode_t mode,
14461446

14471447
void ceph_release_acl_sec_ctx(struct ceph_acl_sec_ctx *as_ctx)
14481448
{
1449-
#ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1450-
struct lsm_context scaff; /* scaffolding */
1451-
#endif
14521449
#ifdef CONFIG_CEPH_FS_POSIX_ACL
14531450
posix_acl_release(as_ctx->acl);
14541451
posix_acl_release(as_ctx->default_acl);
14551452
#endif
14561453
#ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1457-
lsmcontext_init(&scaff, as_ctx->sec_ctx, as_ctx->sec_ctxlen, 0);
1458-
security_release_secctx(&scaff);
1454+
security_release_secctx(&as_ctx->lsmctx);
14591455
#endif
14601456
#ifdef CONFIG_FS_ENCRYPTION
14611457
kfree(as_ctx->fscrypt_auth);

fs/fuse/dir.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -467,29 +467,29 @@ static int get_security_context(struct dentry *entry, umode_t mode,
467467
{
468468
struct fuse_secctx *fctx;
469469
struct fuse_secctx_header *header;
470-
void *ctx = NULL, *ptr;
471-
u32 ctxlen, total_len = sizeof(*header);
470+
struct lsm_context lsmctx = { };
471+
void *ptr;
472+
u32 total_len = sizeof(*header);
472473
int err, nr_ctx = 0;
473-
const char *name;
474+
const char *name = NULL;
474475
size_t namelen;
475476

476477
err = security_dentry_init_security(entry, mode, &entry->d_name,
477-
&name, &ctx, &ctxlen);
478-
if (err) {
479-
if (err != -EOPNOTSUPP)
480-
goto out_err;
481-
/* No LSM is supporting this security hook. Ignore error */
482-
ctxlen = 0;
483-
ctx = NULL;
484-
}
478+
&name, &lsmctx);
479+
480+
/* If no LSM is supporting this security hook ignore error */
481+
if (err && err != -EOPNOTSUPP)
482+
goto out_err;
485483

486-
if (ctxlen) {
484+
if (lsmctx.len) {
487485
nr_ctx = 1;
488486
namelen = strlen(name) + 1;
489487
err = -EIO;
490-
if (WARN_ON(namelen > XATTR_NAME_MAX + 1 || ctxlen > S32_MAX))
488+
if (WARN_ON(namelen > XATTR_NAME_MAX + 1 ||
489+
lsmctx.len > S32_MAX))
491490
goto out_err;
492-
total_len += FUSE_REC_ALIGN(sizeof(*fctx) + namelen + ctxlen);
491+
total_len += FUSE_REC_ALIGN(sizeof(*fctx) + namelen +
492+
lsmctx.len);
493493
}
494494

495495
err = -ENOMEM;
@@ -502,19 +502,20 @@ static int get_security_context(struct dentry *entry, umode_t mode,
502502
ptr += sizeof(*header);
503503
if (nr_ctx) {
504504
fctx = ptr;
505-
fctx->size = ctxlen;
505+
fctx->size = lsmctx.len;
506506
ptr += sizeof(*fctx);
507507

508508
strcpy(ptr, name);
509509
ptr += namelen;
510510

511-
memcpy(ptr, ctx, ctxlen);
511+
memcpy(ptr, lsmctx.context, lsmctx.len);
512512
}
513513
ext->size = total_len;
514514
ext->value = header;
515515
err = 0;
516516
out_err:
517-
kfree(ctx);
517+
if (nr_ctx)
518+
security_release_secctx(&lsmctx);
518519
return err;
519520
}
520521

fs/nfs/nfs4proc.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ static inline struct nfs4_label *
114114
nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
115115
struct iattr *sattr, struct nfs4_label *label)
116116
{
117+
struct lsm_context shim;
117118
int err;
118119

119120
if (label == NULL)
@@ -128,21 +129,24 @@ nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
128129
label->label = NULL;
129130

130131
err = security_dentry_init_security(dentry, sattr->ia_mode,
131-
&dentry->d_name, NULL,
132-
(void **)&label->label, &label->len);
133-
if (err == 0)
134-
return label;
132+
&dentry->d_name, NULL, &shim);
133+
if (err)
134+
return NULL;
135135

136-
return NULL;
136+
label->label = shim.context;
137+
label->len = shim.len;
138+
return label;
137139
}
138140
static inline void
139141
nfs4_label_release_security(struct nfs4_label *label)
140142
{
141-
struct lsm_context scaff; /* scaffolding */
143+
struct lsm_context shim;
142144

143145
if (label) {
144-
lsmcontext_init(&scaff, label->label, label->len, 0);
145-
security_release_secctx(&scaff);
146+
shim.context = label->label;
147+
shim.len = label->len;
148+
shim.id = LSM_ID_UNDEF;
149+
security_release_secctx(&shim);
146150
}
147151
}
148152
static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)

include/linux/lsm_hook_defs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ LSM_HOOK(int, 0, move_mount, const struct path *from_path,
8383
const struct path *to_path)
8484
LSM_HOOK(int, -EOPNOTSUPP, dentry_init_security, struct dentry *dentry,
8585
int mode, const struct qstr *name, const char **xattr_name,
86-
void **ctx, u32 *ctxlen)
86+
struct lsm_context *cp)
8787
LSM_HOOK(int, 0, dentry_create_files_as, struct dentry *dentry, int mode,
8888
struct qstr *name, const struct cred *old, struct cred *new)
8989

include/linux/security.h

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -237,25 +237,6 @@ struct lsm_context {
237237
int id; /* Identifies the module */
238238
};
239239

240-
/**
241-
* lsmcontext_init - initialize an lsmcontext structure.
242-
* @cp: Pointer to the context to initialize
243-
* @context: Initial context, or NULL
244-
* @size: Size of context, or 0
245-
* @id: Which LSM provided the context
246-
*
247-
* Fill in the lsmcontext from the provided information.
248-
* This is a scaffolding function that will be removed when
249-
* lsm_context integration is complete.
250-
*/
251-
static inline void lsmcontext_init(struct lsm_context *cp, char *context,
252-
u32 size, int id)
253-
{
254-
cp->id = id;
255-
cp->context = context;
256-
cp->len = size;
257-
}
258-
259240
/*
260241
* Values used in the task_security_ops calls
261242
*/
@@ -409,8 +390,8 @@ int security_sb_clone_mnt_opts(const struct super_block *oldsb,
409390
int security_move_mount(const struct path *from_path, const struct path *to_path);
410391
int security_dentry_init_security(struct dentry *dentry, int mode,
411392
const struct qstr *name,
412-
const char **xattr_name, void **ctx,
413-
u32 *ctxlen);
393+
const char **xattr_name,
394+
struct lsm_context *lsmcxt);
414395
int security_dentry_create_files_as(struct dentry *dentry, int mode,
415396
struct qstr *name,
416397
const struct cred *old,
@@ -883,8 +864,7 @@ static inline int security_dentry_init_security(struct dentry *dentry,
883864
int mode,
884865
const struct qstr *name,
885866
const char **xattr_name,
886-
void **ctx,
887-
u32 *ctxlen)
867+
struct lsm_context *lsmcxt)
888868
{
889869
return -EOPNOTSUPP;
890870
}

security/security.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,8 +1735,7 @@ void security_inode_free(struct inode *inode)
17351735
* @mode: mode used to determine resource type
17361736
* @name: name of the last path component
17371737
* @xattr_name: name of the security/LSM xattr
1738-
* @ctx: pointer to the resulting LSM context
1739-
* @ctxlen: length of @ctx
1738+
* @lsmctx: pointer to the resulting LSM context
17401739
*
17411740
* Compute a context for a dentry as the inode is not yet available since NFSv4
17421741
* has no label backed by an EA anyway. It is important to note that
@@ -1746,11 +1745,11 @@ void security_inode_free(struct inode *inode)
17461745
*/
17471746
int security_dentry_init_security(struct dentry *dentry, int mode,
17481747
const struct qstr *name,
1749-
const char **xattr_name, void **ctx,
1750-
u32 *ctxlen)
1748+
const char **xattr_name,
1749+
struct lsm_context *lsmctx)
17511750
{
17521751
return call_int_hook(dentry_init_security, dentry, mode, name,
1753-
xattr_name, ctx, ctxlen);
1752+
xattr_name, lsmctx);
17541753
}
17551754
EXPORT_SYMBOL(security_dentry_init_security);
17561755

security/selinux/hooks.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2869,8 +2869,8 @@ static void selinux_inode_free_security(struct inode *inode)
28692869

28702870
static int selinux_dentry_init_security(struct dentry *dentry, int mode,
28712871
const struct qstr *name,
2872-
const char **xattr_name, void **ctx,
2873-
u32 *ctxlen)
2872+
const char **xattr_name,
2873+
struct lsm_context *cp)
28742874
{
28752875
u32 newsid;
28762876
int rc;
@@ -2885,8 +2885,8 @@ static int selinux_dentry_init_security(struct dentry *dentry, int mode,
28852885
if (xattr_name)
28862886
*xattr_name = XATTR_NAME_SELINUX;
28872887

2888-
return security_sid_to_context(newsid, (char **)ctx,
2889-
ctxlen);
2888+
cp->id = LSM_ID_SELINUX;
2889+
return security_sid_to_context(newsid, &cp->context, &cp->len);
28902890
}
28912891

28922892
static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,

0 commit comments

Comments
 (0)