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