Skip to content

Commit 2322392

Browse files
htejungregkh
authored andcommitted
kernfs: implement "trusted.*" xattr support
kernfs inherited "security.*" xattr support from sysfs. This patch extends xattr support to "trusted.*" using simple_xattr_*(). As trusted xattrs are restricted to CAP_SYS_ADMIN, simple_xattr_*() which uses kernel memory for storage shouldn't be problematic. Note that the existing "security.*" support doesn't implement get/remove/list and the this patch only implements those ops for "trusted.*". We probably want to extend those ops to include support for "security.*". This patch will allow using kernfs from cgroup which requires "trusted.*" xattr support. Signed-off-by: Tejun Heo <[email protected]> Cc: David P. Quigley <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 9a8049a commit 2322392

File tree

4 files changed

+74
-11
lines changed

4 files changed

+74
-11
lines changed

fs/kernfs/dir.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,12 @@ void kernfs_put(struct sysfs_dirent *sd)
243243
kernfs_put(sd->s_symlink.target_sd);
244244
if (sysfs_type(sd) & SYSFS_COPY_NAME)
245245
kfree(sd->s_name);
246-
if (sd->s_iattr && sd->s_iattr->ia_secdata)
247-
security_release_secctx(sd->s_iattr->ia_secdata,
248-
sd->s_iattr->ia_secdata_len);
246+
if (sd->s_iattr) {
247+
if (sd->s_iattr->ia_secdata)
248+
security_release_secctx(sd->s_iattr->ia_secdata,
249+
sd->s_iattr->ia_secdata_len);
250+
simple_xattrs_free(&sd->s_iattr->xattrs);
251+
}
249252
kfree(sd->s_iattr);
250253
ida_simple_remove(&root->ino_ida, sd->s_ino);
251254
kmem_cache_free(sysfs_dir_cachep, sd);
@@ -718,6 +721,9 @@ const struct inode_operations sysfs_dir_inode_operations = {
718721
.setattr = sysfs_setattr,
719722
.getattr = sysfs_getattr,
720723
.setxattr = sysfs_setxattr,
724+
.removexattr = sysfs_removexattr,
725+
.getxattr = sysfs_getxattr,
726+
.listxattr = sysfs_listxattr,
721727
};
722728

723729
static struct sysfs_dirent *sysfs_leftmost_descendant(struct sysfs_dirent *pos)

fs/kernfs/inode.c

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ static const struct inode_operations sysfs_inode_operations = {
3535
.setattr = sysfs_setattr,
3636
.getattr = sysfs_getattr,
3737
.setxattr = sysfs_setxattr,
38+
.removexattr = sysfs_removexattr,
39+
.getxattr = sysfs_getxattr,
40+
.listxattr = sysfs_listxattr,
3841
};
3942

4043
void __init sysfs_inode_init(void)
@@ -61,6 +64,8 @@ static struct sysfs_inode_attrs *sysfs_inode_attrs(struct sysfs_dirent *sd)
6164
iattrs->ia_gid = GLOBAL_ROOT_GID;
6265
iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME;
6366

67+
simple_xattrs_init(&sd->s_iattr->xattrs);
68+
6469
return sd->s_iattr;
6570
}
6671

@@ -162,34 +167,76 @@ int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
162167
size_t size, int flags)
163168
{
164169
struct sysfs_dirent *sd = dentry->d_fsdata;
170+
struct sysfs_inode_attrs *attrs;
165171
void *secdata;
166172
int error;
167173
u32 secdata_len = 0;
168174

169-
if (!sd)
170-
return -EINVAL;
175+
attrs = sysfs_inode_attrs(sd);
176+
if (!attrs)
177+
return -ENOMEM;
171178

172179
if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) {
173180
const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
174181
error = security_inode_setsecurity(dentry->d_inode, suffix,
175182
value, size, flags);
176183
if (error)
177-
goto out;
184+
return error;
178185
error = security_inode_getsecctx(dentry->d_inode,
179186
&secdata, &secdata_len);
180187
if (error)
181-
goto out;
188+
return error;
182189

183190
mutex_lock(&sysfs_mutex);
184191
error = sysfs_sd_setsecdata(sd, &secdata, &secdata_len);
185192
mutex_unlock(&sysfs_mutex);
186193

187194
if (secdata)
188195
security_release_secctx(secdata, secdata_len);
189-
} else
190-
return -EINVAL;
191-
out:
192-
return error;
196+
return error;
197+
} else if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
198+
return simple_xattr_set(&attrs->xattrs, name, value, size,
199+
flags);
200+
}
201+
202+
return -EINVAL;
203+
}
204+
205+
int sysfs_removexattr(struct dentry *dentry, const char *name)
206+
{
207+
struct sysfs_dirent *sd = dentry->d_fsdata;
208+
struct sysfs_inode_attrs *attrs;
209+
210+
attrs = sysfs_inode_attrs(sd);
211+
if (!attrs)
212+
return -ENOMEM;
213+
214+
return simple_xattr_remove(&attrs->xattrs, name);
215+
}
216+
217+
ssize_t sysfs_getxattr(struct dentry *dentry, const char *name, void *buf,
218+
size_t size)
219+
{
220+
struct sysfs_dirent *sd = dentry->d_fsdata;
221+
struct sysfs_inode_attrs *attrs;
222+
223+
attrs = sysfs_inode_attrs(sd);
224+
if (!attrs)
225+
return -ENOMEM;
226+
227+
return simple_xattr_get(&attrs->xattrs, name, buf, size);
228+
}
229+
230+
ssize_t sysfs_listxattr(struct dentry *dentry, char *buf, size_t size)
231+
{
232+
struct sysfs_dirent *sd = dentry->d_fsdata;
233+
struct sysfs_inode_attrs *attrs;
234+
235+
attrs = sysfs_inode_attrs(sd);
236+
if (!attrs)
237+
return -ENOMEM;
238+
239+
return simple_xattr_list(&attrs->xattrs, buf, size);
193240
}
194241

195242
static inline void set_default_inode_attr(struct inode *inode, umode_t mode)

fs/kernfs/kernfs-internal.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
#include <linux/lockdep.h>
1515
#include <linux/fs.h>
1616
#include <linux/mutex.h>
17+
#include <linux/xattr.h>
1718

1819
#include <linux/kernfs.h>
1920

2021
struct sysfs_inode_attrs {
2122
struct iattr ia_iattr;
2223
void *ia_secdata;
2324
u32 ia_secdata_len;
25+
26+
struct simple_xattrs xattrs;
2427
};
2528

2629
#define SD_DEACTIVATED_BIAS INT_MIN
@@ -81,6 +84,10 @@ int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
8184
struct kstat *stat);
8285
int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
8386
size_t size, int flags);
87+
int sysfs_removexattr(struct dentry *dentry, const char *name);
88+
ssize_t sysfs_getxattr(struct dentry *dentry, const char *name, void *buf,
89+
size_t size);
90+
ssize_t sysfs_listxattr(struct dentry *dentry, char *buf, size_t size);
8491
void sysfs_inode_init(void);
8592

8693
/*

fs/kernfs/symlink.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd,
140140

141141
const struct inode_operations sysfs_symlink_inode_operations = {
142142
.setxattr = sysfs_setxattr,
143+
.removexattr = sysfs_removexattr,
144+
.getxattr = sysfs_getxattr,
145+
.listxattr = sysfs_listxattr,
143146
.readlink = generic_readlink,
144147
.follow_link = sysfs_follow_link,
145148
.put_link = sysfs_put_link,

0 commit comments

Comments
 (0)