blob: 0367a5dae2b8ad1b57a4c25bcb1862266aac415a [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 Gruenbacherf1f2d872006-11-02 22:07:29 -080049 * The trusted.* namespace can only be accessed by a privileged user.
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080050 */
51 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
52 return (capable(CAP_SYS_ADMIN) ? 0 : -EPERM);
53
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -080054 /* 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.
57 */
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080058 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -080059 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
60 return -EPERM;
61 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
Satyam Sharma3bd858a2007-07-17 15:00:08 +053062 (mask & MAY_WRITE) && !is_owner_or_cap(inode))
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080063 return -EPERM;
64 }
65
Al Virof419a2e2008-07-22 00:07:17 -040066 return inode_permission(inode, mask);
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080067}
68
Christoph Hellwig5be196e2006-01-09 20:51:55 -080069int
David Howells8f0cfa52008-04-29 00:59:41 -070070vfs_setxattr(struct dentry *dentry, const char *name, const void *value,
Christoph Hellwig5be196e2006-01-09 20:51:55 -080071 size_t size, int flags)
72{
73 struct inode *inode = dentry->d_inode;
74 int error;
75
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080076 error = xattr_permission(inode, name, MAY_WRITE);
77 if (error)
78 return error;
79
Christoph Hellwig5be196e2006-01-09 20:51:55 -080080 mutex_lock(&inode->i_mutex);
81 error = security_inode_setxattr(dentry, name, value, size, flags);
82 if (error)
83 goto out;
84 error = -EOPNOTSUPP;
85 if (inode->i_op->setxattr) {
86 error = inode->i_op->setxattr(dentry, name, value, size, flags);
87 if (!error) {
88 fsnotify_xattr(dentry);
89 security_inode_post_setxattr(dentry, name, value,
90 size, flags);
91 }
92 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080093 XATTR_SECURITY_PREFIX_LEN)) {
94 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
Christoph Hellwig5be196e2006-01-09 20:51:55 -080095 error = security_inode_setsecurity(inode, suffix, value,
96 size, flags);
97 if (!error)
98 fsnotify_xattr(dentry);
99 }
100out:
101 mutex_unlock(&inode->i_mutex);
102 return error;
103}
104EXPORT_SYMBOL_GPL(vfs_setxattr);
105
106ssize_t
David P. Quigley42492592008-02-04 22:29:39 -0800107xattr_getsecurity(struct inode *inode, const char *name, void *value,
108 size_t size)
109{
110 void *buffer = NULL;
111 ssize_t len;
112
113 if (!value || !size) {
114 len = security_inode_getsecurity(inode, name, &buffer, false);
115 goto out_noalloc;
116 }
117
118 len = security_inode_getsecurity(inode, name, &buffer, true);
119 if (len < 0)
120 return len;
121 if (size < len) {
122 len = -ERANGE;
123 goto out;
124 }
125 memcpy(value, buffer, len);
126out:
127 security_release_secctx(buffer, len);
128out_noalloc:
129 return len;
130}
131EXPORT_SYMBOL_GPL(xattr_getsecurity);
132
133ssize_t
David Howells8f0cfa52008-04-29 00:59:41 -0700134vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800135{
136 struct inode *inode = dentry->d_inode;
137 int error;
138
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800139 error = xattr_permission(inode, name, MAY_READ);
140 if (error)
141 return error;
142
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800143 error = security_inode_getxattr(dentry, name);
144 if (error)
145 return error;
146
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800147 if (!strncmp(name, XATTR_SECURITY_PREFIX,
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800148 XATTR_SECURITY_PREFIX_LEN)) {
149 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
David P. Quigley42492592008-02-04 22:29:39 -0800150 int ret = xattr_getsecurity(inode, suffix, value, size);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800151 /*
152 * Only overwrite the return value if a security module
153 * is actually active.
154 */
David P. Quigley4bea5802008-02-04 22:29:40 -0800155 if (ret == -EOPNOTSUPP)
156 goto nolsm;
157 return ret;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800158 }
David P. Quigley4bea5802008-02-04 22:29:40 -0800159nolsm:
160 if (inode->i_op->getxattr)
161 error = inode->i_op->getxattr(dentry, name, value, size);
162 else
163 error = -EOPNOTSUPP;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800164
165 return error;
166}
167EXPORT_SYMBOL_GPL(vfs_getxattr);
168
Bill Nottingham659564c2006-10-09 16:10:48 -0400169ssize_t
170vfs_listxattr(struct dentry *d, char *list, size_t size)
171{
172 ssize_t error;
173
174 error = security_inode_listxattr(d);
175 if (error)
176 return error;
177 error = -EOPNOTSUPP;
Al Viroacfa4382008-12-04 10:06:33 -0500178 if (d->d_inode->i_op->listxattr) {
Bill Nottingham659564c2006-10-09 16:10:48 -0400179 error = d->d_inode->i_op->listxattr(d, list, size);
180 } else {
181 error = security_inode_listsecurity(d->d_inode, list, size);
182 if (size && error > size)
183 error = -ERANGE;
184 }
185 return error;
186}
187EXPORT_SYMBOL_GPL(vfs_listxattr);
188
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800189int
David Howells8f0cfa52008-04-29 00:59:41 -0700190vfs_removexattr(struct dentry *dentry, const char *name)
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800191{
192 struct inode *inode = dentry->d_inode;
193 int error;
194
195 if (!inode->i_op->removexattr)
196 return -EOPNOTSUPP;
197
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800198 error = xattr_permission(inode, name, MAY_WRITE);
199 if (error)
200 return error;
201
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800202 error = security_inode_removexattr(dentry, name);
203 if (error)
204 return error;
205
206 mutex_lock(&inode->i_mutex);
207 error = inode->i_op->removexattr(dentry, name);
208 mutex_unlock(&inode->i_mutex);
209
210 if (!error)
211 fsnotify_xattr(dentry);
212 return error;
213}
214EXPORT_SYMBOL_GPL(vfs_removexattr);
215
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/*
218 * Extended attribute SET operations
219 */
220static long
David Howells8f0cfa52008-04-29 00:59:41 -0700221setxattr(struct dentry *d, const char __user *name, const void __user *value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 size_t size, int flags)
223{
224 int error;
225 void *kvalue = NULL;
226 char kname[XATTR_NAME_MAX + 1];
227
228 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
229 return -EINVAL;
230
231 error = strncpy_from_user(kname, name, sizeof(kname));
232 if (error == 0 || error == sizeof(kname))
233 error = -ERANGE;
234 if (error < 0)
235 return error;
236
237 if (size) {
238 if (size > XATTR_SIZE_MAX)
239 return -E2BIG;
240 kvalue = kmalloc(size, GFP_KERNEL);
241 if (!kvalue)
242 return -ENOMEM;
243 if (copy_from_user(kvalue, value, size)) {
244 kfree(kvalue);
245 return -EFAULT;
246 }
247 }
248
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800249 error = vfs_setxattr(d, kname, kvalue, size, flags);
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800250 kfree(kvalue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return error;
252}
253
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100254SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
255 const char __user *, name, const void __user *, value,
256 size_t, size, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Al Viro2d8f3032008-07-22 09:59:21 -0400258 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 int error;
260
Al Viro2d8f3032008-07-22 09:59:21 -0400261 error = user_path(pathname, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (error)
263 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400264 error = mnt_want_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800265 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400266 error = setxattr(path.dentry, name, value, size, flags);
267 mnt_drop_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800268 }
Al Viro2d8f3032008-07-22 09:59:21 -0400269 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return error;
271}
272
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100273SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
274 const char __user *, name, const void __user *, value,
275 size_t, size, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Al Viro2d8f3032008-07-22 09:59:21 -0400277 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 int error;
279
Al Viro2d8f3032008-07-22 09:59:21 -0400280 error = user_lpath(pathname, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (error)
282 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400283 error = mnt_want_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800284 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400285 error = setxattr(path.dentry, name, value, size, flags);
286 mnt_drop_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800287 }
Al Viro2d8f3032008-07-22 09:59:21 -0400288 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return error;
290}
291
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100292SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
293 const void __user *,value, size_t, size, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 struct file *f;
Amy Griffis73241cc2005-11-03 16:00:25 +0000296 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 int error = -EBADF;
298
299 f = fget(fd);
300 if (!f)
301 return error;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800302 dentry = f->f_path.dentry;
Al Viro5a190ae2007-06-07 12:19:32 -0400303 audit_inode(NULL, dentry);
Dave Hansen18f335a2008-02-15 14:37:38 -0800304 error = mnt_want_write(f->f_path.mnt);
305 if (!error) {
306 error = setxattr(dentry, name, value, size, flags);
307 mnt_drop_write(f->f_path.mnt);
308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 fput(f);
310 return error;
311}
312
313/*
314 * Extended attribute GET operations
315 */
316static ssize_t
David Howells8f0cfa52008-04-29 00:59:41 -0700317getxattr(struct dentry *d, const char __user *name, void __user *value,
318 size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 ssize_t error;
321 void *kvalue = NULL;
322 char kname[XATTR_NAME_MAX + 1];
323
324 error = strncpy_from_user(kname, name, sizeof(kname));
325 if (error == 0 || error == sizeof(kname))
326 error = -ERANGE;
327 if (error < 0)
328 return error;
329
330 if (size) {
331 if (size > XATTR_SIZE_MAX)
332 size = XATTR_SIZE_MAX;
James Morrisd381d8a2005-10-30 14:59:22 -0800333 kvalue = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (!kvalue)
335 return -ENOMEM;
336 }
337
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800338 error = vfs_getxattr(d, kname, kvalue, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700339 if (error > 0) {
340 if (size && copy_to_user(value, kvalue, error))
341 error = -EFAULT;
342 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
343 /* The file system tried to returned a value bigger
344 than XATTR_SIZE_MAX bytes. Not possible. */
345 error = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800347 kfree(kvalue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return error;
349}
350
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100351SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
352 const char __user *, name, void __user *, value, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Al Viro2d8f3032008-07-22 09:59:21 -0400354 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 ssize_t error;
356
Al Viro2d8f3032008-07-22 09:59:21 -0400357 error = user_path(pathname, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (error)
359 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400360 error = getxattr(path.dentry, name, value, size);
361 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return error;
363}
364
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100365SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
366 const char __user *, name, void __user *, value, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Al Viro2d8f3032008-07-22 09:59:21 -0400368 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 ssize_t error;
370
Al Viro2d8f3032008-07-22 09:59:21 -0400371 error = user_lpath(pathname, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (error)
373 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400374 error = getxattr(path.dentry, name, value, size);
375 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return error;
377}
378
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100379SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
380 void __user *, value, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 struct file *f;
383 ssize_t error = -EBADF;
384
385 f = fget(fd);
386 if (!f)
387 return error;
Al Viro5a190ae2007-06-07 12:19:32 -0400388 audit_inode(NULL, f->f_path.dentry);
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800389 error = getxattr(f->f_path.dentry, name, value, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 fput(f);
391 return error;
392}
393
394/*
395 * Extended attribute LIST operations
396 */
397static ssize_t
398listxattr(struct dentry *d, char __user *list, size_t size)
399{
400 ssize_t error;
401 char *klist = NULL;
402
403 if (size) {
404 if (size > XATTR_LIST_MAX)
405 size = XATTR_LIST_MAX;
406 klist = kmalloc(size, GFP_KERNEL);
407 if (!klist)
408 return -ENOMEM;
409 }
410
Bill Nottingham659564c2006-10-09 16:10:48 -0400411 error = vfs_listxattr(d, klist, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700412 if (error > 0) {
413 if (size && copy_to_user(list, klist, error))
414 error = -EFAULT;
415 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
416 /* The file system tried to returned a list bigger
417 than XATTR_LIST_MAX bytes. Not possible. */
418 error = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800420 kfree(klist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return error;
422}
423
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100424SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
425 size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Al Viro2d8f3032008-07-22 09:59:21 -0400427 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 ssize_t error;
429
Al Viro2d8f3032008-07-22 09:59:21 -0400430 error = user_path(pathname, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (error)
432 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400433 error = listxattr(path.dentry, list, size);
434 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return error;
436}
437
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100438SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
439 size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Al Viro2d8f3032008-07-22 09:59:21 -0400441 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 ssize_t error;
443
Al Viro2d8f3032008-07-22 09:59:21 -0400444 error = user_lpath(pathname, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (error)
446 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400447 error = listxattr(path.dentry, list, size);
448 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return error;
450}
451
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100452SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 struct file *f;
455 ssize_t error = -EBADF;
456
457 f = fget(fd);
458 if (!f)
459 return error;
Al Viro5a190ae2007-06-07 12:19:32 -0400460 audit_inode(NULL, f->f_path.dentry);
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800461 error = listxattr(f->f_path.dentry, list, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 fput(f);
463 return error;
464}
465
466/*
467 * Extended attribute REMOVE operations
468 */
469static long
David Howells8f0cfa52008-04-29 00:59:41 -0700470removexattr(struct dentry *d, const char __user *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 int error;
473 char kname[XATTR_NAME_MAX + 1];
474
475 error = strncpy_from_user(kname, name, sizeof(kname));
476 if (error == 0 || error == sizeof(kname))
477 error = -ERANGE;
478 if (error < 0)
479 return error;
480
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800481 return vfs_removexattr(d, kname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100484SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
485 const char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Al Viro2d8f3032008-07-22 09:59:21 -0400487 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 int error;
489
Al Viro2d8f3032008-07-22 09:59:21 -0400490 error = user_path(pathname, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (error)
492 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400493 error = mnt_want_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800494 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400495 error = removexattr(path.dentry, name);
496 mnt_drop_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800497 }
Al Viro2d8f3032008-07-22 09:59:21 -0400498 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return error;
500}
501
502asmlinkage long
Al Viro2d8f3032008-07-22 09:59:21 -0400503sys_lremovexattr(const char __user *pathname, const char __user *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Al Viro2d8f3032008-07-22 09:59:21 -0400505 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 int error;
507
Al Viro2d8f3032008-07-22 09:59:21 -0400508 error = user_lpath(pathname, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (error)
510 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400511 error = mnt_want_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800512 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400513 error = removexattr(path.dentry, name);
514 mnt_drop_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800515 }
Al Viro2d8f3032008-07-22 09:59:21 -0400516 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return error;
518}
519
520asmlinkage long
David Howells8f0cfa52008-04-29 00:59:41 -0700521sys_fremovexattr(int fd, const char __user *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 struct file *f;
Amy Griffis73241cc2005-11-03 16:00:25 +0000524 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 int error = -EBADF;
526
527 f = fget(fd);
528 if (!f)
529 return error;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800530 dentry = f->f_path.dentry;
Al Viro5a190ae2007-06-07 12:19:32 -0400531 audit_inode(NULL, dentry);
Dave Hansen18f335a2008-02-15 14:37:38 -0800532 error = mnt_want_write(f->f_path.mnt);
533 if (!error) {
534 error = removexattr(dentry, name);
535 mnt_drop_write(f->f_path.mnt);
536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 fput(f);
538 return error;
539}
540
541
542static const char *
543strcmp_prefix(const char *a, const char *a_prefix)
544{
545 while (*a_prefix && *a == *a_prefix) {
546 a++;
547 a_prefix++;
548 }
549 return *a_prefix ? NULL : a;
550}
551
552/*
553 * In order to implement different sets of xattr operations for each xattr
554 * prefix with the generic xattr API, a filesystem should create a
555 * null-terminated array of struct xattr_handler (one for each prefix) and
556 * hang a pointer to it off of the s_xattr field of the superblock.
557 *
558 * The generic_fooxattr() functions will use this list to dispatch xattr
559 * operations to the correct xattr_handler.
560 */
561#define for_each_xattr_handler(handlers, handler) \
562 for ((handler) = *(handlers)++; \
563 (handler) != NULL; \
564 (handler) = *(handlers)++)
565
566/*
567 * Find the xattr_handler with the matching prefix.
568 */
569static struct xattr_handler *
570xattr_resolve_name(struct xattr_handler **handlers, const char **name)
571{
572 struct xattr_handler *handler;
573
574 if (!*name)
575 return NULL;
576
577 for_each_xattr_handler(handlers, handler) {
578 const char *n = strcmp_prefix(*name, handler->prefix);
579 if (n) {
580 *name = n;
581 break;
582 }
583 }
584 return handler;
585}
586
587/*
588 * Find the handler for the prefix and dispatch its get() operation.
589 */
590ssize_t
591generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
592{
593 struct xattr_handler *handler;
594 struct inode *inode = dentry->d_inode;
595
596 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
597 if (!handler)
598 return -EOPNOTSUPP;
599 return handler->get(inode, name, buffer, size);
600}
601
602/*
603 * Combine the results of the list() operation from every xattr_handler in the
604 * list.
605 */
606ssize_t
607generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
608{
609 struct inode *inode = dentry->d_inode;
610 struct xattr_handler *handler, **handlers = inode->i_sb->s_xattr;
611 unsigned int size = 0;
612
613 if (!buffer) {
614 for_each_xattr_handler(handlers, handler)
615 size += handler->list(inode, NULL, 0, NULL, 0);
616 } else {
617 char *buf = buffer;
618
619 for_each_xattr_handler(handlers, handler) {
620 size = handler->list(inode, buf, buffer_size, NULL, 0);
621 if (size > buffer_size)
622 return -ERANGE;
623 buf += size;
624 buffer_size -= size;
625 }
626 size = buf - buffer;
627 }
628 return size;
629}
630
631/*
632 * Find the handler for the prefix and dispatch its set() operation.
633 */
634int
635generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
636{
637 struct xattr_handler *handler;
638 struct inode *inode = dentry->d_inode;
639
640 if (size == 0)
641 value = ""; /* empty EA, do not remove */
642 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
643 if (!handler)
644 return -EOPNOTSUPP;
645 return handler->set(inode, name, value, size, flags);
646}
647
648/*
649 * Find the handler for the prefix and dispatch its set() operation to remove
650 * any associated extended attribute.
651 */
652int
653generic_removexattr(struct dentry *dentry, const char *name)
654{
655 struct xattr_handler *handler;
656 struct inode *inode = dentry->d_inode;
657
658 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
659 if (!handler)
660 return -EOPNOTSUPP;
661 return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
662}
663
664EXPORT_SYMBOL(generic_getxattr);
665EXPORT_SYMBOL(generic_listxattr);
666EXPORT_SYMBOL(generic_setxattr);
667EXPORT_SYMBOL(generic_removexattr);