Skip to content

Commit 9d8f13b

Browse files
author
Mimi Zohar
committed
security: new security_inode_init_security API adds function callback
This patch changes the security_inode_init_security API by adding a filesystem specific callback to write security extended attributes. This change is in preparation for supporting the initialization of multiple LSM xattrs and the EVM xattr. Initially the callback function walks an array of xattrs, writing each xattr separately, but could be optimized to write multiple xattrs at once. For existing security_inode_init_security() calls, which have not yet been converted to use the new callback function, such as those in reiserfs and ocfs2, this patch defines security_old_inode_init_security(). Signed-off-by: Mimi Zohar <zohar@us.ibm.com>
1 parent 0f2a55d commit 9d8f13b

14 files changed

Lines changed: 250 additions & 183 deletions

File tree

‎fs/btrfs/xattr.c‎

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -360,36 +360,36 @@ int btrfs_removexattr(struct dentry *dentry, const char *name)
360360
XATTR_REPLACE);
361361
}
362362

363-
int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
364-
struct inode *inode, struct inode *dir,
365-
const struct qstr *qstr)
363+
int btrfs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
364+
void *fs_info)
366365
{
367-
int err;
368-
size_t len;
369-
void *value;
370-
char *suffix;
366+
const struct xattr *xattr;
367+
struct btrfs_trans_handle *trans = fs_info;
371368
char *name;
369+
int err = 0;
372370

373-
err = security_inode_init_security(inode, dir, qstr, &suffix, &value,
374-
&len);
375-
if (err) {
376-
if (err == -EOPNOTSUPP)
377-
return 0;
378-
return err;
379-
}
380-
381-
name = kmalloc(XATTR_SECURITY_PREFIX_LEN + strlen(suffix) + 1,
382-
GFP_NOFS);
383-
if (!name) {
384-
err = -ENOMEM;
385-
} else {
371+
for (xattr = xattr_array; xattr->name != NULL; xattr++) {
372+
name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
373+
strlen(xattr->name) + 1, GFP_NOFS);
374+
if (!name) {
375+
err = -ENOMEM;
376+
break;
377+
}
386378
strcpy(name, XATTR_SECURITY_PREFIX);
387-
strcpy(name + XATTR_SECURITY_PREFIX_LEN, suffix);
388-
err = __btrfs_setxattr(trans, inode, name, value, len, 0);
379+
strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
380+
err = __btrfs_setxattr(trans, inode, name,
381+
xattr->value, xattr->value_len, 0);
389382
kfree(name);
383+
if (err < 0)
384+
break;
390385
}
391-
392-
kfree(suffix);
393-
kfree(value);
394386
return err;
395387
}
388+
389+
int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
390+
struct inode *inode, struct inode *dir,
391+
const struct qstr *qstr)
392+
{
393+
return security_inode_init_security(inode, dir, qstr,
394+
&btrfs_initxattrs, trans);
395+
}

‎fs/ext2/xattr_security.c‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,30 @@ ext2_xattr_security_set(struct dentry *dentry, const char *name,
4646
value, size, flags);
4747
}
4848

