blob: 7c30876a31a1b67e495d1447e3b12b4c1274fd7c [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
Ben Hutchings99965372016-06-22 19:43:35 +0100106 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
111out_drop_lock:
112 fh_unlock(fh);
Christoph Hellwig4ac72492013-12-20 05:16:55 -0800113 fh_drop_write(fh);
114out_errno:
Chuck Lever14168d62020-10-01 18:59:54 -0400115 resp->status = nfserrno(error);
Christoph Hellwig4ac72492013-12-20 05:16:55 -0800116out:
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000117 /* 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);
Chuck Levercc028a12020-10-02 15:52:44 -0400121 return rpc_success;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000122}
123
124/*
125 * XDR decode functions
126 */
Christoph Hellwig026fec72017-05-08 19:01:48 +0200127static int nfs3svc_decode_getaclargs(struct svc_rqst *rqstp, __be32 *p)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000128{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200129 struct nfsd3_getaclargs *args = rqstp->rq_argp;
130
Benoit Tained40aa332014-05-22 16:32:30 +0200131 p = nfs3svc_decode_fh(p, &args->fh);
132 if (!p)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000133 return 0;
134 args->mask = ntohl(*p); p++;
135
136 return xdr_argsize_check(rqstp, p);
137}
138
139
Christoph Hellwig026fec72017-05-08 19:01:48 +0200140static int nfs3svc_decode_setaclargs(struct svc_rqst *rqstp, __be32 *p)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000141{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200142 struct nfsd3_setaclargs *args = rqstp->rq_argp;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000143 struct kvec *head = rqstp->rq_arg.head;
144 unsigned int base;
145 int n;
146
Benoit Tained40aa332014-05-22 16:32:30 +0200147 p = nfs3svc_decode_fh(p, &args->fh);
148 if (!p)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000149 return 0;
150 args->mask = ntohl(*p++);
Kinglong Mee7b8f4582015-07-03 19:39:02 +0800151 if (args->mask & ~NFS_ACL_MASK ||
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000152 !xdr_argsize_check(rqstp, p))
153 return 0;
154
155 base = (char *)p - (char *)head->iov_base;
156 n = nfsacl_decode(&rqstp->rq_arg, base, NULL,
157 (args->mask & NFS_ACL) ?
158 &args->acl_access : NULL);
159 if (n > 0)
160 n = nfsacl_decode(&rqstp->rq_arg, base + n, NULL,
161 (args->mask & NFS_DFACL) ?
162 &args->acl_default : NULL);
163 return (n > 0);
164}
165
166/*
167 * XDR encode functions
168 */
169
170/* GETACL */
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200171static int nfs3svc_encode_getaclres(struct svc_rqst *rqstp, __be32 *p)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000172{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200173 struct nfsd3_getaclres *resp = rqstp->rq_resp;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000174 struct dentry *dentry = resp->fh.fh_dentry;
175
Chuck Levercc028a12020-10-02 15:52:44 -0400176 *p++ = resp->status;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000177 p = nfs3svc_encode_post_op_attr(rqstp, p, &resp->fh);
David Howells2b0143b2015-03-17 22:25:59 +0000178 if (resp->status == 0 && dentry && d_really_is_positive(dentry)) {
179 struct inode *inode = d_inode(dentry);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000180 struct kvec *head = rqstp->rq_res.head;
181 unsigned int base;
182 int n;
Jesper Juhl14d2b592006-12-08 02:39:40 -0800183 int w;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000184
185 *p++ = htonl(resp->mask);
186 if (!xdr_ressize_check(rqstp, p))
187 return 0;
188 base = (char *)p - (char *)head->iov_base;
189
Jesper Juhl14d2b592006-12-08 02:39:40 -0800190 rqstp->rq_res.page_len = w = nfsacl_size(
191 (resp->mask & NFS_ACL) ? resp->acl_access : NULL,
192 (resp->mask & NFS_DFACL) ? resp->acl_default : NULL);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000193 while (w > 0) {
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500194 if (!*(rqstp->rq_next_page++))
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000195 return 0;
196 w -= PAGE_SIZE;
197 }
198
199 n = nfsacl_encode(&rqstp->rq_res, base, inode,
200 resp->acl_access,
201 resp->mask & NFS_ACL, 0);
202 if (n > 0)
203 n = nfsacl_encode(&rqstp->rq_res, base + n, inode,
204 resp->acl_default,
205 resp->mask & NFS_DFACL,
206 NFS_ACL_DEFAULT);
207 if (n <= 0)
208 return 0;
209 } else
210 if (!xdr_ressize_check(rqstp, p))
211 return 0;
212
213 return 1;
214}
215
216/* SETACL */
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200217static int nfs3svc_encode_setaclres(struct svc_rqst *rqstp, __be32 *p)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000218{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200219 struct nfsd3_attrstat *resp = rqstp->rq_resp;
220
Chuck Levercc028a12020-10-02 15:52:44 -0400221 *p++ = resp->status;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000222 p = nfs3svc_encode_post_op_attr(rqstp, p, &resp->fh);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000223 return xdr_ressize_check(rqstp, p);
224}
225
226/*
227 * XDR release functions
228 */
Christoph Hellwig85374882017-05-08 18:48:24 +0200229static void nfs3svc_release_getacl(struct svc_rqst *rqstp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000230{
Christoph Hellwig85374882017-05-08 18:48:24 +0200231 struct nfsd3_getaclres *resp = rqstp->rq_resp;
232
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000233 fh_put(&resp->fh);
234 posix_acl_release(resp->acl_access);
235 posix_acl_release(resp->acl_default);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000236}
237
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000238struct nfsd3_voidargs { int dummy; };
239
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000240#define ST 1 /* status*/
241#define AT 21 /* attributes */
242#define pAT (1+AT) /* post attributes - conditional */
243#define ACL (1+NFS_ACL_MAX_ENTRIES*3) /* Access Control List */
244
Chuck Leverba1df792020-10-01 18:59:07 -0400245static const struct svc_procedure nfsd_acl_procedures3[3] = {
246 [ACLPROC3_NULL] = {
247 .pc_func = nfsd3_proc_null,
Chuck Lever788f7182020-11-05 14:48:29 -0500248 .pc_decode = nfssvc_decode_voidarg,
249 .pc_encode = nfssvc_encode_voidres,
250 .pc_argsize = sizeof(struct nfsd_voidargs),
251 .pc_ressize = sizeof(struct nfsd_voidres),
Chuck Leverba1df792020-10-01 18:59:07 -0400252 .pc_cachetype = RC_NOCACHE,
253 .pc_xdrressize = ST,
254 },
255 [ACLPROC3_GETACL] = {
256 .pc_func = nfsd3_proc_getacl,
257 .pc_decode = nfs3svc_decode_getaclargs,
258 .pc_encode = nfs3svc_encode_getaclres,
259 .pc_release = nfs3svc_release_getacl,
260 .pc_argsize = sizeof(struct nfsd3_getaclargs),
261 .pc_ressize = sizeof(struct nfsd3_getaclres),
262 .pc_cachetype = RC_NOCACHE,
263 .pc_xdrressize = ST+1+2*(1+ACL),
264 },
265 [ACLPROC3_SETACL] = {
266 .pc_func = nfsd3_proc_setacl,
267 .pc_decode = nfs3svc_decode_setaclargs,
268 .pc_encode = nfs3svc_encode_setaclres,
269 .pc_release = nfs3svc_release_fhandle,
270 .pc_argsize = sizeof(struct nfsd3_setaclargs),
271 .pc_ressize = sizeof(struct nfsd3_attrstat),
272 .pc_cachetype = RC_NOCACHE,
273 .pc_xdrressize = ST+pAT,
274 },
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000275};
276
Christoph Hellwig7fd38af2017-05-08 23:40:27 +0200277static unsigned int nfsd_acl_count3[ARRAY_SIZE(nfsd_acl_procedures3)];
Christoph Hellwige9679182017-05-12 16:21:37 +0200278const struct svc_version nfsd_acl_version3 = {
279 .vs_vers = 3,
280 .vs_nproc = 3,
281 .vs_proc = nfsd_acl_procedures3,
282 .vs_count = nfsd_acl_count3,
283 .vs_dispatch = nfsd_dispatch,
284 .vs_xdrsize = NFS3_SVC_XDRSIZE,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000285};
286