blob: 791db7d9c8498b80f58e4d9cedf60a15a557f11b [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwigef14f0c2009-06-10 17:07:47 +02002/*
3 * Copyright (c) 2008, Christoph Hellwig
4 * All Rights Reserved.
Christoph Hellwigef14f0c2009-06-10 17:07:47 +02005 */
6#include "xfs.h"
Darrick J. Wong5467b342019-06-28 19:25:35 -07007#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +11008#include "xfs_format.h"
Dave Chinner69432832013-08-12 20:49:23 +10009#include "xfs_log_format.h"
Dave Chinner7fd36c42013-08-12 20:49:32 +100010#include "xfs_trans_resv.h"
Dave Chinner0a8aa192013-06-05 12:09:10 +100011#include "xfs_mount.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110012#include "xfs_inode.h"
Dave Chinnere0c41082022-05-12 15:12:52 +100013#include "xfs_da_format.h"
14#include "xfs_da_btree.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110015#include "xfs_attr.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000016#include "xfs_trace.h"
Darrick J. Wonga5155b82019-11-02 09:40:53 -070017#include "xfs_error.h"
Darrick J. Wong5f213dd2019-11-06 17:19:33 -080018#include "xfs_acl.h"
Christoph Hellwig5d24ec4c2020-12-10 20:00:39 -080019#include "xfs_trans.h"
Darrick J. Wongefc2efe2022-05-27 10:34:04 +100020#include "xfs_xattr.h"
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020021
Darrick J. Wong5f213dd2019-11-06 17:19:33 -080022#include <linux/posix_acl_xattr.h>
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020023
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020024/*
25 * Locking scheme:
26 * - all ACL updates are protected by inode->i_mutex, which is taken before
27 * calling into this file.
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020028 */
29
30STATIC struct posix_acl *
Dave Chinner0a8aa192013-06-05 12:09:10 +100031xfs_acl_from_disk(
Darrick J. Wonga5155b82019-11-02 09:40:53 -070032 struct xfs_mount *mp,
Andreas Gruenbacher86a21c72015-11-03 12:41:59 +110033 const struct xfs_acl *aclp,
34 int len,
35 int max_entries)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020036{
37 struct posix_acl_entry *acl_e;
38 struct posix_acl *acl;
Andreas Gruenbacher86a21c72015-11-03 12:41:59 +110039 const struct xfs_acl_entry *ace;
Xi Wang093019c2011-12-12 21:55:52 +000040 unsigned int count, i;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020041
Darrick J. Wonga5155b82019-11-02 09:40:53 -070042 if (len < sizeof(*aclp)) {
43 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, aclp,
44 len);
Andreas Gruenbacher86a21c72015-11-03 12:41:59 +110045 return ERR_PTR(-EFSCORRUPTED);
Darrick J. Wonga5155b82019-11-02 09:40:53 -070046 }
47
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020048 count = be32_to_cpu(aclp->acl_cnt);
Darrick J. Wonga5155b82019-11-02 09:40:53 -070049 if (count > max_entries || XFS_ACL_SIZE(count) != len) {
50 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, aclp,
51 len);
Christoph Hellwigfa8b18e2011-11-20 15:35:32 +000052 return ERR_PTR(-EFSCORRUPTED);
Darrick J. Wonga5155b82019-11-02 09:40:53 -070053 }
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020054
55 acl = posix_acl_alloc(count, GFP_KERNEL);
56 if (!acl)
57 return ERR_PTR(-ENOMEM);
58
59 for (i = 0; i < count; i++) {
60 acl_e = &acl->a_entries[i];
61 ace = &aclp->acl_entry[i];
62
63 /*
64 * The tag is 32 bits on disk and 16 bits in core.
65 *
66 * Because every access to it goes through the core
67 * format first this is not a problem.
68 */
69 acl_e->e_tag = be32_to_cpu(ace->ae_tag);
70 acl_e->e_perm = be16_to_cpu(ace->ae_perm);
71
72 switch (acl_e->e_tag) {
73 case ACL_USER:
Christoph Hellwigba8adad2020-02-21 08:31:27 -080074 acl_e->e_uid = make_kuid(&init_user_ns,
75 be32_to_cpu(ace->ae_id));
Dwight Engen288bbe02013-08-15 14:07:59 -040076 break;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020077 case ACL_GROUP:
Christoph Hellwigba8adad2020-02-21 08:31:27 -080078 acl_e->e_gid = make_kgid(&init_user_ns,
79 be32_to_cpu(ace->ae_id));
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020080 break;
81 case ACL_USER_OBJ:
82 case ACL_GROUP_OBJ:
83 case ACL_MASK:
84 case ACL_OTHER:
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020085 break;
86 default:
87 goto fail;
88 }
89 }
90 return acl;
91
92fail:
93 posix_acl_release(acl);
94 return ERR_PTR(-EINVAL);
95}
96
97STATIC void
98xfs_acl_to_disk(struct xfs_acl *aclp, const struct posix_acl *acl)
99{
100 const struct posix_acl_entry *acl_e;
101 struct xfs_acl_entry *ace;
102 int i;
103
104 aclp->acl_cnt = cpu_to_be32(acl->a_count);
105 for (i = 0; i < acl->a_count; i++) {
106 ace = &aclp->acl_entry[i];
107 acl_e = &acl->a_entries[i];
108
109 ace->ae_tag = cpu_to_be32(acl_e->e_tag);
Dwight Engen288bbe02013-08-15 14:07:59 -0400110 switch (acl_e->e_tag) {
111 case ACL_USER:
Christoph Hellwigba8adad2020-02-21 08:31:27 -0800112 ace->ae_id = cpu_to_be32(
113 from_kuid(&init_user_ns, acl_e->e_uid));
Dwight Engen288bbe02013-08-15 14:07:59 -0400114 break;
115 case ACL_GROUP:
Christoph Hellwigba8adad2020-02-21 08:31:27 -0800116 ace->ae_id = cpu_to_be32(
117 from_kgid(&init_user_ns, acl_e->e_gid));
Dwight Engen288bbe02013-08-15 14:07:59 -0400118 break;
119 default:
120 ace->ae_id = cpu_to_be32(ACL_UNDEFINED_ID);
121 break;
122 }
123
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200124 ace->ae_perm = cpu_to_be16(acl_e->e_perm);
125 }
126}
127
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200128struct posix_acl *
Miklos Szeredi0cad6242021-08-18 22:08:24 +0200129xfs_get_acl(struct inode *inode, int type, bool rcu)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200130{
Christoph Hellwige5171d72020-02-26 17:30:34 -0800131 struct xfs_inode *ip = XFS_I(inode);
132 struct xfs_mount *mp = ip->i_mount;
133 struct posix_acl *acl = NULL;
134 struct xfs_da_args args = {
135 .dp = ip,
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800136 .attr_filter = XFS_ATTR_ROOT,
Christoph Hellwige5171d72020-02-26 17:30:34 -0800137 .valuelen = XFS_ACL_MAX_SIZE(mp),
138 };
139 int error;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200140
Miklos Szeredi0cad6242021-08-18 22:08:24 +0200141 if (rcu)
142 return ERR_PTR(-ECHILD);
143
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200144 trace_xfs_get_acl(ip);
145
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200146 switch (type) {
147 case ACL_TYPE_ACCESS:
Christoph Hellwige5171d72020-02-26 17:30:34 -0800148 args.name = SGI_ACL_FILE;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200149 break;
150 case ACL_TYPE_DEFAULT:
Christoph Hellwige5171d72020-02-26 17:30:34 -0800151 args.name = SGI_ACL_DEFAULT;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200152 break;
153 default:
Al Viro1cbd20d2009-06-09 13:29:39 -0400154 BUG();
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200155 }
Christoph Hellwige5171d72020-02-26 17:30:34 -0800156 args.namelen = strlen(args.name);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200157
Christoph Hellwigd49db182020-02-26 17:30:35 -0800158 /*
159 * If the attribute doesn't exist make sure we have a negative cache
160 * entry, for any other error assume it is transient.
161 */
Christoph Hellwige5171d72020-02-26 17:30:34 -0800162 error = xfs_attr_get(&args);
Christoph Hellwigd49db182020-02-26 17:30:35 -0800163 if (!error) {
Christoph Hellwige5171d72020-02-26 17:30:34 -0800164 acl = xfs_acl_from_disk(mp, args.value, args.valuelen,
165 XFS_ACL_MAX_ENTRIES(mp));
Christoph Hellwigd49db182020-02-26 17:30:35 -0800166 } else if (error != -ENOATTR) {
167 acl = ERR_PTR(error);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200168 }
Christoph Hellwigd49db182020-02-26 17:30:35 -0800169
170 kmem_free(args.value);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200171 return acl;
172}
173
Jan Kara8ba35872017-06-26 08:48:18 -0700174int
175__xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200176{
Christoph Hellwiga2544622020-02-26 17:30:33 -0800177 struct xfs_inode *ip = XFS_I(inode);
178 struct xfs_da_args args = {
179 .dp = ip,
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800180 .attr_filter = XFS_ATTR_ROOT,
Christoph Hellwiga2544622020-02-26 17:30:33 -0800181 };
182 int error;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200183
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200184 switch (type) {
185 case ACL_TYPE_ACCESS:
Christoph Hellwiga2544622020-02-26 17:30:33 -0800186 args.name = SGI_ACL_FILE;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200187 break;
188 case ACL_TYPE_DEFAULT:
189 if (!S_ISDIR(inode->i_mode))
190 return acl ? -EACCES : 0;
Christoph Hellwiga2544622020-02-26 17:30:33 -0800191 args.name = SGI_ACL_DEFAULT;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200192 break;
193 default:
194 return -EINVAL;
195 }
Christoph Hellwiga2544622020-02-26 17:30:33 -0800196 args.namelen = strlen(args.name);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200197
198 if (acl) {
Christoph Hellwiged02d132020-02-26 17:30:44 -0800199 args.valuelen = XFS_ACL_SIZE(acl->a_count);
Carlos Maiolino8ca79df2020-09-01 11:47:12 -0700200 args.value = kvzalloc(args.valuelen, GFP_KERNEL);
Christoph Hellwiga2544622020-02-26 17:30:33 -0800201 if (!args.value)
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000202 return -ENOMEM;
Christoph Hellwiga2544622020-02-26 17:30:33 -0800203 xfs_acl_to_disk(args.value, acl);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200204 }
205
Darrick J. Wongefc2efe2022-05-27 10:34:04 +1000206 error = xfs_attr_change(&args);
Christoph Hellwiga2544622020-02-26 17:30:33 -0800207 kmem_free(args.value);
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800208
209 /*
210 * If the attribute didn't exist to start with that's fine.
211 */
212 if (!acl && error == -ENOATTR)
213 error = 0;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200214 if (!error)
Al Viro1cbd20d2009-06-09 13:29:39 -0400215 set_cached_acl(inode, type, acl);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200216 return error;
217}
218
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200219static int
Christoph Hellwig5d24ec4c2020-12-10 20:00:39 -0800220xfs_acl_set_mode(
221 struct inode *inode,
222 umode_t mode)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200223{
Christoph Hellwig5d24ec4c2020-12-10 20:00:39 -0800224 struct xfs_inode *ip = XFS_I(inode);
225 struct xfs_mount *mp = ip->i_mount;
226 struct xfs_trans *tp;
227 int error;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200228
Christoph Hellwig5d24ec4c2020-12-10 20:00:39 -0800229 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
230 if (error)
231 return error;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200232
Christoph Hellwig5d24ec4c2020-12-10 20:00:39 -0800233 xfs_ilock(ip, XFS_ILOCK_EXCL);
234 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
235 inode->i_mode = mode;
236 inode->i_ctime = current_time(inode);
237 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200238
Dave Chinner0560f312021-08-18 18:46:52 -0700239 if (xfs_has_wsync(mp))
Christoph Hellwig5d24ec4c2020-12-10 20:00:39 -0800240 xfs_trans_set_sync(tp);
241 return xfs_trans_commit(tp);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200242}
243
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200244int
Christian Brauner13e83a42023-01-13 12:49:20 +0100245xfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
Christian Brauner549c7292021-01-21 14:19:43 +0100246 struct posix_acl *acl, int type)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200247{
Dave Chinner67f2ffe2017-10-09 11:37:23 -0700248 umode_t mode;
249 bool set_mode = false;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000250 int error = 0;
Christian Brauner138060b2022-09-23 10:29:39 +0200251 struct inode *inode = d_inode(dentry);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200252
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800253 if (!acl)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200254 goto set_acl;
255
Jie Liu4ae69fe2014-02-07 15:26:11 +1100256 error = -E2BIG;
Dave Chinner0a8aa192013-06-05 12:09:10 +1000257 if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb)))
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800258 return error;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200259
260 if (type == ACL_TYPE_ACCESS) {
Christian Brauner700b7942023-01-13 12:49:24 +0100261 error = posix_acl_update_mode(idmap, inode, &mode, &acl);
Jan Kara07393102016-09-19 17:39:09 +0200262 if (error)
263 return error;
Dave Chinner67f2ffe2017-10-09 11:37:23 -0700264 set_mode = true;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200265 }
266
267 set_acl:
Dave Chinner67f2ffe2017-10-09 11:37:23 -0700268 /*
269 * We set the mode after successfully updating the ACL xattr because the
270 * xattr update can fail at ENOSPC and we don't want to change the mode
271 * if the ACL update hasn't been applied.
272 */
Christoph Hellwig5d24ec4c2020-12-10 20:00:39 -0800273 error = __xfs_set_acl(inode, acl, type);
274 if (!error && set_mode && mode != inode->i_mode)
275 error = xfs_acl_set_mode(inode, mode);
Dave Chinner67f2ffe2017-10-09 11:37:23 -0700276 return error;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200277}
Christoph Hellwig5a3930e2020-02-26 17:30:41 -0800278
279/*
280 * Invalidate any cached ACLs if the user has bypassed the ACL interface.
281 * We don't validate the content whatsoever so it is caller responsibility to
282 * provide data in valid format and ensure i_mode is consistent.
283 */
284void
285xfs_forget_acl(
286 struct inode *inode,
287 const char *name)
288{
289 if (!strcmp(name, SGI_ACL_FILE))
290 forget_cached_acl(inode, ACL_TYPE_ACCESS);
291 else if (!strcmp(name, SGI_ACL_DEFAULT))
292 forget_cached_acl(inode, ACL_TYPE_DEFAULT);
293}