Skip to content

Commit 57fe60d

Browse files
jeffmahoneytorvalds
authored andcommitted
reiserfs: add atomic addition of selinux attributes during inode creation
Some time ago, some changes were made to make security inode attributes be atomically written during inode creation. ReiserFS fell behind in this area, but with the reworking of the xattr code, it's now fairly easy to add. The following patch adds the ability for security attributes to be added automatically during inode creation. Signed-off-by: Jeff Mahoney <jeffm@suse.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent a41f1a4 commit 57fe60d

5 files changed

Lines changed: 137 additions & 6 deletions

File tree

‎fs/reiserfs/inode.c‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1747,7 +1747,8 @@ int reiserfs_new_inode(struct reiserfs_transaction_handle *th,
17471747
/* 0 for regular, EMTRY_DIR_SIZE for dirs,
17481748
strlen (symname) for symlinks) */
17491749
loff_t i_size, struct dentry *dentry,
1750-
struct inode *inode)
1750+
struct inode *inode,
1751+
struct reiserfs_security_handle *security)
17511752
{
17521753
struct super_block *sb;
17531754
struct reiserfs_iget_args args;
@@ -1929,6 +1930,19 @@ int reiserfs_new_inode(struct reiserfs_transaction_handle *th,
19291930
} else if (IS_PRIVATE(dir))
19301931
inode->i_flags |= S_PRIVATE;
19311932

1933+
if (security->name) {
1934+
retval = reiserfs_security_write(th, inode, security);
1935+
if (retval) {
1936+
err = retval;
1937+
reiserfs_check_path(&path_to_key);
1938+
retval = journal_end(th, th->t_super,
1939+
th->t_blocks_allocated);
1940+
if (retval)
1941+
err = retval;
1942+
goto out_inserted_sd;
1943+
}
1944+
}
1945+
19321946
reiserfs_update_sd(th, inode);
19331947
reiserfs_check_path(&path_to_key);
19341948

‎fs/reiserfs/namei.c‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -598,13 +598,20 @@ static int reiserfs_create(struct inode *dir, struct dentry *dentry, int mode,
598598
2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
599599
REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
600600
struct reiserfs_transaction_handle th;
601+
struct reiserfs_security_handle security;
601602

602603
if (!(inode = new_inode(dir->i_sb))) {
603604
return -ENOMEM;
604605
}
605606
new_inode_init(inode, dir, mode);
606607

607608
jbegin_count += reiserfs_cache_default_acl(dir);
609+
retval = reiserfs_security_init(dir, inode, &security);
610+
if (retval < 0) {
611+
drop_new_inode(inode);
612+
return retval;
613+
}
614+
jbegin_count += retval;
608615
reiserfs_write_lock(dir->i_sb);
609616

610617
retval = journal_begin(&th, dir->i_sb, jbegin_count);
@@ -615,7 +622,7 @@ static int reiserfs_create(struct inode *dir, struct dentry *dentry, int mode,
615622

616623
retval =
617624
reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
618-
inode);
625+
inode, &security);
619626
if (retval)
620627
goto out_failed;
621628

@@ -655,6 +662,7 @@ static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
655662
int retval;
656663
struct inode *inode;
657664
struct reiserfs_transaction_handle th;
665+
struct reiserfs_security_handle security;
658666
/* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
659667
int jbegin_count =
660668
JOURNAL_PER_BALANCE_CNT * 3 +
@@ -670,6 +678,12 @@ static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
670678
new_inode_init(inode, dir, mode);
671679

672680
jbegin_count += reiserfs_cache_default_acl(dir);
681+
retval = reiserfs_security_init(dir, inode, &security);
682+
if (retval < 0) {
683+
drop_new_inode(inode);
684+
return retval;
685+
}
686+
jbegin_count += retval;
673687
reiserfs_write_lock(dir->i_sb);
674688

675689
retval = journal_begin(&th, dir->i_sb, jbegin_count);
@@ -680,7 +694,7 @@ static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
680694

681695
retval =
682696
reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
683-
inode);
697+
inode, &security);
684698
if (retval) {
685699
goto out_failed;
686700
}
@@ -723,6 +737,7 @@ static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
723737
int retval;
724738
struct inode *inode;
725739
struct reiserfs_transaction_handle th;
740+
struct reiserfs_security_handle security;
726741
/* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
727742
int jbegin_count =
728743
JOURNAL_PER_BALANCE_CNT * 3 +
@@ -740,6 +755,12 @@ static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
740755
new_inode_init(inode, dir, mode);
741756

742757
jbegin_count += reiserfs_cache_default_acl(dir);
758+
retval = reiserfs_security_init(dir, inode, &security);
759+
if (retval < 0) {
760+
drop_new_inode(inode);
761+
return retval;
762+
}
763+
jbegin_count += retval;
743764
reiserfs_write_lock(dir->i_sb);
744765

745766
retval = journal_begin(&th, dir->i_sb, jbegin_count);
@@ -756,7 +777,7 @@ static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
756777
retval = reiserfs_new_inode(&th, dir, mode, NULL /*symlink */ ,
757778
old_format_only(dir->i_sb) ?
758779
EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
759-
dentry, inode);
780+
dentry, inode, &security);
760781
if (retval) {
761782
dir->i_nlink--;
762783
goto out_failed;
@@ -999,6 +1020,7 @@ static int reiserfs_symlink(struct inode *parent_dir,
9991020
char *name;
10001021
int item_len;
10011022
struct reiserfs_transaction_handle th;
1023+
struct reiserfs_security_handle security;
10021024
int mode = S_IFLNK | S_IRWXUGO;
10031025
/* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
10041026
int jbegin_count =
@@ -1011,6 +1033,13 @@ static int reiserfs_symlink(struct inode *parent_dir,
10111033
}
10121034
new_inode_init(inode, parent_dir, mode);
10131035

1036+
retval = reiserfs_security_init(parent_dir, inode, &security);
1037+
if (retval < 0) {
1038+
drop_new_inode(inode);
1039+
return retval;
1040+
}
1041+
jbegin_count += retval;
1042+
10141043
reiserfs_write_lock(parent_dir->i_sb);
10151044
item_len = ROUND_UP(strlen(symname));
10161045
if (item_len > MAX_DIRECT_ITEM_LEN(parent_dir->i_sb->s_blocksize)) {
@@ -1037,7 +1066,7 @@ static int reiserfs_symlink(struct inode *parent_dir,
10371066

10381067
retval =
10391068
reiserfs_new_inode(&th, parent_dir, mode, name, strlen(symname),
1040-
dentry, inode);
1069+
dentry, inode, &security);
10411070
kfree(name);
10421071
if (retval) { /* reiserfs_new_inode iputs for us */
10431072
goto out_failed;

‎fs/reiserfs/xattr_security.c‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <linux/pagemap.h>
55
#include <linux/xattr.h>
66
#include <linux/reiserfs_xattr.h>
7+
#include <linux/security.h>
78
#include <asm/uaccess.h>
89

910
static int
@@ -47,6 +48,59 @@ static size_t security_list(struct inode *inode, char *list, size_t list_len,
4748
return len;
4849
}
4950

51+
/* Initializes the security context for a new inode and returns the number
52+
* of blocks needed for the transaction. If successful, reiserfs_security
53+
* must be released using reiserfs_security_free when the caller is done. */
54+
int reiserfs_security_init(struct inode *dir, struct inode *inode,
55+
struct reiserfs_security_handle *sec)
56+
{
57+
int blocks = 0;
58+
int error = security_inode_init_security(inode, dir, &sec->name,
59+
&sec->value, &sec->length);
60+
if (error) {
61+
if (error == -EOPNOTSUPP)
62+
error = 0;
63+
64+
sec->name = NULL;
65+
sec->value = NULL;
66+
sec->length = 0;
67+
return error;
68+
}
69+
70+
if (sec->length) {
71+
blocks = reiserfs_xattr_jcreate_nblocks(inode) +
72+
reiserfs_xattr_nblocks(inode, sec->length);
73+
/* We don't want to count the directories twice if we have
74+
* a default ACL. */
75+
REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
76+
}
77+
return blocks;
78+
}
79+
80+
int reiserfs_security_write(struct reiserfs_transaction_handle *th,
81+
struct inode *inode,
82+
struct reiserfs_security_handle *sec)
83+
{
84+
int error;
85+
if (strlen(sec->name) < sizeof(XATTR_SECURITY_PREFIX))
86+
return -EINVAL;
87+
88+
error = reiserfs_xattr_set_handle(th, inode, sec->name, sec->value,
89+
sec->length, XATTR_CREATE);
90+
if (error == -ENODATA || error == -EOPNOTSUPP)
91+
error = 0;
92+
93+
return error;
94+
}
95+
96+
void reiserfs_security_free(struct reiserfs_security_handle *sec)
97+
{
98+
kfree(sec->name);
99+
kfree(sec->value);
100+
sec->name = NULL;
101+
sec->value = NULL;
102+
}
103+
50104
struct xattr_handler reiserfs_xattr_security_handler = {
51105
.prefix = XATTR_SECURITY_PREFIX,
52106
.get = security_get,

‎include/linux/reiserfs_fs.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1915,10 +1915,12 @@ void make_le_item_head(struct item_head *ih, const struct cpu_key *key,
19151915
loff_t offset, int type, int length, int entry_count);
19161916
struct inode *reiserfs_iget(struct super_block *s, const struct cpu_key *key);
19171917

1918+
struct reiserfs_security_handle;
19181919
int reiserfs_new_inode(struct reiserfs_transaction_handle *th,
19191920
struct inode *dir, int mode,
19201921
const char *symname, loff_t i_size,
1921-
struct dentry *dentry, struct inode *inode);
1922+
struct dentry *dentry, struct inode *inode,
1923+
struct reiserfs_security_handle *security);
19221924

19231925
void reiserfs_update_sd_size(struct reiserfs_transaction_handle *th,
19241926
struct inode *inode, loff_t size);

‎include/linux/reiserfs_xattr.h‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ struct reiserfs_xattr_header {
1515
__le32 h_hash; /* hash of the value */
1616
};
1717

18+
struct reiserfs_security_handle {
19+
char *name;
20+
void *value;
21+
size_t length;
22+
};
23+
1824
#ifdef __KERNEL__
1925

2026
#include <linux/init.h>
@@ -54,6 +60,14 @@ int reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *,
5460
extern struct xattr_handler reiserfs_xattr_user_handler;
5561
extern struct xattr_handler reiserfs_xattr_trusted_handler;
5662
extern struct xattr_handler reiserfs_xattr_security_handler;
63+
#ifdef CONFIG_REISERFS_FS_SECURITY
64+
int reiserfs_security_init(struct inode *dir, struct inode *inode,
65+
struct reiserfs_security_handle *sec);
66+
int reiserfs_security_write(struct reiserfs_transaction_handle *th,
67+
struct inode *inode,
68+
struct reiserfs_security_handle *sec);
69+
void reiserfs_security_free(struct reiserfs_security_handle *sec);
70+
#endif
5771

5872
#define xattr_size(size) ((size) + sizeof(struct reiserfs_xattr_header))
5973
static inline loff_t reiserfs_xattr_nblocks(struct inode *inode, loff_t size)
@@ -109,6 +123,24 @@ static inline void reiserfs_init_xattr_rwsem(struct inode *inode)
109123
}
110124
#endif /* CONFIG_REISERFS_FS_XATTR */
111125

126+
#ifndef CONFIG_REISERFS_FS_SECURITY
127+
static inline int reiserfs_security_init(struct inode *dir,
128+
struct inode *inode,
129+
struct reiserfs_security_handle *sec)
130+
{
131+
return 0;
132+
}
133+
static inline int
134+
reiserfs_security_write(struct reiserfs_transaction_handle *th,
135+
struct inode *inode,
136+
struct reiserfs_security_handle *sec)
137+
{
138+
return 0;
139+
}
140+
static inline void reiserfs_security_free(struct reiserfs_security_handle *sec)
141+
{}
142+
#endif
143+
112144
#endif /* __KERNEL__ */
113145

114146
#endif /* _LINUX_REISERFS_XATTR_H */

0 commit comments

Comments
 (0)