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/file.h>
13 #include <linux/xattr.h>
14 #include <linux/mount.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
) {
36 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
41 * No restriction for security.* and system.* from the VFS. Decision
42 * on these is left to the underlying filesystem / security module.
44 if (!strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
) ||
45 !strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
49 * The trusted.* namespace can only be accessed by a privileged user.
51 if (!strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
))
52 return (capable(CAP_SYS_ADMIN
) ? 0 : -EPERM
);
54 /* In user.* namespace, only regular files and directories can have
55 * extended attributes. For sticky directories, only the owner and
56 * privileged user can write attributes.
58 if (!strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
)) {
59 if (!S_ISREG(inode
->i_mode
) && !S_ISDIR(inode
->i_mode
))
61 if (S_ISDIR(inode
->i_mode
) && (inode
->i_mode
& S_ISVTX
) &&
62 (mask
& MAY_WRITE
) && !is_owner_or_cap(inode
))
66 return inode_permission(inode
, mask
);
70 vfs_setxattr(struct dentry
*dentry
, const char *name
, const void *value
,
71 size_t size
, int flags
)
73 struct inode
*inode
= dentry
->d_inode
;
76 error
= xattr_permission(inode
, name
, MAY_WRITE
);
80 mutex_lock(&inode
->i_mutex
);
81 error
= security_inode_setxattr(dentry
, name
, value
, size
, flags
);
85 if (inode
->i_op
->setxattr
) {
86 error
= inode
->i_op
->setxattr(dentry
, name
, value
, size
, flags
);
88 fsnotify_xattr(dentry
);
89 security_inode_post_setxattr(dentry
, name
, value
,
92 } else if (!strncmp(name
, XATTR_SECURITY_PREFIX
,
93 XATTR_SECURITY_PREFIX_LEN
)) {
94 const char *suffix
= name
+ XATTR_SECURITY_PREFIX_LEN
;
95 error
= security_inode_setsecurity(inode
, suffix
, value
,
98 fsnotify_xattr(dentry
);
101 mutex_unlock(&inode
->i_mutex
);
104 EXPORT_SYMBOL_GPL(vfs_setxattr
);
107 xattr_getsecurity(struct inode
*inode
, const char *name
, void *value
,
113 if (!value
|| !size
) {
114 len
= security_inode_getsecurity(inode
, name
, &buffer
, false);
118 len
= security_inode_getsecurity(inode
, name
, &buffer
, true);
125 memcpy(value
, buffer
, len
);
127 security_release_secctx(buffer
, len
);
131 EXPORT_SYMBOL_GPL(xattr_getsecurity
);
134 vfs_getxattr(struct dentry
*dentry
, const char *name
, void *value
, size_t size
)
136 struct inode
*inode
= dentry
->d_inode
;
139 error
= xattr_permission(inode
, name
, MAY_READ
);
143 error
= security_inode_getxattr(dentry
, name
);
147 if (!strncmp(name
, XATTR_SECURITY_PREFIX
,
148 XATTR_SECURITY_PREFIX_LEN
)) {
149 const char *suffix
= name
+ XATTR_SECURITY_PREFIX_LEN
;
150 int ret
= xattr_getsecurity(inode
, suffix
, value
, size
);
152 * Only overwrite the return value if a security module
153 * is actually active.
155 if (ret
== -EOPNOTSUPP
)
160 if (inode
->i_op
->getxattr
)
161 error
= inode
->i_op
->getxattr(dentry
, name
, value
, size
);
167 EXPORT_SYMBOL_GPL(vfs_getxattr
);
170 vfs_listxattr(struct dentry
*d
, char *list
, size_t size
)
174 error
= security_inode_listxattr(d
);
178 if (d
->d_inode
->i_op
->listxattr
) {
179 error
= d
->d_inode
->i_op
->listxattr(d
, list
, size
);
181 error
= security_inode_listsecurity(d
->d_inode
, list
, size
);
182 if (size
&& error
> size
)
187 EXPORT_SYMBOL_GPL(vfs_listxattr
);
190 vfs_removexattr(struct dentry
*dentry
, const char *name
)
192 struct inode
*inode
= dentry
->d_inode
;
195 if (!inode
->i_op
->removexattr
)
198 error
= xattr_permission(inode
, name
, MAY_WRITE
);
202 error
= security_inode_removexattr(dentry
, name
);
206 mutex_lock(&inode
->i_mutex
);
207 error
= inode
->i_op
->removexattr(dentry
, name
);
208 mutex_unlock(&inode
->i_mutex
);
211 fsnotify_xattr(dentry
);
214 EXPORT_SYMBOL_GPL(vfs_removexattr
);
218 * Extended attribute SET operations
221 setxattr(struct dentry
*d
, const char __user
*name
, const void __user
*value
,
222 size_t size
, int flags
)
226 char kname
[XATTR_NAME_MAX
+ 1];
228 if (flags
& ~(XATTR_CREATE
|XATTR_REPLACE
))
231 error
= strncpy_from_user(kname
, name
, sizeof(kname
));
232 if (error
== 0 || error
== sizeof(kname
))
238 if (size
> XATTR_SIZE_MAX
)
240 kvalue
= memdup_user(value
, size
);
242 return PTR_ERR(kvalue
);
245 error
= vfs_setxattr(d
, kname
, kvalue
, size
, flags
);
250 SYSCALL_DEFINE5(setxattr
, const char __user
*, pathname
,
251 const char __user
*, name
, const void __user
*, value
,
252 size_t, size
, int, flags
)
257 error
= user_path(pathname
, &path
);
260 error
= mnt_want_write(path
.mnt
);
262 error
= setxattr(path
.dentry
, name
, value
, size
, flags
);
263 mnt_drop_write(path
.mnt
);
269 SYSCALL_DEFINE5(lsetxattr
, const char __user
*, pathname
,
270 const char __user
*, name
, const void __user
*, value
,
271 size_t, size
, int, flags
)
276 error
= user_lpath(pathname
, &path
);
279 error
= mnt_want_write(path
.mnt
);
281 error
= setxattr(path
.dentry
, name
, value
, size
, flags
);
282 mnt_drop_write(path
.mnt
);
288 SYSCALL_DEFINE5(fsetxattr
, int, fd
, const char __user
*, name
,
289 const void __user
*,value
, size_t, size
, int, flags
)
292 struct dentry
*dentry
;
298 dentry
= f
->f_path
.dentry
;
299 audit_inode(NULL
, dentry
);
300 error
= mnt_want_write_file(f
);
302 error
= setxattr(dentry
, name
, value
, size
, flags
);
303 mnt_drop_write(f
->f_path
.mnt
);
310 * Extended attribute GET operations
313 getxattr(struct dentry
*d
, const char __user
*name
, void __user
*value
,
318 char kname
[XATTR_NAME_MAX
+ 1];
320 error
= strncpy_from_user(kname
, name
, sizeof(kname
));
321 if (error
== 0 || error
== sizeof(kname
))
327 if (size
> XATTR_SIZE_MAX
)
328 size
= XATTR_SIZE_MAX
;
329 kvalue
= kzalloc(size
, GFP_KERNEL
);
334 error
= vfs_getxattr(d
, kname
, kvalue
, size
);
336 if (size
&& copy_to_user(value
, kvalue
, error
))
338 } else if (error
== -ERANGE
&& size
>= XATTR_SIZE_MAX
) {
339 /* The file system tried to returned a value bigger
340 than XATTR_SIZE_MAX bytes. Not possible. */
347 SYSCALL_DEFINE4(getxattr
, const char __user
*, pathname
,
348 const char __user
*, name
, void __user
*, value
, size_t, size
)
353 error
= user_path(pathname
, &path
);
356 error
= getxattr(path
.dentry
, name
, value
, size
);
361 SYSCALL_DEFINE4(lgetxattr
, const char __user
*, pathname
,
362 const char __user
*, name
, void __user
*, value
, size_t, size
)
367 error
= user_lpath(pathname
, &path
);
370 error
= getxattr(path
.dentry
, name
, value
, size
);
375 SYSCALL_DEFINE4(fgetxattr
, int, fd
, const char __user
*, name
,
376 void __user
*, value
, size_t, size
)
379 ssize_t error
= -EBADF
;
384 audit_inode(NULL
, f
->f_path
.dentry
);
385 error
= getxattr(f
->f_path
.dentry
, name
, value
, size
);
391 * Extended attribute LIST operations
394 listxattr(struct dentry
*d
, char __user
*list
, size_t size
)
400 if (size
> XATTR_LIST_MAX
)
401 size
= XATTR_LIST_MAX
;
402 klist
= kmalloc(size
, GFP_KERNEL
);
407 error
= vfs_listxattr(d
, klist
, size
);
409 if (size
&& copy_to_user(list
, klist
, error
))
411 } else if (error
== -ERANGE
&& size
>= XATTR_LIST_MAX
) {
412 /* The file system tried to returned a list bigger
413 than XATTR_LIST_MAX bytes. Not possible. */
420 SYSCALL_DEFINE3(listxattr
, const char __user
*, pathname
, char __user
*, list
,
426 error
= user_path(pathname
, &path
);
429 error
= listxattr(path
.dentry
, list
, size
);
434 SYSCALL_DEFINE3(llistxattr
, const char __user
*, pathname
, char __user
*, list
,
440 error
= user_lpath(pathname
, &path
);
443 error
= listxattr(path
.dentry
, list
, size
);
448 SYSCALL_DEFINE3(flistxattr
, int, fd
, char __user
*, list
, size_t, size
)
451 ssize_t error
= -EBADF
;
456 audit_inode(NULL
, f
->f_path
.dentry
);
457 error
= listxattr(f
->f_path
.dentry
, list
, size
);
463 * Extended attribute REMOVE operations
466 removexattr(struct dentry
*d
, const char __user
*name
)
469 char kname
[XATTR_NAME_MAX
+ 1];
471 error
= strncpy_from_user(kname
, name
, sizeof(kname
));
472 if (error
== 0 || error
== sizeof(kname
))
477 return vfs_removexattr(d
, kname
);
480 SYSCALL_DEFINE2(removexattr
, const char __user
*, pathname
,
481 const char __user
*, name
)
486 error
= user_path(pathname
, &path
);
489 error
= mnt_want_write(path
.mnt
);
491 error
= removexattr(path
.dentry
, name
);
492 mnt_drop_write(path
.mnt
);
498 SYSCALL_DEFINE2(lremovexattr
, const char __user
*, pathname
,
499 const char __user
*, name
)
504 error
= user_lpath(pathname
, &path
);
507 error
= mnt_want_write(path
.mnt
);
509 error
= removexattr(path
.dentry
, name
);
510 mnt_drop_write(path
.mnt
);
516 SYSCALL_DEFINE2(fremovexattr
, int, fd
, const char __user
*, name
)
519 struct dentry
*dentry
;
525 dentry
= f
->f_path
.dentry
;
526 audit_inode(NULL
, dentry
);
527 error
= mnt_want_write_file(f
);
529 error
= removexattr(dentry
, name
);
530 mnt_drop_write(f
->f_path
.mnt
);
538 strcmp_prefix(const char *a
, const char *a_prefix
)
540 while (*a_prefix
&& *a
== *a_prefix
) {
544 return *a_prefix
? NULL
: a
;
548 * In order to implement different sets of xattr operations for each xattr
549 * prefix with the generic xattr API, a filesystem should create a
550 * null-terminated array of struct xattr_handler (one for each prefix) and
551 * hang a pointer to it off of the s_xattr field of the superblock.
553 * The generic_fooxattr() functions will use this list to dispatch xattr
554 * operations to the correct xattr_handler.
556 #define for_each_xattr_handler(handlers, handler) \
557 for ((handler) = *(handlers)++; \
559 (handler) = *(handlers)++)
562 * Find the xattr_handler with the matching prefix.
564 static struct xattr_handler
*
565 xattr_resolve_name(struct xattr_handler
**handlers
, const char **name
)
567 struct xattr_handler
*handler
;
572 for_each_xattr_handler(handlers
, handler
) {
573 const char *n
= strcmp_prefix(*name
, handler
->prefix
);
583 * Find the handler for the prefix and dispatch its get() operation.
586 generic_getxattr(struct dentry
*dentry
, const char *name
, void *buffer
, size_t size
)
588 struct xattr_handler
*handler
;
589 struct inode
*inode
= dentry
->d_inode
;
591 handler
= xattr_resolve_name(inode
->i_sb
->s_xattr
, &name
);
594 return handler
->get(inode
, name
, buffer
, size
);
598 * Combine the results of the list() operation from every xattr_handler in the
602 generic_listxattr(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
604 struct inode
*inode
= dentry
->d_inode
;
605 struct xattr_handler
*handler
, **handlers
= inode
->i_sb
->s_xattr
;
606 unsigned int size
= 0;
609 for_each_xattr_handler(handlers
, handler
)
610 size
+= handler
->list(inode
, NULL
, 0, NULL
, 0);
614 for_each_xattr_handler(handlers
, handler
) {
615 size
= handler
->list(inode
, buf
, buffer_size
, NULL
, 0);
616 if (size
> buffer_size
)
627 * Find the handler for the prefix and dispatch its set() operation.
630 generic_setxattr(struct dentry
*dentry
, const char *name
, const void *value
, size_t size
, int flags
)
632 struct xattr_handler
*handler
;
633 struct inode
*inode
= dentry
->d_inode
;
636 value
= ""; /* empty EA, do not remove */
637 handler
= xattr_resolve_name(inode
->i_sb
->s_xattr
, &name
);
640 return handler
->set(inode
, name
, value
, size
, flags
);
644 * Find the handler for the prefix and dispatch its set() operation to remove
645 * any associated extended attribute.
648 generic_removexattr(struct dentry
*dentry
, const char *name
)
650 struct xattr_handler
*handler
;
651 struct inode
*inode
= dentry
->d_inode
;
653 handler
= xattr_resolve_name(inode
->i_sb
->s_xattr
, &name
);
656 return handler
->set(inode
, name
, NULL
, 0, XATTR_REPLACE
);
659 EXPORT_SYMBOL(generic_getxattr
);
660 EXPORT_SYMBOL(generic_listxattr
);
661 EXPORT_SYMBOL(generic_setxattr
);
662 EXPORT_SYMBOL(generic_removexattr
);