blob: 35b2ebda14dacc64829635002fcb49bec2c82041 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002/*
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00003 * Process version 3 NFSACL requests.
4 *
5 * Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de>
6 */
7
Boaz Harrosh9a74af22009-12-03 20:30:56 +02008#include "nfsd.h"
9/* FIXME: nfsacl.h is a broken header */
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000010#include <linux/nfsacl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/gfp.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020012#include "cache.h"
13#include "xdr3.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050014#include "vfs.h"
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000015
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000016/*
17 * NULL call.
18 */
Al Viro7111c662006-10-19 23:28:45 -070019static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +020020nfsd3_proc_null(struct svc_rqst *rqstp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000021{
Chuck Levercc028a12020-10-02 15:52:44 -040022 return rpc_success;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000023}
24
25/*
26 * Get the Access and/or Default ACL of a file.
27 */
Christoph Hellwiga6beb732017-05-08 17:35:49 +020028static __be32 nfsd3_proc_getacl(struct svc_rqst *rqstp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000029{
Christoph Hellwiga6beb732017-05-08 17:35:49 +020030 struct nfsd3_getaclargs *argp = rqstp->rq_argp;
31 struct nfsd3_getaclres *resp = rqstp->rq_resp;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000032 struct posix_acl *acl;
Christoph Hellwig4ac72492013-12-20 05:16:55 -080033 struct inode *inode;
34 svc_fh *fh;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000035
36 fh = fh_copy(&resp->fh, &argp->fh);
Chuck Lever14168d62020-10-01 18:59:54 -040037 resp->status = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
38 if (resp->status != nfs_ok)
39 goto out;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000040
David Howells2b0143b2015-03-17 22:25:59 +000041 inode = d_inode(fh->fh_dentry);
Christoph Hellwig4ac72492013-12-20 05:16:55 -080042
Chuck Lever14168d62020-10-01 18:59:54 -040043 if (argp->mask & ~NFS_ACL_MASK) {
44 resp->status = nfserr_inval;
45 goto out;
46 }
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000047 resp->mask = argp->mask;
48
49 if (resp->mask & (NFS_ACL|NFS_ACLCNT)) {
Christoph Hellwig4ac72492013-12-20 05:16:55 -080050 acl = get_acl(inode, ACL_TYPE_ACCESS);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000051 if (acl == NULL) {
52 /* Solaris returns the inode's minimum ACL. */
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000053 acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
54 }
Kinglong Mee35e634b2014-07-09 21:54:16 +080055 if (IS_ERR(acl)) {
Chuck Lever14168d62020-10-01 18:59:54 -040056 resp->status = nfserrno(PTR_ERR(acl));
Kinglong Mee35e634b2014-07-09 21:54:16 +080057 goto fail;
58 }
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000059 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! */
Christoph Hellwig4ac72492013-12-20 05:16:55 -080064 acl = get_acl(inode, ACL_TYPE_DEFAULT);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000065 if (IS_ERR(acl)) {
Chuck Lever14168d62020-10-01 18:59:54 -040066 resp->status = nfserrno(PTR_ERR(acl));
Christoph Hellwig4ac72492013-12-20 05:16:55 -080067 goto fail;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000068 }
69 resp->acl_default = acl;
70 }
71
72 /* resp->acl_{access,default} are released in nfs3svc_release_getacl. */
Chuck Lever14168d62020-10-01 18:59:54 -040073out:
Chuck Levercc028a12020-10-02 15:52:44 -040074 return rpc_success;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000075
76fail:
77 posix_acl_release(resp->acl_access);
78 posix_acl_release(resp->acl_default);
Chuck Lever14168d62020-10-01 18:59:54 -040079 goto out;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000080}
81
82/*
83 * Set the Access and/or Default ACL of a file.
84 */
Christoph Hellwiga6beb732017-05-08 17:35:49 +020085static __be32 nfsd3_proc_setacl(struct svc_rqst *rqstp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000086{
Christoph Hellwiga6beb732017-05-08 17:35:49 +020087 struct nfsd3_setaclargs *argp = rqstp->rq_argp;
88 struct nfsd3_attrstat *resp = rqstp->rq_resp;
Christoph Hellwig4ac72492013-12-20 05:16:55 -080089 struct inode *inode;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000090 svc_fh *fh;
Christoph Hellwig4ac72492013-12-20 05:16:55 -080091 int error;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000092
93 fh = fh_copy(&resp->fh, &argp->fh);
Chuck Lever14168d62020-10-01 18:59:54 -040094 resp->status = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR);
95 if (resp->status != nfs_ok)
Christoph Hellwig4ac72492013-12-20 05:16:55 -080096 goto out;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000097
David Howells2b0143b2015-03-17 22:25:59 +000098 inode = d_inode(fh->fh_dentry);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000099
Christoph Hellwig4ac72492013-12-20 05:16:55 -0800100 error = fh_want_write(fh);
101 if (error)
102 goto out_errno;
103
Ben Hutchings99965372016-06-22 19:43:35 +0100104 fh_lock(fh);
Christoph Hellwig4ac72492013-12-20 05:16:55 -0800105
Christian Braunere65ce2a2021-01-21 14:19:27 +0100106 error = set_posix_acl(&init_user_ns, inode, ACL_TYPE_ACCESS,
107 argp->acl_access);
Ben Hutchings99965372016-06-22 19:43:35 +0100108 if (error)
109 goto out_drop_lock;
Christian Braunere65ce2a2021-01-21 14:19:27 +0100110 error = set_posix_acl(&init_user_ns, inode, ACL_TYPE_DEFAULT,
111 argp->acl_default);
Ben Hutchings99965372016-06-22 19:43:35 +0100112
113out_drop_lock:
114 fh_unlock(fh);
Christoph Hellwig4ac72492013-12-20 05:16:55 -0800115 fh_drop_write(fh);
116out_errno:
Chuck Lever14168d62020-10-01 18:59:54 -0400117 resp->status = nfserrno(error);
Christoph Hellwig4ac72492013-12-20 05:16:55 -0800118out:
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000119 /* argp->acl_{access,default} may have been allocated in
120 nfs3svc_decode_setaclargs. */
121 posix_acl_release(argp->acl_access);
122 posix_acl_release(argp->acl_default);
Chuck Levercc028a12020-10-02 15:52:44 -0400123 return rpc_success;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000124}
125
126/*
127 * XDR decode functions
128 */
Chuck Lever05027ea2020-11-17 11:52:04 -0500129
Chuck Leverc44b31c2021-10-12 11:57:28 -0400130static bool
Chuck Lever16c66362021-10-12 11:57:22 -0400131nfs3svc_decode_getaclargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000132{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200133 struct nfsd3_getaclargs *args = rqstp->rq_argp;
134
Chuck Lever05027ea2020-11-17 11:52:04 -0500135 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400136 return false;
Chuck Lever05027ea2020-11-17 11:52:04 -0500137 if (xdr_stream_decode_u32(xdr, &args->mask) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400138 return false;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000139
Chuck Leverc44b31c2021-10-12 11:57:28 -0400140 return true;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000141}
142
Chuck Leverc44b31c2021-10-12 11:57:28 -0400143static bool
Chuck Lever16c66362021-10-12 11:57:22 -0400144nfs3svc_decode_setaclargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000145{
Chuck Lever68519ff2020-11-17 11:56:26 -0500146 struct nfsd3_setaclargs *argp = rqstp->rq_argp;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000147
Chuck Lever68519ff2020-11-17 11:56:26 -0500148 if (!svcxdr_decode_nfs_fh3(xdr, &argp->fh))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400149 return false;
Chuck Lever68519ff2020-11-17 11:56:26 -0500150 if (xdr_stream_decode_u32(xdr, &argp->mask) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400151 return false;
Chuck Lever68519ff2020-11-17 11:56:26 -0500152 if (argp->mask & ~NFS_ACL_MASK)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400153 return false;
Chuck Lever68519ff2020-11-17 11:56:26 -0500154 if (!nfs_stream_decode_acl(xdr, NULL, (argp->mask & NFS_ACL) ?
155 &argp->acl_access : NULL))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400156 return false;
Chuck Lever68519ff2020-11-17 11:56:26 -0500157 if (!nfs_stream_decode_acl(xdr, NULL, (argp->mask & NFS_DFACL) ?
158 &argp->acl_default : NULL))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400159 return false;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000160
Chuck Leverc44b31c2021-10-12 11:57:28 -0400161 return true;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000162}
163
164/*
165 * XDR encode functions
166 */
167
168/* GETACL */
Chuck Lever130e2052021-10-13 10:41:13 -0400169static bool
Chuck Leverfda49442021-10-13 10:41:06 -0400170nfs3svc_encode_getaclres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000171{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200172 struct nfsd3_getaclres *resp = rqstp->rq_resp;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000173 struct dentry *dentry = resp->fh.fh_dentry;
Chuck Lever20798df2020-11-18 16:11:42 -0500174 struct kvec *head = rqstp->rq_res.head;
J. Bruce Fieldsab1016d2021-07-01 20:06:56 -0400175 struct inode *inode;
Chuck Lever20798df2020-11-18 16:11:42 -0500176 unsigned int base;
177 int n;
178 int w;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000179
Chuck Lever20798df2020-11-18 16:11:42 -0500180 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
Chuck Lever130e2052021-10-13 10:41:13 -0400181 return false;
Chuck Lever20798df2020-11-18 16:11:42 -0500182 switch (resp->status) {
183 case nfs_ok:
J. Bruce Fieldsab1016d2021-07-01 20:06:56 -0400184 inode = d_inode(dentry);
Chuck Lever20798df2020-11-18 16:11:42 -0500185 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
Chuck Lever130e2052021-10-13 10:41:13 -0400186 return false;
Chuck Lever20798df2020-11-18 16:11:42 -0500187 if (xdr_stream_encode_u32(xdr, resp->mask) < 0)
Chuck Lever130e2052021-10-13 10:41:13 -0400188 return false;
Chuck Lever20798df2020-11-18 16:11:42 -0500189
190 base = (char *)xdr->p - (char *)head->iov_base;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000191
Jesper Juhl14d2b592006-12-08 02:39:40 -0800192 rqstp->rq_res.page_len = w = nfsacl_size(
193 (resp->mask & NFS_ACL) ? resp->acl_access : NULL,
194 (resp->mask & NFS_DFACL) ? resp->acl_default : NULL);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000195 while (w > 0) {
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500196 if (!*(rqstp->rq_next_page++))
Chuck Lever130e2052021-10-13 10:41:13 -0400197 return false;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000198 w -= PAGE_SIZE;
199 }
200
201 n = nfsacl_encode(&rqstp->rq_res, base, inode,
202 resp->acl_access,
203 resp->mask & NFS_ACL, 0);
204 if (n > 0)
205 n = nfsacl_encode(&rqstp->rq_res, base + n, inode,
206 resp->acl_default,
207 resp->mask & NFS_DFACL,
208 NFS_ACL_DEFAULT);
209 if (n <= 0)
Chuck Lever130e2052021-10-13 10:41:13 -0400210 return false;
Chuck Lever20798df2020-11-18 16:11:42 -0500211 break;
212 default:
213 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
Chuck Lever130e2052021-10-13 10:41:13 -0400214 return false;
Chuck Lever20798df2020-11-18 16:11:42 -0500215 }
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000216
Chuck Lever130e2052021-10-13 10:41:13 -0400217 return true;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000218}
219
220/* SETACL */
Chuck Lever130e2052021-10-13 10:41:13 -0400221static bool
Chuck Leverfda49442021-10-13 10:41:06 -0400222nfs3svc_encode_setaclres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000223{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200224 struct nfsd3_attrstat *resp = rqstp->rq_resp;
225
Chuck Lever15e432b2020-11-18 16:21:24 -0500226 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
227 svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000228}
229
230/*
231 * XDR release functions
232 */
Christoph Hellwig85374882017-05-08 18:48:24 +0200233static void nfs3svc_release_getacl(struct svc_rqst *rqstp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000234{
Christoph Hellwig85374882017-05-08 18:48:24 +0200235 struct nfsd3_getaclres *resp = rqstp->rq_resp;
236
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000237 fh_put(&resp->fh);
238 posix_acl_release(resp->acl_access);
239 posix_acl_release(resp->acl_default);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000240}
241
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000242struct nfsd3_voidargs { int dummy; };
243
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000244#define ST 1 /* status*/
245#define AT 21 /* attributes */
246#define pAT (1+AT) /* post attributes - conditional */
247#define ACL (1+NFS_ACL_MAX_ENTRIES*3) /* Access Control List */
248
Chuck Leverba1df792020-10-01 18:59:07 -0400249static const struct svc_procedure nfsd_acl_procedures3[3] = {
250 [ACLPROC3_NULL] = {
251 .pc_func = nfsd3_proc_null,
Chuck Lever788f7182020-11-05 14:48:29 -0500252 .pc_decode = nfssvc_decode_voidarg,
253 .pc_encode = nfssvc_encode_voidres,
254 .pc_argsize = sizeof(struct nfsd_voidargs),
255 .pc_ressize = sizeof(struct nfsd_voidres),
Chuck Leverba1df792020-10-01 18:59:07 -0400256 .pc_cachetype = RC_NOCACHE,
257 .pc_xdrressize = ST,
Chuck Lever2289e872020-09-17 17:22:49 -0400258 .pc_name = "NULL",
Chuck Leverba1df792020-10-01 18:59:07 -0400259 },
260 [ACLPROC3_GETACL] = {
261 .pc_func = nfsd3_proc_getacl,
262 .pc_decode = nfs3svc_decode_getaclargs,
263 .pc_encode = nfs3svc_encode_getaclres,
264 .pc_release = nfs3svc_release_getacl,
265 .pc_argsize = sizeof(struct nfsd3_getaclargs),
266 .pc_ressize = sizeof(struct nfsd3_getaclres),
267 .pc_cachetype = RC_NOCACHE,
268 .pc_xdrressize = ST+1+2*(1+ACL),
Chuck Lever2289e872020-09-17 17:22:49 -0400269 .pc_name = "GETACL",
Chuck Leverba1df792020-10-01 18:59:07 -0400270 },
271 [ACLPROC3_SETACL] = {
272 .pc_func = nfsd3_proc_setacl,
273 .pc_decode = nfs3svc_decode_setaclargs,
274 .pc_encode = nfs3svc_encode_setaclres,
275 .pc_release = nfs3svc_release_fhandle,
276 .pc_argsize = sizeof(struct nfsd3_setaclargs),
277 .pc_ressize = sizeof(struct nfsd3_attrstat),
278 .pc_cachetype = RC_NOCACHE,
279 .pc_xdrressize = ST+pAT,
Chuck Lever2289e872020-09-17 17:22:49 -0400280 .pc_name = "SETACL",
Chuck Leverba1df792020-10-01 18:59:07 -0400281 },
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000282};
283
Christoph Hellwig7fd38af2017-05-08 23:40:27 +0200284static unsigned int nfsd_acl_count3[ARRAY_SIZE(nfsd_acl_procedures3)];
Christoph Hellwige9679182017-05-12 16:21:37 +0200285const struct svc_version nfsd_acl_version3 = {
286 .vs_vers = 3,
287 .vs_nproc = 3,
288 .vs_proc = nfsd_acl_procedures3,
289 .vs_count = nfsd_acl_count3,
290 .vs_dispatch = nfsd_dispatch,
291 .vs_xdrsize = NFS3_SVC_XDRSIZE,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000292};
293