49-
int
50-
ext2_init_security(struct inode *inode, struct inode *dir,
51-
const struct qstr *qstr)
49+
int ext2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
50+
void *fs_info)
5251
{
53-
int err;
54-
size_t len;
55-
void *value;
56-
char *name;
52+
const struct xattr *xattr;
53+
int err = 0;
5754

58-
err = security_inode_init_security(inode, dir, qstr, &name, &value, &len);
59-
if (err) {
60-
if (err == -EOPNOTSUPP)
61-
return 0;
62-
return err;
55+
for (xattr = xattr_array; xattr->name != NULL; xattr++) {
56+
err = ext2_xattr_set(inode, EXT2_XATTR_INDEX_SECURITY,
57+
xattr->name, xattr->value,
58+
xattr->value_len, 0);
59+
if (err < 0)
60+
break;
6361
}
64-
err = ext2_xattr_set(inode, EXT2_XATTR_INDEX_SECURITY,
65-
name, value, len, 0);
66-
kfree(name);
67-
kfree(value);
6862
return err;
6963
}
7064

65+
int
66+
ext2_init_security(struct inode *inode, struct inode *dir,
67+
const struct qstr *qstr)
68+
{
69+
return security_inode_init_security(inode, dir, qstr,
70+
&ext2_initxattrs, NULL);
71+
}
72+
7173
const struct xattr_handler ext2_xattr_security_handler = {
7274
.prefix = XATTR_SECURITY_PREFIX,
7375
.list = ext2_xattr_security_list,

‎fs/ext3/xattr_security.c‎

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,32 @@ ext3_xattr_security_set(struct dentry *dentry, const char *name,
4848
name, value, size, flags);
4949
}
5050

51-
int
52-
ext3_init_security(handle_t *handle, struct inode *inode, struct inode *dir,
53-
const struct qstr *qstr)
51+
int ext3_initxattrs(struct inode *inode, const struct xattr *xattr_array,
52+
void *fs_info)
5453
{
55-
int err;
56-
size_t len;
57-
void *value;
58-
char *name;
54+
const struct xattr *xattr;
55+
handle_t *handle = fs_info;
56+
int err = 0;
5957

60-
err = security_inode_init_security(inode, dir, qstr, &name, &value, &len);
61-
if (err) {
62-
if (err == -EOPNOTSUPP)
63-
return 0;
64-
return err;
58+
for (xattr = xattr_array; xattr->name != NULL; xattr++) {
59+
err = ext3_xattr_set_handle(handle, inode,
60+
EXT3_XATTR_INDEX_SECURITY,
61+
xattr->name, xattr->value,
62+
xattr->value_len, 0);
63+
if (err < 0)
64+
break;
6565
}
66-
err = ext3_xattr_set_handle(handle, inode, EXT3_XATTR_INDEX_SECURITY,
67-
name, value, len, 0);
68-
kfree(name);
69-
kfree(value);
7066
return err;
7167
}
7268

69+
int
70+
ext3_init_security(handle_t *handle, struct inode *inode, struct inode *dir,
71+
const struct qstr *qstr)
72+
{
73+
return security_inode_init_security(inode, dir, qstr,
74+
&ext3_initxattrs, handle);
75+
}
76+
7377
const struct xattr_handler ext3_xattr_security_handler = {
7478
.prefix = XATTR_SECURITY_PREFIX,
7579
.list = ext3_xattr_security_list,

‎fs/ext4/xattr_security.c‎

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,32 @@ ext4_xattr_security_set(struct dentry *dentry, const char *name,
4848
name, value, size, flags);
4949
}
5050

51-
int
52-
ext4_init_security(handle_t *handle, struct inode *inode, struct inode *dir,
53-
const struct qstr *qstr)
51+
int ext4_initxattrs(struct inode *inode, const struct xattr *xattr_array,
52+
void *fs_info)
5453
{
55-
int err;
56-
size_t len;
57-
void *value;
58-
char *name;
54+
const struct xattr *xattr;
55+
handle_t *handle = fs_info;
56+
int err = 0;
5957

60-
err = security_inode_init_security(inode, dir, qstr, &name, &value, &len);
61-
if (err) {
62-
if (err == -EOPNOTSUPP)
63-
return 0;
64-
return err;
58+
for (xattr = xattr_array; xattr->name != NULL; xattr++) {
59+
err = ext4_xattr_set_handle(handle, inode,
60+
EXT4_XATTR_INDEX_SECURITY,
61+
xattr->name, xattr->value,
62+
xattr->value_len, 0);
63+
if (err < 0)
64+
break;
6565
}
66-
err = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_SECURITY,
67-
name, value, len, 0);
68-
kfree(name);
69-
kfree(value);
7066
return err;
7167
}
7268

69+
int
70+
ext4_init_security(handle_t *handle, struct inode *inode, struct inode *dir,
71+
const struct qstr *qstr)
72+
{
73+
return security_inode_init_security(inode, dir, qstr,
74+
&ext4_initxattrs, handle);
75+
}
76+
7377
const struct xattr_handler ext4_xattr_security_handler = {
7478
.prefix = XATTR_SECURITY_PREFIX,
7579
.list = ext4_xattr_security_list,

‎fs/gfs2/inode.c‎

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -624,31 +624,29 @@ static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
624624
return error;
625625
}
626626

627-
static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip,
628-
const struct qstr *qstr)
627+
int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
628+
void *fs_info)
629629
{
630-
int err;
631-
size_t len;
632-
void *value;
633-
char *name;
634-
635-
err = security_inode_init_security(&ip->i_inode, &dip->i_inode, qstr,
636-
&name, &value, &len);
637-
638-
if (err) {
639-
if (err == -EOPNOTSUPP)
640-
return 0;
641-
return err;
630+
const struct xattr *xattr;
631+
int err = 0;
632+
633+
for (xattr = xattr_array; xattr->name != NULL; xattr++) {
634+
err = __gfs2_xattr_set(inode, xattr->name, xattr->value,
635+
xattr->value_len, 0,
636+
GFS2_EATYPE_SECURITY);
637+
if (err < 0)
638+
break;
642639
}
643-
644-
err = __gfs2_xattr_set(&ip->i_inode, name, value, len, 0,
645-
GFS2_EATYPE_SECURITY);
646-
kfree(value);
647-
kfree(name);
648-
649640
return err;
650641
}
651642

643+
static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip,
644+
const struct qstr *qstr)
645+
{
646+
return security_inode_init_security(&ip->i_inode, &dip->i_inode, qstr,
647+
&gfs2_initxattrs, NULL);
648+
}
649+
652650
/**
653651
* gfs2_create_inode - Create a new inode
654652
* @dir: The parent directory

‎fs/jffs2/security.c‎

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,29 @@
2222
#include <linux/security.h>
2323
#include "nodelist.h"
2424

25-
/* ---- Initial Security Label Attachment -------------- */
26-
int jffs2_init_security(struct inode *inode, struct inode *dir,
27-
const struct qstr *qstr)
25+
/* ---- Initial Security Label(s) Attachment callback --- */
26+
int jffs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
27+
void *fs_info)
2828
{
29-
int rc;
30-
size_t len;
31-
void *value;
32-
char *name;
29+
const struct xattr *xattr;
30+
int err = 0;
3331

34-
rc = security_inode_init_security(inode, dir, qstr, &name, &value, &len);
35-
if (rc) {
36-
if (rc == -EOPNOTSUPP)
37-
return 0;
38-
return rc;
32+
for (xattr = xattr_array; xattr->name != NULL; xattr++) {
33+
err = do_jffs2_setxattr(inode, JFFS2_XPREFIX_SECURITY,
34+
xattr->name, xattr->value,
35+
xattr->value_len, 0);
36+
if (err < 0)
37+
break;
3938
}
40-
rc = do_jffs2_setxattr(inode, JFFS2_XPREFIX_SECURITY, name, value, len, 0);
39+
return err;
40+
}
4141

42-
kfree(name);
43-
kfree(value);
44-
return rc;
42+
/* ---- Initial Security Label(s) Attachment ----------- */
43+
int jffs2_init_security(struct inode *inode, struct inode *dir,
44+
const struct qstr *qstr)
45+
{
46+
return security_inode_init_security(inode, dir, qstr,
47+
&jffs2_initxattrs, NULL);
4548
}
4649

4750
/* ---- XATTR Handler for "security.*" ----------------- */

‎fs/jfs/xattr.c‎

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,38 +1091,37 @@ int jfs_removexattr(struct dentry *dentry, const char *name)
10911091
}
10921092

