Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/file.h> |
| 13 | #include <linux/xattr.h> |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 14 | #include <linux/mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/namei.h> |
| 16 | #include <linux/security.h> |
| 17 | #include <linux/syscalls.h> |
| 18 | #include <linux/module.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 19 | #include <linux/fsnotify.h> |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 20 | #include <linux/audit.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/uaccess.h> |
| 22 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 23 | |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 24 | /* |
| 25 | * Check permissions for extended attribute access. This is a bit complicated |
| 26 | * because different namespaces have very different rules. |
| 27 | */ |
| 28 | static int |
| 29 | xattr_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.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 36 | 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 Gruenbacher | f1f2d87 | 2006-11-02 22:07:29 -0800 | [diff] [blame] | 49 | * The trusted.* namespace can only be accessed by a privileged user. |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 50 | */ |
| 51 | if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) |
| 52 | return (capable(CAP_SYS_ADMIN) ? 0 : -EPERM); |
| 53 | |
Andreas Gruenbacher | f1f2d87 | 2006-11-02 22:07:29 -0800 | [diff] [blame] | 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. |
| 57 | */ |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 58 | if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) { |
Andreas Gruenbacher | f1f2d87 | 2006-11-02 22:07:29 -0800 | [diff] [blame] | 59 | 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 Sharma | 3bd858a | 2007-07-17 15:00:08 +0530 | [diff] [blame] | 62 | (mask & MAY_WRITE) && !is_owner_or_cap(inode)) |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 63 | return -EPERM; |
| 64 | } |
| 65 | |
Al Viro | f419a2e | 2008-07-22 00:07:17 -0400 | [diff] [blame] | 66 | return inode_permission(inode, mask); |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 69 | int |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 70 | vfs_setxattr(struct dentry *dentry, const char *name, const void *value, |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 71 | size_t size, int flags) |
| 72 | { |
| 73 | struct inode *inode = dentry->d_inode; |
| 74 | int error; |
| 75 | |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 76 | error = xattr_permission(inode, name, MAY_WRITE); |
| 77 | if (error) |
| 78 | return error; |
| 79 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 80 | 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.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 93 | XATTR_SECURITY_PREFIX_LEN)) { |
| 94 | const char *suffix = name + XATTR_SECURITY_PREFIX_LEN; |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 95 | error = security_inode_setsecurity(inode, suffix, value, |
| 96 | size, flags); |
| 97 | if (!error) |
| 98 | fsnotify_xattr(dentry); |
| 99 | } |
| 100 | out: |
| 101 | mutex_unlock(&inode->i_mutex); |
| 102 | return error; |
| 103 | } |
| 104 | EXPORT_SYMBOL_GPL(vfs_setxattr); |
| 105 | |
| 106 | ssize_t |
David P. Quigley | 4249259 | 2008-02-04 22:29:39 -0800 | [diff] [blame] | 107 | xattr_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); |
| 126 | out: |
| 127 | security_release_secctx(buffer, len); |
| 128 | out_noalloc: |
| 129 | return len; |
| 130 | } |
| 131 | EXPORT_SYMBOL_GPL(xattr_getsecurity); |
| 132 | |
| 133 | ssize_t |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 134 | vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size) |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 135 | { |
| 136 | struct inode *inode = dentry->d_inode; |
| 137 | int error; |
| 138 | |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 139 | error = xattr_permission(inode, name, MAY_READ); |
| 140 | if (error) |
| 141 | return error; |
| 142 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 143 | error = security_inode_getxattr(dentry, name); |
| 144 | if (error) |
| 145 | return error; |
| 146 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 147 | if (!strncmp(name, XATTR_SECURITY_PREFIX, |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 148 | XATTR_SECURITY_PREFIX_LEN)) { |
| 149 | const char *suffix = name + XATTR_SECURITY_PREFIX_LEN; |
David P. Quigley | 4249259 | 2008-02-04 22:29:39 -0800 | [diff] [blame] | 150 | int ret = xattr_getsecurity(inode, suffix, value, size); |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 151 | /* |
| 152 | * Only overwrite the return value if a security module |
| 153 | * is actually active. |
| 154 | */ |
David P. Quigley | 4bea580 | 2008-02-04 22:29:40 -0800 | [diff] [blame] | 155 | if (ret == -EOPNOTSUPP) |
| 156 | goto nolsm; |
| 157 | return ret; |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 158 | } |
David P. Quigley | 4bea580 | 2008-02-04 22:29:40 -0800 | [diff] [blame] | 159 | nolsm: |
| 160 | if (inode->i_op->getxattr) |
| 161 | error = inode->i_op->getxattr(dentry, name, value, size); |
| 162 | else |
| 163 | error = -EOPNOTSUPP; |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 164 | |
| 165 | return error; |
| 166 | } |
| 167 | EXPORT_SYMBOL_GPL(vfs_getxattr); |
| 168 | |
Bill Nottingham | 659564c | 2006-10-09 16:10:48 -0400 | [diff] [blame] | 169 | ssize_t |
| 170 | vfs_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 Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 178 | if (d->d_inode->i_op->listxattr) { |
Bill Nottingham | 659564c | 2006-10-09 16:10:48 -0400 | [diff] [blame] | 179 | 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 | } |
| 187 | EXPORT_SYMBOL_GPL(vfs_listxattr); |
| 188 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 189 | int |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 190 | vfs_removexattr(struct dentry *dentry, const char *name) |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 191 | { |
| 192 | struct inode *inode = dentry->d_inode; |
| 193 | int error; |
| 194 | |
| 195 | if (!inode->i_op->removexattr) |
| 196 | return -EOPNOTSUPP; |
| 197 | |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 198 | error = xattr_permission(inode, name, MAY_WRITE); |
| 199 | if (error) |
| 200 | return error; |
| 201 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 202 | 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 | } |
| 214 | EXPORT_SYMBOL_GPL(vfs_removexattr); |
| 215 | |
| 216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | /* |
| 218 | * Extended attribute SET operations |
| 219 | */ |
| 220 | static long |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 221 | setxattr(struct dentry *d, const char __user *name, const void __user *value, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | 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; |
Li Zefan | 3939fcd | 2009-04-08 15:06:12 +0800 | [diff] [blame] | 240 | kvalue = memdup_user(value, size); |
| 241 | if (IS_ERR(kvalue)) |
| 242 | return PTR_ERR(kvalue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 245 | error = vfs_setxattr(d, kname, kvalue, size, flags); |
Jesper Juhl | f99d49a | 2005-11-07 01:01:34 -0800 | [diff] [blame] | 246 | kfree(kvalue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | return error; |
| 248 | } |
| 249 | |
Heiko Carstens | 64fd1de | 2009-01-14 14:14:14 +0100 | [diff] [blame] | 250 | SYSCALL_DEFINE5(setxattr, const char __user *, pathname, |
| 251 | const char __user *, name, const void __user *, value, |
| 252 | size_t, size, int, flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 254 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | int error; |
| 256 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 257 | error = user_path(pathname, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | if (error) |
| 259 | return error; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 260 | error = mnt_want_write(path.mnt); |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 261 | if (!error) { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 262 | error = setxattr(path.dentry, name, value, size, flags); |
| 263 | mnt_drop_write(path.mnt); |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 264 | } |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 265 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | return error; |
| 267 | } |
| 268 | |
Heiko Carstens | 64fd1de | 2009-01-14 14:14:14 +0100 | [diff] [blame] | 269 | SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname, |
| 270 | const char __user *, name, const void __user *, value, |
| 271 | size_t, size, int, flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 273 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | int error; |
| 275 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 276 | error = user_lpath(pathname, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | if (error) |
| 278 | return error; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 279 | error = mnt_want_write(path.mnt); |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 280 | if (!error) { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 281 | error = setxattr(path.dentry, name, value, size, flags); |
| 282 | mnt_drop_write(path.mnt); |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 283 | } |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 284 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | return error; |
| 286 | } |
| 287 | |
Heiko Carstens | 64fd1de | 2009-01-14 14:14:14 +0100 | [diff] [blame] | 288 | SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name, |
| 289 | const void __user *,value, size_t, size, int, flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { |
| 291 | struct file *f; |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 292 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | int error = -EBADF; |
| 294 | |
| 295 | f = fget(fd); |
| 296 | if (!f) |
| 297 | return error; |
Josef "Jeff" Sipek | 0f7fc9e4 | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 298 | dentry = f->f_path.dentry; |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 299 | audit_inode(NULL, dentry); |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 300 | error = mnt_want_write(f->f_path.mnt); |
| 301 | if (!error) { |
| 302 | error = setxattr(dentry, name, value, size, flags); |
| 303 | mnt_drop_write(f->f_path.mnt); |
| 304 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | fput(f); |
| 306 | return error; |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Extended attribute GET operations |
| 311 | */ |
| 312 | static ssize_t |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 313 | getxattr(struct dentry *d, const char __user *name, void __user *value, |
| 314 | size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
| 316 | ssize_t error; |
| 317 | void *kvalue = NULL; |
| 318 | char kname[XATTR_NAME_MAX + 1]; |
| 319 | |
| 320 | error = strncpy_from_user(kname, name, sizeof(kname)); |
| 321 | if (error == 0 || error == sizeof(kname)) |
| 322 | error = -ERANGE; |
| 323 | if (error < 0) |
| 324 | return error; |
| 325 | |
| 326 | if (size) { |
| 327 | if (size > XATTR_SIZE_MAX) |
| 328 | size = XATTR_SIZE_MAX; |
James Morris | d381d8a | 2005-10-30 14:59:22 -0800 | [diff] [blame] | 329 | kvalue = kzalloc(size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | if (!kvalue) |
| 331 | return -ENOMEM; |
| 332 | } |
| 333 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 334 | error = vfs_getxattr(d, kname, kvalue, size); |
Stephen Smalley | f549d6c | 2005-09-03 15:55:18 -0700 | [diff] [blame] | 335 | if (error > 0) { |
| 336 | if (size && copy_to_user(value, kvalue, error)) |
| 337 | error = -EFAULT; |
| 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. */ |
| 341 | error = -E2BIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | } |
Jesper Juhl | f99d49a | 2005-11-07 01:01:34 -0800 | [diff] [blame] | 343 | kfree(kvalue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | return error; |
| 345 | } |
| 346 | |
Heiko Carstens | 64fd1de | 2009-01-14 14:14:14 +0100 | [diff] [blame] | 347 | SYSCALL_DEFINE4(getxattr, const char __user *, pathname, |
| 348 | const char __user *, name, void __user *, value, size_t, size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 350 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | ssize_t error; |
| 352 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 353 | error = user_path(pathname, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | if (error) |
| 355 | return error; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 356 | error = getxattr(path.dentry, name, value, size); |
| 357 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | return error; |
| 359 | } |
| 360 | |
Heiko Carstens | 64fd1de | 2009-01-14 14:14:14 +0100 | [diff] [blame] | 361 | SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname, |
| 362 | const char __user *, name, void __user *, value, size_t, size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 364 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | ssize_t error; |
| 366 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 367 | error = user_lpath(pathname, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | if (error) |
| 369 | return error; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 370 | error = getxattr(path.dentry, name, value, size); |
| 371 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | return error; |
| 373 | } |
| 374 | |
Heiko Carstens | 64fd1de | 2009-01-14 14:14:14 +0100 | [diff] [blame] | 375 | SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name, |
| 376 | void __user *, value, size_t, size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | { |
| 378 | struct file *f; |
| 379 | ssize_t error = -EBADF; |
| 380 | |
| 381 | f = fget(fd); |
| 382 | if (!f) |
| 383 | return error; |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 384 | audit_inode(NULL, f->f_path.dentry); |
Josef "Jeff" Sipek | 0f7fc9e4 | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 385 | error = getxattr(f->f_path.dentry, name, value, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | fput(f); |
| 387 | return error; |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * Extended attribute LIST operations |
| 392 | */ |
| 393 | static ssize_t |
| 394 | listxattr(struct dentry *d, char __user *list, size_t size) |
| 395 | { |
| 396 | ssize_t error; |
| 397 | char *klist = NULL; |
| 398 | |
| 399 | if (size) { |
| 400 | if (size > XATTR_LIST_MAX) |
| 401 | size = XATTR_LIST_MAX; |
| 402 | klist = kmalloc(size, GFP_KERNEL); |
| 403 | if (!klist) |
| 404 | return -ENOMEM; |
| 405 | } |
| 406 | |
Bill Nottingham | 659564c | 2006-10-09 16:10:48 -0400 | [diff] [blame] | 407 | error = vfs_listxattr(d, klist, size); |
Stephen Smalley | f549d6c | 2005-09-03 15:55:18 -0700 | [diff] [blame] | 408 | if (error > 0) { |
| 409 | if (size && copy_to_user(list, klist, error)) |
| 410 | error = -EFAULT; |
| 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. */ |
| 414 | error = -E2BIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
Jesper Juhl | f99d49a | 2005-11-07 01:01:34 -0800 | [diff] [blame] | 416 | kfree(klist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | return error; |
| 418 | } |
| 419 | |
Heiko Carstens | 64fd1de | 2009-01-14 14:14:14 +0100 | [diff] [blame] | 420 | SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list, |
| 421 | size_t, size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 423 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | ssize_t error; |
| 425 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 426 | error = user_path(pathname, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | if (error) |
| 428 | return error; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 429 | error = listxattr(path.dentry, list, size); |
| 430 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | return error; |
| 432 | } |
| 433 | |
Heiko Carstens | 64fd1de | 2009-01-14 14:14:14 +0100 | [diff] [blame] | 434 | SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list, |
| 435 | size_t, size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 437 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | ssize_t error; |
| 439 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 440 | error = user_lpath(pathname, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | if (error) |
| 442 | return error; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 443 | error = listxattr(path.dentry, list, size); |
| 444 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | return error; |
| 446 | } |
| 447 | |
Heiko Carstens | 64fd1de | 2009-01-14 14:14:14 +0100 | [diff] [blame] | 448 | SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | { |
| 450 | struct file *f; |
| 451 | ssize_t error = -EBADF; |
| 452 | |
| 453 | f = fget(fd); |
| 454 | if (!f) |
| 455 | return error; |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 456 | audit_inode(NULL, f->f_path.dentry); |
Josef "Jeff" Sipek | 0f7fc9e4 | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 457 | error = listxattr(f->f_path.dentry, list, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | fput(f); |
| 459 | return error; |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * Extended attribute REMOVE operations |
| 464 | */ |
| 465 | static long |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 466 | removexattr(struct dentry *d, const char __user *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
| 468 | int error; |
| 469 | char kname[XATTR_NAME_MAX + 1]; |
| 470 | |
| 471 | error = strncpy_from_user(kname, name, sizeof(kname)); |
| 472 | if (error == 0 || error == sizeof(kname)) |
| 473 | error = -ERANGE; |
| 474 | if (error < 0) |
| 475 | return error; |
| 476 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 477 | return vfs_removexattr(d, kname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Heiko Carstens | 64fd1de | 2009-01-14 14:14:14 +0100 | [diff] [blame] | 480 | SYSCALL_DEFINE2(removexattr, const char __user *, pathname, |
| 481 | const char __user *, name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 483 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | int error; |
| 485 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 486 | error = user_path(pathname, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | if (error) |
| 488 | return error; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 489 | error = mnt_want_write(path.mnt); |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 490 | if (!error) { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 491 | error = removexattr(path.dentry, name); |
| 492 | mnt_drop_write(path.mnt); |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 493 | } |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 494 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | return error; |
| 496 | } |
| 497 | |
Heiko Carstens | 6a6160a | 2009-01-14 14:14:15 +0100 | [diff] [blame] | 498 | SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname, |
| 499 | const char __user *, name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 501 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | int error; |
| 503 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 504 | error = user_lpath(pathname, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | if (error) |
| 506 | return error; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 507 | error = mnt_want_write(path.mnt); |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 508 | if (!error) { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 509 | error = removexattr(path.dentry, name); |
| 510 | mnt_drop_write(path.mnt); |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 511 | } |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 512 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | return error; |
| 514 | } |
| 515 | |
Heiko Carstens | 6a6160a | 2009-01-14 14:14:15 +0100 | [diff] [blame] | 516 | SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | { |
| 518 | struct file *f; |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 519 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | int error = -EBADF; |
| 521 | |
| 522 | f = fget(fd); |
| 523 | if (!f) |
| 524 | return error; |
Josef "Jeff" Sipek | 0f7fc9e4 | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 525 | dentry = f->f_path.dentry; |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 526 | audit_inode(NULL, dentry); |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 527 | error = mnt_want_write(f->f_path.mnt); |
| 528 | if (!error) { |
| 529 | error = removexattr(dentry, name); |
| 530 | mnt_drop_write(f->f_path.mnt); |
| 531 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | fput(f); |
| 533 | return error; |
| 534 | } |
| 535 | |
| 536 | |
| 537 | static const char * |
| 538 | strcmp_prefix(const char *a, const char *a_prefix) |
| 539 | { |
| 540 | while (*a_prefix && *a == *a_prefix) { |
| 541 | a++; |
| 542 | a_prefix++; |
| 543 | } |
| 544 | return *a_prefix ? NULL : a; |
| 545 | } |
| 546 | |
| 547 | /* |
| 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. |
| 552 | * |
| 553 | * The generic_fooxattr() functions will use this list to dispatch xattr |
| 554 | * operations to the correct xattr_handler. |
| 555 | */ |
| 556 | #define for_each_xattr_handler(handlers, handler) \ |
| 557 | for ((handler) = *(handlers)++; \ |
| 558 | (handler) != NULL; \ |
| 559 | (handler) = *(handlers)++) |
| 560 | |
| 561 | /* |
| 562 | * Find the xattr_handler with the matching prefix. |
| 563 | */ |
| 564 | static struct xattr_handler * |
| 565 | xattr_resolve_name(struct xattr_handler **handlers, const char **name) |
| 566 | { |
| 567 | struct xattr_handler *handler; |
| 568 | |
| 569 | if (!*name) |
| 570 | return NULL; |
| 571 | |
| 572 | for_each_xattr_handler(handlers, handler) { |
| 573 | const char *n = strcmp_prefix(*name, handler->prefix); |
| 574 | if (n) { |
| 575 | *name = n; |
| 576 | break; |
| 577 | } |
| 578 | } |
| 579 | return handler; |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Find the handler for the prefix and dispatch its get() operation. |
| 584 | */ |
| 585 | ssize_t |
| 586 | generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size) |
| 587 | { |
| 588 | struct xattr_handler *handler; |
| 589 | struct inode *inode = dentry->d_inode; |
| 590 | |
| 591 | handler = xattr_resolve_name(inode->i_sb->s_xattr, &name); |
| 592 | if (!handler) |
| 593 | return -EOPNOTSUPP; |
| 594 | return handler->get(inode, name, buffer, size); |
| 595 | } |
| 596 | |
| 597 | /* |
| 598 | * Combine the results of the list() operation from every xattr_handler in the |
| 599 | * list. |
| 600 | */ |
| 601 | ssize_t |
| 602 | generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) |
| 603 | { |
| 604 | struct inode *inode = dentry->d_inode; |
| 605 | struct xattr_handler *handler, **handlers = inode->i_sb->s_xattr; |
| 606 | unsigned int size = 0; |
| 607 | |
| 608 | if (!buffer) { |
| 609 | for_each_xattr_handler(handlers, handler) |
| 610 | size += handler->list(inode, NULL, 0, NULL, 0); |
| 611 | } else { |
| 612 | char *buf = buffer; |
| 613 | |
| 614 | for_each_xattr_handler(handlers, handler) { |
| 615 | size = handler->list(inode, buf, buffer_size, NULL, 0); |
| 616 | if (size > buffer_size) |
| 617 | return -ERANGE; |
| 618 | buf += size; |
| 619 | buffer_size -= size; |
| 620 | } |
| 621 | size = buf - buffer; |
| 622 | } |
| 623 | return size; |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | * Find the handler for the prefix and dispatch its set() operation. |
| 628 | */ |
| 629 | int |
| 630 | generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags) |
| 631 | { |
| 632 | struct xattr_handler *handler; |
| 633 | struct inode *inode = dentry->d_inode; |
| 634 | |
| 635 | if (size == 0) |
| 636 | value = ""; /* empty EA, do not remove */ |
| 637 | handler = xattr_resolve_name(inode->i_sb->s_xattr, &name); |
| 638 | if (!handler) |
| 639 | return -EOPNOTSUPP; |
| 640 | return handler->set(inode, name, value, size, flags); |
| 641 | } |
| 642 | |
| 643 | /* |
| 644 | * Find the handler for the prefix and dispatch its set() operation to remove |
| 645 | * any associated extended attribute. |
| 646 | */ |
| 647 | int |
| 648 | generic_removexattr(struct dentry *dentry, const char *name) |
| 649 | { |
| 650 | struct xattr_handler *handler; |
| 651 | struct inode *inode = dentry->d_inode; |
| 652 | |
| 653 | handler = xattr_resolve_name(inode->i_sb->s_xattr, &name); |
| 654 | if (!handler) |
| 655 | return -EOPNOTSUPP; |
| 656 | return handler->set(inode, name, NULL, 0, XATTR_REPLACE); |
| 657 | } |
| 658 | |
| 659 | EXPORT_SYMBOL(generic_getxattr); |
| 660 | EXPORT_SYMBOL(generic_listxattr); |
| 661 | EXPORT_SYMBOL(generic_setxattr); |
| 662 | EXPORT_SYMBOL(generic_removexattr); |