xref: /linux/fs/nfsd/nfs3acl.c (revision 0853ac544c590880d797b04daa33fcb72b6be0e1)
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_inode_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_inode_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 	resp->acl_access = NULL;
80 	resp->acl_default = NULL;
81 	goto out;
82 }
83 
84 /*
85  * Set the Access and/or Default ACL of a file.
86  */
87 static __be32 nfsd3_proc_setacl(struct svc_rqst *rqstp)
88 {
89 	struct nfsd3_setaclargs *argp = rqstp->rq_argp;
90 	struct nfsd3_attrstat *resp = rqstp->rq_resp;
91 	struct inode *inode;
92 	svc_fh *fh;
93 	int error;
94 
95 	fh = fh_copy(&resp->fh, &argp->fh);
96 	resp->status = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR);
97 	if (resp->status != nfs_ok)
98 		goto out;
99 
100 	inode = d_inode(fh->fh_dentry);
101 
102 	error = fh_want_write(fh);
103 	if (error)
104 		goto out_errno;
105 
106 	inode_lock(inode);
107 
108 	error = set_posix_acl(&nop_mnt_idmap, fh->fh_dentry, ACL_TYPE_ACCESS,
109 			      argp->acl_access);
110 	if (error)
111 		goto out_drop_lock;
112 	error = set_posix_acl(&nop_mnt_idmap, fh->fh_dentry, ACL_TYPE_DEFAULT,
113 			      argp->acl_default);
114 
115 out_drop_lock:
116 	inode_unlock(inode);
117 	fh_drop_write(fh);
118 out_errno:
119 	resp->status = nfserrno(error);
120 out:
121 	/* argp->acl_{access,default} are released in nfs3svc_release_setacl. */
122 	return rpc_success;
123 }
124 
125 /*
126  * XDR decode functions
127  */
128 
129 static bool
130 nfs3svc_decode_getaclargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
131 {
132 	struct nfsd3_getaclargs *args = rqstp->rq_argp;
133 
134 	if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
135 		return false;
136 	if (xdr_stream_decode_u32(xdr, &args->mask) < 0)
137 		return false;
138 
139 	return true;
140 }
141 
142 static bool
143 nfs3svc_decode_setaclargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
144 {
145 	struct nfsd3_setaclargs *argp = rqstp->rq_argp;
146 
147 	if (!svcxdr_decode_nfs_fh3(xdr, &argp->fh))
148 		return false;
149 	if (xdr_stream_decode_u32(xdr, &argp->mask) < 0)
150 		return false;
151 	if (argp->mask & ~NFS_ACL_MASK)
152 		return false;
153 	if (!nfs_stream_decode_acl(xdr, NULL, (argp->mask & NFS_ACL) ?
154 				   &argp->acl_access : NULL))
155 		return false;
156 	if (!nfs_stream_decode_acl(xdr, NULL, (argp->mask & NFS_DFACL) ?
157 				   &argp->acl_default : NULL))
158 		return false;
159 
160 	return true;
161 }
162 
163 /*
164  * XDR encode functions
165  */
166 
167 /* GETACL */
168 static bool
169 nfs3svc_encode_getaclres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
170 {
171 	struct nfsd3_getaclres *resp = rqstp->rq_resp;
172 	struct dentry *dentry = resp->fh.fh_dentry;
173 	struct inode *inode;
174 
175 	if (!svcxdr_encode_nfsstat3(xdr, resp->status))
176 		return false;
177 	switch (resp->status) {
178 	case nfs_ok:
179 		inode = d_inode(dentry);
180 		if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
181 			return false;
182 		if (xdr_stream_encode_u32(xdr, resp->mask) < 0)
183 			return false;
184 
185 		if (!nfs_stream_encode_acl(xdr, inode, resp->acl_access,
186 					   resp->mask & NFS_ACL, 0))
187 			return false;
188 		if (!nfs_stream_encode_acl(xdr, inode, resp->acl_default,
189 					   resp->mask & NFS_DFACL,
190 					   NFS_ACL_DEFAULT))
191 			return false;
192 		break;
193 	default:
194 		if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
195 			return false;
196 	}
197 
198 	return true;
199 }
200 
201 /* SETACL */
202 static bool
203 nfs3svc_encode_setaclres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
204 {
205 	struct nfsd3_attrstat *resp = rqstp->rq_resp;
206 
207 	return svcxdr_encode_nfsstat3(xdr, resp->status) &&
208 		svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh);
209 }
210 
211 /*
212  * XDR release functions
213  */
214 static void nfs3svc_release_getacl(struct svc_rqst *rqstp)
215 {
216 	struct nfsd3_getaclres *resp = rqstp->rq_resp;
217 
218 	fh_put(&resp->fh);
219 	posix_acl_release(resp->acl_access);
220 	posix_acl_release(resp->acl_default);
221 }
222 
223 static void nfs3svc_release_setacl(struct svc_rqst *rqstp)
224 {
225 	struct nfsd3_setaclargs *argp = rqstp->rq_argp;
226 	struct nfsd3_attrstat *resp = rqstp->rq_resp;
227 
228 	fh_put(&resp->fh);
229 	posix_acl_release(argp->acl_access);
230 	posix_acl_release(argp->acl_default);
231 }
232 
233 #define ST 1		/* status*/
234 #define AT 21		/* attributes */
235 #define pAT (1+AT)	/* post attributes - conditional */
236 #define ACL (1+NFS_ACL_MAX_ENTRIES*3)  /* Access Control List */
237 
238 static const struct svc_procedure nfsd_acl_procedures3[3] = {
239 	[ACLPROC3_NULL] = {
240 		.pc_func = nfsd3_proc_null,
241 		.pc_decode = nfssvc_decode_voidarg,
242 		.pc_encode = nfssvc_encode_voidres,
243 		.pc_argsize = sizeof(struct nfsd_voidargs),
244 		.pc_argzero = sizeof(struct nfsd_voidargs),
245 		.pc_ressize = sizeof(struct nfsd_voidres),
246 		.pc_cachetype = RC_NOCACHE,
247 		.pc_xdrressize = ST,
248 		.pc_name = "NULL",
249 	},
250 	[ACLPROC3_GETACL] = {
251 		.pc_func = nfsd3_proc_getacl,
252 		.pc_decode = nfs3svc_decode_getaclargs,
253 		.pc_encode = nfs3svc_encode_getaclres,
254 		.pc_release = nfs3svc_release_getacl,
255 		.pc_argsize = sizeof(struct nfsd3_getaclargs),
256 		.pc_argzero = sizeof(struct nfsd3_getaclargs),
257 		.pc_ressize = sizeof(struct nfsd3_getaclres),
258 		.pc_cachetype = RC_NOCACHE,
259 		.pc_xdrressize = ST+1+2*(1+ACL),
260 		.pc_name = "GETACL",
261 	},
262 	[ACLPROC3_SETACL] = {
263 		.pc_func = nfsd3_proc_setacl,
264 		.pc_decode = nfs3svc_decode_setaclargs,
265 		.pc_encode = nfs3svc_encode_setaclres,
266 		.pc_release = nfs3svc_release_setacl,
267 		.pc_argsize = sizeof(struct nfsd3_setaclargs),
268 		.pc_argzero = 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 DEFINE_PER_CPU_ALIGNED(unsigned long,
277 			      nfsd_acl_count3[ARRAY_SIZE(nfsd_acl_procedures3)]);
278 const struct svc_version nfsd_acl_version3 = {
279 	.vs_vers	= 3,
280 	.vs_nproc	= ARRAY_SIZE(nfsd_acl_procedures3),
281 	.vs_proc	= nfsd_acl_procedures3,
282 	.vs_count	= nfsd_acl_count3,
283 	.vs_dispatch	= nfsd_dispatch,
284 	.vs_xdrsize	= NFS3_SVC_XDRSIZE,
285 };
286 
287