1
2
3
4
5
6
7
8
9
10#include <linux/pagemap.h>
11#include <linux/backing-dev.h>
12#include <linux/capability.h>
13#include <linux/errno.h>
14#include <linux/slab.h>
15#include <linux/xattr.h>
16#include <linux/security.h>
17
18#include "kernfs-internal.h"
19
20static const struct inode_operations kernfs_iops = {
21 .permission = kernfs_iop_permission,
22 .setattr = kernfs_iop_setattr,
23 .getattr = kernfs_iop_getattr,
24 .listxattr = kernfs_iop_listxattr,
25};
26
27static struct kernfs_iattrs *__kernfs_iattrs(struct kernfs_node *kn, int alloc)
28{
29 static DEFINE_MUTEX(iattr_mutex);
30 struct kernfs_iattrs *ret;
31
32 mutex_lock(&iattr_mutex);
33
34 if (kn->iattr || !alloc)
35 goto out_unlock;
36
37 kn->iattr = kmem_cache_zalloc(kernfs_iattrs_cache, GFP_KERNEL);
38 if (!kn->iattr)
39 goto out_unlock;
40
41
42 kn->iattr->ia_uid = GLOBAL_ROOT_UID;
43 kn->iattr->ia_gid = GLOBAL_ROOT_GID;
44
45 ktime_get_real_ts64(&kn->iattr->ia_atime);
46 kn->iattr->ia_mtime = kn->iattr->ia_atime;
47 kn->iattr->ia_ctime = kn->iattr->ia_atime;
48
49 simple_xattrs_init(&kn->iattr->xattrs);
50 atomic_set(&kn->iattr->nr_user_xattrs, 0);
51 atomic_set(&kn->iattr->user_xattr_size, 0);
52out_unlock:
53 ret = kn->iattr;
54 mutex_unlock(&iattr_mutex);
55 return ret;
56}
57
58static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn)
59{
60 return __kernfs_iattrs(kn, 1);
61}
62
63static struct kernfs_iattrs *kernfs_iattrs_noalloc(struct kernfs_node *kn)
64{
65 return __kernfs_iattrs(kn, 0);
66}
67
68int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
69{
70 struct kernfs_iattrs *attrs;
71 unsigned int ia_valid = iattr->ia_valid;
72
73 attrs = kernfs_iattrs(kn);
74 if (!attrs)
75 return -ENOMEM;
76
77 if (ia_valid & ATTR_UID)
78 attrs->ia_uid = iattr->ia_uid;
79 if (ia_valid & ATTR_GID)
80 attrs->ia_gid = iattr->ia_gid;
81 if (ia_valid & ATTR_ATIME)
82 attrs->ia_atime = iattr->ia_atime;
83 if (ia_valid & ATTR_MTIME)
84 attrs->ia_mtime = iattr->ia_mtime;
85 if (ia_valid & ATTR_CTIME)
86 attrs->ia_ctime = iattr->ia_ctime;
87 if (ia_valid & ATTR_MODE)
88 kn->mode = iattr->ia_mode;
89 return 0;
90}
91
92
93
94
95
96
97
98
99int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
100{
101 int ret;
102
103 down_write(&kernfs_rwsem);
104 ret = __kernfs_setattr(kn, iattr);
105 up_write(&kernfs_rwsem);
106 return ret;
107}
108
109int kernfs_iop_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
110 struct iattr *iattr)
111{
112 struct inode *inode = d_inode(dentry);
113 struct kernfs_node *kn = inode->i_private;
114 int error;
115
116 if (!kn)
117 return -EINVAL;
118
119 down_write(&kernfs_rwsem);
120 error = setattr_prepare(&init_user_ns, dentry, iattr);
121 if (error)
122 goto out;
123
124 error = __kernfs_setattr(kn, iattr);
125 if (error)
126 goto out;
127
128
129 setattr_copy(&init_user_ns, inode, iattr);
130
131out:
132 up_write(&kernfs_rwsem);
133 return error;
134}
135
136ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size)
137{
138 struct kernfs_node *kn = kernfs_dentry_node(dentry);
139 struct kernfs_iattrs *attrs;
140
141 attrs = kernfs_iattrs(kn);
142 if (!attrs)
143 return -ENOMEM;
144
145 return simple_xattr_list(d_inode(dentry), &attrs->xattrs, buf, size);
146}
147
148static inline void set_default_inode_attr(struct inode *inode, umode_t mode)
149{
150 inode->i_mode = mode;
151 inode->i_atime = inode->i_mtime =
152 inode->i_ctime = current_time(inode);
153}
154
155static inline void set_inode_attr(struct inode *inode,
156 struct kernfs_iattrs *attrs)
157{
158 inode->i_uid = attrs->ia_uid;
159 inode->i_gid = attrs->ia_gid;
160 inode->i_atime = attrs->ia_atime;
161 inode->i_mtime = attrs->ia_mtime;
162 inode->i_ctime = attrs->ia_ctime;
163}
164
165static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
166{
167 struct kernfs_iattrs *attrs = kn->iattr;
168
169 inode->i_mode = kn->mode;
170 if (attrs)
171
172
173
174
175 set_inode_attr(inode, attrs);
176
177 if (kernfs_type(kn) == KERNFS_DIR)
178 set_nlink(inode, kn->dir.subdirs + 2);
179}
180
181int kernfs_iop_getattr(struct user_namespace *mnt_userns,
182 const struct path *path, struct kstat *stat,
183 u32 request_mask, unsigned int query_flags)
184{
185 struct inode *inode = d_inode(path->dentry);
186 struct kernfs_node *kn = inode->i_private;
187
188 down_read(&kernfs_rwsem);
189 spin_lock(&inode->i_lock);
190 kernfs_refresh_inode(kn, inode);
191 generic_fillattr(&init_user_ns, inode, stat);
192 spin_unlock(&inode->i_lock);
193 up_read(&kernfs_rwsem);
194
195 return 0;
196}
197
198static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
199{
200 kernfs_get(kn);
201 inode->i_private = kn;
202 inode->i_mapping->a_ops = &ram_aops;
203 inode->i_op = &kernfs_iops;
204 inode->i_generation = kernfs_gen(kn);
205
206 set_default_inode_attr(inode, kn->mode);
207 kernfs_refresh_inode(kn, inode);
208
209
210 switch (kernfs_type(kn)) {
211 case KERNFS_DIR:
212 inode->i_op = &kernfs_dir_iops;
213 inode->i_fop = &kernfs_dir_fops;
214 if (kn->flags & KERNFS_EMPTY_DIR)
215 make_empty_dir_inode(inode);
216 break;
217 case KERNFS_FILE:
218 inode->i_size = kn->attr.size;
219 inode->i_fop = &kernfs_file_fops;
220 break;
221 case KERNFS_LINK:
222 inode->i_op = &kernfs_symlink_iops;
223 break;
224 default:
225 BUG();
226 }
227
228 unlock_new_inode(inode);
229}
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
247{
248 struct inode *inode;
249
250 inode = iget_locked(sb, kernfs_ino(kn));
251 if (inode && (inode->i_state & I_NEW))
252 kernfs_init_inode(kn, inode);
253
254 return inode;
255}
256
257
258
259
260
261
262
263
264void kernfs_evict_inode(struct inode *inode)
265{
266 struct kernfs_node *kn = inode->i_private;
267
268 truncate_inode_pages_final(&inode->i_data);
269 clear_inode(inode);
270 kernfs_put(kn);
271}
272
273int kernfs_iop_permission(struct user_namespace *mnt_userns,
274 struct inode *inode, int mask)
275{
276 struct kernfs_node *kn;
277 int ret;
278
279 if (mask & MAY_NOT_BLOCK)
280 return -ECHILD;
281
282 kn = inode->i_private;
283
284 down_read(&kernfs_rwsem);
285 spin_lock(&inode->i_lock);
286 kernfs_refresh_inode(kn, inode);
287 ret = generic_permission(&init_user_ns, inode, mask);
288 spin_unlock(&inode->i_lock);
289 up_read(&kernfs_rwsem);
290
291 return ret;
292}
293
294int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
295 void *value, size_t size)
296{
297 struct kernfs_iattrs *attrs = kernfs_iattrs_noalloc(kn);
298 if (!attrs)
299 return -ENODATA;
300
301 return simple_xattr_get(&attrs->xattrs, name, value, size);
302}
303
304int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
305 const void *value, size_t size, int flags)
306{
307 struct kernfs_iattrs *attrs = kernfs_iattrs(kn);
308 if (!attrs)
309 return -ENOMEM;
310
311 return simple_xattr_set(&attrs->xattrs, name, value, size, flags, NULL);
312}
313
314static int kernfs_vfs_xattr_get(const struct xattr_handler *handler,
315 struct dentry *unused, struct inode *inode,
316 const char *suffix, void *value, size_t size)
317{
318 const char *name = xattr_full_name(handler, suffix);
319 struct kernfs_node *kn = inode->i_private;
320
321 return kernfs_xattr_get(kn, name, value, size);
322}
323
324static int kernfs_vfs_xattr_set(const struct xattr_handler *handler,
325 struct user_namespace *mnt_userns,
326 struct dentry *unused, struct inode *inode,
327 const char *suffix, const void *value,
328 size_t size, int flags)
329{
330 const char *name = xattr_full_name(handler, suffix);
331 struct kernfs_node *kn = inode->i_private;
332
333 return kernfs_xattr_set(kn, name, value, size, flags);
334}
335
336static int kernfs_vfs_user_xattr_add(struct kernfs_node *kn,
337 const char *full_name,
338 struct simple_xattrs *xattrs,
339 const void *value, size_t size, int flags)
340{
341 atomic_t *sz = &kn->iattr->user_xattr_size;
342 atomic_t *nr = &kn->iattr->nr_user_xattrs;
343 ssize_t removed_size;
344 int ret;
345
346 if (atomic_inc_return(nr) > KERNFS_MAX_USER_XATTRS) {
347 ret = -ENOSPC;
348 goto dec_count_out;
349 }
350
351 if (atomic_add_return(size, sz) > KERNFS_USER_XATTR_SIZE_LIMIT) {
352 ret = -ENOSPC;
353 goto dec_size_out;
354 }
355
356 ret = simple_xattr_set(xattrs, full_name, value, size, flags,
357 &removed_size);
358
359 if (!ret && removed_size >= 0)
360 size = removed_size;
361 else if (!ret)
362 return 0;
363dec_size_out:
364 atomic_sub(size, sz);
365dec_count_out:
366 atomic_dec(nr);
367 return ret;
368}
369
370static int kernfs_vfs_user_xattr_rm(struct kernfs_node *kn,
371 const char *full_name,
372 struct simple_xattrs *xattrs,
373 const void *value, size_t size, int flags)
374{
375 atomic_t *sz = &kn->iattr->user_xattr_size;
376 atomic_t *nr = &kn->iattr->nr_user_xattrs;
377 ssize_t removed_size;
378 int ret;
379
380 ret = simple_xattr_set(xattrs, full_name, value, size, flags,
381 &removed_size);
382
383 if (removed_size >= 0) {
384 atomic_sub(removed_size, sz);
385 atomic_dec(nr);
386 }
387
388 return ret;
389}
390
391static int kernfs_vfs_user_xattr_set(const struct xattr_handler *handler,
392 struct user_namespace *mnt_userns,
393 struct dentry *unused, struct inode *inode,
394 const char *suffix, const void *value,
395 size_t size, int flags)
396{
397 const char *full_name = xattr_full_name(handler, suffix);
398 struct kernfs_node *kn = inode->i_private;
399 struct kernfs_iattrs *attrs;
400
401 if (!(kernfs_root(kn)->flags & KERNFS_ROOT_SUPPORT_USER_XATTR))
402 return -EOPNOTSUPP;
403
404 attrs = kernfs_iattrs(kn);
405 if (!attrs)
406 return -ENOMEM;
407
408 if (value)
409 return kernfs_vfs_user_xattr_add(kn, full_name, &attrs->xattrs,
410 value, size, flags);
411 else
412 return kernfs_vfs_user_xattr_rm(kn, full_name, &attrs->xattrs,
413 value, size, flags);
414
415}
416
417static const struct xattr_handler kernfs_trusted_xattr_handler = {
418 .prefix = XATTR_TRUSTED_PREFIX,
419 .get = kernfs_vfs_xattr_get,
420 .set = kernfs_vfs_xattr_set,
421};
422
423static const struct xattr_handler kernfs_security_xattr_handler = {
424 .prefix = XATTR_SECURITY_PREFIX,
425 .get = kernfs_vfs_xattr_get,
426 .set = kernfs_vfs_xattr_set,
427};
428
429static const struct xattr_handler kernfs_user_xattr_handler = {
430 .prefix = XATTR_USER_PREFIX,
431 .get = kernfs_vfs_xattr_get,
432 .set = kernfs_vfs_user_xattr_set,
433};
434
435const struct xattr_handler *kernfs_xattr_handlers[] = {
436 &kernfs_trusted_xattr_handler,
437 &kernfs_security_xattr_handler,
438 &kernfs_user_xattr_handler,
439 NULL
440};
441