xref: /kernel/linux/linux-5.10/fs/xfs/xfs_acl.c (revision 8c2ecf20)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2008, Christoph Hellwig
4 * All Rights Reserved.
5 */
6#include "xfs.h"
7#include "xfs_shared.h"
8#include "xfs_format.h"
9#include "xfs_log_format.h"
10#include "xfs_trans_resv.h"
11#include "xfs_mount.h"
12#include "xfs_inode.h"
13#include "xfs_attr.h"
14#include "xfs_trace.h"
15#include "xfs_error.h"
16#include "xfs_acl.h"
17#include "xfs_da_format.h"
18#include "xfs_da_btree.h"
19
20#include <linux/posix_acl_xattr.h>
21
22/*
23 * Locking scheme:
24 *  - all ACL updates are protected by inode->i_mutex, which is taken before
25 *    calling into this file.
26 */
27
28STATIC struct posix_acl *
29xfs_acl_from_disk(
30	struct xfs_mount	*mp,
31	const struct xfs_acl	*aclp,
32	int			len,
33	int			max_entries)
34{
35	struct posix_acl_entry *acl_e;
36	struct posix_acl *acl;
37	const struct xfs_acl_entry *ace;
38	unsigned int count, i;
39
40	if (len < sizeof(*aclp)) {
41		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, aclp,
42				len);
43		return ERR_PTR(-EFSCORRUPTED);
44	}
45
46	count = be32_to_cpu(aclp->acl_cnt);
47	if (count > max_entries || XFS_ACL_SIZE(count) != len) {
48		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, aclp,
49				len);
50		return ERR_PTR(-EFSCORRUPTED);
51	}
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:
72			acl_e->e_uid = make_kuid(&init_user_ns,
73						 be32_to_cpu(ace->ae_id));
74			break;
75		case ACL_GROUP:
76			acl_e->e_gid = make_kgid(&init_user_ns,
77						 be32_to_cpu(ace->ae_id));
78			break;
79		case ACL_USER_OBJ:
80		case ACL_GROUP_OBJ:
81		case ACL_MASK:
82		case ACL_OTHER:
83			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);
108		switch (acl_e->e_tag) {
109		case ACL_USER:
110			ace->ae_id = cpu_to_be32(
111					from_kuid(&init_user_ns, acl_e->e_uid));
112			break;
113		case ACL_GROUP:
114			ace->ae_id = cpu_to_be32(
115					from_kgid(&init_user_ns, acl_e->e_gid));
116			break;
117		default:
118			ace->ae_id = cpu_to_be32(ACL_UNDEFINED_ID);
119			break;
120		}
121
122		ace->ae_perm = cpu_to_be16(acl_e->e_perm);
123	}
124}
125
126struct posix_acl *
127xfs_get_acl(struct inode *inode, int type)
128{
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,
134		.attr_filter	= XFS_ATTR_ROOT,
135		.valuelen	= XFS_ACL_MAX_SIZE(mp),
136	};
137	int			error;
138
139	trace_xfs_get_acl(ip);
140
141	switch (type) {
142	case ACL_TYPE_ACCESS:
143		args.name = SGI_ACL_FILE;
144		break;
145	case ACL_TYPE_DEFAULT:
146		args.name = SGI_ACL_DEFAULT;
147		break;
148	default:
149		BUG();
150	}
151	args.namelen = strlen(args.name);
152
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	 */
157	error = xfs_attr_get(&args);
158	if (!error) {
159		acl = xfs_acl_from_disk(mp, args.value, args.valuelen,
160					XFS_ACL_MAX_ENTRIES(mp));
161	} else if (error != -ENOATTR) {
162		acl = ERR_PTR(error);
163	}
164
165	kmem_free(args.value);
166	return acl;
167}
168
169int
170__xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
171{
172	struct xfs_inode	*ip = XFS_I(inode);
173	struct xfs_da_args	args = {
174		.dp		= ip,
175		.attr_filter	= XFS_ATTR_ROOT,
176	};
177	int			error;
178
179	switch (type) {
180	case ACL_TYPE_ACCESS:
181		args.name = SGI_ACL_FILE;
182		break;
183	case ACL_TYPE_DEFAULT:
184		if (!S_ISDIR(inode->i_mode))
185			return acl ? -EACCES : 0;
186		args.name = SGI_ACL_DEFAULT;
187		break;
188	default:
189		return -EINVAL;
190	}
191	args.namelen = strlen(args.name);
192
193	if (acl) {
194		args.valuelen = XFS_ACL_SIZE(acl->a_count);
195		args.value = kvzalloc(args.valuelen, GFP_KERNEL);
196		if (!args.value)
197			return -ENOMEM;
198		xfs_acl_to_disk(args.value, acl);
199	}
200
201	error = xfs_attr_set(&args);
202	kmem_free(args.value);
203
204	/*
205	 * If the attribute didn't exist to start with that's fine.
206	 */
207	if (!acl && error == -ENOATTR)
208		error = 0;
209	if (!error)
210		set_cached_acl(inode, type, acl);
211	return error;
212}
213
214static int
215xfs_set_mode(struct inode *inode, umode_t mode)
216{
217	int error = 0;
218
219	if (mode != inode->i_mode) {
220		struct iattr iattr;
221
222		iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
223		iattr.ia_mode = mode;
224		iattr.ia_ctime = current_time(inode);
225
226		error = xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL);
227	}
228
229	return error;
230}
231
232int
233xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
234{
235	umode_t mode;
236	bool set_mode = false;
237	int error = 0;
238
239	if (!acl)
240		goto set_acl;
241
242	error = -E2BIG;
243	if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb)))
244		return error;
245
246	if (type == ACL_TYPE_ACCESS) {
247		error = posix_acl_update_mode(inode, &mode, &acl);
248		if (error)
249			return error;
250		set_mode = true;
251	}
252
253 set_acl:
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;
267}
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 */
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}
284