xref: /openbmc/linux/fs/nfs/nfs3acl.c (revision e2bd6bad)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/fs.h>
3 #include <linux/gfp.h>
4 #include <linux/nfs.h>
5 #include <linux/nfs3.h>
6 #include <linux/nfs_fs.h>
7 #include <linux/posix_acl_xattr.h>
8 #include <linux/nfsacl.h>
9 
10 #include "internal.h"
11 #include "nfs3_fs.h"
12 
13 #define NFSDBG_FACILITY	NFSDBG_PROC
14 
15 /*
16  * nfs3_prepare_get_acl, nfs3_complete_get_acl, nfs3_abort_get_acl: Helpers for
17  * caching get_acl results in a race-free way.  See fs/posix_acl.c:get_acl()
18  * for explanations.
19  */
20 static void nfs3_prepare_get_acl(struct posix_acl **p)
21 {
22 	struct posix_acl *sentinel = uncached_acl_sentinel(current);
23 
24 	if (cmpxchg(p, ACL_NOT_CACHED, sentinel) != ACL_NOT_CACHED) {
25 		/* Not the first reader or sentinel already in place. */
26 	}
27 }
28 
29 static void nfs3_complete_get_acl(struct posix_acl **p, struct posix_acl *acl)
30 {
31 	struct posix_acl *sentinel = uncached_acl_sentinel(current);
32 
33 	/* Only cache the ACL if our sentinel is still in place. */
34 	posix_acl_dup(acl);
35 	if (cmpxchg(p, sentinel, acl) != sentinel)
36 		posix_acl_release(acl);
37 }
38 
39 static void nfs3_abort_get_acl(struct posix_acl **p)
40 {
41 	struct posix_acl *sentinel = uncached_acl_sentinel(current);
42 
43 	/* Remove our sentinel upon failure. */
44 	cmpxchg(p, sentinel, ACL_NOT_CACHED);
45 }
46 
47 struct posix_acl *nfs3_get_acl(struct inode *inode, int type)
48 {
49 	struct nfs_server *server = NFS_SERVER(inode);
50 	struct page *pages[NFSACL_MAXPAGES] = { };
51 	struct nfs3_getaclargs args = {
52 		.fh = NFS_FH(inode),
53 		/* The xdr layer may allocate pages here. */
54 		.pages = pages,
55 	};
56 	struct nfs3_getaclres res = {
57 		NULL,
58 	};
59 	struct rpc_message msg = {
60 		.rpc_argp	= &args,
61 		.rpc_resp	= &res,
62 	};
63 	int status, count;
64 
65 	if (!nfs_server_capable(inode, NFS_CAP_ACLS))
66 		return ERR_PTR(-EOPNOTSUPP);
67 
68 	status = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
69 	if (status < 0)
70 		return ERR_PTR(status);
71 
72 	/*
73 	 * Only get the access acl when explicitly requested: We don't
74 	 * need it for access decisions, and only some applications use
75 	 * it. Applications which request the access acl first are not
76 	 * penalized from this optimization.
77 	 */
78 	if (type == ACL_TYPE_ACCESS)
79 		args.mask |= NFS_ACLCNT|NFS_ACL;
80 	if (S_ISDIR(inode->i_mode))
81 		args.mask |= NFS_DFACLCNT|NFS_DFACL;
82 	if (args.mask == 0)
83 		return NULL;
84 
85 	dprintk("NFS call getacl\n");
86 	msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_GETACL];
87 	res.fattr = nfs_alloc_fattr();
88 	if (res.fattr == NULL)
89 		return ERR_PTR(-ENOMEM);
90 
91 	if (args.mask & NFS_ACL)
92 		nfs3_prepare_get_acl(&inode->i_acl);
93 	if (args.mask & NFS_DFACL)
94 		nfs3_prepare_get_acl(&inode->i_default_acl);
95 
96 	status = rpc_call_sync(server->client_acl, &msg, 0);
97 	dprintk("NFS reply getacl: %d\n", status);
98 
99 	/* pages may have been allocated at the xdr layer. */
100 	for (count = 0; count < NFSACL_MAXPAGES && args.pages[count]; count++)
101 		__free_page(args.pages[count]);
102 
103 	switch (status) {
104 		case 0:
105 			status = nfs_refresh_inode(inode, res.fattr);
106 			break;
107 		case -EPFNOSUPPORT:
108 		case -EPROTONOSUPPORT:
109 			dprintk("NFS_V3_ACL extension not supported; disabling\n");
110 			server->caps &= ~NFS_CAP_ACLS;
111 			fallthrough;
112 		case -ENOTSUPP:
113 			status = -EOPNOTSUPP;
114 			goto getout;
115 		default:
116 			goto getout;
117 	}
118 	if ((args.mask & res.mask) != args.mask) {
119 		status = -EIO;
120 		goto getout;
121 	}
122 
123 	if (res.acl_access != NULL) {
124 		if ((posix_acl_equiv_mode(res.acl_access, NULL) == 0) ||
125 		    res.acl_access->a_count == 0) {
126 			posix_acl_release(res.acl_access);
127 			res.acl_access = NULL;
128 		}
129 	}
130 
131 	if (res.mask & NFS_ACL)
132 		nfs3_complete_get_acl(&inode->i_acl, res.acl_access);
133 	else
134 		forget_cached_acl(inode, ACL_TYPE_ACCESS);
135 
136 	if (res.mask & NFS_DFACL)
137 		nfs3_complete_get_acl(&inode->i_default_acl, res.acl_default);
138 	else
139 		forget_cached_acl(inode, ACL_TYPE_DEFAULT);
140 
141 	nfs_free_fattr(res.fattr);
142 	if (type == ACL_TYPE_ACCESS) {
143 		posix_acl_release(res.acl_default);
144 		return res.acl_access;
145 	} else {
146 		posix_acl_release(res.acl_access);
147 		return res.acl_default;
148 	}
149 
150 getout:
151 	nfs3_abort_get_acl(&inode->i_acl);
152 	nfs3_abort_get_acl(&inode->i_default_acl);
153 	posix_acl_release(res.acl_access);
154 	posix_acl_release(res.acl_default);
155 	nfs_free_fattr(res.fattr);
156 	return ERR_PTR(status);
157 }
158 
159 static int __nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
160 		struct posix_acl *dfacl)
161 {
162 	struct nfs_server *server = NFS_SERVER(inode);
163 	struct nfs_fattr *fattr;
164 	struct page *pages[NFSACL_MAXPAGES];
165 	struct nfs3_setaclargs args = {
166 		.inode = inode,
167 		.mask = NFS_ACL,
168 		.acl_access = acl,
169 		.pages = pages,
170 	};
171 	struct rpc_message msg = {
172 		.rpc_argp	= &args,
173 		.rpc_resp	= &fattr,
174 	};
175 	int status = 0;
176 
177 	if (acl == NULL && (!S_ISDIR(inode->i_mode) || dfacl == NULL))
178 		goto out;
179 
180 	status = -EOPNOTSUPP;
181 	if (!nfs_server_capable(inode, NFS_CAP_ACLS))
182 		goto out;
183 
184 	/* We are doing this here because XDR marshalling does not
185 	 * return any results, it BUGs. */
186 	status = -ENOSPC;
187 	if (acl != NULL && acl->a_count > NFS_ACL_MAX_ENTRIES)
188 		goto out;
189 	if (dfacl != NULL && dfacl->a_count > NFS_ACL_MAX_ENTRIES)
190 		goto out;
191 	if (S_ISDIR(inode->i_mode)) {
192 		args.mask |= NFS_DFACL;
193 		args.acl_default = dfacl;
194 		args.len = nfsacl_size(acl, dfacl);
195 	} else
196 		args.len = nfsacl_size(acl, NULL);
197 
198 	if (args.len > NFS_ACL_INLINE_BUFSIZE) {
199 		unsigned int npages = 1 + ((args.len - 1) >> PAGE_SHIFT);
200 
201 		status = -ENOMEM;
202 		do {
203 			args.pages[args.npages] = alloc_page(GFP_KERNEL);
204 			if (args.pages[args.npages] == NULL)
205 				goto out_freepages;
206 			args.npages++;
207 		} while (args.npages < npages);
208 	}
209 
210 	dprintk("NFS call setacl\n");
211 	status = -ENOMEM;
212 	fattr = nfs_alloc_fattr();
213 	if (fattr == NULL)
214 		goto out_freepages;
215 
216 	msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_SETACL];
217 	msg.rpc_resp = fattr;
218 	status = rpc_call_sync(server->client_acl, &msg, 0);
219 	nfs_access_zap_cache(inode);
220 	nfs_zap_acl_cache(inode);
221 	dprintk("NFS reply setacl: %d\n", status);
222 
223 	switch (status) {
224 		case 0:
225 			status = nfs_refresh_inode(inode, fattr);
226 			break;
227 		case -EPFNOSUPPORT:
228 		case -EPROTONOSUPPORT:
229 			dprintk("NFS_V3_ACL SETACL RPC not supported"
230 					"(will not retry)\n");
231 			server->caps &= ~NFS_CAP_ACLS;
232 			fallthrough;
233 		case -ENOTSUPP:
234 			status = -EOPNOTSUPP;
235 	}
236 	nfs_free_fattr(fattr);
237 out_freepages:
238 	while (args.npages != 0) {
239 		args.npages--;
240 		__free_page(args.pages[args.npages]);
241 	}
242 out:
243 	return status;
244 }
245 
246 int nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
247 		struct posix_acl *dfacl)
248 {
249 	int ret;
250 	ret = __nfs3_proc_setacls(inode, acl, dfacl);
251 	return (ret == -EOPNOTSUPP) ? 0 : ret;
252 
253 }
254 
255 int nfs3_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
256 		 struct posix_acl *acl, int type)
257 {
258 	struct posix_acl *orig = acl, *dfacl = NULL, *alloc;
259 	int status;
260 
261 	if (S_ISDIR(inode->i_mode)) {
262 		switch(type) {
263 		case ACL_TYPE_ACCESS:
264 			alloc = get_acl(inode, ACL_TYPE_DEFAULT);
265 			if (IS_ERR(alloc))
266 				goto fail;
267 			dfacl = alloc;
268 			break;
269 
270 		case ACL_TYPE_DEFAULT:
271 			alloc = get_acl(inode, ACL_TYPE_ACCESS);
272 			if (IS_ERR(alloc))
273 				goto fail;
274 			dfacl = acl;
275 			acl = alloc;
276 			break;
277 		}
278 	}
279 
280 	if (acl == NULL) {
281 		alloc = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
282 		if (IS_ERR(alloc))
283 			goto fail;
284 		acl = alloc;
285 	}
286 	status = __nfs3_proc_setacls(inode, acl, dfacl);
287 out:
288 	if (acl != orig)
289 		posix_acl_release(acl);
290 	if (dfacl != orig)
291 		posix_acl_release(dfacl);
292 	return status;
293 
294 fail:
295 	status = PTR_ERR(alloc);
296 	goto out;
297 }
298 
299 const struct xattr_handler *nfs3_xattr_handlers[] = {
300 	&posix_acl_access_xattr_handler,
301 	&posix_acl_default_xattr_handler,
302 	NULL,
303 };
304 
305 static int
306 nfs3_list_one_acl(struct inode *inode, int type, const char *name, void *data,
307 		size_t size, ssize_t *result)
308 {
309 	struct posix_acl *acl;
310 	char *p = data + *result;
311 
312 	acl = get_acl(inode, type);
313 	if (IS_ERR_OR_NULL(acl))
314 		return 0;
315 
316 	posix_acl_release(acl);
317 
318 	*result += strlen(name);
319 	*result += 1;
320 	if (!size)
321 		return 0;
322 	if (*result > size)
323 		return -ERANGE;
324 
325 	strcpy(p, name);
326 	return 0;
327 }
328 
329 ssize_t
330 nfs3_listxattr(struct dentry *dentry, char *data, size_t size)
331 {
332 	struct inode *inode = d_inode(dentry);
333 	ssize_t result = 0;
334 	int error;
335 
336 	error = nfs3_list_one_acl(inode, ACL_TYPE_ACCESS,
337 			XATTR_NAME_POSIX_ACL_ACCESS, data, size, &result);
338 	if (error)
339 		return error;
340 
341 	error = nfs3_list_one_acl(inode, ACL_TYPE_DEFAULT,
342 			XATTR_NAME_POSIX_ACL_DEFAULT, data, size, &result);
343 	if (error)
344 		return error;
345 	return result;
346 }
347