blob: 13bca4a2f89d2680e74c4816bebf356ca1ec3846 [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
16#define RETURN_STATUS(st) { resp->status = (st); return (st); }
17
18/*
19 * NULL call.
20 */
Al Viro7111c662006-10-19 23:28:45 -070021static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +020022nfsd3_proc_null(struct svc_rqst *rqstp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000023{
24 return nfs_ok;
25}
26
27/*
28 * Get the Access and/or Default ACL of a file.
29 */
Christoph Hellwiga6beb732017-05-08 17:35:49 +020030static __be32 nfsd3_proc_getacl(struct svc_rqst *rqstp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000031{
Christoph Hellwiga6beb732017-05-08 17:35:49 +020032 struct nfsd3_getaclargs *argp = rqstp->rq_argp;
33 struct nfsd3_getaclres *resp = rqstp->rq_resp;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000034 struct posix_acl *acl;
Christoph Hellwig4ac72492013-12-20 05:16:55 -080035 struct inode *inode;
36 svc_fh *fh;
Al Viroc4d987b2006-10-19 23:29:00 -070037 __be32 nfserr = 0;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000038
39 fh = fh_copy(&resp->fh, &argp->fh);
Miklos Szeredi8837abc2008-06-16 13:20:29 +020040 nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
41 if (nfserr)
J. Bruce Fieldsac8587d2007-11-12 16:05:02 -050042 RETURN_STATUS(nfserr);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000043
David Howells2b0143b2015-03-17 22:25:59 +000044 inode = d_inode(fh->fh_dentry);
Christoph Hellwig4ac72492013-12-20 05:16:55 -080045
Kinglong Mee7b8f4582015-07-03 19:39:02 +080046 if (argp->mask & ~NFS_ACL_MASK)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000047 RETURN_STATUS(nfserr_inval);
48 resp->mask = argp->mask;
49
50 if (resp->mask & (NFS_ACL|NFS_ACLCNT)) {
Christoph Hellwig4ac72492013-12-20 05:16:55 -080051 acl = get_acl(inode, ACL_TYPE_ACCESS);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000052 if (acl == NULL) {
53 /* Solaris returns the inode's minimum ACL. */
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000054 acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
55 }
Kinglong Mee35e634b2014-07-09 21:54:16 +080056 if (IS_ERR(acl)) {
57 nfserr = nfserrno(PTR_ERR(acl));
58 goto fail;
59 }
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000060 resp->acl_access = acl;
61 }
62 if (resp->mask & (NFS_DFACL|NFS_DFACLCNT)) {
63 /* Check how Solaris handles requests for the Default ACL
64 of a non-directory! */
Christoph Hellwig4ac72492013-12-20 05:16:55 -080065 acl = get_acl(inode, ACL_TYPE_DEFAULT);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000066 if (IS_ERR(acl)) {
Christoph Hellwig4ac72492013-12-20 05:16:55 -080067 nfserr = nfserrno(PTR_ERR(acl));
68 goto fail;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000069 }
70 resp->acl_default = acl;
71 }
72
73 /* resp->acl_{access,default} are released in nfs3svc_release_getacl. */
74 RETURN_STATUS(0);
75
76fail:
77 posix_acl_release(resp->acl_access);
78 posix_acl_release(resp->acl_default);
79 RETURN_STATUS(nfserr);
80}
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;
Al Viroc4d987b2006-10-19 23:29:00 -070091 __be32 nfserr = 0;
Christoph Hellwig4ac72492013-12-20 05:16:55 -080092 int error;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000093
94 fh = fh_copy(&resp->fh, &argp->fh);
Miklos Szeredi8837abc2008-06-16 13:20:29 +020095 nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR);
Christoph Hellwig4ac72492013-12-20 05:16:55 -080096 if (nfserr)
97 goto out;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000098
David Howells2b0143b2015-03-17 22:25:59 +000099 inode = d_inode(fh->fh_dentry);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000100
Christoph Hellwig4ac72492013-12-20 05:16:55 -0800101 error = fh_want_write(fh);
102 if (error)
103 goto out_errno;
104
Ben Hutchings99965372016-06-22 19:43:35 +0100105 fh_lock(fh);
Christoph Hellwig4ac72492013-12-20 05:16:55 -0800106
Ben Hutchings99965372016-06-22 19:43:35 +0100107 error = set_posix_acl(inode, ACL_TYPE_ACCESS, argp->acl_access);
108 if (error)
109 goto out_drop_lock;
110 error = set_posix_acl(inode, ACL_TYPE_DEFAULT, argp->acl_default);
111
112out_drop_lock:
113 fh_unlock(fh);
Christoph Hellwig4ac72492013-12-20 05:16:55 -0800114 fh_drop_write(fh);
115out_errno:
116 nfserr = nfserrno(error);
117out:
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000118 /* argp->acl_{access,default} may have been allocated in
119 nfs3svc_decode_setaclargs. */
120 posix_acl_release(argp->acl_access);
121 posix_acl_release(argp->acl_default);
122 RETURN_STATUS(nfserr);
123}
124
125/*
126 * XDR decode functions
127 */
Christoph Hellwig026fec72017-05-08 19:01:48 +0200128static int nfs3svc_decode_getaclargs(struct svc_rqst *rqstp, __be32 *p)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000129{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200130 struct nfsd3_getaclargs *args = rqstp->rq_argp;
131
Benoit Tained40aa332014-05-22 16:32:30 +0200132 p = nfs3svc_decode_fh(p, &args->fh);
133 if (!p)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000134 return 0;
135 args->mask = ntohl(*p); p++;
136
137 return xdr_argsize_check(rqstp, p);
138}
139
140
Christoph Hellwig026fec72017-05-08 19:01:48 +0200141static int nfs3svc_decode_setaclargs(struct svc_rqst *rqstp, __be32 *p)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000142{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200143 struct nfsd3_setaclargs *args = rqstp->rq_argp;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000144 struct kvec *head = rqstp->rq_arg.head;
145 unsigned int base;
146 int n;
147
Benoit Tained40aa332014-05-22 16:32:30 +0200148 p = nfs3svc_decode_fh(p, &args->fh);
149 if (!p)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000150 return 0;
151 args->mask = ntohl(*p++);
Kinglong Mee7b8f4582015-07-03 19:39:02 +0800152 if (args->mask & ~NFS_ACL_MASK ||
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000153 !xdr_argsize_check(rqstp, p))
154 return 0;
155
156 base = (char *)p - (char *)head->iov_base;
157 n = nfsacl_decode(&rqstp->rq_arg, base, NULL,
158 (args->mask & NFS_ACL) ?
159 &args->acl_access : NULL);
160 if (n > 0)
161 n = nfsacl_decode(&rqstp->rq_arg, base + n, NULL,
162 (args->mask & NFS_DFACL) ?
163 &args->acl_default : NULL);
164 return (n > 0);
165}
166
167/*
168 * XDR encode functions
169 */
170
171/* GETACL */
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200172static int nfs3svc_encode_getaclres(struct svc_rqst *rqstp, __be32 *p)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000173{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200174 struct nfsd3_getaclres *resp = rqstp->rq_resp;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000175 struct dentry *dentry = resp->fh.fh_dentry;
176
177 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
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000221 p = nfs3svc_encode_post_op_attr(rqstp, p, &resp->fh);
222
223 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
238#define nfs3svc_decode_voidargs NULL
239#define nfs3svc_release_void NULL
240#define nfsd3_setaclres nfsd3_attrstat
241#define nfsd3_voidres nfsd3_voidargs
242struct nfsd3_voidargs { int dummy; };
243
Christoph Hellwigf7235b62017-05-08 17:59:13 +0200244#define PROC(name, argt, rest, relt, cache, respsize) \
245{ \
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200246 .pc_func = nfsd3_proc_##name, \
Christoph Hellwig026fec72017-05-08 19:01:48 +0200247 .pc_decode = nfs3svc_decode_##argt##args, \
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200248 .pc_encode = nfs3svc_encode_##rest##res, \
Christoph Hellwig85374882017-05-08 18:48:24 +0200249 .pc_release = nfs3svc_release_##relt, \
Christoph Hellwigf7235b62017-05-08 17:59:13 +0200250 .pc_argsize = sizeof(struct nfsd3_##argt##args), \
251 .pc_ressize = sizeof(struct nfsd3_##rest##res), \
252 .pc_cachetype = cache, \
253 .pc_xdrressize = respsize, \
254}
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000255
256#define ST 1 /* status*/
257#define AT 21 /* attributes */
258#define pAT (1+AT) /* post attributes - conditional */
259#define ACL (1+NFS_ACL_MAX_ENTRIES*3) /* Access Control List */
260
Christoph Hellwig860bda22017-05-12 16:11:49 +0200261static const struct svc_procedure nfsd_acl_procedures3[] = {
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000262 PROC(null, void, void, void, RC_NOCACHE, ST),
263 PROC(getacl, getacl, getacl, getacl, RC_NOCACHE, ST+1+2*(1+ACL)),
264 PROC(setacl, setacl, setacl, fhandle, RC_NOCACHE, ST+pAT),
265};
266
Christoph Hellwig7fd38af2017-05-08 23:40:27 +0200267static unsigned int nfsd_acl_count3[ARRAY_SIZE(nfsd_acl_procedures3)];
Christoph Hellwige9679182017-05-12 16:21:37 +0200268const struct svc_version nfsd_acl_version3 = {
269 .vs_vers = 3,
270 .vs_nproc = 3,
271 .vs_proc = nfsd_acl_procedures3,
272 .vs_count = nfsd_acl_count3,
273 .vs_dispatch = nfsd_dispatch,
274 .vs_xdrsize = NFS3_SVC_XDRSIZE,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000275};
276