4 Extended attribute handling.
6 Copyright (C) 2001 by Andreas Gruenbacher <a.gruenbacher@computer.org>
7 Copyright (C) 2001 SGI - Silicon Graphics, Inc <linux-xfs@oss.sgi.com>
8 Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
11 #include <linux/slab.h>
12 #include <linux/smp_lock.h>
13 #include <linux/file.h>
14 #include <linux/xattr.h>
15 #include <linux/namei.h>
16 #include <linux/security.h>
17 #include <linux/syscalls.h>
18 #include <linux/module.h>
19 #include <linux/fsnotify.h>
20 #include <linux/audit.h>
21 #include <asm/uaccess.h>
25 * Check permissions for extended attribute access. This is a bit complicated
26 * because different namespaces have very different rules.
29 xattr_permission(struct inode
*inode
, const char *name
, int mask
)
32 * We can never set or remove an extended attribute on a read-only
33 * filesystem or on an immutable / append-only inode.
35 if (mask
& MAY_WRITE
) {
38 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
43 * No restriction for security.* and system.* from the VFS. Decision
44 * on these is left to the underlying filesystem / security module.
46 if (!strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
) ||
47 !strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
51 * The trusted.* namespace can only be accessed by a privileged user.
53 if (!strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
))
54 return (capable(CAP_SYS_ADMIN
) ? 0 : -EPERM
);
56 /* In user.* namespace, only regular files and directories can have
57 * extended attributes. For sticky directories, only the owner and
58 * privileged user can write attributes.
60 if (!strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
)) {
61 if (!S_ISREG(inode
->i_mode
) && !S_ISDIR(inode
->i_mode
))
63 if (S_ISDIR(inode
->i_mode
) && (inode
->i_mode
& S_ISVTX
) &&
64 (mask
& MAY_WRITE
) && (current
->fsuid
!= inode
->i_uid
) &&
69 return permission(inode
, mask
, NULL
);
73 vfs_setxattr(struct dentry
*dentry
, char *name
, void *value
,
74 size_t size
, int flags
)
76 struct inode
*inode
= dentry
->d_inode
;
79 error
= xattr_permission(inode
, name
, MAY_WRITE
);
83 mutex_lock(&inode
->i_mutex
);
84 error
= security_inode_setxattr(dentry
, name
, value
, size
, flags
);
88 if (inode
->i_op
->setxattr
) {
89 error
= inode
->i_op
->setxattr(dentry
, name
, value
, size
, flags
);
91 fsnotify_xattr(dentry
);
92 security_inode_post_setxattr(dentry
, name
, value
,
95 } else if (!strncmp(name
, XATTR_SECURITY_PREFIX
,
96 XATTR_SECURITY_PREFIX_LEN
)) {
97 const char *suffix
= name
+ XATTR_SECURITY_PREFIX_LEN
;
98 error
= security_inode_setsecurity(inode
, suffix
, value
,
101 fsnotify_xattr(dentry
);
104 mutex_unlock(&inode
->i_mutex
);
107 EXPORT_SYMBOL_GPL(vfs_setxattr
);
110 vfs_getxattr(struct dentry
*dentry
, char *name
, void *value
, size_t size
)
112 struct inode
*inode
= dentry
->d_inode
;
115 error
= xattr_permission(inode
, name
, MAY_READ
);
119 error
= security_inode_getxattr(dentry
, name
);
123 if (inode
->i_op
->getxattr
)
124 error
= inode
->i_op
->getxattr(dentry
, name
, value
, size
);
128 if (!strncmp(name
, XATTR_SECURITY_PREFIX
,
129 XATTR_SECURITY_PREFIX_LEN
)) {
130 const char *suffix
= name
+ XATTR_SECURITY_PREFIX_LEN
;
131 int ret
= security_inode_getsecurity(inode
, suffix
, value
,
134 * Only overwrite the return value if a security module
135 * is actually active.
137 if (ret
!= -EOPNOTSUPP
)
143 EXPORT_SYMBOL_GPL(vfs_getxattr
);
146 vfs_listxattr(struct dentry
*d
, char *list
, size_t size
)
150 error
= security_inode_listxattr(d
);
154 if (d
->d_inode
->i_op
&& d
->d_inode
->i_op
->listxattr
) {
155 error
= d
->d_inode
->i_op
->listxattr(d
, list
, size
);
157 error
= security_inode_listsecurity(d
->d_inode
, list
, size
);
158 if (size
&& error
> size
)
163 EXPORT_SYMBOL_GPL(vfs_listxattr
);
166 vfs_removexattr(struct dentry
*dentry
, char *name
)
168 struct inode
*inode
= dentry
->d_inode
;
171 if (!inode
->i_op
->removexattr
)
174 error
= xattr_permission(inode
, name
, MAY_WRITE
);
178 error
= security_inode_removexattr(dentry
, name
);
182 mutex_lock(&inode
->i_mutex
);
183 error
= inode
->i_op
->removexattr(dentry
, name
);
184 mutex_unlock(&inode
->i_mutex
);
187 fsnotify_xattr(dentry
);
190 EXPORT_SYMBOL_GPL(vfs_removexattr
);
194 * Extended attribute SET operations
197 setxattr(struct dentry
*d
, char __user
*name
, void __user
*value
,
198 size_t size
, int flags
)
202 char kname
[XATTR_NAME_MAX
+ 1];
204 if (flags
& ~(XATTR_CREATE
|XATTR_REPLACE
))
207 error
= strncpy_from_user(kname
, name
, sizeof(kname
));
208 if (error
== 0 || error
== sizeof(kname
))
214 if (size
> XATTR_SIZE_MAX
)
216 kvalue
= kmalloc(size
, GFP_KERNEL
);
219 if (copy_from_user(kvalue
, value
, size
)) {
225 error
= vfs_setxattr(d
, kname
, kvalue
, size
, flags
);
231 sys_setxattr(char __user
*path
, char __user
*name
, void __user
*value
,
232 size_t size
, int flags
)
237 error
= user_path_walk(path
, &nd
);
240 error
= setxattr(nd
.dentry
, name
, value
, size
, flags
);
246 sys_lsetxattr(char __user
*path
, char __user
*name
, void __user
*value
,
247 size_t size
, int flags
)
252 error
= user_path_walk_link(path
, &nd
);
255 error
= setxattr(nd
.dentry
, name
, value
, size
, flags
);
261 sys_fsetxattr(int fd
, char __user
*name
, void __user
*value
,
262 size_t size
, int flags
)
265 struct dentry
*dentry
;
271 dentry
= f
->f_path
.dentry
;
272 audit_inode(NULL
, dentry
->d_inode
);
273 error
= setxattr(dentry
, name
, value
, size
, flags
);
279 * Extended attribute GET operations
282 getxattr(struct dentry
*d
, char __user
*name
, void __user
*value
, size_t size
)
286 char kname
[XATTR_NAME_MAX
+ 1];
288 error
= strncpy_from_user(kname
, name
, sizeof(kname
));
289 if (error
== 0 || error
== sizeof(kname
))
295 if (size
> XATTR_SIZE_MAX
)
296 size
= XATTR_SIZE_MAX
;
297 kvalue
= kzalloc(size
, GFP_KERNEL
);
302 error
= vfs_getxattr(d
, kname
, kvalue
, size
);
304 if (size
&& copy_to_user(value
, kvalue
, error
))
306 } else if (error
== -ERANGE
&& size
>= XATTR_SIZE_MAX
) {
307 /* The file system tried to returned a value bigger
308 than XATTR_SIZE_MAX bytes. Not possible. */
316 sys_getxattr(char __user
*path
, char __user
*name
, void __user
*value
,
322 error
= user_path_walk(path
, &nd
);
325 error
= getxattr(nd
.dentry
, name
, value
, size
);
331 sys_lgetxattr(char __user
*path
, char __user
*name
, void __user
*value
,
337 error
= user_path_walk_link(path
, &nd
);
340 error
= getxattr(nd
.dentry
, name
, value
, size
);
346 sys_fgetxattr(int fd
, char __user
*name
, void __user
*value
, size_t size
)
349 ssize_t error
= -EBADF
;
354 error
= getxattr(f
->f_path
.dentry
, name
, value
, size
);
360 * Extended attribute LIST operations
363 listxattr(struct dentry
*d
, char __user
*list
, size_t size
)
369 if (size
> XATTR_LIST_MAX
)
370 size
= XATTR_LIST_MAX
;
371 klist
= kmalloc(size
, GFP_KERNEL
);
376 error
= vfs_listxattr(d
, klist
, size
);
378 if (size
&& copy_to_user(list
, klist
, error
))
380 } else if (error
== -ERANGE
&& size
>= XATTR_LIST_MAX
) {
381 /* The file system tried to returned a list bigger
382 than XATTR_LIST_MAX bytes. Not possible. */
390 sys_listxattr(char __user
*path
, char __user
*list
, size_t size
)
395 error
= user_path_walk(path
, &nd
);
398 error
= listxattr(nd
.dentry
, list
, size
);
404 sys_llistxattr(char __user
*path
, char __user
*list
, size_t size
)
409 error
= user_path_walk_link(path
, &nd
);
412 error
= listxattr(nd
.dentry
, list
, size
);
418 sys_flistxattr(int fd
, char __user
*list
, size_t size
)
421 ssize_t error
= -EBADF
;
426 error
= listxattr(f
->f_path
.dentry
, list
, size
);
432 * Extended attribute REMOVE operations
435 removexattr(struct dentry
*d
, char __user
*name
)
438 char kname
[XATTR_NAME_MAX
+ 1];
440 error
= strncpy_from_user(kname
, name
, sizeof(kname
));
441 if (error
== 0 || error
== sizeof(kname
))
446 return vfs_removexattr(d
, kname
);
450 sys_removexattr(char __user
*path
, char __user
*name
)
455 error
= user_path_walk(path
, &nd
);
458 error
= removexattr(nd
.dentry
, name
);
464 sys_lremovexattr(char __user
*path
, char __user
*name
)
469 error
= user_path_walk_link(path
, &nd
);
472 error
= removexattr(nd
.dentry
, name
);
478 sys_fremovexattr(int fd
, char __user
*name
)
481 struct dentry
*dentry
;
487 dentry
= f
->f_path
.dentry
;
488 audit_inode(NULL
, dentry
->d_inode
);
489 error
= removexattr(dentry
, name
);
496 strcmp_prefix(const char *a
, const char *a_prefix
)
498 while (*a_prefix
&& *a
== *a_prefix
) {
502 return *a_prefix
? NULL
: a
;
506 * In order to implement different sets of xattr operations for each xattr
507 * prefix with the generic xattr API, a filesystem should create a
508 * null-terminated array of struct xattr_handler (one for each prefix) and
509 * hang a pointer to it off of the s_xattr field of the superblock.
511 * The generic_fooxattr() functions will use this list to dispatch xattr
512 * operations to the correct xattr_handler.
514 #define for_each_xattr_handler(handlers, handler) \
515 for ((handler) = *(handlers)++; \
517 (handler) = *(handlers)++)
520 * Find the xattr_handler with the matching prefix.
522 static struct xattr_handler
*
523 xattr_resolve_name(struct xattr_handler
**handlers
, const char **name
)
525 struct xattr_handler
*handler
;
530 for_each_xattr_handler(handlers
, handler
) {
531 const char *n
= strcmp_prefix(*name
, handler
->prefix
);
541 * Find the handler for the prefix and dispatch its get() operation.
544 generic_getxattr(struct dentry
*dentry
, const char *name
, void *buffer
, size_t size
)
546 struct xattr_handler
*handler
;
547 struct inode
*inode
= dentry
->d_inode
;
549 handler
= xattr_resolve_name(inode
->i_sb
->s_xattr
, &name
);
552 return handler
->get(inode
, name
, buffer
, size
);
556 * Combine the results of the list() operation from every xattr_handler in the
560 generic_listxattr(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
562 struct inode
*inode
= dentry
->d_inode
;
563 struct xattr_handler
*handler
, **handlers
= inode
->i_sb
->s_xattr
;
564 unsigned int size
= 0;
567 for_each_xattr_handler(handlers
, handler
)
568 size
+= handler
->list(inode
, NULL
, 0, NULL
, 0);
572 for_each_xattr_handler(handlers
, handler
) {
573 size
= handler
->list(inode
, buf
, buffer_size
, NULL
, 0);
574 if (size
> buffer_size
)
585 * Find the handler for the prefix and dispatch its set() operation.
588 generic_setxattr(struct dentry
*dentry
, const char *name
, const void *value
, size_t size
, int flags
)
590 struct xattr_handler
*handler
;
591 struct inode
*inode
= dentry
->d_inode
;
594 value
= ""; /* empty EA, do not remove */
595 handler
= xattr_resolve_name(inode
->i_sb
->s_xattr
, &name
);
598 return handler
->set(inode
, name
, value
, size
, flags
);
602 * Find the handler for the prefix and dispatch its set() operation to remove
603 * any associated extended attribute.
606 generic_removexattr(struct dentry
*dentry
, const char *name
)
608 struct xattr_handler
*handler
;
609 struct inode
*inode
= dentry
->d_inode
;
611 handler
= xattr_resolve_name(inode
->i_sb
->s_xattr
, &name
);
614 return handler
->set(inode
, name
, NULL
, 0, XATTR_REPLACE
);
617 EXPORT_SYMBOL(generic_getxattr
);
618 EXPORT_SYMBOL(generic_listxattr
);
619 EXPORT_SYMBOL(generic_setxattr
);
620 EXPORT_SYMBOL(generic_removexattr
);