blob: 851808c92b3002990aed3bcd8840a1ffa5bb1ba6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 File: fs/xattr.c
3
4 Extended attribute handling.
5
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>
9 */
10#include <linux/fs.h>
11#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/file.h>
13#include <linux/xattr.h>
Dave Hansen18f335a2008-02-15 14:37:38 -080014#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/namei.h>
16#include <linux/security.h>
17#include <linux/syscalls.h>
18#include <linux/module.h>
Robert Love0eeca282005-07-12 17:06:03 -040019#include <linux/fsnotify.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000020#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/uaccess.h>
22
Christoph Hellwig5be196e2006-01-09 20:51:55 -080023
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080024/*
25 * Check permissions for extended attribute access. This is a bit complicated
26 * because different namespaces have very different rules.
27 */
28static int
29xattr_permission(struct inode *inode, const char *name, int mask)
30{
31 /*
32 * We can never set or remove an extended attribute on a read-only
33 * filesystem or on an immutable / append-only inode.
34 */
35 if (mask & MAY_WRITE) {
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080036 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
37 return -EPERM;
38 }
39
40 /*
41 * No restriction for security.* and system.* from the VFS. Decision
42 * on these is left to the underlying filesystem / security module.
43 */
44 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
45 !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
46 return 0;
47
48 /*
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +020049 * The trusted.* namespace can only be accessed by privileged users.
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080050 */
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +020051 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
52 if (!capable(CAP_SYS_ADMIN))
53 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
54 return 0;
55 }
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080056
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +020057 /*
58 * In the user.* namespace, only regular files and directories can have
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -080059 * extended attributes. For sticky directories, only the owner and
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +020060 * privileged users can write attributes.
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -080061 */
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080062 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -080063 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +020064 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -080065 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
Serge E. Hallyn2e149672011-03-23 16:43:26 -070066 (mask & MAY_WRITE) && !inode_owner_or_capable(inode))
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080067 return -EPERM;
68 }
69
Al Virof419a2e2008-07-22 00:07:17 -040070 return inode_permission(inode, mask);
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080071}
72
David P. Quigleyb1ab7e42009-09-03 14:25:56 -040073/**
74 * __vfs_setxattr_noperm - perform setxattr operation without performing
75 * permission checks.
76 *
77 * @dentry - object to perform setxattr on
78 * @name - xattr name to set
79 * @value - value to set @name to
80 * @size - size of @value
81 * @flags - flags to pass into filesystem operations
82 *
83 * returns the result of the internal setxattr or setsecurity operations.
84 *
85 * This function requires the caller to lock the inode's i_mutex before it
86 * is executed. It also assumes that the caller will make the appropriate
87 * permission checks.
88 */
89int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
90 const void *value, size_t size, int flags)
Christoph Hellwig5be196e2006-01-09 20:51:55 -080091{
92 struct inode *inode = dentry->d_inode;
David P. Quigleyb1ab7e42009-09-03 14:25:56 -040093 int error = -EOPNOTSUPP;
Andi Kleen69b45732011-05-28 08:25:51 -070094 int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
95 XATTR_SECURITY_PREFIX_LEN);
Christoph Hellwig5be196e2006-01-09 20:51:55 -080096
Andi Kleen69b45732011-05-28 08:25:51 -070097 if (issec)
98 inode->i_flags &= ~S_NOSEC;
Christoph Hellwig5be196e2006-01-09 20:51:55 -080099 if (inode->i_op->setxattr) {
100 error = inode->i_op->setxattr(dentry, name, value, size, flags);
101 if (!error) {
102 fsnotify_xattr(dentry);
103 security_inode_post_setxattr(dentry, name, value,
104 size, flags);
105 }
Andi Kleen69b45732011-05-28 08:25:51 -0700106 } else if (issec) {
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800107 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800108 error = security_inode_setsecurity(inode, suffix, value,
109 size, flags);
110 if (!error)
111 fsnotify_xattr(dentry);
112 }
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400113
114 return error;
115}
116
117
118int
119vfs_setxattr(struct dentry *dentry, const char *name, const void *value,
120 size_t size, int flags)
121{
122 struct inode *inode = dentry->d_inode;
123 int error;
124
125 error = xattr_permission(inode, name, MAY_WRITE);
126 if (error)
127 return error;
128
129 mutex_lock(&inode->i_mutex);
130 error = security_inode_setxattr(dentry, name, value, size, flags);
131 if (error)
132 goto out;
133
134 error = __vfs_setxattr_noperm(dentry, name, value, size, flags);
135
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800136out:
137 mutex_unlock(&inode->i_mutex);
138 return error;
139}
140EXPORT_SYMBOL_GPL(vfs_setxattr);
141
142ssize_t
David P. Quigley42492592008-02-04 22:29:39 -0800143xattr_getsecurity(struct inode *inode, const char *name, void *value,
144 size_t size)
145{
146 void *buffer = NULL;
147 ssize_t len;
148
149 if (!value || !size) {
150 len = security_inode_getsecurity(inode, name, &buffer, false);
151 goto out_noalloc;
152 }
153
154 len = security_inode_getsecurity(inode, name, &buffer, true);
155 if (len < 0)
156 return len;
157 if (size < len) {
158 len = -ERANGE;
159 goto out;
160 }
161 memcpy(value, buffer, len);
162out:
163 security_release_secctx(buffer, len);
164out_noalloc:
165 return len;
166}
167EXPORT_SYMBOL_GPL(xattr_getsecurity);
168
Mimi Zohar1601fba2011-03-09 14:23:34 -0500169/*
170 * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
171 *
172 * Allocate memory, if not already allocated, or re-allocate correct size,
173 * before retrieving the extended attribute.
174 *
175 * Returns the result of alloc, if failed, or the getxattr operation.
176 */
177ssize_t
178vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
179 size_t xattr_size, gfp_t flags)
180{
181 struct inode *inode = dentry->d_inode;
182 char *value = *xattr_value;
183 int error;
184
185 error = xattr_permission(inode, name, MAY_READ);
186 if (error)
187 return error;
188
189 if (!inode->i_op->getxattr)
190 return -EOPNOTSUPP;
191
192 error = inode->i_op->getxattr(dentry, name, NULL, 0);
193 if (error < 0)
194 return error;
195
196 if (!value || (error > xattr_size)) {
197 value = krealloc(*xattr_value, error + 1, flags);
198 if (!value)
199 return -ENOMEM;
200 memset(value, 0, error + 1);
201 }
202
203 error = inode->i_op->getxattr(dentry, name, value, error);
204 *xattr_value = value;
205 return error;
206}
207
208/* Compare an extended attribute value with the given value */
209int vfs_xattr_cmp(struct dentry *dentry, const char *xattr_name,
210 const char *value, size_t size, gfp_t flags)
211{
212 char *xattr_value = NULL;
213 int rc;
214
215 rc = vfs_getxattr_alloc(dentry, xattr_name, &xattr_value, 0, flags);
216 if (rc < 0)
217 return rc;
218
219 if ((rc != size) || (memcmp(xattr_value, value, rc) != 0))
220 rc = -EINVAL;
221 else
222 rc = 0;
223 kfree(xattr_value);
224 return rc;
225}
226
David P. Quigley42492592008-02-04 22:29:39 -0800227ssize_t
David Howells8f0cfa52008-04-29 00:59:41 -0700228vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800229{
230 struct inode *inode = dentry->d_inode;
231 int error;
232
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800233 error = xattr_permission(inode, name, MAY_READ);
234 if (error)
235 return error;
236
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800237 error = security_inode_getxattr(dentry, name);
238 if (error)
239 return error;
240
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800241 if (!strncmp(name, XATTR_SECURITY_PREFIX,
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800242 XATTR_SECURITY_PREFIX_LEN)) {
243 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
David P. Quigley42492592008-02-04 22:29:39 -0800244 int ret = xattr_getsecurity(inode, suffix, value, size);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800245 /*
246 * Only overwrite the return value if a security module
247 * is actually active.
248 */
David P. Quigley4bea5802008-02-04 22:29:40 -0800249 if (ret == -EOPNOTSUPP)
250 goto nolsm;
251 return ret;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800252 }
David P. Quigley4bea5802008-02-04 22:29:40 -0800253nolsm:
254 if (inode->i_op->getxattr)
255 error = inode->i_op->getxattr(dentry, name, value, size);
256 else
257 error = -EOPNOTSUPP;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800258
259 return error;
260}
261EXPORT_SYMBOL_GPL(vfs_getxattr);
262
Bill Nottingham659564c2006-10-09 16:10:48 -0400263ssize_t
264vfs_listxattr(struct dentry *d, char *list, size_t size)
265{
266 ssize_t error;
267
268 error = security_inode_listxattr(d);
269 if (error)
270 return error;
271 error = -EOPNOTSUPP;
Al Viroacfa4382008-12-04 10:06:33 -0500272 if (d->d_inode->i_op->listxattr) {
Bill Nottingham659564c2006-10-09 16:10:48 -0400273 error = d->d_inode->i_op->listxattr(d, list, size);
274 } else {
275 error = security_inode_listsecurity(d->d_inode, list, size);
276 if (size && error > size)
277 error = -ERANGE;
278 }
279 return error;
280}
281EXPORT_SYMBOL_GPL(vfs_listxattr);
282
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800283int
David Howells8f0cfa52008-04-29 00:59:41 -0700284vfs_removexattr(struct dentry *dentry, const char *name)
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800285{
286 struct inode *inode = dentry->d_inode;
287 int error;
288
289 if (!inode->i_op->removexattr)
290 return -EOPNOTSUPP;
291
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800292 error = xattr_permission(inode, name, MAY_WRITE);
293 if (error)
294 return error;
295
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800296 error = security_inode_removexattr(dentry, name);
297 if (error)
298 return error;
299
300 mutex_lock(&inode->i_mutex);
301 error = inode->i_op->removexattr(dentry, name);
302 mutex_unlock(&inode->i_mutex);
303
304 if (!error)
305 fsnotify_xattr(dentry);
306 return error;
307}
308EXPORT_SYMBOL_GPL(vfs_removexattr);
309
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311/*
312 * Extended attribute SET operations
313 */
314static long
David Howells8f0cfa52008-04-29 00:59:41 -0700315setxattr(struct dentry *d, const char __user *name, const void __user *value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 size_t size, int flags)
317{
318 int error;
319 void *kvalue = NULL;
320 char kname[XATTR_NAME_MAX + 1];
321
322 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
323 return -EINVAL;
324
325 error = strncpy_from_user(kname, name, sizeof(kname));
326 if (error == 0 || error == sizeof(kname))
327 error = -ERANGE;
328 if (error < 0)
329 return error;
330
331 if (size) {
332 if (size > XATTR_SIZE_MAX)
333 return -E2BIG;
Li Zefan3939fcd2009-04-08 15:06:12 +0800334 kvalue = memdup_user(value, size);
335 if (IS_ERR(kvalue))
336 return PTR_ERR(kvalue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800339 error = vfs_setxattr(d, kname, kvalue, size, flags);
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800340 kfree(kvalue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return error;
342}
343
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100344SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
345 const char __user *, name, const void __user *, value,
346 size_t, size, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Al Viro2d8f3032008-07-22 09:59:21 -0400348 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 int error;
350
Al Viro2d8f3032008-07-22 09:59:21 -0400351 error = user_path(pathname, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (error)
353 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400354 error = mnt_want_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800355 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400356 error = setxattr(path.dentry, name, value, size, flags);
357 mnt_drop_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800358 }
Al Viro2d8f3032008-07-22 09:59:21 -0400359 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return error;
361}
362
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100363SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
364 const char __user *, name, const void __user *, value,
365 size_t, size, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Al Viro2d8f3032008-07-22 09:59:21 -0400367 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 int error;
369
Al Viro2d8f3032008-07-22 09:59:21 -0400370 error = user_lpath(pathname, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (error)
372 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400373 error = mnt_want_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800374 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400375 error = setxattr(path.dentry, name, value, size, flags);
376 mnt_drop_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800377 }
Al Viro2d8f3032008-07-22 09:59:21 -0400378 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return error;
380}
381
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100382SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
383 const void __user *,value, size_t, size, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 struct file *f;
Amy Griffis73241cc2005-11-03 16:00:25 +0000386 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 int error = -EBADF;
388
389 f = fget(fd);
390 if (!f)
391 return error;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800392 dentry = f->f_path.dentry;
Al Viro5a190ae2007-06-07 12:19:32 -0400393 audit_inode(NULL, dentry);
npiggin@suse.de96029c42009-04-26 20:25:55 +1000394 error = mnt_want_write_file(f);
Dave Hansen18f335a2008-02-15 14:37:38 -0800395 if (!error) {
396 error = setxattr(dentry, name, value, size, flags);
397 mnt_drop_write(f->f_path.mnt);
398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 fput(f);
400 return error;
401}
402
403/*
404 * Extended attribute GET operations
405 */
406static ssize_t
David Howells8f0cfa52008-04-29 00:59:41 -0700407getxattr(struct dentry *d, const char __user *name, void __user *value,
408 size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 ssize_t error;
411 void *kvalue = NULL;
412 char kname[XATTR_NAME_MAX + 1];
413
414 error = strncpy_from_user(kname, name, sizeof(kname));
415 if (error == 0 || error == sizeof(kname))
416 error = -ERANGE;
417 if (error < 0)
418 return error;
419
420 if (size) {
421 if (size > XATTR_SIZE_MAX)
422 size = XATTR_SIZE_MAX;
James Morrisd381d8a2005-10-30 14:59:22 -0800423 kvalue = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (!kvalue)
425 return -ENOMEM;
426 }
427
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800428 error = vfs_getxattr(d, kname, kvalue, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700429 if (error > 0) {
430 if (size && copy_to_user(value, kvalue, error))
431 error = -EFAULT;
432 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
433 /* The file system tried to returned a value bigger
434 than XATTR_SIZE_MAX bytes. Not possible. */
435 error = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800437 kfree(kvalue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return error;
439}
440
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100441SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
442 const char __user *, name, void __user *, value, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Al Viro2d8f3032008-07-22 09:59:21 -0400444 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 ssize_t error;
446
Al Viro2d8f3032008-07-22 09:59:21 -0400447 error = user_path(pathname, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (error)
449 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400450 error = getxattr(path.dentry, name, value, size);
451 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return error;
453}
454
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100455SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
456 const char __user *, name, void __user *, value, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Al Viro2d8f3032008-07-22 09:59:21 -0400458 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 ssize_t error;
460
Al Viro2d8f3032008-07-22 09:59:21 -0400461 error = user_lpath(pathname, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (error)
463 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400464 error = getxattr(path.dentry, name, value, size);
465 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return error;
467}
468
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100469SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
470 void __user *, value, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 struct file *f;
473 ssize_t error = -EBADF;
474
475 f = fget(fd);
476 if (!f)
477 return error;
Al Viro5a190ae2007-06-07 12:19:32 -0400478 audit_inode(NULL, f->f_path.dentry);
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800479 error = getxattr(f->f_path.dentry, name, value, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 fput(f);
481 return error;
482}
483
484/*
485 * Extended attribute LIST operations
486 */
487static ssize_t
488listxattr(struct dentry *d, char __user *list, size_t size)
489{
490 ssize_t error;
491 char *klist = NULL;
492
493 if (size) {
494 if (size > XATTR_LIST_MAX)
495 size = XATTR_LIST_MAX;
496 klist = kmalloc(size, GFP_KERNEL);
497 if (!klist)
498 return -ENOMEM;
499 }
500
Bill Nottingham659564c2006-10-09 16:10:48 -0400501 error = vfs_listxattr(d, klist, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700502 if (error > 0) {
503 if (size && copy_to_user(list, klist, error))
504 error = -EFAULT;
505 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
506 /* The file system tried to returned a list bigger
507 than XATTR_LIST_MAX bytes. Not possible. */
508 error = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800510 kfree(klist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return error;
512}
513
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100514SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
515 size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Al Viro2d8f3032008-07-22 09:59:21 -0400517 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 ssize_t error;
519
Al Viro2d8f3032008-07-22 09:59:21 -0400520 error = user_path(pathname, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (error)
522 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400523 error = listxattr(path.dentry, list, size);
524 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return error;
526}
527
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100528SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
529 size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Al Viro2d8f3032008-07-22 09:59:21 -0400531 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 ssize_t error;
533
Al Viro2d8f3032008-07-22 09:59:21 -0400534 error = user_lpath(pathname, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (error)
536 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400537 error = listxattr(path.dentry, list, size);
538 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return error;
540}
541
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100542SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 struct file *f;
545 ssize_t error = -EBADF;
546
547 f = fget(fd);
548 if (!f)
549 return error;
Al Viro5a190ae2007-06-07 12:19:32 -0400550 audit_inode(NULL, f->f_path.dentry);
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800551 error = listxattr(f->f_path.dentry, list, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 fput(f);
553 return error;
554}
555
556/*
557 * Extended attribute REMOVE operations
558 */
559static long
David Howells8f0cfa52008-04-29 00:59:41 -0700560removexattr(struct dentry *d, const char __user *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 int error;
563 char kname[XATTR_NAME_MAX + 1];
564
565 error = strncpy_from_user(kname, name, sizeof(kname));
566 if (error == 0 || error == sizeof(kname))
567 error = -ERANGE;
568 if (error < 0)
569 return error;
570
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800571 return vfs_removexattr(d, kname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100574SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
575 const char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Al Viro2d8f3032008-07-22 09:59:21 -0400577 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 int error;
579
Al Viro2d8f3032008-07-22 09:59:21 -0400580 error = user_path(pathname, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (error)
582 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400583 error = mnt_want_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800584 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400585 error = removexattr(path.dentry, name);
586 mnt_drop_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800587 }
Al Viro2d8f3032008-07-22 09:59:21 -0400588 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return error;
590}
591
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100592SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
593 const char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Al Viro2d8f3032008-07-22 09:59:21 -0400595 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 int error;
597
Al Viro2d8f3032008-07-22 09:59:21 -0400598 error = user_lpath(pathname, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (error)
600 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400601 error = mnt_want_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800602 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400603 error = removexattr(path.dentry, name);
604 mnt_drop_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800605 }
Al Viro2d8f3032008-07-22 09:59:21 -0400606 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return error;
608}
609
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100610SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 struct file *f;
Amy Griffis73241cc2005-11-03 16:00:25 +0000613 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 int error = -EBADF;
615
616 f = fget(fd);
617 if (!f)
618 return error;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800619 dentry = f->f_path.dentry;
Al Viro5a190ae2007-06-07 12:19:32 -0400620 audit_inode(NULL, dentry);
npiggin@suse.de96029c42009-04-26 20:25:55 +1000621 error = mnt_want_write_file(f);
Dave Hansen18f335a2008-02-15 14:37:38 -0800622 if (!error) {
623 error = removexattr(dentry, name);
624 mnt_drop_write(f->f_path.mnt);
625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 fput(f);
627 return error;
628}
629
630
631static const char *
632strcmp_prefix(const char *a, const char *a_prefix)
633{
634 while (*a_prefix && *a == *a_prefix) {
635 a++;
636 a_prefix++;
637 }
638 return *a_prefix ? NULL : a;
639}
640
641/*
642 * In order to implement different sets of xattr operations for each xattr
643 * prefix with the generic xattr API, a filesystem should create a
644 * null-terminated array of struct xattr_handler (one for each prefix) and
645 * hang a pointer to it off of the s_xattr field of the superblock.
646 *
647 * The generic_fooxattr() functions will use this list to dispatch xattr
648 * operations to the correct xattr_handler.
649 */
650#define for_each_xattr_handler(handlers, handler) \
651 for ((handler) = *(handlers)++; \
652 (handler) != NULL; \
653 (handler) = *(handlers)++)
654
655/*
656 * Find the xattr_handler with the matching prefix.
657 */
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700658static const struct xattr_handler *
659xattr_resolve_name(const struct xattr_handler **handlers, const char **name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700661 const struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 if (!*name)
664 return NULL;
665
666 for_each_xattr_handler(handlers, handler) {
667 const char *n = strcmp_prefix(*name, handler->prefix);
668 if (n) {
669 *name = n;
670 break;
671 }
672 }
673 return handler;
674}
675
676/*
677 * Find the handler for the prefix and dispatch its get() operation.
678 */
679ssize_t
680generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
681{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700682 const struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Christoph Hellwig431547b2009-11-13 09:52:56 +0000684 handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (!handler)
686 return -EOPNOTSUPP;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000687 return handler->get(dentry, name, buffer, size, handler->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690/*
691 * Combine the results of the list() operation from every xattr_handler in the
692 * list.
693 */
694ssize_t
695generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
696{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700697 const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 unsigned int size = 0;
699
700 if (!buffer) {
Christoph Hellwig431547b2009-11-13 09:52:56 +0000701 for_each_xattr_handler(handlers, handler) {
702 size += handler->list(dentry, NULL, 0, NULL, 0,
703 handler->flags);
704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 } else {
706 char *buf = buffer;
707
708 for_each_xattr_handler(handlers, handler) {
Christoph Hellwig431547b2009-11-13 09:52:56 +0000709 size = handler->list(dentry, buf, buffer_size,
710 NULL, 0, handler->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (size > buffer_size)
712 return -ERANGE;
713 buf += size;
714 buffer_size -= size;
715 }
716 size = buf - buffer;
717 }
718 return size;
719}
720
721/*
722 * Find the handler for the prefix and dispatch its set() operation.
723 */
724int
725generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
726{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700727 const struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 if (size == 0)
730 value = ""; /* empty EA, do not remove */
Christoph Hellwig431547b2009-11-13 09:52:56 +0000731 handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (!handler)
733 return -EOPNOTSUPP;
Jan Karadf7e1302011-04-20 20:30:40 +0200734 return handler->set(dentry, name, value, size, flags, handler->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
737/*
738 * Find the handler for the prefix and dispatch its set() operation to remove
739 * any associated extended attribute.
740 */
741int
742generic_removexattr(struct dentry *dentry, const char *name)
743{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700744 const struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Christoph Hellwig431547b2009-11-13 09:52:56 +0000746 handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (!handler)
748 return -EOPNOTSUPP;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000749 return handler->set(dentry, name, NULL, 0,
750 XATTR_REPLACE, handler->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
753EXPORT_SYMBOL(generic_getxattr);
754EXPORT_SYMBOL(generic_listxattr);
755EXPORT_SYMBOL(generic_setxattr);
756EXPORT_SYMBOL(generic_removexattr);