1 /*
2  * linux/fs/nfsd/xdr.c
3  *
4  * XDR support for nfsd
5  *
6  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7  */
8 
9 #include <linux/types.h>
10 #include <linux/sched.h>
11 #include <linux/nfs.h>
12 
13 #include <linux/sunrpc/xdr.h>
14 #include <linux/sunrpc/svc.h>
15 #include <linux/nfsd/nfsd.h>
16 #include <linux/nfsd/xdr.h>
17 
18 #define NFSDDBG_FACILITY		NFSDDBG_XDR
19 
20 
21 #ifdef NFSD_OPTIMIZE_SPACE
22 # define inline
23 #endif
24 
25 /*
26  * Mapping of S_IF* types to NFS file types
27  */
28 static u32	nfs_ftypes[] = {
29 	NFNON,  NFCHR,  NFCHR, NFBAD,
30 	NFDIR,  NFBAD,  NFBLK, NFBAD,
31 	NFREG,  NFBAD,  NFLNK, NFBAD,
32 	NFSOCK, NFBAD,  NFLNK, NFBAD,
33 };
34 
35 
36 /*
37  * XDR functions for basic NFS types
38  */
39 static inline u32 *
decode_fh(u32 * p,struct svc_fh * fhp)40 decode_fh(u32 *p, struct svc_fh *fhp)
41 {
42 	fh_init(fhp, NFS_FHSIZE);
43 	memcpy(&fhp->fh_handle.fh_base, p, NFS_FHSIZE);
44 	fhp->fh_handle.fh_size = NFS_FHSIZE;
45 
46 	/* FIXME: Look up export pointer here and verify
47 	 * Sun Secure RPC if requested */
48 	return p + (NFS_FHSIZE >> 2);
49 }
50 
51 static inline u32 *
encode_fh(u32 * p,struct svc_fh * fhp)52 encode_fh(u32 *p, struct svc_fh *fhp)
53 {
54 	memcpy(p, &fhp->fh_handle.fh_base, NFS_FHSIZE);
55 	return p + (NFS_FHSIZE>> 2);
56 }
57 
58 /*
59  * Decode a file name and make sure that the path contains
60  * no slashes or null bytes.
61  */
62 static inline u32 *
decode_filename(u32 * p,char ** namp,int * lenp)63 decode_filename(u32 *p, char **namp, int *lenp)
64 {
65 	char		*name;
66 	int		i;
67 
68 	if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS_MAXNAMLEN)) != NULL) {
69 		for (i = 0, name = *namp; i < *lenp; i++, name++) {
70 			if (*name == '\0' || *name == '/')
71 				return NULL;
72 		}
73 	}
74 
75 	return p;
76 }
77 
78 static inline u32 *
decode_pathname(u32 * p,char ** namp,int * lenp)79 decode_pathname(u32 *p, char **namp, int *lenp)
80 {
81 	char		*name;
82 	int		i;
83 
84 	if ((p = xdr_decode_string(p, namp, lenp, NFS_MAXPATHLEN)) != NULL) {
85 		for (i = 0, name = *namp; i < *lenp; i++, name++) {
86 			if (*name == '\0')
87 				return NULL;
88 		}
89 	}
90 
91 	return p;
92 }
93 
94 static inline u32 *
decode_sattr(u32 * p,struct iattr * iap)95 decode_sattr(u32 *p, struct iattr *iap)
96 {
97 	u32	tmp, tmp1;
98 
99 	iap->ia_valid = 0;
100 
101 	/* Sun client bug compatibility check: some sun clients seem to
102 	 * put 0xffff in the mode field when they mean 0xffffffff.
103 	 * Quoting the 4.4BSD nfs server code: Nah nah nah nah na nah.
104 	 */
105 	if ((tmp = ntohl(*p++)) != (u32)-1 && tmp != 0xffff) {
106 		iap->ia_valid |= ATTR_MODE;
107 		iap->ia_mode = tmp;
108 	}
109 	if ((tmp = ntohl(*p++)) != (u32)-1) {
110 		iap->ia_valid |= ATTR_UID;
111 		iap->ia_uid = tmp;
112 	}
113 	if ((tmp = ntohl(*p++)) != (u32)-1) {
114 		iap->ia_valid |= ATTR_GID;
115 		iap->ia_gid = tmp;
116 	}
117 	if ((tmp = ntohl(*p++)) != (u32)-1) {
118 		iap->ia_valid |= ATTR_SIZE;
119 		iap->ia_size = tmp;
120 	}
121 	tmp  = ntohl(*p++); tmp1 = ntohl(*p++);
122 	if (tmp != (u32)-1 && tmp1 != (u32)-1) {
123 		iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
124 		iap->ia_atime = tmp;
125 	}
126 	tmp  = ntohl(*p++); tmp1 = ntohl(*p++);
127 	if (tmp != (u32)-1 && tmp1 != (u32)-1) {
128 		iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
129 		iap->ia_mtime = tmp;
130 		/*
131 		 * Passing the invalid value useconds=1000000 for mtime
132 		 * is a Sun convention for "set both mtime and atime to
133 		 * current server time".  It's needed to make permissions
134 		 * checks for the "touch" program across v2 mounts to
135 		 * Solaris and Irix boxes work correctly. See description of
136 		 * sattr in section 6.1 of "NFS Illustrated" by
137 		 * Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5
138 		 */
139 		if (tmp1 == 1000000)
140 			iap->ia_valid &= ~(ATTR_ATIME_SET|ATTR_MTIME_SET);
141 	}
142 	return p;
143 }
144 
145 static inline u32 *
encode_fattr(struct svc_rqst * rqstp,u32 * p,struct svc_fh * fhp)146 encode_fattr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
147 {
148 	struct inode *inode = fhp->fh_dentry->d_inode;
149 	int type = (inode->i_mode & S_IFMT);
150 
151 	*p++ = htonl(nfs_ftypes[type >> 12]);
152 	*p++ = htonl((u32) inode->i_mode);
153 	*p++ = htonl((u32) inode->i_nlink);
154 	*p++ = htonl((u32) nfsd_ruid(rqstp, inode->i_uid));
155 	*p++ = htonl((u32) nfsd_rgid(rqstp, inode->i_gid));
156 
157 	if (S_ISLNK(type) && inode->i_size > NFS_MAXPATHLEN) {
158 		*p++ = htonl(NFS_MAXPATHLEN);
159 	} else {
160 		*p++ = htonl((u32) inode->i_size);
161 	}
162 	*p++ = htonl((u32) inode->i_blksize);
163 	if (S_ISCHR(type) || S_ISBLK(type))
164 		*p++ = htonl((u32) inode->i_rdev);
165 	else
166 		*p++ = htonl(0xffffffff);
167 	*p++ = htonl((u32) inode->i_blocks);
168 	if (rqstp->rq_reffh->fh_version == 1
169 	    && rqstp->rq_reffh->fh_fsid_type == 1
170 	    && (fhp->fh_export->ex_flags & NFSEXP_FSID))
171 		*p++ = htonl((u32) fhp->fh_export->ex_fsid);
172 	else
173 		*p++ = htonl((u32) inode->i_dev);
174 	*p++ = htonl((u32) inode->i_ino);
175 	*p++ = htonl((u32) inode->i_atime);
176 	*p++ = 0;
177 	*p++ = htonl((u32) lease_get_mtime(inode));
178 	*p++ = 0;
179 	*p++ = htonl((u32) inode->i_ctime);
180 	*p++ = 0;
181 
182 	return p;
183 }
184 
185 /*
186  * Check buffer bounds after decoding arguments
187  */
188 static inline int
xdr_argsize_check(struct svc_rqst * rqstp,u32 * p)189 xdr_argsize_check(struct svc_rqst *rqstp, u32 *p)
190 {
191 	struct svc_buf	*buf = &rqstp->rq_argbuf;
192 
193 	return p - buf->base <= buf->buflen;
194 }
195 
196 static inline int
xdr_ressize_check(struct svc_rqst * rqstp,u32 * p)197 xdr_ressize_check(struct svc_rqst *rqstp, u32 *p)
198 {
199 	struct svc_buf	*buf = &rqstp->rq_resbuf;
200 
201 	buf->len = p - buf->base;
202 	dprintk("nfsd: ressize_check p %p base %p len %d\n",
203 			p, buf->base, buf->buflen);
204 	return (buf->len <= buf->buflen);
205 }
206 
207 /*
208  * XDR decode functions
209  */
210 int
nfssvc_decode_void(struct svc_rqst * rqstp,u32 * p,void * dummy)211 nfssvc_decode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
212 {
213 	return xdr_argsize_check(rqstp, p);
214 }
215 
216 int
nfssvc_decode_fhandle(struct svc_rqst * rqstp,u32 * p,struct svc_fh * fhp)217 nfssvc_decode_fhandle(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
218 {
219 	if (!(p = decode_fh(p, fhp)))
220 		return 0;
221 	return xdr_argsize_check(rqstp, p);
222 }
223 
224 int
nfssvc_decode_sattrargs(struct svc_rqst * rqstp,u32 * p,struct nfsd_sattrargs * args)225 nfssvc_decode_sattrargs(struct svc_rqst *rqstp, u32 *p,
226 					struct nfsd_sattrargs *args)
227 {
228 	if (!(p = decode_fh(p, &args->fh))
229 	 || !(p = decode_sattr(p, &args->attrs)))
230 		return 0;
231 
232 	return xdr_argsize_check(rqstp, p);
233 }
234 
235 int
nfssvc_decode_diropargs(struct svc_rqst * rqstp,u32 * p,struct nfsd_diropargs * args)236 nfssvc_decode_diropargs(struct svc_rqst *rqstp, u32 *p,
237 					struct nfsd_diropargs *args)
238 {
239 	if (!(p = decode_fh(p, &args->fh))
240 	 || !(p = decode_filename(p, &args->name, &args->len)))
241 		return 0;
242 
243 	 return xdr_argsize_check(rqstp, p);
244 }
245 
246 int
nfssvc_decode_readargs(struct svc_rqst * rqstp,u32 * p,struct nfsd_readargs * args)247 nfssvc_decode_readargs(struct svc_rqst *rqstp, u32 *p,
248 					struct nfsd_readargs *args)
249 {
250 	if (!(p = decode_fh(p, &args->fh)))
251 		return 0;
252 
253 	args->offset    = ntohl(*p++);
254 	args->count     = ntohl(*p++);
255 	args->totalsize = ntohl(*p++);
256 
257 	return xdr_argsize_check(rqstp, p);
258 }
259 
260 int
nfssvc_decode_writeargs(struct svc_rqst * rqstp,u32 * p,struct nfsd_writeargs * args)261 nfssvc_decode_writeargs(struct svc_rqst *rqstp, u32 *p,
262 					struct nfsd_writeargs *args)
263 {
264 	if (!(p = decode_fh(p, &args->fh)))
265 		return 0;
266 
267 	p++;				/* beginoffset */
268 	args->offset = ntohl(*p++);	/* offset */
269 	p++;				/* totalcount */
270 	args->len = ntohl(*p++);
271 	args->data = (char *) p;
272 	p += XDR_QUADLEN(args->len);
273 
274 	return xdr_argsize_check(rqstp, p);
275 }
276 
277 int
nfssvc_decode_createargs(struct svc_rqst * rqstp,u32 * p,struct nfsd_createargs * args)278 nfssvc_decode_createargs(struct svc_rqst *rqstp, u32 *p,
279 					struct nfsd_createargs *args)
280 {
281 	if (!(p = decode_fh(p, &args->fh))
282 	 || !(p = decode_filename(p, &args->name, &args->len))
283 	 || !(p = decode_sattr(p, &args->attrs)))
284 		return 0;
285 
286 	return xdr_argsize_check(rqstp, p);
287 }
288 
289 int
nfssvc_decode_renameargs(struct svc_rqst * rqstp,u32 * p,struct nfsd_renameargs * args)290 nfssvc_decode_renameargs(struct svc_rqst *rqstp, u32 *p,
291 					struct nfsd_renameargs *args)
292 {
293 	if (!(p = decode_fh(p, &args->ffh))
294 	 || !(p = decode_filename(p, &args->fname, &args->flen))
295 	 || !(p = decode_fh(p, &args->tfh))
296 	 || !(p = decode_filename(p, &args->tname, &args->tlen)))
297 		return 0;
298 
299 	return xdr_argsize_check(rqstp, p);
300 }
301 
302 int
nfssvc_decode_linkargs(struct svc_rqst * rqstp,u32 * p,struct nfsd_linkargs * args)303 nfssvc_decode_linkargs(struct svc_rqst *rqstp, u32 *p,
304 					struct nfsd_linkargs *args)
305 {
306 	if (!(p = decode_fh(p, &args->ffh))
307 	 || !(p = decode_fh(p, &args->tfh))
308 	 || !(p = decode_filename(p, &args->tname, &args->tlen)))
309 		return 0;
310 
311 	return xdr_argsize_check(rqstp, p);
312 }
313 
314 int
nfssvc_decode_symlinkargs(struct svc_rqst * rqstp,u32 * p,struct nfsd_symlinkargs * args)315 nfssvc_decode_symlinkargs(struct svc_rqst *rqstp, u32 *p,
316 					struct nfsd_symlinkargs *args)
317 {
318 	if (!(p = decode_fh(p, &args->ffh))
319 	 || !(p = decode_filename(p, &args->fname, &args->flen))
320 	 || !(p = decode_pathname(p, &args->tname, &args->tlen))
321 	 || !(p = decode_sattr(p, &args->attrs)))
322 		return 0;
323 
324 	return xdr_argsize_check(rqstp, p);
325 }
326 
327 int
nfssvc_decode_readdirargs(struct svc_rqst * rqstp,u32 * p,struct nfsd_readdirargs * args)328 nfssvc_decode_readdirargs(struct svc_rqst *rqstp, u32 *p,
329 					struct nfsd_readdirargs *args)
330 {
331 	if (!(p = decode_fh(p, &args->fh)))
332 		return 0;
333 	args->cookie = ntohl(*p++);
334 	args->count  = ntohl(*p++);
335 
336 	return xdr_argsize_check(rqstp, p);
337 }
338 
339 /*
340  * XDR encode functions
341  */
342 int
nfssvc_encode_void(struct svc_rqst * rqstp,u32 * p,void * dummy)343 nfssvc_encode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
344 {
345 	return xdr_ressize_check(rqstp, p);
346 }
347 
348 int
nfssvc_encode_attrstat(struct svc_rqst * rqstp,u32 * p,struct nfsd_attrstat * resp)349 nfssvc_encode_attrstat(struct svc_rqst *rqstp, u32 *p,
350 					struct nfsd_attrstat *resp)
351 {
352 	p = encode_fattr(rqstp, p, &resp->fh);
353 	return xdr_ressize_check(rqstp, p);
354 }
355 
356 int
nfssvc_encode_diropres(struct svc_rqst * rqstp,u32 * p,struct nfsd_diropres * resp)357 nfssvc_encode_diropres(struct svc_rqst *rqstp, u32 *p,
358 					struct nfsd_diropres *resp)
359 {
360 	p = encode_fh(p, &resp->fh);
361 	p = encode_fattr(rqstp, p, &resp->fh);
362 	return xdr_ressize_check(rqstp, p);
363 }
364 
365 int
nfssvc_encode_readlinkres(struct svc_rqst * rqstp,u32 * p,struct nfsd_readlinkres * resp)366 nfssvc_encode_readlinkres(struct svc_rqst *rqstp, u32 *p,
367 					struct nfsd_readlinkres *resp)
368 {
369 	*p++ = htonl(resp->len);
370 	p += XDR_QUADLEN(resp->len);
371 	return xdr_ressize_check(rqstp, p);
372 }
373 
374 int
nfssvc_encode_readres(struct svc_rqst * rqstp,u32 * p,struct nfsd_readres * resp)375 nfssvc_encode_readres(struct svc_rqst *rqstp, u32 *p,
376 					struct nfsd_readres *resp)
377 {
378 	p = encode_fattr(rqstp, p, &resp->fh);
379 	*p++ = htonl(resp->count);
380 	p += XDR_QUADLEN(resp->count);
381 
382 	return xdr_ressize_check(rqstp, p);
383 }
384 
385 int
nfssvc_encode_readdirres(struct svc_rqst * rqstp,u32 * p,struct nfsd_readdirres * resp)386 nfssvc_encode_readdirres(struct svc_rqst *rqstp, u32 *p,
387 					struct nfsd_readdirres *resp)
388 {
389 	p += XDR_QUADLEN(resp->count);
390 	return xdr_ressize_check(rqstp, p);
391 }
392 
393 int
nfssvc_encode_statfsres(struct svc_rqst * rqstp,u32 * p,struct nfsd_statfsres * resp)394 nfssvc_encode_statfsres(struct svc_rqst *rqstp, u32 *p,
395 					struct nfsd_statfsres *resp)
396 {
397 	struct statfs	*stat = &resp->stats;
398 
399 	*p++ = htonl(NFSSVC_MAXBLKSIZE);	/* max transfer size */
400 	*p++ = htonl(stat->f_bsize);
401 	*p++ = htonl(stat->f_blocks);
402 	*p++ = htonl(stat->f_bfree);
403 	*p++ = htonl(stat->f_bavail);
404 	return xdr_ressize_check(rqstp, p);
405 }
406 
407 int
nfssvc_encode_entry(struct readdir_cd * cd,const char * name,int namlen,loff_t offset,ino_t ino,unsigned int d_type)408 nfssvc_encode_entry(struct readdir_cd *cd, const char *name,
409 		    int namlen, loff_t offset, ino_t ino, unsigned int d_type)
410 {
411 	u32	*p = cd->buffer;
412 	int	buflen, slen;
413 
414 	/*
415 	dprintk("nfsd: entry(%.*s off %ld ino %ld)\n",
416 			namlen, name, offset, ino);
417 	 */
418 
419 	if (offset > ~((u32) 0))
420 		return -EINVAL;
421 	if (cd->offset)
422 		*cd->offset = htonl(offset);
423 	if (namlen > NFS2_MAXNAMLEN)
424 		namlen = NFS2_MAXNAMLEN;/* truncate filename */
425 
426 	slen = XDR_QUADLEN(namlen);
427 	if ((buflen = cd->buflen - slen - 4) < 0) {
428 		cd->eob = 1;
429 		return -EINVAL;
430 	}
431 	*p++ = xdr_one;				/* mark entry present */
432 	*p++ = htonl((u32) ino);		/* file id */
433 	p    = xdr_encode_array(p, name, namlen);/* name length & name */
434 	cd->offset = p;			/* remember pointer */
435 	*p++ = ~(u32) 0;		/* offset of next entry */
436 
437 	cd->buflen = buflen;
438 	cd->buffer = p;
439 	return 0;
440 }
441 
442 /*
443  * XDR release functions
444  */
445 int
nfssvc_release_fhandle(struct svc_rqst * rqstp,u32 * p,struct nfsd_fhandle * resp)446 nfssvc_release_fhandle(struct svc_rqst *rqstp, u32 *p,
447 					struct nfsd_fhandle *resp)
448 {
449 	fh_put(&resp->fh);
450 	return 1;
451 }
452