Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2008, Christoph Hellwig |
| 4 | * All Rights Reserved. |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 5 | */ |
| 6 | #include "xfs.h" |
Darrick J. Wong | 5467b34 | 2019-06-28 19:25:35 -0700 | [diff] [blame] | 7 | #include "xfs_shared.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 8 | #include "xfs_format.h" |
Dave Chinner | 6943283 | 2013-08-12 20:49:23 +1000 | [diff] [blame] | 9 | #include "xfs_log_format.h" |
Dave Chinner | 7fd36c4 | 2013-08-12 20:49:32 +1000 | [diff] [blame] | 10 | #include "xfs_trans_resv.h" |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 11 | #include "xfs_mount.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 12 | #include "xfs_inode.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 13 | #include "xfs_attr.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 14 | #include "xfs_trace.h" |
Darrick J. Wong | a5155b8 | 2019-11-02 09:40:53 -0700 | [diff] [blame] | 15 | #include "xfs_error.h" |
Darrick J. Wong | 5f213dd | 2019-11-06 17:19:33 -0800 | [diff] [blame] | 16 | #include "xfs_acl.h" |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 17 | #include "xfs_da_format.h" |
| 18 | #include "xfs_da_btree.h" |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 19 | |
Darrick J. Wong | 5f213dd | 2019-11-06 17:19:33 -0800 | [diff] [blame] | 20 | #include <linux/posix_acl_xattr.h> |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 21 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 22 | /* |
| 23 | * Locking scheme: |
| 24 | * - all ACL updates are protected by inode->i_mutex, which is taken before |
| 25 | * calling into this file. |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | STATIC struct posix_acl * |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 29 | xfs_acl_from_disk( |
Darrick J. Wong | a5155b8 | 2019-11-02 09:40:53 -0700 | [diff] [blame] | 30 | struct xfs_mount *mp, |
Andreas Gruenbacher | 86a21c7 | 2015-11-03 12:41:59 +1100 | [diff] [blame] | 31 | const struct xfs_acl *aclp, |
| 32 | int len, |
| 33 | int max_entries) |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 34 | { |
| 35 | struct posix_acl_entry *acl_e; |
| 36 | struct posix_acl *acl; |
Andreas Gruenbacher | 86a21c7 | 2015-11-03 12:41:59 +1100 | [diff] [blame] | 37 | const struct xfs_acl_entry *ace; |
Xi Wang | 093019c | 2011-12-12 21:55:52 +0000 | [diff] [blame] | 38 | unsigned int count, i; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 39 | |
Darrick J. Wong | a5155b8 | 2019-11-02 09:40:53 -0700 | [diff] [blame] | 40 | if (len < sizeof(*aclp)) { |
| 41 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, aclp, |
| 42 | len); |
Andreas Gruenbacher | 86a21c7 | 2015-11-03 12:41:59 +1100 | [diff] [blame] | 43 | return ERR_PTR(-EFSCORRUPTED); |
Darrick J. Wong | a5155b8 | 2019-11-02 09:40:53 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 46 | count = be32_to_cpu(aclp->acl_cnt); |
Darrick J. Wong | a5155b8 | 2019-11-02 09:40:53 -0700 | [diff] [blame] | 47 | if (count > max_entries || XFS_ACL_SIZE(count) != len) { |
| 48 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, aclp, |
| 49 | len); |
Christoph Hellwig | fa8b18e | 2011-11-20 15:35:32 +0000 | [diff] [blame] | 50 | return ERR_PTR(-EFSCORRUPTED); |
Darrick J. Wong | a5155b8 | 2019-11-02 09:40:53 -0700 | [diff] [blame] | 51 | } |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 52 | |
| 53 | acl = posix_acl_alloc(count, GFP_KERNEL); |
| 54 | if (!acl) |
| 55 | return ERR_PTR(-ENOMEM); |
| 56 | |
| 57 | for (i = 0; i < count; i++) { |
| 58 | acl_e = &acl->a_entries[i]; |
| 59 | ace = &aclp->acl_entry[i]; |
| 60 | |
| 61 | /* |
| 62 | * The tag is 32 bits on disk and 16 bits in core. |
| 63 | * |
| 64 | * Because every access to it goes through the core |
| 65 | * format first this is not a problem. |
| 66 | */ |
| 67 | acl_e->e_tag = be32_to_cpu(ace->ae_tag); |
| 68 | acl_e->e_perm = be16_to_cpu(ace->ae_perm); |
| 69 | |
| 70 | switch (acl_e->e_tag) { |
| 71 | case ACL_USER: |
Christoph Hellwig | ba8adad | 2020-02-21 08:31:27 -0800 | [diff] [blame] | 72 | acl_e->e_uid = make_kuid(&init_user_ns, |
| 73 | be32_to_cpu(ace->ae_id)); |
Dwight Engen | 288bbe0 | 2013-08-15 14:07:59 -0400 | [diff] [blame] | 74 | break; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 75 | case ACL_GROUP: |
Christoph Hellwig | ba8adad | 2020-02-21 08:31:27 -0800 | [diff] [blame] | 76 | acl_e->e_gid = make_kgid(&init_user_ns, |
| 77 | be32_to_cpu(ace->ae_id)); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 78 | break; |
| 79 | case ACL_USER_OBJ: |
| 80 | case ACL_GROUP_OBJ: |
| 81 | case ACL_MASK: |
| 82 | case ACL_OTHER: |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 83 | break; |
| 84 | default: |
| 85 | goto fail; |
| 86 | } |
| 87 | } |
| 88 | return acl; |
| 89 | |
| 90 | fail: |
| 91 | posix_acl_release(acl); |
| 92 | return ERR_PTR(-EINVAL); |
| 93 | } |
| 94 | |
| 95 | STATIC void |
| 96 | xfs_acl_to_disk(struct xfs_acl *aclp, const struct posix_acl *acl) |
| 97 | { |
| 98 | const struct posix_acl_entry *acl_e; |
| 99 | struct xfs_acl_entry *ace; |
| 100 | int i; |
| 101 | |
| 102 | aclp->acl_cnt = cpu_to_be32(acl->a_count); |
| 103 | for (i = 0; i < acl->a_count; i++) { |
| 104 | ace = &aclp->acl_entry[i]; |
| 105 | acl_e = &acl->a_entries[i]; |
| 106 | |
| 107 | ace->ae_tag = cpu_to_be32(acl_e->e_tag); |
Dwight Engen | 288bbe0 | 2013-08-15 14:07:59 -0400 | [diff] [blame] | 108 | switch (acl_e->e_tag) { |
| 109 | case ACL_USER: |
Christoph Hellwig | ba8adad | 2020-02-21 08:31:27 -0800 | [diff] [blame] | 110 | ace->ae_id = cpu_to_be32( |
| 111 | from_kuid(&init_user_ns, acl_e->e_uid)); |
Dwight Engen | 288bbe0 | 2013-08-15 14:07:59 -0400 | [diff] [blame] | 112 | break; |
| 113 | case ACL_GROUP: |
Christoph Hellwig | ba8adad | 2020-02-21 08:31:27 -0800 | [diff] [blame] | 114 | ace->ae_id = cpu_to_be32( |
| 115 | from_kgid(&init_user_ns, acl_e->e_gid)); |
Dwight Engen | 288bbe0 | 2013-08-15 14:07:59 -0400 | [diff] [blame] | 116 | break; |
| 117 | default: |
| 118 | ace->ae_id = cpu_to_be32(ACL_UNDEFINED_ID); |
| 119 | break; |
| 120 | } |
| 121 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 122 | ace->ae_perm = cpu_to_be16(acl_e->e_perm); |
| 123 | } |
| 124 | } |
| 125 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 126 | struct posix_acl * |
| 127 | xfs_get_acl(struct inode *inode, int type) |
| 128 | { |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 129 | struct xfs_inode *ip = XFS_I(inode); |
| 130 | struct xfs_mount *mp = ip->i_mount; |
| 131 | struct posix_acl *acl = NULL; |
| 132 | struct xfs_da_args args = { |
| 133 | .dp = ip, |
Christoph Hellwig | d5f0f49 | 2020-02-26 17:30:42 -0800 | [diff] [blame] | 134 | .attr_filter = XFS_ATTR_ROOT, |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 135 | .valuelen = XFS_ACL_MAX_SIZE(mp), |
| 136 | }; |
| 137 | int error; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 138 | |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 139 | trace_xfs_get_acl(ip); |
| 140 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 141 | switch (type) { |
| 142 | case ACL_TYPE_ACCESS: |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 143 | args.name = SGI_ACL_FILE; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 144 | break; |
| 145 | case ACL_TYPE_DEFAULT: |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 146 | args.name = SGI_ACL_DEFAULT; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 147 | break; |
| 148 | default: |
Al Viro | 1cbd20d | 2009-06-09 13:29:39 -0400 | [diff] [blame] | 149 | BUG(); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 150 | } |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 151 | args.namelen = strlen(args.name); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 152 | |
Christoph Hellwig | d49db18 | 2020-02-26 17:30:35 -0800 | [diff] [blame] | 153 | /* |
| 154 | * If the attribute doesn't exist make sure we have a negative cache |
| 155 | * entry, for any other error assume it is transient. |
| 156 | */ |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 157 | error = xfs_attr_get(&args); |
Christoph Hellwig | d49db18 | 2020-02-26 17:30:35 -0800 | [diff] [blame] | 158 | if (!error) { |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 159 | acl = xfs_acl_from_disk(mp, args.value, args.valuelen, |
| 160 | XFS_ACL_MAX_ENTRIES(mp)); |
Christoph Hellwig | d49db18 | 2020-02-26 17:30:35 -0800 | [diff] [blame] | 161 | } else if (error != -ENOATTR) { |
| 162 | acl = ERR_PTR(error); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 163 | } |
Christoph Hellwig | d49db18 | 2020-02-26 17:30:35 -0800 | [diff] [blame] | 164 | |
| 165 | kmem_free(args.value); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 166 | return acl; |
| 167 | } |
| 168 | |
Jan Kara | 8ba3587 | 2017-06-26 08:48:18 -0700 | [diff] [blame] | 169 | int |
| 170 | __xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type) |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 171 | { |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 172 | struct xfs_inode *ip = XFS_I(inode); |
| 173 | struct xfs_da_args args = { |
| 174 | .dp = ip, |
Christoph Hellwig | d5f0f49 | 2020-02-26 17:30:42 -0800 | [diff] [blame] | 175 | .attr_filter = XFS_ATTR_ROOT, |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 176 | }; |
| 177 | int error; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 178 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 179 | switch (type) { |
| 180 | case ACL_TYPE_ACCESS: |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 181 | args.name = SGI_ACL_FILE; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 182 | break; |
| 183 | case ACL_TYPE_DEFAULT: |
| 184 | if (!S_ISDIR(inode->i_mode)) |
| 185 | return acl ? -EACCES : 0; |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 186 | args.name = SGI_ACL_DEFAULT; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 187 | break; |
| 188 | default: |
| 189 | return -EINVAL; |
| 190 | } |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 191 | args.namelen = strlen(args.name); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 192 | |
| 193 | if (acl) { |
Christoph Hellwig | ed02d13 | 2020-02-26 17:30:44 -0800 | [diff] [blame] | 194 | args.valuelen = XFS_ACL_SIZE(acl->a_count); |
Carlos Maiolino | 8ca79df | 2020-09-01 11:47:12 -0700 | [diff] [blame] | 195 | args.value = kvzalloc(args.valuelen, GFP_KERNEL); |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 196 | if (!args.value) |
Dave Chinner | fdd3cce | 2013-09-02 20:53:00 +1000 | [diff] [blame] | 197 | return -ENOMEM; |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 198 | xfs_acl_to_disk(args.value, acl); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 201 | error = xfs_attr_set(&args); |
| 202 | kmem_free(args.value); |
Christoph Hellwig | 0eb81a5 | 2020-02-26 17:30:29 -0800 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * If the attribute didn't exist to start with that's fine. |
| 206 | */ |
| 207 | if (!acl && error == -ENOATTR) |
| 208 | error = 0; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 209 | if (!error) |
Al Viro | 1cbd20d | 2009-06-09 13:29:39 -0400 | [diff] [blame] | 210 | set_cached_acl(inode, type, acl); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 211 | return error; |
| 212 | } |
| 213 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 214 | static int |
Al Viro | d3fb612 | 2011-07-23 18:37:50 -0400 | [diff] [blame] | 215 | xfs_set_mode(struct inode *inode, umode_t mode) |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 216 | { |
| 217 | int error = 0; |
| 218 | |
| 219 | if (mode != inode->i_mode) { |
| 220 | struct iattr iattr; |
| 221 | |
Christoph Hellwig | d6d59ba | 2009-12-23 16:09:13 +0000 | [diff] [blame] | 222 | iattr.ia_valid = ATTR_MODE | ATTR_CTIME; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 223 | iattr.ia_mode = mode; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 224 | iattr.ia_ctime = current_time(inode); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 225 | |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 226 | error = xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | return error; |
| 230 | } |
| 231 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 232 | int |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 233 | xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type) |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 234 | { |
Dave Chinner | 67f2ffe | 2017-10-09 11:37:23 -0700 | [diff] [blame] | 235 | umode_t mode; |
| 236 | bool set_mode = false; |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 237 | int error = 0; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 238 | |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 239 | if (!acl) |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 240 | goto set_acl; |
| 241 | |
Jie Liu | 4ae69fe | 2014-02-07 15:26:11 +1100 | [diff] [blame] | 242 | error = -E2BIG; |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 243 | if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb))) |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 244 | return error; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 245 | |
| 246 | if (type == ACL_TYPE_ACCESS) { |
Jan Kara | 0739310 | 2016-09-19 17:39:09 +0200 | [diff] [blame] | 247 | error = posix_acl_update_mode(inode, &mode, &acl); |
| 248 | if (error) |
| 249 | return error; |
Dave Chinner | 67f2ffe | 2017-10-09 11:37:23 -0700 | [diff] [blame] | 250 | set_mode = true; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | set_acl: |
Dave Chinner | 67f2ffe | 2017-10-09 11:37:23 -0700 | [diff] [blame] | 254 | error = __xfs_set_acl(inode, acl, type); |
| 255 | if (error) |
| 256 | return error; |
| 257 | |
| 258 | /* |
| 259 | * We set the mode after successfully updating the ACL xattr because the |
| 260 | * xattr update can fail at ENOSPC and we don't want to change the mode |
| 261 | * if the ACL update hasn't been applied. |
| 262 | */ |
| 263 | if (set_mode) |
| 264 | error = xfs_set_mode(inode, mode); |
| 265 | |
| 266 | return error; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 267 | } |
Christoph Hellwig | 5a3930e | 2020-02-26 17:30:41 -0800 | [diff] [blame] | 268 | |
| 269 | /* |
| 270 | * Invalidate any cached ACLs if the user has bypassed the ACL interface. |
| 271 | * We don't validate the content whatsoever so it is caller responsibility to |
| 272 | * provide data in valid format and ensure i_mode is consistent. |
| 273 | */ |
| 274 | void |
| 275 | xfs_forget_acl( |
| 276 | struct inode *inode, |
| 277 | const char *name) |
| 278 | { |
| 279 | if (!strcmp(name, SGI_ACL_FILE)) |
| 280 | forget_cached_acl(inode, ACL_TYPE_ACCESS); |
| 281 | else if (!strcmp(name, SGI_ACL_DEFAULT)) |
| 282 | forget_cached_acl(inode, ACL_TYPE_DEFAULT); |
| 283 | } |