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_btree.h" |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 16 | |
| 17 | #include <linux/posix_acl_xattr.h> |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 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, |
Mark Salyzyn | 3484eba | 2019-11-04 08:57:10 -0800 | [diff] [blame] | 22 | struct inode *inode, const char *name, void *value, size_t size, |
| 23 | int flags) |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 24 | { |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 25 | struct xfs_da_args args = { |
| 26 | .dp = XFS_I(inode), |
Christoph Hellwig | d5f0f49 | 2020-02-26 17:30:42 -0800 | [diff] [blame] | 27 | .attr_filter = handler->flags, |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 28 | .name = name, |
| 29 | .namelen = strlen(name), |
| 30 | .value = value, |
| 31 | .valuelen = size, |
| 32 | }; |
| 33 | int error; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 34 | |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 35 | error = xfs_attr_get(&args); |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 36 | if (error) |
| 37 | return error; |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 38 | return args.valuelen; |
Andreas Gruenbacher | 47e1bf6 | 2015-11-03 12:56:17 +1100 | [diff] [blame] | 39 | } |
| 40 | |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 41 | static int |
Al Viro | 5930122 | 2016-05-27 10:19:30 -0400 | [diff] [blame] | 42 | xfs_xattr_set(const struct xattr_handler *handler, struct dentry *unused, |
| 43 | struct inode *inode, const char *name, const void *value, |
| 44 | size_t size, int flags) |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 45 | { |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 46 | struct xfs_da_args args = { |
| 47 | .dp = XFS_I(inode), |
Christoph Hellwig | d5f0f49 | 2020-02-26 17:30:42 -0800 | [diff] [blame] | 48 | .attr_filter = handler->flags, |
| 49 | .attr_flags = flags, |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 50 | .name = name, |
| 51 | .namelen = strlen(name), |
| 52 | .value = (void *)value, |
| 53 | .valuelen = size, |
| 54 | }; |
Brian Foster | 67d8e04 | 2015-11-03 12:40:59 +1100 | [diff] [blame] | 55 | int error; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 56 | |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 57 | error = xfs_attr_set(&args); |
Christoph Hellwig | d5f0f49 | 2020-02-26 17:30:42 -0800 | [diff] [blame] | 58 | if (!error && (handler->flags & XFS_ATTR_ROOT)) |
Christoph Hellwig | 5a3930e | 2020-02-26 17:30:41 -0800 | [diff] [blame] | 59 | xfs_forget_acl(inode, name); |
Brian Foster | 67d8e04 | 2015-11-03 12:40:59 +1100 | [diff] [blame] | 60 | return error; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 61 | } |
| 62 | |
Stephen Hemminger | 46e5876 | 2010-05-13 17:53:20 -0700 | [diff] [blame] | 63 | static const struct xattr_handler xfs_xattr_user_handler = { |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 64 | .prefix = XATTR_USER_PREFIX, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 65 | .flags = 0, /* no flags implies user namespace */ |
| 66 | .get = xfs_xattr_get, |
| 67 | .set = xfs_xattr_set, |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 68 | }; |
| 69 | |
Stephen Hemminger | 46e5876 | 2010-05-13 17:53:20 -0700 | [diff] [blame] | 70 | static const struct xattr_handler xfs_xattr_trusted_handler = { |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 71 | .prefix = XATTR_TRUSTED_PREFIX, |
Christoph Hellwig | d5f0f49 | 2020-02-26 17:30:42 -0800 | [diff] [blame] | 72 | .flags = XFS_ATTR_ROOT, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 73 | .get = xfs_xattr_get, |
| 74 | .set = xfs_xattr_set, |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 75 | }; |
| 76 | |
Stephen Hemminger | 46e5876 | 2010-05-13 17:53:20 -0700 | [diff] [blame] | 77 | static const struct xattr_handler xfs_xattr_security_handler = { |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 78 | .prefix = XATTR_SECURITY_PREFIX, |
Christoph Hellwig | d5f0f49 | 2020-02-26 17:30:42 -0800 | [diff] [blame] | 79 | .flags = XFS_ATTR_SECURE, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 80 | .get = xfs_xattr_get, |
| 81 | .set = xfs_xattr_set, |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 82 | }; |
| 83 | |
Stephen Hemminger | 46e5876 | 2010-05-13 17:53:20 -0700 | [diff] [blame] | 84 | const struct xattr_handler *xfs_xattr_handlers[] = { |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 85 | &xfs_xattr_user_handler, |
| 86 | &xfs_xattr_trusted_handler, |
| 87 | &xfs_xattr_security_handler, |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 88 | #ifdef CONFIG_XFS_POSIX_ACL |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 89 | &posix_acl_access_xattr_handler, |
| 90 | &posix_acl_default_xattr_handler, |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 91 | #endif |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 92 | NULL |
| 93 | }; |
| 94 | |
Eric Sandeen | f7a136a | 2016-12-05 12:32:14 +1100 | [diff] [blame] | 95 | static void |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 96 | __xfs_xattr_put_listent( |
| 97 | struct xfs_attr_list_context *context, |
| 98 | char *prefix, |
| 99 | int prefix_len, |
| 100 | unsigned char *name, |
| 101 | int namelen) |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 102 | { |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 103 | char *offset; |
| 104 | int arraytop; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 105 | |
Darrick J. Wong | 3b50086 | 2019-02-13 11:15:17 -0800 | [diff] [blame] | 106 | if (context->count < 0 || context->seen_enough) |
| 107 | return; |
| 108 | |
Christoph Hellwig | a9c8c69 | 2020-02-26 17:30:37 -0800 | [diff] [blame] | 109 | if (!context->buffer) |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 110 | goto compute_size; |
| 111 | |
| 112 | arraytop = context->count + prefix_len + namelen + 1; |
| 113 | if (arraytop > context->firstu) { |
| 114 | context->count = -1; /* insufficient space */ |
Artem Savkov | 791cc43 | 2016-09-14 07:40:35 +1000 | [diff] [blame] | 115 | context->seen_enough = 1; |
Eric Sandeen | f7a136a | 2016-12-05 12:32:14 +1100 | [diff] [blame] | 116 | return; |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 117 | } |
Christoph Hellwig | a9c8c69 | 2020-02-26 17:30:37 -0800 | [diff] [blame] | 118 | offset = context->buffer + context->count; |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 119 | strncpy(offset, prefix, prefix_len); |
| 120 | offset += prefix_len; |
| 121 | strncpy(offset, (char *)name, namelen); /* real name */ |
| 122 | offset += namelen; |
| 123 | *offset = '\0'; |
| 124 | |
| 125 | compute_size: |
| 126 | context->count += prefix_len + namelen + 1; |
Eric Sandeen | f7a136a | 2016-12-05 12:32:14 +1100 | [diff] [blame] | 127 | return; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 128 | } |
| 129 | |
Eric Sandeen | f7a136a | 2016-12-05 12:32:14 +1100 | [diff] [blame] | 130 | static void |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 131 | xfs_xattr_put_listent( |
| 132 | struct xfs_attr_list_context *context, |
| 133 | int flags, |
| 134 | unsigned char *name, |
| 135 | int namelen, |
Eric Sandeen | e5bd12b | 2016-04-06 07:57:32 +1000 | [diff] [blame] | 136 | int valuelen) |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 137 | { |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 138 | char *prefix; |
| 139 | int prefix_len; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 140 | |
| 141 | ASSERT(context->count >= 0); |
| 142 | |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 143 | if (flags & XFS_ATTR_ROOT) { |
| 144 | #ifdef CONFIG_XFS_POSIX_ACL |
| 145 | if (namelen == SGI_ACL_FILE_SIZE && |
| 146 | strncmp(name, SGI_ACL_FILE, |
| 147 | SGI_ACL_FILE_SIZE) == 0) { |
Eric Sandeen | f7a136a | 2016-12-05 12:32:14 +1100 | [diff] [blame] | 148 | __xfs_xattr_put_listent( |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 149 | context, XATTR_SYSTEM_PREFIX, |
| 150 | XATTR_SYSTEM_PREFIX_LEN, |
| 151 | XATTR_POSIX_ACL_ACCESS, |
| 152 | strlen(XATTR_POSIX_ACL_ACCESS)); |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 153 | } else if (namelen == SGI_ACL_DEFAULT_SIZE && |
| 154 | strncmp(name, SGI_ACL_DEFAULT, |
| 155 | SGI_ACL_DEFAULT_SIZE) == 0) { |
Eric Sandeen | f7a136a | 2016-12-05 12:32:14 +1100 | [diff] [blame] | 156 | __xfs_xattr_put_listent( |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 157 | context, XATTR_SYSTEM_PREFIX, |
| 158 | XATTR_SYSTEM_PREFIX_LEN, |
| 159 | XATTR_POSIX_ACL_DEFAULT, |
| 160 | strlen(XATTR_POSIX_ACL_DEFAULT)); |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 161 | } |
| 162 | #endif |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 163 | |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 164 | /* |
| 165 | * Only show root namespace entries if we are actually allowed to |
| 166 | * see them. |
| 167 | */ |
| 168 | if (!capable(CAP_SYS_ADMIN)) |
Eric Sandeen | f7a136a | 2016-12-05 12:32:14 +1100 | [diff] [blame] | 169 | return; |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 170 | |
| 171 | prefix = XATTR_TRUSTED_PREFIX; |
| 172 | prefix_len = XATTR_TRUSTED_PREFIX_LEN; |
| 173 | } else if (flags & XFS_ATTR_SECURE) { |
| 174 | prefix = XATTR_SECURITY_PREFIX; |
| 175 | prefix_len = XATTR_SECURITY_PREFIX_LEN; |
| 176 | } else { |
| 177 | prefix = XATTR_USER_PREFIX; |
| 178 | prefix_len = XATTR_USER_PREFIX_LEN; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 179 | } |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 180 | |
Eric Sandeen | f7a136a | 2016-12-05 12:32:14 +1100 | [diff] [blame] | 181 | __xfs_xattr_put_listent(context, prefix, prefix_len, name, |
| 182 | namelen); |
| 183 | return; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | ssize_t |
Eric Sandeen | 2a6fba6 | 2016-04-06 07:57:18 +1000 | [diff] [blame] | 187 | xfs_vn_listxattr( |
| 188 | struct dentry *dentry, |
| 189 | char *data, |
| 190 | size_t size) |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 191 | { |
| 192 | struct xfs_attr_list_context context; |
Eric Sandeen | 2a6fba6 | 2016-04-06 07:57:18 +1000 | [diff] [blame] | 193 | struct inode *inode = d_inode(dentry); |
| 194 | int error; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 195 | |
| 196 | /* |
| 197 | * First read the regular on-disk attributes. |
| 198 | */ |
| 199 | memset(&context, 0, sizeof(context)); |
| 200 | context.dp = XFS_I(inode); |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 201 | context.resynch = 1; |
Christoph Hellwig | a9c8c69 | 2020-02-26 17:30:37 -0800 | [diff] [blame] | 202 | context.buffer = size ? data : NULL; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 203 | context.bufsize = size; |
| 204 | context.firstu = context.bufsize; |
Andreas Gruenbacher | 5d92b75 | 2015-12-02 14:44:40 +0100 | [diff] [blame] | 205 | context.put_listent = xfs_xattr_put_listent; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 206 | |
Christoph Hellwig | 17e1dd8 | 2020-02-26 17:30:39 -0800 | [diff] [blame] | 207 | error = xfs_attr_list(&context); |
Eric Sandeen | 2a6fba6 | 2016-04-06 07:57:18 +1000 | [diff] [blame] | 208 | if (error) |
| 209 | return error; |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 210 | if (context.count < 0) |
| 211 | return -ERANGE; |
| 212 | |
Lachlan McIlroy | f9e09f0 | 2008-06-23 13:34:09 +1000 | [diff] [blame] | 213 | return context.count; |
| 214 | } |