aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorTejun Heo <tj@kernel.org>2013-11-23 17:40:02 -0500
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2013-12-04 10:34:45 -0500
commit2322392b020badfe49730f1529b9c1a15248c387 (patch)
treeda17c0d183045351ae25f8173f2c0cfd7c7b8a55 /fs
parent9a8049affd55a2c857a89faece27b878416fbf91 (diff)
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 <tj@kernel.org> Cc: David P. Quigley <dpquigl@tycho.nsa.gov> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'fs')
-rw-r--r--fs/kernfs/dir.c12
-rw-r--r--fs/kernfs/inode.c63
-rw-r--r--fs/kernfs/kernfs-internal.h7
-rw-r--r--fs/kernfs/symlink.c3
4 files changed, 74 insertions, 11 deletions
diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index f51e0625e666..a441e3be8052 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -243,9 +243,12 @@ void kernfs_put(struct sysfs_dirent *sd)
243 kernfs_put(sd->s_symlink.target_sd); 243 kernfs_put(sd->s_symlink.target_sd);
244 if (sysfs_type(sd) & SYSFS_COPY_NAME) 244 if (sysfs_type(sd) & SYSFS_COPY_NAME)
245 kfree(sd->s_name); 245 kfree(sd->s_name);
246 if (sd->s_iattr && sd->s_iattr->ia_secdata) 246 if (sd->s_iattr) {
247 security_release_secctx(sd->s_iattr->ia_secdata, 247 if (sd->s_iattr->ia_secdata)
248 sd->s_iattr->ia_secdata_len); 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 }
249 kfree(sd->s_iattr); 252 kfree(sd->s_iattr);
250 ida_simple_remove(&root->ino_ida, sd->s_ino); 253 ida_simple_remove(&root->ino_ida, sd->s_ino);
251 kmem_cache_free(sysfs_dir_cachep, sd); 254 kmem_cache_free(sysfs_dir_cachep, sd);
@@ -718,6 +721,9 @@ const struct inode_operations sysfs_dir_inode_operations = {
718 .setattr = sysfs_setattr, 721 .setattr = sysfs_setattr,
719 .getattr = sysfs_getattr, 722 .getattr = sysfs_getattr,
720 .setxattr = sysfs_setxattr, 723 .setxattr = sysfs_setxattr,
724 .removexattr = sysfs_removexattr,
725 .getxattr = sysfs_getxattr,
726 .listxattr = sysfs_listxattr,
721}; 727};
722 728
723static struct sysfs_dirent *sysfs_leftmost_descendant(struct sysfs_dirent *pos) 729static struct sysfs_dirent *sysfs_leftmost_descendant(struct sysfs_dirent *pos)
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index a1f83825afca..18ad431e8c2a 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -35,6 +35,9 @@ static const struct inode_operations sysfs_inode_operations = {
35 .setattr = sysfs_setattr, 35 .setattr = sysfs_setattr,
36 .getattr = sysfs_getattr, 36 .getattr = sysfs_getattr,
37 .setxattr = sysfs_setxattr, 37 .setxattr = sysfs_setxattr,
38 .removexattr = sysfs_removexattr,
39 .getxattr = sysfs_getxattr,
40 .listxattr = sysfs_listxattr,
38}; 41};
39 42
40void __init sysfs_inode_init(void) 43void __init sysfs_inode_init(void)
@@ -61,6 +64,8 @@ static struct sysfs_inode_attrs *sysfs_inode_attrs(struct sysfs_dirent *sd)
61 iattrs->ia_gid = GLOBAL_ROOT_GID; 64 iattrs->ia_gid = GLOBAL_ROOT_GID;
62 iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME; 65 iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME;
63 66
67 simple_xattrs_init(&sd->s_iattr->xattrs);
68
64 return sd->s_iattr; 69 return sd->s_iattr;
65} 70}
66 71
@@ -162,23 +167,25 @@ int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
162 size_t size, int flags) 167 size_t size, int flags)
163{ 168{
164 struct sysfs_dirent *sd = dentry->d_fsdata; 169 struct sysfs_dirent *sd = dentry->d_fsdata;
170 struct sysfs_inode_attrs *attrs;
165 void *secdata; 171 void *secdata;
166 int error; 172 int error;
167 u32 secdata_len = 0; 173 u32 secdata_len = 0;
168 174
169 if (!sd) 175 attrs = sysfs_inode_attrs(sd);
170 return -EINVAL; 176 if (!attrs)
177 return -ENOMEM;
171 178
172 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) { 179 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) {
173 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN; 180 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
174 error = security_inode_setsecurity(dentry->d_inode, suffix, 181 error = security_inode_setsecurity(dentry->d_inode, suffix,
175 value, size, flags); 182 value, size, flags);
176 if (error) 183 if (error)
177 goto out; 184 return error;
178 error = security_inode_getsecctx(dentry->d_inode, 185 error = security_inode_getsecctx(dentry->d_inode,
179 &secdata, &secdata_len); 186 &secdata, &secdata_len);
180 if (error) 187 if (error)
181 goto out; 188 return error;
182 189
183 mutex_lock(&sysfs_mutex); 190 mutex_lock(&sysfs_mutex);
184 error = sysfs_sd_setsecdata(sd, &secdata, &secdata_len); 191 error = sysfs_sd_setsecdata(sd, &secdata, &secdata_len);
@@ -186,10 +193,50 @@ int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
186 193
187 if (secdata) 194 if (secdata)
188 security_release_secctx(secdata, secdata_len); 195 security_release_secctx(secdata, secdata_len);
189 } else 196 return error;
190 return -EINVAL; 197 } else if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
191out: 198 return simple_xattr_set(&attrs->xattrs, name, value, size,
192 return error; 199 flags);
200 }
201
202 return -EINVAL;
203}
204
205int 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
217ssize_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
230ssize_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);
193} 240}
194 241
195static inline void set_default_inode_attr(struct inode *inode, umode_t mode) 242static inline void set_default_inode_attr(struct inode *inode, umode_t mode)
diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h
index f25b3548bcca..910e485b7333 100644
--- a/fs/kernfs/kernfs-internal.h
+++ b/fs/kernfs/kernfs-internal.h
@@ -14,6 +14,7 @@
14#include <linux/lockdep.h> 14#include <linux/lockdep.h>
15#include <linux/fs.h> 15#include <linux/fs.h>
16#include <linux/mutex.h> 16#include <linux/mutex.h>
17#include <linux/xattr.h>
17 18
18#include <linux/kernfs.h> 19#include <linux/kernfs.h>
19 20
@@ -21,6 +22,8 @@ struct sysfs_inode_attrs {
21 struct iattr ia_iattr; 22 struct iattr ia_iattr;
22 void *ia_secdata; 23 void *ia_secdata;
23 u32 ia_secdata_len; 24 u32 ia_secdata_len;
25
26 struct simple_xattrs xattrs;
24}; 27};
25 28
26#define SD_DEACTIVATED_BIAS INT_MIN 29#define SD_DEACTIVATED_BIAS INT_MIN
@@ -81,6 +84,10 @@ int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
81 struct kstat *stat); 84 struct kstat *stat);
82int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value, 85int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
83 size_t size, int flags); 86 size_t size, int flags);
87int sysfs_removexattr(struct dentry *dentry, const char *name);
88ssize_t sysfs_getxattr(struct dentry *dentry, const char *name, void *buf,
89 size_t size);
90ssize_t sysfs_listxattr(struct dentry *dentry, char *buf, size_t size);
84void sysfs_inode_init(void); 91void sysfs_inode_init(void);
85 92
86/* 93/*
diff --git a/fs/kernfs/symlink.c b/fs/kernfs/symlink.c
index 12569a738837..adf28755b0ee 100644
--- a/fs/kernfs/symlink.c
+++ b/fs/kernfs/symlink.c
@@ -140,6 +140,9 @@ static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd,
140 140
141const struct inode_operations sysfs_symlink_inode_operations = { 141const struct inode_operations sysfs_symlink_inode_operations = {
142 .setxattr = sysfs_setxattr, 142 .setxattr = sysfs_setxattr,
143 .removexattr = sysfs_removexattr,
144 .getxattr = sysfs_getxattr,
145 .listxattr = sysfs_listxattr,
143 .readlink = generic_readlink, 146 .readlink = generic_readlink,
144 .follow_link = sysfs_follow_link, 147 .follow_link = sysfs_follow_link,
145 .put_link = sysfs_put_link, 148 .put_link = sysfs_put_link,