10931093
#ifdef CONFIG_JFS_SECURITY
1094-
int jfs_init_security(tid_t tid, struct inode *inode, struct inode *dir,
1095-
const struct qstr *qstr)
1094+
int jfs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
1095+
void *fs_info)
10961096
{
1097-
int rc;
1098-
size_t len;
1099-
void *value;
1100-
char *suffix;
1097+
const struct xattr *xattr;
1098+
tid_t *tid = fs_info;
11011099
char *name;
1102-
1103-
rc = security_inode_init_security(inode, dir, qstr, &suffix, &value,
1104-
&len);
1105-
if (rc) {
1106-
if (rc == -EOPNOTSUPP)
1107-
return 0;
1108-
return rc;
1109-
}
1110-
name = kmalloc(XATTR_SECURITY_PREFIX_LEN + 1 + strlen(suffix),
1111-
GFP_NOFS);
1112-
if (!name) {
1113-
rc = -ENOMEM;
1114-
goto kmalloc_failed;
1100+
int err = 0;
1101+
1102+
for (xattr = xattr_array; xattr->name != NULL; xattr++) {
1103+
name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
1104+
strlen(xattr->name) + 1, GFP_NOFS);
1105+
if (!name) {
1106+
err = -ENOMEM;
1107+
break;
1108+
}
1109+
strcpy(name, XATTR_SECURITY_PREFIX);
1110+
strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
1111+
1112+
err = __jfs_setxattr(*tid, inode, name,
1113+
xattr->value, xattr->value_len, 0);
1114+
kfree(name);
1115+
if (err < 0)
1116+
break;
11151117
}
1116-
strcpy(name, XATTR_SECURITY_PREFIX);
1117-
strcpy(name + XATTR_SECURITY_PREFIX_LEN, suffix);
1118-
1119-
rc = __jfs_setxattr(tid, inode, name, value, len, 0);
1120-
1121-
kfree(name);
1122-
kmalloc_failed:
1123-
kfree(suffix);
1124-
kfree(value);
1118+
return err;
1119+
}
11251120

1126-
return rc;
1121+
int jfs_init_security(tid_t tid, struct inode *inode, struct inode *dir,
1122+
const struct qstr *qstr)
1123+
{
1124+
return security_inode_init_security(inode, dir, qstr,
1125+
&jfs_initxattrs, &tid);
11271126
}
11281127
#endif

0 commit comments

Comments
 (0)