Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2008 Christoph Hellwig. |
| 4 | * Portions Copyright (C) 2000-2008 Silicon Graphics, Inc. |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include "xfs.h" |
Darrick J. Wong | 5467b34 | 2019-06-28 19:25:35 -0700 | [diff] [blame] | 8 | #include "xfs_shared.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 9 | #include "xfs_format.h" |
Dave Chinner | 6943283 | 2013-08-12 20:49:23 +1000 | [diff] [blame] | 10 | #include "xfs_log_format.h" |
Dave Chinner | 5706278 | 2013-10-15 09:17:51 +1100 | [diff] [blame] | 11 | #include "xfs_da_format.h" |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 12 | #include "xfs_inode.h" |
| 13 | #include "xfs_attr.h" |
Darrick J. Wong | 5f213dd | 2019-11-06 17:19:33 -0800 | [diff] [blame] | 14 | #include "xfs_acl.h" |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 15 | #include "xfs_da_format.h" |
| 16 | #include "xfs_da_btree.h" |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 17 | |
| 18 | #include <linux/posix_acl_xattr.h> |
| 19 | #include <linux/xattr.h> |
| 20 | |
| 21 | |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 22 | static int |
Al Viro | b296821 | 2016-04-10 20:48:24 -0400 | [diff] [blame] | 23 | xfs_xattr_get(const struct xattr_handler *handler, struct dentry *unused, |
| 24 | struct inode *inode, const char *name, void *value, size_t size) |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 25 | { |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 26 | struct xfs_da_args args = { |
| 27 | .dp = XFS_I(inode), |
| 28 | .flags = handler->flags, |
| 29 | .name = name, |
| 30 | .namelen = strlen(name), |
| 31 | .value = value, |
| 32 | .valuelen = size, |
| 33 | }; |
| 34 | int error; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 35 | |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 36 | error = xfs_attr_get(&args); |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 37 | if (error) |
| 38 | return error; |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 39 | return args.valuelen; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 40 | } |
| 41 | |
Andreas Gruenbacher | 47e1bf6 | 2015-11-03 12:56:17 +1100 | [diff] [blame] | 42 | void |
| 43 | xfs_forget_acl( |
| 44 | struct inode *inode, |
| 45 | const char *name, |
| 46 | int xflags) |
| 47 | { |
| 48 | /* |
| 49 | * Invalidate any cached ACLs if the user has bypassed the ACL |
| 50 | * interface. We don't validate the content whatsoever so it is caller |
| 51 | * responsibility to provide data in valid format and ensure i_mode is |
| 52 | * consistent. |
| 53 | */ |
| 54 | if (xflags & ATTR_ROOT) { |
| 55 | #ifdef CONFIG_XFS_POSIX_ACL |
| 56 | if (!strcmp(name, SGI_ACL_FILE)) |
| 57 | forget_cached_acl(inode, ACL_TYPE_ACCESS); |
| 58 | else if (!strcmp(name, SGI_ACL_DEFAULT)) |
| 59 | forget_cached_acl(inode, ACL_TYPE_DEFAULT); |
| 60 | #endif |
| 61 | } |
| 62 | } |
| 63 | |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 64 | static int |
Al Viro | 5930122 | 2016-05-27 10:19:30 -0400 | [diff] [blame] | 65 | xfs_xattr_set(const struct xattr_handler *handler, struct dentry *unused, |
| 66 | struct inode *inode, const char *name, const void *value, |
| 67 | size_t size, int flags) |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 68 | { |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 69 | struct xfs_da_args args = { |
| 70 | .dp = XFS_I(inode), |
| 71 | .flags = handler->flags, |
| 72 | .name = name, |
| 73 | .namelen = strlen(name), |
| 74 | .value = (void *)value, |
| 75 | .valuelen = size, |
| 76 | }; |
Brian Foster | 67d8e04 | 2015-11-03 12:40:59 +1100 | [diff] [blame] | 77 | int error; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 78 | |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 79 | /* Convert Linux syscall to XFS internal ATTR flags */ |
| 80 | if (flags & XATTR_CREATE) |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 81 | args.flags |= ATTR_CREATE; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 82 | if (flags & XATTR_REPLACE) |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 83 | args.flags |= ATTR_REPLACE; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 84 | |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 85 | error = xfs_attr_set(&args); |
Andreas Gruenbacher | 47e1bf6 | 2015-11-03 12:56:17 +1100 | [diff] [blame] | 86 | if (!error) |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 87 | xfs_forget_acl(inode, name, args.flags); |
Brian Foster | 67d8e04 | 2015-11-03 12:40:59 +1100 | [diff] [blame] | 88 | return error; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 89 | } |
| 90 | |
Stephen Hemminger | 46e5876 | 2010-05-13 17:53:20 -0700 | [diff] [blame] | 91 | static const struct xattr_handler xfs_xattr_user_handler = { |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 92 | .prefix = XATTR_USER_PREFIX, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 93 | .flags = 0, /* no flags implies user namespace */ |
| 94 | .get = xfs_xattr_get, |
| 95 | .set = xfs_xattr_set, |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 96 | }; |
| 97 | |
Stephen Hemminger | 46e5876 | 2010-05-13 17:53:20 -0700 | [diff] [blame] | 98 | static const struct xattr_handler xfs_xattr_trusted_handler = { |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 99 | .prefix = XATTR_TRUSTED_PREFIX, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 100 | .flags = ATTR_ROOT, |
| 101 | .get = xfs_xattr_get, |
| 102 | .set = xfs_xattr_set, |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 103 | }; |
| 104 | |
Stephen Hemminger | 46e5876 | 2010-05-13 17:53:20 -0700 | [diff] [blame] | 105 | static const struct xattr_handler xfs_xattr_security_handler = { |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 106 | .prefix = XATTR_SECURITY_PREFIX, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 107 | .flags = ATTR_SECURE, |
| 108 | .get = xfs_xattr_get, |
| 109 | .set = xfs_xattr_set, |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 110 | }; |
| 111 | |
Stephen Hemminger | 46e5876 | 2010-05-13 17:53:20 -0700 | [diff] [blame] | 112 | const struct xattr_handler *xfs_xattr_handlers[] = { |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 113 | &xfs_xattr_user_handler, |
| 114 | &xfs_xattr_trusted_handler, |
| 115 | &xfs_xattr_security_handler, |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 116 | #ifdef CONFIG_XFS_POSIX_ACL |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 117 | &posix_acl_access_xattr_handler, |
| 118 | &posix_acl_default_xattr_handler, |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 119 | #endif |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 120 | NULL |
| 121 | }; |
| 122 | |
Eric Sandeen | f7a136a | 2016-12-05 12:32:14 +1100 | [diff] [blame] | 123 | static void |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 124 | __xfs_xattr_put_listent( |
| 125 | struct xfs_attr_list_context *context, |
| 126 | char *prefix, |
| 127 | int prefix_len, |
| 128 | unsigned char *name, |
| 129 | int namelen) |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 130 | { |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 131 | char *offset; |
| 132 | int arraytop; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 133 | |
Darrick J. Wong | 3b50086 | 2019-02-13 11:15:17 -0800 | [diff] [blame] | 134 | if (context->count < 0 || context->seen_enough) |
| 135 | return; |
| 136 | |
Christoph Hellwig | a9c8c69 | 2020-02-26 17:30:37 -0800 | [diff] [blame] | 137 | if (!context->buffer) |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 138 | goto compute_size; |
| 139 | |
| 140 | arraytop = context->count + prefix_len + namelen + 1; |
| 141 | if (arraytop > context->firstu) { |
| 142 | context->count = -1; /* insufficient space */ |
Artem Savkov | 791cc43 | 2016-09-14 07:40:35 +1000 | [diff] [blame] | 143 | context->seen_enough = 1; |
Eric Sandeen | f7a136a | 2016-12-05 12:32:14 +1100 | [diff] [blame] | 144 | return; |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 145 | } |
Christoph Hellwig | a9c8c69 | 2020-02-26 17:30:37 -0800 | [diff] [blame] | 146 | offset = context->buffer + context->count; |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 147 | strncpy(offset, prefix, prefix_len); |
| 148 | offset += prefix_len; |
| 149 | strncpy(offset, (char *)name, namelen); /* real name */ |
| 150 | offset += namelen; |
| 151 | *offset = '\0'; |
| 152 | |
| 153 | compute_size: |
| 154 | context->count += prefix_len + namelen + 1; |
Eric Sandeen | f7a136a | 2016-12-05 12:32:14 +1100 | [diff] [blame] | 155 | return; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 156 | } |
| 157 | |
Eric Sandeen | f7a136a | 2016-12-05 12:32:14 +1100 | [diff] [blame] | 158 | static void |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 159 | xfs_xattr_put_listent( |
| 160 | struct xfs_attr_list_context *context, |
| 161 | int flags, |
| 162 | unsigned char *name, |
| 163 | int namelen, |
Eric Sandeen | e5bd12b | 2016-04-06 07:57:32 +1000 | [diff] [blame] | 164 | int valuelen) |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 165 | { |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 166 | char *prefix; |
| 167 | int prefix_len; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 168 | |
| 169 | ASSERT(context->count >= 0); |
| 170 | |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 171 | if (flags & XFS_ATTR_ROOT) { |
| 172 | #ifdef CONFIG_XFS_POSIX_ACL |
| 173 | if (namelen == SGI_ACL_FILE_SIZE && |
| 174 | strncmp(name, SGI_ACL_FILE, |
| 175 | SGI_ACL_FILE_SIZE) == 0) { |
Eric Sandeen | f7a136a | 2016-12-05 12:32:14 +1100 | [diff] [blame] | 176 | __xfs_xattr_put_listent( |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 177 | context, XATTR_SYSTEM_PREFIX, |
| 178 | XATTR_SYSTEM_PREFIX_LEN, |
| 179 | XATTR_POSIX_ACL_ACCESS, |
| 180 | strlen(XATTR_POSIX_ACL_ACCESS)); |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 181 | } else if (namelen == SGI_ACL_DEFAULT_SIZE && |
| 182 | strncmp(name, SGI_ACL_DEFAULT, |
| 183 | SGI_ACL_DEFAULT_SIZE) == 0) { |
Eric Sandeen | f7a136a | 2016-12-05 12:32:14 +1100 | [diff] [blame] | 184 | __xfs_xattr_put_listent( |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 185 | context, XATTR_SYSTEM_PREFIX, |
| 186 | XATTR_SYSTEM_PREFIX_LEN, |
| 187 | XATTR_POSIX_ACL_DEFAULT, |
| 188 | strlen(XATTR_POSIX_ACL_DEFAULT)); |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 189 | } |
| 190 | #endif |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 191 | |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 192 | /* |
| 193 | * Only show root namespace entries if we are actually allowed to |
| 194 | * see them. |
| 195 | */ |
| 196 | if (!capable(CAP_SYS_ADMIN)) |
Eric Sandeen | f7a136a | 2016-12-05 12:32:14 +1100 | [diff] [blame] | 197 | return; |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 198 | |
| 199 | prefix = XATTR_TRUSTED_PREFIX; |
| 200 | prefix_len = XATTR_TRUSTED_PREFIX_LEN; |
| 201 | } else if (flags & XFS_ATTR_SECURE) { |
| 202 | prefix = XATTR_SECURITY_PREFIX; |
| 203 | prefix_len = XATTR_SECURITY_PREFIX_LEN; |
| 204 | } else { |
| 205 | prefix = XATTR_USER_PREFIX; |
| 206 | prefix_len = XATTR_USER_PREFIX_LEN; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 207 | } |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 208 | |
Eric Sandeen | f7a136a | 2016-12-05 12:32:14 +1100 | [diff] [blame] | 209 | __xfs_xattr_put_listent(context, prefix, prefix_len, name, |
| 210 | namelen); |
| 211 | return; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | ssize_t |
Eric Sandeen | 2a6fba6 | 2016-04-06 07:57:18 +1000 | [diff] [blame] | 215 | xfs_vn_listxattr( |
| 216 | struct dentry *dentry, |
| 217 | char *data, |
| 218 | size_t size) |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 219 | { |
| 220 | struct xfs_attr_list_context context; |
| 221 | struct attrlist_cursor_kern cursor = { 0 }; |
Eric Sandeen | 2a6fba6 | 2016-04-06 07:57:18 +1000 | [diff] [blame] | 222 | struct inode *inode = d_inode(dentry); |
| 223 | int error; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 224 | |
| 225 | /* |
| 226 | * First read the regular on-disk attributes. |
| 227 | */ |
| 228 | memset(&context, 0, sizeof(context)); |
| 229 | context.dp = XFS_I(inode); |
| 230 | context.cursor = &cursor; |
| 231 | context.resynch = 1; |
Christoph Hellwig | a9c8c69 | 2020-02-26 17:30:37 -0800 | [diff] [blame] | 232 | context.buffer = size ? data : NULL; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 233 | context.bufsize = size; |
| 234 | context.firstu = context.bufsize; |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 235 | context.put_listent = xfs_xattr_put_listent; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 236 | |
Christoph Hellwig | 17e1dd8 | 2020-02-26 17:30:39 -0800 | [diff] [blame^] | 237 | error = xfs_attr_list(&context); |
Eric Sandeen | 2a6fba6 | 2016-04-06 07:57:18 +1000 | [diff] [blame] | 238 | if (error) |
| 239 | return error; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 240 | if (context.count < 0) |
| 241 | return -ERANGE; |
| 242 | |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 243 | return context.count; |
| 244 | } |