blob: c544951a0c07f3d81e068e46d73f9e1ef7d149b2 [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 Chinnera4fbe6a2013-10-23 10:51:50 +110013#include "xfs_attr.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000014#include "xfs_trace.h"
Darrick J. Wonga5155b82019-11-02 09:40:53 -070015#include "xfs_error.h"
Darrick J. Wong5f213dd2019-11-06 17:19:33 -080016#include "xfs_acl.h"
Christoph Hellwiga2544622020-02-26 17:30:33 -080017#include "xfs_da_format.h"
18#include "xfs_da_btree.h"
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020019
Darrick J. Wong5f213dd2019-11-06 17:19:33 -080020#include <linux/posix_acl_xattr.h>
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020021
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020022/*
23 * Locking scheme:
24 * - all ACL updates are protected by inode->i_mutex, which is taken before
25 * calling into this file.
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020026 */
27
28STATIC struct posix_acl *
Dave Chinner0a8aa192013-06-05 12:09:10 +100029xfs_acl_from_disk(
Darrick J. Wonga5155b82019-11-02 09:40:53 -070030 struct xfs_mount *mp,
Andreas Gruenbacher86a21c72015-11-03 12:41:59 +110031 const struct xfs_acl *aclp,
32 int len,
33 int max_entries)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020034{
35 struct posix_acl_entry *acl_e;
36 struct posix_acl *acl;
Andreas Gruenbacher86a21c72015-11-03 12:41:59 +110037 const struct xfs_acl_entry *ace;
Xi Wang093019c2011-12-12 21:55:52 +000038 unsigned int count, i;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020039
Darrick J. Wonga5155b82019-11-02 09:40:53 -070040 if (len < sizeof(*aclp)) {
41 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, aclp,
42 len);
Andreas Gruenbacher86a21c72015-11-03 12:41:59 +110043 return ERR_PTR(-EFSCORRUPTED);
Darrick J. Wonga5155b82019-11-02 09:40:53 -070044 }
45
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020046 count = be32_to_cpu(aclp->acl_cnt);
Darrick J. Wonga5155b82019-11-02 09:40:53 -070047 if (count > max_entries || XFS_ACL_SIZE(count) != len) {
48 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, aclp,
49 len);
Christoph Hellwigfa8b18e2011-11-20 15:35:32 +000050 return ERR_PTR(-EFSCORRUPTED);
Darrick J. Wonga5155b82019-11-02 09:40:53 -070051 }
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020052
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 Hellwigba8adad2020-02-21 08:31:27 -080072 acl_e->e_uid = make_kuid(&init_user_ns,
73 be32_to_cpu(ace->ae_id));
Dwight Engen288bbe02013-08-15 14:07:59 -040074 break;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020075 case ACL_GROUP:
Christoph Hellwigba8adad2020-02-21 08:31:27 -080076 acl_e->e_gid = make_kgid(&init_user_ns,
77 be32_to_cpu(ace->ae_id));
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020078 break;
79 case ACL_USER_OBJ:
80 case ACL_GROUP_OBJ:
81 case ACL_MASK:
82 case ACL_OTHER:
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020083 break;
84 default:
85 goto fail;
86 }
87 }
88 return acl;
89
90fail:
91 posix_acl_release(acl);
92 return ERR_PTR(-EINVAL);
93}
94
95STATIC void
96xfs_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 Engen288bbe02013-08-15 14:07:59 -0400108 switch (acl_e->e_tag) {
109 case ACL_USER:
Christoph Hellwigba8adad2020-02-21 08:31:27 -0800110 ace->ae_id = cpu_to_be32(
111 from_kuid(&init_user_ns, acl_e->e_uid));
Dwight Engen288bbe02013-08-15 14:07:59 -0400112 break;
113 case ACL_GROUP:
Christoph Hellwigba8adad2020-02-21 08:31:27 -0800114 ace->ae_id = cpu_to_be32(
115 from_kgid(&init_user_ns, acl_e->e_gid));
Dwight Engen288bbe02013-08-15 14:07:59 -0400116 break;
117 default:
118 ace->ae_id = cpu_to_be32(ACL_UNDEFINED_ID);
119 break;
120 }
121
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200122 ace->ae_perm = cpu_to_be16(acl_e->e_perm);
123 }
124}
125
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200126struct posix_acl *
127xfs_get_acl(struct inode *inode, int type)
128{
Christoph Hellwige5171d72020-02-26 17:30:34 -0800129 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 Hellwigd5f0f492020-02-26 17:30:42 -0800134 .attr_filter = XFS_ATTR_ROOT,
Christoph Hellwige5171d72020-02-26 17:30:34 -0800135 .valuelen = XFS_ACL_MAX_SIZE(mp),
136 };
137 int error;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200138
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200139 trace_xfs_get_acl(ip);
140
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200141 switch (type) {
142 case ACL_TYPE_ACCESS:
Christoph Hellwige5171d72020-02-26 17:30:34 -0800143 args.name = SGI_ACL_FILE;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200144 break;
145 case ACL_TYPE_DEFAULT:
Christoph Hellwige5171d72020-02-26 17:30:34 -0800146 args.name = SGI_ACL_DEFAULT;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200147 break;
148 default:
Al Viro1cbd20d2009-06-09 13:29:39 -0400149 BUG();
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200150 }
Christoph Hellwige5171d72020-02-26 17:30:34 -0800151 args.namelen = strlen(args.name);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200152
Christoph Hellwigd49db182020-02-26 17:30:35 -0800153 /*
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 Hellwige5171d72020-02-26 17:30:34 -0800157 error = xfs_attr_get(&args);
Christoph Hellwigd49db182020-02-26 17:30:35 -0800158 if (!error) {
Christoph Hellwige5171d72020-02-26 17:30:34 -0800159 acl = xfs_acl_from_disk(mp, args.value, args.valuelen,
160 XFS_ACL_MAX_ENTRIES(mp));
Christoph Hellwigd49db182020-02-26 17:30:35 -0800161 } else if (error != -ENOATTR) {
162 acl = ERR_PTR(error);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200163 }
Christoph Hellwigd49db182020-02-26 17:30:35 -0800164
165 kmem_free(args.value);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200166 return acl;
167}
168
Jan Kara8ba35872017-06-26 08:48:18 -0700169int
170__xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200171{
Christoph Hellwiga2544622020-02-26 17:30:33 -0800172 struct xfs_inode *ip = XFS_I(inode);
173 struct xfs_da_args args = {
174 .dp = ip,
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800175 .attr_filter = XFS_ATTR_ROOT,
Christoph Hellwiga2544622020-02-26 17:30:33 -0800176 };
177 int error;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200178
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200179 switch (type) {
180 case ACL_TYPE_ACCESS:
Christoph Hellwiga2544622020-02-26 17:30:33 -0800181 args.name = SGI_ACL_FILE;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200182 break;
183 case ACL_TYPE_DEFAULT:
184 if (!S_ISDIR(inode->i_mode))
185 return acl ? -EACCES : 0;
Christoph Hellwiga2544622020-02-26 17:30:33 -0800186 args.name = SGI_ACL_DEFAULT;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200187 break;
188 default:
189 return -EINVAL;
190 }
Christoph Hellwiga2544622020-02-26 17:30:33 -0800191 args.namelen = strlen(args.name);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200192
193 if (acl) {
Christoph Hellwiged02d132020-02-26 17:30:44 -0800194 args.valuelen = XFS_ACL_SIZE(acl->a_count);
Carlos Maiolino8ca79df2020-09-01 11:47:12 -0700195 args.value = kvzalloc(args.valuelen, GFP_KERNEL);
Christoph Hellwiga2544622020-02-26 17:30:33 -0800196 if (!args.value)
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000197 return -ENOMEM;
Christoph Hellwiga2544622020-02-26 17:30:33 -0800198 xfs_acl_to_disk(args.value, acl);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200199 }
200
Christoph Hellwiga2544622020-02-26 17:30:33 -0800201 error = xfs_attr_set(&args);
202 kmem_free(args.value);
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800203
204 /*
205 * If the attribute didn't exist to start with that's fine.
206 */
207 if (!acl && error == -ENOATTR)
208 error = 0;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200209 if (!error)
Al Viro1cbd20d2009-06-09 13:29:39 -0400210 set_cached_acl(inode, type, acl);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200211 return error;
212}
213
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200214static int
Al Virod3fb6122011-07-23 18:37:50 -0400215xfs_set_mode(struct inode *inode, umode_t mode)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200216{
217 int error = 0;
218
219 if (mode != inode->i_mode) {
220 struct iattr iattr;
221
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000222 iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200223 iattr.ia_mode = mode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700224 iattr.ia_ctime = current_time(inode);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200225
Dave Chinner24513372014-06-25 14:58:08 +1000226 error = xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200227 }
228
229 return error;
230}
231
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200232int
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800233xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200234{
Dave Chinner67f2ffe2017-10-09 11:37:23 -0700235 umode_t mode;
236 bool set_mode = false;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000237 int error = 0;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200238
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800239 if (!acl)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200240 goto set_acl;
241
Jie Liu4ae69fe2014-02-07 15:26:11 +1100242 error = -E2BIG;
Dave Chinner0a8aa192013-06-05 12:09:10 +1000243 if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb)))
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800244 return error;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200245
246 if (type == ACL_TYPE_ACCESS) {
Jan Kara07393102016-09-19 17:39:09 +0200247 error = posix_acl_update_mode(inode, &mode, &acl);
248 if (error)
249 return error;
Dave Chinner67f2ffe2017-10-09 11:37:23 -0700250 set_mode = true;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200251 }
252
253 set_acl:
Dave Chinner67f2ffe2017-10-09 11:37:23 -0700254 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 Hellwigef14f0c2009-06-10 17:07:47 +0200267}
Christoph Hellwig5a3930e2020-02-26 17:30:41 -0800268
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 */
274void
275xfs_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}