xref: /openbmc/linux/fs/nfsd/nfs3acl.c (revision b5f184fbdb03b4fcc1141de34dd5ec964ca5d99e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Process version 3 NFSACL requests.
4  *
5  * Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de>
6  */
7 
8 #include "nfsd.h"
9 /* FIXME: nfsacl.h is a broken header */
10 #include <linux/nfsacl.h>
11 #include <linux/gfp.h>
12 #include "cache.h"
13 #include "xdr3.h"
14 #include "vfs.h"
15 
16 /*
17  * NULL call.
18  */
19 static __be32
20 nfsd3_proc_null(struct svc_rqst *rqstp)
21 {
22 	return rpc_success;
23 }
24 
25 /*
26  * Get the Access and/or Default ACL of a file.
27  */
28 static __be32 nfsd3_proc_getacl(struct svc_rqst *rqstp)
29 {
30 	struct nfsd3_getaclargs *argp = rqstp->rq_argp;
31 	struct nfsd3_getaclres *resp = rqstp->rq_resp;
32 	struct posix_acl *acl;
33 	struct inode *inode;
34 	svc_fh *fh;
35 
36 	fh = fh_copy(&resp->fh, &argp->fh);
37 	resp->status = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
38 	if (resp->status != nfs_ok)
39 		goto out;
40 
41 	inode = d_inode(fh->fh_dentry);
42 
43 	if (argp->mask & ~NFS_ACL_MASK) {
44 		resp->status = nfserr_inval;
45 		goto out;
46 	}
47 	resp->mask = argp->mask;
48 
49 	if (resp->mask & (NFS_ACL|NFS_ACLCNT)) {
50 		acl = get_acl(inode, ACL_TYPE_ACCESS);
51 		if (acl == NULL) {
52 			/* Solaris returns the inode's minimum ACL. */
53 			acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
54 		}
55 		if (IS_ERR(acl)) {
56 			resp->status = nfserrno(PTR_ERR(acl));
57 			goto fail;
58 		}
59 		resp->acl_access = acl;
60 	}
61 	if (resp->mask & (NFS_DFACL|NFS_DFACLCNT)) {
62 		/* Check how Solaris handles requests for the Default ACL
63 		   of a non-directory! */
64 		acl = get_acl(inode, ACL_TYPE_DEFAULT);
65 		if (IS_ERR(acl)) {
66 			resp->status = nfserrno(PTR_ERR(acl));
67 			goto fail;
68 		}
69 		resp->acl_default = acl;
70 	}
71 
72 	/* resp->acl_{access,default} are released in nfs3svc_release_getacl. */
73 out:
74 	return rpc_success;
75 
76 fail:
77 	posix_acl_release(resp->acl_access);
78 	posix_acl_release(resp->acl_default);
79 	goto out;
80 }
81 
82 /*
83  * Set the Access and/or Default ACL of a file.
84  */
85 static __be32 nfsd3_proc_setacl(struct svc_rqst *rqstp)
86 {
87 	struct nfsd3_setaclargs *argp = rqstp->rq_argp;
88 	struct nfsd3_attrstat *resp = rqstp->rq_resp;
89 	struct inode *inode;
90 	svc_fh *fh;
91 	int error;
92 
93 	fh = fh_copy(&resp->fh, &argp->fh);
94 	resp->status = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR);
95 	if (resp->status != nfs_ok)
96 		goto out;
97 
98 	inode = d_inode(fh->fh_dentry);
99 
100 	error = fh_want_write(fh);
101 	if (error)
102 		goto out_errno;
103 
104 	fh_lock(fh);
105 
106 	error = set_posix_acl(inode, ACL_TYPE_ACCESS, argp->acl_access);
107 	if (error)
108 		goto out_drop_lock;
109 	error = set_posix_acl(inode, ACL_TYPE_DEFAULT, argp->acl_default);
110 
111 out_drop_lock:
112 	fh_unlock(fh);
113 	fh_drop_write(fh);
114 out_errno:
115 	resp->status = nfserrno(error);
116 out:
117 	/* argp->acl_{access,default} may have been allocated in
118 	   nfs3svc_decode_setaclargs. */
119 	posix_acl_release(argp->acl_access);
120 	posix_acl_release(argp->acl_default);
121 	return rpc_success;
122 }
123 
124 /*
125  * XDR decode functions
126  */
127 
128 static int nfs3svc_decode_getaclargs(struct svc_rqst *rqstp, __be32 *p)
129 {
130 	struct xdr_stream *xdr = &rqstp->rq_arg_stream;
131 	struct nfsd3_getaclargs *args = rqstp->rq_argp;
132 
133 	if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
134 		return 0;
135 	if (xdr_stream_decode_u32(xdr, &args->mask) < 0)
136 		return 0;
137 
138 	return 1;
139 }
140 
141 static int nfs3svc_decode_setaclargs(struct svc_rqst *rqstp, __be32 *p)
142 {
143 	struct xdr_stream *xdr = &rqstp->rq_arg_stream;
144 	struct nfsd3_setaclargs *argp = rqstp->rq_argp;
145 
146 	if (!svcxdr_decode_nfs_fh3(xdr, &argp->fh))
147 		return 0;
148 	if (xdr_stream_decode_u32(xdr, &argp->mask) < 0)
149 		return 0;
150 	if (argp->mask & ~NFS_ACL_MASK)
151 		return 0;
152 	if (!nfs_stream_decode_acl(xdr, NULL, (argp->mask & NFS_ACL) ?
153 				   &argp->acl_access : NULL))
154 		return 0;
155 	if (!nfs_stream_decode_acl(xdr, NULL, (argp->mask & NFS_DFACL) ?
156 				   &argp->acl_default : NULL))
157 		return 0;
158 
159 	return 1;
160 }
161 
162 /*
163  * XDR encode functions
164  */
165 
166 /* GETACL */
167 static int nfs3svc_encode_getaclres(struct svc_rqst *rqstp, __be32 *p)
168 {
169 	struct nfsd3_getaclres *resp = rqstp->rq_resp;
170 	struct dentry *dentry = resp->fh.fh_dentry;
171 
172 	*p++ = resp->status;
173 	p = nfs3svc_encode_post_op_attr(rqstp, p, &resp->fh);
174 	if (resp->status == 0 && dentry && d_really_is_positive(dentry)) {
175 		struct inode *inode = d_inode(dentry);
176 		struct kvec *head = rqstp->rq_res.head;
177 		unsigned int base;
178 		int n;
179 		int w;
180 
181 		*p++ = htonl(resp->mask);
182 		if (!xdr_ressize_check(rqstp, p))
183 			return 0;
184 		base = (char *)p - (char *)head->iov_base;
185 
186 		rqstp->rq_res.page_len = w = nfsacl_size(
187 			(resp->mask & NFS_ACL)   ? resp->acl_access  : NULL,
188 			(resp->mask & NFS_DFACL) ? resp->acl_default : NULL);
189 		while (w > 0) {
190 			if (!*(rqstp->rq_next_page++))
191 				return 0;
192 			w -= PAGE_SIZE;
193 		}
194 
195 		n = nfsacl_encode(&rqstp->rq_res, base, inode,
196 				  resp->acl_access,
197 				  resp->mask & NFS_ACL, 0);
198 		if (n > 0)
199 			n = nfsacl_encode(&rqstp->rq_res, base + n, inode,
200 					  resp->acl_default,
201 					  resp->mask & NFS_DFACL,
202 					  NFS_ACL_DEFAULT);
203 		if (n <= 0)
204 			return 0;
205 	} else
206 		if (!xdr_ressize_check(rqstp, p))
207 			return 0;
208 
209 	return 1;
210 }
211 
212 /* SETACL */
213 static int nfs3svc_encode_setaclres(struct svc_rqst *rqstp, __be32 *p)
214 {
215 	struct nfsd3_attrstat *resp = rqstp->rq_resp;
216 
217 	*p++ = resp->status;
218 	p = nfs3svc_encode_post_op_attr(rqstp, p, &resp->fh);
219 	return xdr_ressize_check(rqstp, p);
220 }
221 
222 /*
223  * XDR release functions
224  */
225 static void nfs3svc_release_getacl(struct svc_rqst *rqstp)
226 {
227 	struct nfsd3_getaclres *resp = rqstp->rq_resp;
228 
229 	fh_put(&resp->fh);
230 	posix_acl_release(resp->acl_access);
231 	posix_acl_release(resp->acl_default);
232 }
233 
234 struct nfsd3_voidargs { int dummy; };
235 
236 #define ST 1		/* status*/
237 #define AT 21		/* attributes */
238 #define pAT (1+AT)	/* post attributes - conditional */
239 #define ACL (1+NFS_ACL_MAX_ENTRIES*3)  /* Access Control List */
240 
241 static const struct svc_procedure nfsd_acl_procedures3[3] = {
242 	[ACLPROC3_NULL] = {
243 		.pc_func = nfsd3_proc_null,
244 		.pc_decode = nfssvc_decode_voidarg,
245 		.pc_encode = nfssvc_encode_voidres,
246 		.pc_argsize = sizeof(struct nfsd_voidargs),
247 		.pc_ressize = sizeof(struct nfsd_voidres),
248 		.pc_cachetype = RC_NOCACHE,
249 		.pc_xdrressize = ST,
250 		.pc_name = "NULL",
251 	},
252 	[ACLPROC3_GETACL] = {
253 		.pc_func = nfsd3_proc_getacl,
254 		.pc_decode = nfs3svc_decode_getaclargs,
255 		.pc_encode = nfs3svc_encode_getaclres,
256 		.pc_release = nfs3svc_release_getacl,
257 		.pc_argsize = sizeof(struct nfsd3_getaclargs),
258 		.pc_ressize = sizeof(struct nfsd3_getaclres),
259 		.pc_cachetype = RC_NOCACHE,
260 		.pc_xdrressize = ST+1+2*(1+ACL),
261 		.pc_name = "GETACL",
262 	},
263 	[ACLPROC3_SETACL] = {
264 		.pc_func = nfsd3_proc_setacl,
265 		.pc_decode = nfs3svc_decode_setaclargs,
266 		.pc_encode = nfs3svc_encode_setaclres,
267 		.pc_release = nfs3svc_release_fhandle,
268 		.pc_argsize = sizeof(struct nfsd3_setaclargs),
269 		.pc_ressize = sizeof(struct nfsd3_attrstat),
270 		.pc_cachetype = RC_NOCACHE,
271 		.pc_xdrressize = ST+pAT,
272 		.pc_name = "SETACL",
273 	},
274 };
275 
276 static unsigned int nfsd_acl_count3[ARRAY_SIZE(nfsd_acl_procedures3)];
277 const struct svc_version nfsd_acl_version3 = {
278 	.vs_vers	= 3,
279 	.vs_nproc	= 3,
280 	.vs_proc	= nfsd_acl_procedures3,
281 	.vs_count	= nfsd_acl_count3,
282 	.vs_dispatch	= nfsd_dispatch,
283 	.vs_xdrsize	= NFS3_SVC_XDRSIZE,
284 };
285 
286