xref: /linux/fs/nfsd/export.c (revision ce3f5bb7504ca802efa710280a4601a06545bd2e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * NFS exporting and validation.
4  *
5  * We maintain a list of clients, each of which has a list of
6  * exports. To export an fs to a given client, you first have
7  * to create the client entry with NFSCTL_ADDCLIENT, which
8  * creates a client control block and adds it to the hash
9  * table. Then, you call NFSCTL_EXPORT for each fs.
10  *
11  *
12  * Copyright (C) 1995, 1996 Olaf Kirch, <okir@monad.swb.de>
13  */
14 
15 #include <linux/slab.h>
16 #include <linux/namei.h>
17 #include <linux/module.h>
18 #include <linux/exportfs.h>
19 #include <linux/sunrpc/svc_xprt.h>
20 
21 #include "nfsd.h"
22 #include "nfsfh.h"
23 #include "netns.h"
24 #include "pnfs.h"
25 #include "filecache.h"
26 #include "trace.h"
27 
28 #define NFSDDBG_FACILITY	NFSDDBG_EXPORT
29 
30 /*
31  * We have two caches.
32  * One maps client+vfsmnt+dentry to export options - the export map
33  * The other maps client+filehandle-fragment to export options. - the expkey map
34  *
35  * The export options are actually stored in the first map, and the
36  * second map contains a reference to the entry in the first map.
37  */
38 
39 #define	EXPKEY_HASHBITS		8
40 #define	EXPKEY_HASHMAX		(1 << EXPKEY_HASHBITS)
41 #define	EXPKEY_HASHMASK		(EXPKEY_HASHMAX -1)
42 
expkey_put(struct kref * ref)43 static void expkey_put(struct kref *ref)
44 {
45 	struct svc_expkey *key = container_of(ref, struct svc_expkey, h.ref);
46 
47 	if (test_bit(CACHE_VALID, &key->h.flags) &&
48 	    !test_bit(CACHE_NEGATIVE, &key->h.flags))
49 		path_put(&key->ek_path);
50 	auth_domain_put(key->ek_client);
51 	kfree_rcu(key, ek_rcu);
52 }
53 
expkey_upcall(struct cache_detail * cd,struct cache_head * h)54 static int expkey_upcall(struct cache_detail *cd, struct cache_head *h)
55 {
56 	return sunrpc_cache_pipe_upcall(cd, h);
57 }
58 
expkey_request(struct cache_detail * cd,struct cache_head * h,char ** bpp,int * blen)59 static void expkey_request(struct cache_detail *cd,
60 			   struct cache_head *h,
61 			   char **bpp, int *blen)
62 {
63 	/* client fsidtype \xfsid */
64 	struct svc_expkey *ek = container_of(h, struct svc_expkey, h);
65 	char type[5];
66 
67 	qword_add(bpp, blen, ek->ek_client->name);
68 	snprintf(type, 5, "%d", ek->ek_fsidtype);
69 	qword_add(bpp, blen, type);
70 	qword_addhex(bpp, blen, (char*)ek->ek_fsid, key_len(ek->ek_fsidtype));
71 	(*bpp)[-1] = '\n';
72 }
73 
74 static struct svc_expkey *svc_expkey_update(struct cache_detail *cd, struct svc_expkey *new,
75 					    struct svc_expkey *old);
76 static struct svc_expkey *svc_expkey_lookup(struct cache_detail *cd, struct svc_expkey *);
77 
expkey_parse(struct cache_detail * cd,char * mesg,int mlen)78 static int expkey_parse(struct cache_detail *cd, char *mesg, int mlen)
79 {
80 	/* client fsidtype fsid expiry [path] */
81 	char *buf;
82 	int len;
83 	struct auth_domain *dom = NULL;
84 	int err;
85 	u8 fsidtype;
86 	struct svc_expkey key;
87 	struct svc_expkey *ek = NULL;
88 
89 	if (mesg[mlen - 1] != '\n')
90 		return -EINVAL;
91 	mesg[mlen-1] = 0;
92 
93 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
94 	err = -ENOMEM;
95 	if (!buf)
96 		goto out;
97 
98 	err = -EINVAL;
99 	if (qword_get(&mesg, buf, PAGE_SIZE) <= 0)
100 		goto out;
101 
102 	err = -ENOENT;
103 	dom = auth_domain_find(buf);
104 	if (!dom)
105 		goto out;
106 	dprintk("found domain %s\n", buf);
107 
108 	err = -EINVAL;
109 	if (qword_get(&mesg, buf, PAGE_SIZE) <= 0)
110 		goto out;
111 	if (kstrtou8(buf, 10, &fsidtype))
112 		goto out;
113 	dprintk("found fsidtype %u\n", fsidtype);
114 	if (key_len(fsidtype)==0) /* invalid type */
115 		goto out;
116 	if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
117 		goto out;
118 	dprintk("found fsid length %d\n", len);
119 	if (len != key_len(fsidtype))
120 		goto out;
121 
122 	/* OK, we seem to have a valid key */
123 	key.h.flags = 0;
124 	err = get_expiry(&mesg, &key.h.expiry_time);
125 	if (err)
126 		goto out;
127 
128 	key.ek_client = dom;
129 	key.ek_fsidtype = fsidtype;
130 	memcpy(key.ek_fsid, buf, len);
131 
132 	ek = svc_expkey_lookup(cd, &key);
133 	err = -ENOMEM;
134 	if (!ek)
135 		goto out;
136 
137 	/* now we want a pathname, or empty meaning NEGATIVE  */
138 	err = -EINVAL;
139 	len = qword_get(&mesg, buf, PAGE_SIZE);
140 	if (len < 0)
141 		goto out;
142 	dprintk("Path seems to be <%s>\n", buf);
143 	err = 0;
144 	if (len == 0) {
145 		set_bit(CACHE_NEGATIVE, &key.h.flags);
146 		ek = svc_expkey_update(cd, &key, ek);
147 		if (ek)
148 			trace_nfsd_expkey_update(ek, NULL);
149 		else
150 			err = -ENOMEM;
151 	} else {
152 		err = kern_path(buf, 0, &key.ek_path);
153 		if (err)
154 			goto out;
155 
156 		dprintk("Found the path %s\n", buf);
157 
158 		ek = svc_expkey_update(cd, &key, ek);
159 		if (ek)
160 			trace_nfsd_expkey_update(ek, buf);
161 		else
162 			err = -ENOMEM;
163 		path_put(&key.ek_path);
164 	}
165 	cache_flush();
166  out:
167 	if (ek)
168 		cache_put(&ek->h, cd);
169 	if (dom)
170 		auth_domain_put(dom);
171 	kfree(buf);
172 	return err;
173 }
174 
expkey_show(struct seq_file * m,struct cache_detail * cd,struct cache_head * h)175 static int expkey_show(struct seq_file *m,
176 		       struct cache_detail *cd,
177 		       struct cache_head *h)
178 {
179 	struct svc_expkey *ek ;
180 	int i;
181 
182 	if (h ==NULL) {
183 		seq_puts(m, "#domain fsidtype fsid [path]\n");
184 		return 0;
185 	}
186 	ek = container_of(h, struct svc_expkey, h);
187 	seq_printf(m, "%s %d 0x", ek->ek_client->name,
188 		   ek->ek_fsidtype);
189 	for (i=0; i < key_len(ek->ek_fsidtype)/4; i++)
190 		seq_printf(m, "%08x", ek->ek_fsid[i]);
191 	if (test_bit(CACHE_VALID, &h->flags) &&
192 	    !test_bit(CACHE_NEGATIVE, &h->flags)) {
193 		seq_printf(m, " ");
194 		seq_path(m, &ek->ek_path, "\\ \t\n");
195 	}
196 	seq_printf(m, "\n");
197 	return 0;
198 }
199 
expkey_match(struct cache_head * a,struct cache_head * b)200 static inline int expkey_match (struct cache_head *a, struct cache_head *b)
201 {
202 	struct svc_expkey *orig = container_of(a, struct svc_expkey, h);
203 	struct svc_expkey *new = container_of(b, struct svc_expkey, h);
204 
205 	if (orig->ek_fsidtype != new->ek_fsidtype ||
206 	    orig->ek_client != new->ek_client ||
207 	    memcmp(orig->ek_fsid, new->ek_fsid, key_len(orig->ek_fsidtype)) != 0)
208 		return 0;
209 	return 1;
210 }
211 
expkey_init(struct cache_head * cnew,struct cache_head * citem)212 static inline void expkey_init(struct cache_head *cnew,
213 				   struct cache_head *citem)
214 {
215 	struct svc_expkey *new = container_of(cnew, struct svc_expkey, h);
216 	struct svc_expkey *item = container_of(citem, struct svc_expkey, h);
217 
218 	kref_get(&item->ek_client->ref);
219 	new->ek_client = item->ek_client;
220 	new->ek_fsidtype = item->ek_fsidtype;
221 
222 	memcpy(new->ek_fsid, item->ek_fsid, sizeof(new->ek_fsid));
223 }
224 
expkey_update(struct cache_head * cnew,struct cache_head * citem)225 static inline void expkey_update(struct cache_head *cnew,
226 				   struct cache_head *citem)
227 {
228 	struct svc_expkey *new = container_of(cnew, struct svc_expkey, h);
229 	struct svc_expkey *item = container_of(citem, struct svc_expkey, h);
230 
231 	new->ek_path = item->ek_path;
232 	path_get(&item->ek_path);
233 }
234 
expkey_alloc(void)235 static struct cache_head *expkey_alloc(void)
236 {
237 	struct svc_expkey *i = kmalloc(sizeof(*i), GFP_KERNEL);
238 	if (i)
239 		return &i->h;
240 	else
241 		return NULL;
242 }
243 
expkey_flush(void)244 static void expkey_flush(void)
245 {
246 	/*
247 	 * Take the nfsd_mutex here to ensure that the file cache is not
248 	 * destroyed while we're in the middle of flushing.
249 	 */
250 	mutex_lock(&nfsd_mutex);
251 	nfsd_file_cache_purge(current->nsproxy->net_ns);
252 	mutex_unlock(&nfsd_mutex);
253 }
254 
255 static const struct cache_detail svc_expkey_cache_template = {
256 	.owner		= THIS_MODULE,
257 	.hash_size	= EXPKEY_HASHMAX,
258 	.name		= "nfsd.fh",
259 	.cache_put	= expkey_put,
260 	.cache_upcall	= expkey_upcall,
261 	.cache_request	= expkey_request,
262 	.cache_parse	= expkey_parse,
263 	.cache_show	= expkey_show,
264 	.match		= expkey_match,
265 	.init		= expkey_init,
266 	.update       	= expkey_update,
267 	.alloc		= expkey_alloc,
268 	.flush		= expkey_flush,
269 };
270 
271 static int
svc_expkey_hash(struct svc_expkey * item)272 svc_expkey_hash(struct svc_expkey *item)
273 {
274 	int hash = item->ek_fsidtype;
275 	char * cp = (char*)item->ek_fsid;
276 	int len = key_len(item->ek_fsidtype);
277 
278 	hash ^= hash_mem(cp, len, EXPKEY_HASHBITS);
279 	hash ^= hash_ptr(item->ek_client, EXPKEY_HASHBITS);
280 	hash &= EXPKEY_HASHMASK;
281 	return hash;
282 }
283 
284 static struct svc_expkey *
svc_expkey_lookup(struct cache_detail * cd,struct svc_expkey * item)285 svc_expkey_lookup(struct cache_detail *cd, struct svc_expkey *item)
286 {
287 	struct cache_head *ch;
288 	int hash = svc_expkey_hash(item);
289 
290 	ch = sunrpc_cache_lookup_rcu(cd, &item->h, hash);
291 	if (ch)
292 		return container_of(ch, struct svc_expkey, h);
293 	else
294 		return NULL;
295 }
296 
297 static struct svc_expkey *
svc_expkey_update(struct cache_detail * cd,struct svc_expkey * new,struct svc_expkey * old)298 svc_expkey_update(struct cache_detail *cd, struct svc_expkey *new,
299 		  struct svc_expkey *old)
300 {
301 	struct cache_head *ch;
302 	int hash = svc_expkey_hash(new);
303 
304 	ch = sunrpc_cache_update(cd, &new->h, &old->h, hash);
305 	if (ch)
306 		return container_of(ch, struct svc_expkey, h);
307 	else
308 		return NULL;
309 }
310 
311 
312 #define	EXPORT_HASHBITS		8
313 #define	EXPORT_HASHMAX		(1<< EXPORT_HASHBITS)
314 
nfsd4_fslocs_free(struct nfsd4_fs_locations * fsloc)315 static void nfsd4_fslocs_free(struct nfsd4_fs_locations *fsloc)
316 {
317 	struct nfsd4_fs_location *locations = fsloc->locations;
318 	int i;
319 
320 	if (!locations)
321 		return;
322 
323 	for (i = 0; i < fsloc->locations_count; i++) {
324 		kfree(locations[i].path);
325 		kfree(locations[i].hosts);
326 	}
327 
328 	kfree(locations);
329 	fsloc->locations = NULL;
330 }
331 
export_stats_init(struct export_stats * stats)332 static int export_stats_init(struct export_stats *stats)
333 {
334 	stats->start_time = ktime_get_seconds();
335 	return percpu_counter_init_many(stats->counter, 0, GFP_KERNEL,
336 					EXP_STATS_COUNTERS_NUM);
337 }
338 
export_stats_reset(struct export_stats * stats)339 static void export_stats_reset(struct export_stats *stats)
340 {
341 	if (stats) {
342 		int i;
343 
344 		for (i = 0; i < EXP_STATS_COUNTERS_NUM; i++)
345 			percpu_counter_set(&stats->counter[i], 0);
346 	}
347 }
348 
export_stats_destroy(struct export_stats * stats)349 static void export_stats_destroy(struct export_stats *stats)
350 {
351 	if (stats)
352 		percpu_counter_destroy_many(stats->counter,
353 					    EXP_STATS_COUNTERS_NUM);
354 }
355 
svc_export_release(struct rcu_head * rcu_head)356 static void svc_export_release(struct rcu_head *rcu_head)
357 {
358 	struct svc_export *exp = container_of(rcu_head, struct svc_export,
359 			ex_rcu);
360 
361 	nfsd4_fslocs_free(&exp->ex_fslocs);
362 	export_stats_destroy(exp->ex_stats);
363 	kfree(exp->ex_stats);
364 	kfree(exp->ex_uuid);
365 	kfree(exp);
366 }
367 
svc_export_put(struct kref * ref)368 static void svc_export_put(struct kref *ref)
369 {
370 	struct svc_export *exp = container_of(ref, struct svc_export, h.ref);
371 
372 	path_put(&exp->ex_path);
373 	auth_domain_put(exp->ex_client);
374 	call_rcu(&exp->ex_rcu, svc_export_release);
375 }
376 
svc_export_upcall(struct cache_detail * cd,struct cache_head * h)377 static int svc_export_upcall(struct cache_detail *cd, struct cache_head *h)
378 {
379 	return sunrpc_cache_pipe_upcall(cd, h);
380 }
381 
svc_export_request(struct cache_detail * cd,struct cache_head * h,char ** bpp,int * blen)382 static void svc_export_request(struct cache_detail *cd,
383 			       struct cache_head *h,
384 			       char **bpp, int *blen)
385 {
386 	/*  client path */
387 	struct svc_export *exp = container_of(h, struct svc_export, h);
388 	char *pth;
389 
390 	qword_add(bpp, blen, exp->ex_client->name);
391 	pth = d_path(&exp->ex_path, *bpp, *blen);
392 	if (IS_ERR(pth)) {
393 		/* is this correct? */
394 		(*bpp)[0] = '\n';
395 		return;
396 	}
397 	qword_add(bpp, blen, pth);
398 	(*bpp)[-1] = '\n';
399 }
400 
401 static struct svc_export *svc_export_update(struct svc_export *new,
402 					    struct svc_export *old);
403 static struct svc_export *svc_export_lookup(struct svc_export *);
404 
check_export(struct path * path,int * flags,unsigned char * uuid)405 static int check_export(struct path *path, int *flags, unsigned char *uuid)
406 {
407 	struct inode *inode = d_inode(path->dentry);
408 
409 	/*
410 	 * We currently export only dirs, regular files, and (for v4
411 	 * pseudoroot) symlinks.
412 	 */
413 	if (!S_ISDIR(inode->i_mode) &&
414 	    !S_ISLNK(inode->i_mode) &&
415 	    !S_ISREG(inode->i_mode))
416 		return -ENOTDIR;
417 
418 	/*
419 	 * Mountd should never pass down a writeable V4ROOT export, but,
420 	 * just to make sure:
421 	 */
422 	if (*flags & NFSEXP_V4ROOT)
423 		*flags |= NFSEXP_READONLY;
424 
425 	/* There are two requirements on a filesystem to be exportable.
426 	 * 1:  We must be able to identify the filesystem from a number.
427 	 *       either a device number (so FS_REQUIRES_DEV needed)
428 	 *       or an FSID number (so NFSEXP_FSID or ->uuid is needed).
429 	 * 2:  We must be able to find an inode from a filehandle.
430 	 *       This means that s_export_op must be set.
431 	 * 3: We must not currently be on an idmapped mount.
432 	 */
433 	if (!(inode->i_sb->s_type->fs_flags & FS_REQUIRES_DEV) &&
434 	    !(*flags & NFSEXP_FSID) &&
435 	    uuid == NULL) {
436 		dprintk("exp_export: export of non-dev fs without fsid\n");
437 		return -EINVAL;
438 	}
439 
440 	if (!exportfs_can_decode_fh(inode->i_sb->s_export_op)) {
441 		dprintk("exp_export: export of invalid fs type.\n");
442 		return -EINVAL;
443 	}
444 
445 	if (is_idmapped_mnt(path->mnt)) {
446 		dprintk("exp_export: export of idmapped mounts not yet supported.\n");
447 		return -EINVAL;
448 	}
449 
450 	if (inode->i_sb->s_export_op->flags & EXPORT_OP_NOSUBTREECHK &&
451 	    !(*flags & NFSEXP_NOSUBTREECHECK)) {
452 		dprintk("%s: %s does not support subtree checking!\n",
453 			__func__, inode->i_sb->s_type->name);
454 		return -EINVAL;
455 	}
456 	return 0;
457 }
458 
459 #ifdef CONFIG_NFSD_V4
460 
461 static int
fsloc_parse(char ** mesg,char * buf,struct nfsd4_fs_locations * fsloc)462 fsloc_parse(char **mesg, char *buf, struct nfsd4_fs_locations *fsloc)
463 {
464 	int len;
465 	int migrated, i, err;
466 
467 	/* more than one fsloc */
468 	if (fsloc->locations)
469 		return -EINVAL;
470 
471 	/* listsize */
472 	err = get_uint(mesg, &fsloc->locations_count);
473 	if (err)
474 		return err;
475 	if (fsloc->locations_count > MAX_FS_LOCATIONS)
476 		return -EINVAL;
477 	if (fsloc->locations_count == 0)
478 		return 0;
479 
480 	fsloc->locations = kcalloc(fsloc->locations_count,
481 				   sizeof(struct nfsd4_fs_location),
482 				   GFP_KERNEL);
483 	if (!fsloc->locations)
484 		return -ENOMEM;
485 	for (i=0; i < fsloc->locations_count; i++) {
486 		/* colon separated host list */
487 		err = -EINVAL;
488 		len = qword_get(mesg, buf, PAGE_SIZE);
489 		if (len <= 0)
490 			goto out_free_all;
491 		err = -ENOMEM;
492 		fsloc->locations[i].hosts = kstrdup(buf, GFP_KERNEL);
493 		if (!fsloc->locations[i].hosts)
494 			goto out_free_all;
495 		err = -EINVAL;
496 		/* slash separated path component list */
497 		len = qword_get(mesg, buf, PAGE_SIZE);
498 		if (len <= 0)
499 			goto out_free_all;
500 		err = -ENOMEM;
501 		fsloc->locations[i].path = kstrdup(buf, GFP_KERNEL);
502 		if (!fsloc->locations[i].path)
503 			goto out_free_all;
504 	}
505 	/* migrated */
506 	err = get_int(mesg, &migrated);
507 	if (err)
508 		goto out_free_all;
509 	err = -EINVAL;
510 	if (migrated < 0 || migrated > 1)
511 		goto out_free_all;
512 	fsloc->migrated = migrated;
513 	return 0;
514 out_free_all:
515 	nfsd4_fslocs_free(fsloc);
516 	return err;
517 }
518 
secinfo_parse(char ** mesg,char * buf,struct svc_export * exp)519 static int secinfo_parse(char **mesg, char *buf, struct svc_export *exp)
520 {
521 	struct exp_flavor_info *f;
522 	u32 listsize;
523 	int err;
524 
525 	/* more than one secinfo */
526 	if (exp->ex_nflavors)
527 		return -EINVAL;
528 
529 	err = get_uint(mesg, &listsize);
530 	if (err)
531 		return err;
532 	if (listsize > MAX_SECINFO_LIST)
533 		return -EINVAL;
534 
535 	for (f = exp->ex_flavors; f < exp->ex_flavors + listsize; f++) {
536 		err = get_uint(mesg, &f->pseudoflavor);
537 		if (err)
538 			return err;
539 		/*
540 		 * XXX: It would be nice to also check whether this
541 		 * pseudoflavor is supported, so we can discover the
542 		 * problem at export time instead of when a client fails
543 		 * to authenticate.
544 		 */
545 		err = get_uint(mesg, &f->flags);
546 		if (err)
547 			return err;
548 		/* Only some flags are allowed to differ between flavors: */
549 		if (~NFSEXP_SECINFO_FLAGS & (f->flags ^ exp->ex_flags))
550 			return -EINVAL;
551 	}
552 	exp->ex_nflavors = listsize;
553 	return 0;
554 }
555 
556 #else /* CONFIG_NFSD_V4 */
557 static inline int
fsloc_parse(char ** mesg,char * buf,struct nfsd4_fs_locations * fsloc)558 fsloc_parse(char **mesg, char *buf, struct nfsd4_fs_locations *fsloc){return 0;}
559 static inline int
secinfo_parse(char ** mesg,char * buf,struct svc_export * exp)560 secinfo_parse(char **mesg, char *buf, struct svc_export *exp) { return 0; }
561 #endif
562 
xprtsec_parse(char ** mesg,char * buf,struct svc_export * exp)563 static int xprtsec_parse(char **mesg, char *buf, struct svc_export *exp)
564 {
565 	unsigned int i, mode, listsize;
566 	int err;
567 
568 	err = get_uint(mesg, &listsize);
569 	if (err)
570 		return err;
571 	if (listsize > NFSEXP_XPRTSEC_NUM)
572 		return -EINVAL;
573 
574 	exp->ex_xprtsec_modes = 0;
575 	for (i = 0; i < listsize; i++) {
576 		err = get_uint(mesg, &mode);
577 		if (err)
578 			return err;
579 		if (mode > NFSEXP_XPRTSEC_MTLS)
580 			return -EINVAL;
581 		exp->ex_xprtsec_modes |= mode;
582 	}
583 	return 0;
584 }
585 
586 static inline int
nfsd_uuid_parse(char ** mesg,char * buf,unsigned char ** puuid)587 nfsd_uuid_parse(char **mesg, char *buf, unsigned char **puuid)
588 {
589 	int len;
590 
591 	/* more than one uuid */
592 	if (*puuid)
593 		return -EINVAL;
594 
595 	/* expect a 16 byte uuid encoded as \xXXXX... */
596 	len = qword_get(mesg, buf, PAGE_SIZE);
597 	if (len != EX_UUID_LEN)
598 		return -EINVAL;
599 
600 	*puuid = kmemdup(buf, EX_UUID_LEN, GFP_KERNEL);
601 	if (*puuid == NULL)
602 		return -ENOMEM;
603 
604 	return 0;
605 }
606 
svc_export_parse(struct cache_detail * cd,char * mesg,int mlen)607 static int svc_export_parse(struct cache_detail *cd, char *mesg, int mlen)
608 {
609 	/* client path expiry [flags anonuid anongid fsid] */
610 	char *buf;
611 	int err;
612 	struct auth_domain *dom = NULL;
613 	struct svc_export exp = {}, *expp;
614 	int an_int;
615 
616 	if (mesg[mlen-1] != '\n')
617 		return -EINVAL;
618 	mesg[mlen-1] = 0;
619 
620 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
621 	if (!buf)
622 		return -ENOMEM;
623 
624 	/* client */
625 	err = -EINVAL;
626 	if (qword_get(&mesg, buf, PAGE_SIZE) <= 0)
627 		goto out;
628 
629 	err = -ENOENT;
630 	dom = auth_domain_find(buf);
631 	if (!dom)
632 		goto out;
633 
634 	/* path */
635 	err = -EINVAL;
636 	if (qword_get(&mesg, buf, PAGE_SIZE) <= 0)
637 		goto out1;
638 
639 	err = kern_path(buf, 0, &exp.ex_path);
640 	if (err)
641 		goto out1;
642 
643 	exp.ex_client = dom;
644 	exp.cd = cd;
645 	exp.ex_devid_map = NULL;
646 	exp.ex_xprtsec_modes = NFSEXP_XPRTSEC_ALL;
647 
648 	/* expiry */
649 	err = get_expiry(&mesg, &exp.h.expiry_time);
650 	if (err)
651 		goto out3;
652 
653 	/* flags */
654 	err = get_int(&mesg, &an_int);
655 	if (err == -ENOENT) {
656 		err = 0;
657 		set_bit(CACHE_NEGATIVE, &exp.h.flags);
658 	} else {
659 		if (err || an_int < 0)
660 			goto out3;
661 		exp.ex_flags= an_int;
662 
663 		/* anon uid */
664 		err = get_int(&mesg, &an_int);
665 		if (err)
666 			goto out3;
667 		exp.ex_anon_uid= make_kuid(current_user_ns(), an_int);
668 
669 		/* anon gid */
670 		err = get_int(&mesg, &an_int);
671 		if (err)
672 			goto out3;
673 		exp.ex_anon_gid= make_kgid(current_user_ns(), an_int);
674 
675 		/* fsid */
676 		err = get_int(&mesg, &an_int);
677 		if (err)
678 			goto out3;
679 		exp.ex_fsid = an_int;
680 
681 		while (qword_get(&mesg, buf, PAGE_SIZE) > 0) {
682 			if (strcmp(buf, "fsloc") == 0)
683 				err = fsloc_parse(&mesg, buf, &exp.ex_fslocs);
684 			else if (strcmp(buf, "uuid") == 0)
685 				err = nfsd_uuid_parse(&mesg, buf, &exp.ex_uuid);
686 			else if (strcmp(buf, "secinfo") == 0)
687 				err = secinfo_parse(&mesg, buf, &exp);
688 			else if (strcmp(buf, "xprtsec") == 0)
689 				err = xprtsec_parse(&mesg, buf, &exp);
690 			else
691 				/* quietly ignore unknown words and anything
692 				 * following. Newer user-space can try to set
693 				 * new values, then see what the result was.
694 				 */
695 				break;
696 			if (err)
697 				goto out4;
698 		}
699 
700 		err = check_export(&exp.ex_path, &exp.ex_flags, exp.ex_uuid);
701 		if (err)
702 			goto out4;
703 
704 		/*
705 		 * No point caching this if it would immediately expire.
706 		 * Also, this protects exportfs's dummy export from the
707 		 * anon_uid/anon_gid checks:
708 		 */
709 		if (exp.h.expiry_time < seconds_since_boot())
710 			goto out4;
711 		/*
712 		 * For some reason exportfs has been passing down an
713 		 * invalid (-1) uid & gid on the "dummy" export which it
714 		 * uses to test export support.  To make sure exportfs
715 		 * sees errors from check_export we therefore need to
716 		 * delay these checks till after check_export:
717 		 */
718 		err = -EINVAL;
719 		if (!uid_valid(exp.ex_anon_uid))
720 			goto out4;
721 		if (!gid_valid(exp.ex_anon_gid))
722 			goto out4;
723 		err = 0;
724 
725 		nfsd4_setup_layout_type(&exp);
726 	}
727 
728 	expp = svc_export_lookup(&exp);
729 	if (!expp) {
730 		err = -ENOMEM;
731 		goto out4;
732 	}
733 	expp = svc_export_update(&exp, expp);
734 	if (expp) {
735 		trace_nfsd_export_update(expp);
736 		cache_flush();
737 		exp_put(expp);
738 	} else
739 		err = -ENOMEM;
740 out4:
741 	nfsd4_fslocs_free(&exp.ex_fslocs);
742 	kfree(exp.ex_uuid);
743 out3:
744 	path_put(&exp.ex_path);
745 out1:
746 	auth_domain_put(dom);
747 out:
748 	kfree(buf);
749 	return err;
750 }
751 
752 static void exp_flags(struct seq_file *m, int flag, int fsid,
753 		kuid_t anonu, kgid_t anong, struct nfsd4_fs_locations *fslocs);
754 static void show_secinfo(struct seq_file *m, struct svc_export *exp);
755 
is_export_stats_file(struct seq_file * m)756 static int is_export_stats_file(struct seq_file *m)
757 {
758 	/*
759 	 * The export_stats file uses the same ops as the exports file.
760 	 * We use the file's name to determine the reported info per export.
761 	 * There is no rename in nsfdfs, so d_name.name is stable.
762 	 */
763 	return !strcmp(m->file->f_path.dentry->d_name.name, "export_stats");
764 }
765 
svc_export_show(struct seq_file * m,struct cache_detail * cd,struct cache_head * h)766 static int svc_export_show(struct seq_file *m,
767 			   struct cache_detail *cd,
768 			   struct cache_head *h)
769 {
770 	struct svc_export *exp;
771 	bool export_stats = is_export_stats_file(m);
772 
773 	if (h == NULL) {
774 		if (export_stats)
775 			seq_puts(m, "#path domain start-time\n#\tstats\n");
776 		else
777 			seq_puts(m, "#path domain(flags)\n");
778 		return 0;
779 	}
780 	exp = container_of(h, struct svc_export, h);
781 	seq_path(m, &exp->ex_path, " \t\n\\");
782 	seq_putc(m, '\t');
783 	seq_escape(m, exp->ex_client->name, " \t\n\\");
784 	if (export_stats) {
785 		struct percpu_counter *counter = exp->ex_stats->counter;
786 
787 		seq_printf(m, "\t%lld\n", exp->ex_stats->start_time);
788 		seq_printf(m, "\tfh_stale: %lld\n",
789 			   percpu_counter_sum_positive(&counter[EXP_STATS_FH_STALE]));
790 		seq_printf(m, "\tio_read: %lld\n",
791 			   percpu_counter_sum_positive(&counter[EXP_STATS_IO_READ]));
792 		seq_printf(m, "\tio_write: %lld\n",
793 			   percpu_counter_sum_positive(&counter[EXP_STATS_IO_WRITE]));
794 		seq_putc(m, '\n');
795 		return 0;
796 	}
797 	seq_putc(m, '(');
798 	if (test_bit(CACHE_VALID, &h->flags) &&
799 	    !test_bit(CACHE_NEGATIVE, &h->flags)) {
800 		exp_flags(m, exp->ex_flags, exp->ex_fsid,
801 			  exp->ex_anon_uid, exp->ex_anon_gid, &exp->ex_fslocs);
802 		if (exp->ex_uuid) {
803 			int i;
804 			seq_puts(m, ",uuid=");
805 			for (i = 0; i < EX_UUID_LEN; i++) {
806 				if ((i&3) == 0 && i)
807 					seq_putc(m, ':');
808 				seq_printf(m, "%02x", exp->ex_uuid[i]);
809 			}
810 		}
811 		show_secinfo(m, exp);
812 	}
813 	seq_puts(m, ")\n");
814 	return 0;
815 }
svc_export_match(struct cache_head * a,struct cache_head * b)816 static int svc_export_match(struct cache_head *a, struct cache_head *b)
817 {
818 	struct svc_export *orig = container_of(a, struct svc_export, h);
819 	struct svc_export *new = container_of(b, struct svc_export, h);
820 	return orig->ex_client == new->ex_client &&
821 		path_equal(&orig->ex_path, &new->ex_path);
822 }
823 
svc_export_init(struct cache_head * cnew,struct cache_head * citem)824 static void svc_export_init(struct cache_head *cnew, struct cache_head *citem)
825 {
826 	struct svc_export *new = container_of(cnew, struct svc_export, h);
827 	struct svc_export *item = container_of(citem, struct svc_export, h);
828 
829 	kref_get(&item->ex_client->ref);
830 	new->ex_client = item->ex_client;
831 	new->ex_path = item->ex_path;
832 	path_get(&item->ex_path);
833 	new->ex_fslocs.locations = NULL;
834 	new->ex_fslocs.locations_count = 0;
835 	new->ex_fslocs.migrated = 0;
836 	new->ex_layout_types = 0;
837 	new->ex_uuid = NULL;
838 	new->cd = item->cd;
839 	export_stats_reset(new->ex_stats);
840 }
841 
export_update(struct cache_head * cnew,struct cache_head * citem)842 static void export_update(struct cache_head *cnew, struct cache_head *citem)
843 {
844 	struct svc_export *new = container_of(cnew, struct svc_export, h);
845 	struct svc_export *item = container_of(citem, struct svc_export, h);
846 	int i;
847 
848 	new->ex_flags = item->ex_flags;
849 	new->ex_anon_uid = item->ex_anon_uid;
850 	new->ex_anon_gid = item->ex_anon_gid;
851 	new->ex_fsid = item->ex_fsid;
852 	new->ex_devid_map = item->ex_devid_map;
853 	item->ex_devid_map = NULL;
854 	new->ex_uuid = item->ex_uuid;
855 	item->ex_uuid = NULL;
856 	new->ex_fslocs.locations = item->ex_fslocs.locations;
857 	item->ex_fslocs.locations = NULL;
858 	new->ex_fslocs.locations_count = item->ex_fslocs.locations_count;
859 	item->ex_fslocs.locations_count = 0;
860 	new->ex_fslocs.migrated = item->ex_fslocs.migrated;
861 	item->ex_fslocs.migrated = 0;
862 	new->ex_layout_types = item->ex_layout_types;
863 	new->ex_nflavors = item->ex_nflavors;
864 	for (i = 0; i < MAX_SECINFO_LIST; i++) {
865 		new->ex_flavors[i] = item->ex_flavors[i];
866 	}
867 	new->ex_xprtsec_modes = item->ex_xprtsec_modes;
868 }
869 
svc_export_alloc(void)870 static struct cache_head *svc_export_alloc(void)
871 {
872 	struct svc_export *i = kmalloc(sizeof(*i), GFP_KERNEL);
873 	if (!i)
874 		return NULL;
875 
876 	i->ex_stats = kmalloc(sizeof(*(i->ex_stats)), GFP_KERNEL);
877 	if (!i->ex_stats) {
878 		kfree(i);
879 		return NULL;
880 	}
881 
882 	if (export_stats_init(i->ex_stats)) {
883 		kfree(i->ex_stats);
884 		kfree(i);
885 		return NULL;
886 	}
887 
888 	return &i->h;
889 }
890 
891 static const struct cache_detail svc_export_cache_template = {
892 	.owner		= THIS_MODULE,
893 	.hash_size	= EXPORT_HASHMAX,
894 	.name		= "nfsd.export",
895 	.cache_put	= svc_export_put,
896 	.cache_upcall	= svc_export_upcall,
897 	.cache_request	= svc_export_request,
898 	.cache_parse	= svc_export_parse,
899 	.cache_show	= svc_export_show,
900 	.match		= svc_export_match,
901 	.init		= svc_export_init,
902 	.update		= export_update,
903 	.alloc		= svc_export_alloc,
904 };
905 
906 static int
svc_export_hash(struct svc_export * exp)907 svc_export_hash(struct svc_export *exp)
908 {
909 	int hash;
910 
911 	hash = hash_ptr(exp->ex_client, EXPORT_HASHBITS);
912 	hash ^= hash_ptr(exp->ex_path.dentry, EXPORT_HASHBITS);
913 	hash ^= hash_ptr(exp->ex_path.mnt, EXPORT_HASHBITS);
914 	return hash;
915 }
916 
917 static struct svc_export *
svc_export_lookup(struct svc_export * exp)918 svc_export_lookup(struct svc_export *exp)
919 {
920 	struct cache_head *ch;
921 	int hash = svc_export_hash(exp);
922 
923 	ch = sunrpc_cache_lookup_rcu(exp->cd, &exp->h, hash);
924 	if (ch)
925 		return container_of(ch, struct svc_export, h);
926 	else
927 		return NULL;
928 }
929 
930 static struct svc_export *
svc_export_update(struct svc_export * new,struct svc_export * old)931 svc_export_update(struct svc_export *new, struct svc_export *old)
932 {
933 	struct cache_head *ch;
934 	int hash = svc_export_hash(old);
935 
936 	ch = sunrpc_cache_update(old->cd, &new->h, &old->h, hash);
937 	if (ch)
938 		return container_of(ch, struct svc_export, h);
939 	else
940 		return NULL;
941 }
942 
943 
944 static struct svc_expkey *
exp_find_key(struct cache_detail * cd,struct auth_domain * clp,int fsid_type,u32 * fsidv,struct cache_req * reqp)945 exp_find_key(struct cache_detail *cd, struct auth_domain *clp, int fsid_type,
946 	     u32 *fsidv, struct cache_req *reqp)
947 {
948 	struct svc_expkey key, *ek;
949 	int err;
950 
951 	if (!clp)
952 		return ERR_PTR(-ENOENT);
953 
954 	key.ek_client = clp;
955 	key.ek_fsidtype = fsid_type;
956 	memcpy(key.ek_fsid, fsidv, key_len(fsid_type));
957 
958 	ek = svc_expkey_lookup(cd, &key);
959 	if (ek == NULL)
960 		return ERR_PTR(-ENOMEM);
961 	err = cache_check(cd, &ek->h, reqp);
962 	if (err) {
963 		trace_nfsd_exp_find_key(&key, err);
964 		return ERR_PTR(err);
965 	}
966 	return ek;
967 }
968 
969 static struct svc_export *
exp_get_by_name(struct cache_detail * cd,struct auth_domain * clp,const struct path * path,struct cache_req * reqp)970 exp_get_by_name(struct cache_detail *cd, struct auth_domain *clp,
971 		const struct path *path, struct cache_req *reqp)
972 {
973 	struct svc_export *exp, key;
974 	int err;
975 
976 	if (!clp)
977 		return ERR_PTR(-ENOENT);
978 
979 	key.ex_client = clp;
980 	key.ex_path = *path;
981 	key.cd = cd;
982 
983 	exp = svc_export_lookup(&key);
984 	if (exp == NULL)
985 		return ERR_PTR(-ENOMEM);
986 	err = cache_check(cd, &exp->h, reqp);
987 	if (err) {
988 		trace_nfsd_exp_get_by_name(&key, err);
989 		return ERR_PTR(err);
990 	}
991 	return exp;
992 }
993 
994 /*
995  * Find the export entry for a given dentry.
996  */
997 static struct svc_export *
exp_parent(struct cache_detail * cd,struct auth_domain * clp,struct path * path)998 exp_parent(struct cache_detail *cd, struct auth_domain *clp, struct path *path)
999 {
1000 	struct dentry *saved = dget(path->dentry);
1001 	struct svc_export *exp = exp_get_by_name(cd, clp, path, NULL);
1002 
1003 	while (PTR_ERR(exp) == -ENOENT && !IS_ROOT(path->dentry)) {
1004 		struct dentry *parent = dget_parent(path->dentry);
1005 		dput(path->dentry);
1006 		path->dentry = parent;
1007 		exp = exp_get_by_name(cd, clp, path, NULL);
1008 	}
1009 	dput(path->dentry);
1010 	path->dentry = saved;
1011 	return exp;
1012 }
1013 
1014 
1015 
1016 /*
1017  * Obtain the root fh on behalf of a client.
1018  * This could be done in user space, but I feel that it adds some safety
1019  * since its harder to fool a kernel module than a user space program.
1020  */
1021 int
exp_rootfh(struct net * net,struct auth_domain * clp,char * name,struct knfsd_fh * f,int maxsize)1022 exp_rootfh(struct net *net, struct auth_domain *clp, char *name,
1023 	   struct knfsd_fh *f, int maxsize)
1024 {
1025 	struct svc_export	*exp;
1026 	struct path		path;
1027 	struct inode		*inode;
1028 	struct svc_fh		fh;
1029 	int			err;
1030 	struct nfsd_net		*nn = net_generic(net, nfsd_net_id);
1031 	struct cache_detail	*cd = nn->svc_export_cache;
1032 
1033 	err = -EPERM;
1034 	/* NB: we probably ought to check that it's NUL-terminated */
1035 	if (kern_path(name, 0, &path)) {
1036 		printk("nfsd: exp_rootfh path not found %s", name);
1037 		return err;
1038 	}
1039 	inode = d_inode(path.dentry);
1040 
1041 	dprintk("nfsd: exp_rootfh(%s [%p] %s:%s/%ld)\n",
1042 		 name, path.dentry, clp->name,
1043 		 inode->i_sb->s_id, inode->i_ino);
1044 	exp = exp_parent(cd, clp, &path);
1045 	if (IS_ERR(exp)) {
1046 		err = PTR_ERR(exp);
1047 		goto out;
1048 	}
1049 
1050 	/*
1051 	 * fh must be initialized before calling fh_compose
1052 	 */
1053 	fh_init(&fh, maxsize);
1054 	if (fh_compose(&fh, exp, path.dentry, NULL))
1055 		err = -EINVAL;
1056 	else
1057 		err = 0;
1058 	memcpy(f, &fh.fh_handle, sizeof(struct knfsd_fh));
1059 	fh_put(&fh);
1060 	exp_put(exp);
1061 out:
1062 	path_put(&path);
1063 	return err;
1064 }
1065 
exp_find(struct cache_detail * cd,struct auth_domain * clp,int fsid_type,u32 * fsidv,struct cache_req * reqp)1066 static struct svc_export *exp_find(struct cache_detail *cd,
1067 				   struct auth_domain *clp, int fsid_type,
1068 				   u32 *fsidv, struct cache_req *reqp)
1069 {
1070 	struct svc_export *exp;
1071 	struct nfsd_net *nn = net_generic(cd->net, nfsd_net_id);
1072 	struct svc_expkey *ek = exp_find_key(nn->svc_expkey_cache, clp, fsid_type, fsidv, reqp);
1073 	if (IS_ERR(ek))
1074 		return ERR_CAST(ek);
1075 
1076 	exp = exp_get_by_name(cd, clp, &ek->ek_path, reqp);
1077 	cache_put(&ek->h, nn->svc_expkey_cache);
1078 
1079 	if (IS_ERR(exp))
1080 		return ERR_CAST(exp);
1081 	return exp;
1082 }
1083 
1084 /**
1085  * check_nfsd_access - check if access to export is allowed.
1086  * @exp: svc_export that is being accessed.
1087  * @rqstp: svc_rqst attempting to access @exp (will be NULL for LOCALIO).
1088  * @may_bypass_gss: reduce strictness of authorization check
1089  *
1090  * Return values:
1091  *   %nfs_ok if access is granted, or
1092  *   %nfserr_wrongsec if access is denied
1093  */
check_nfsd_access(struct svc_export * exp,struct svc_rqst * rqstp,bool may_bypass_gss)1094 __be32 check_nfsd_access(struct svc_export *exp, struct svc_rqst *rqstp,
1095 			 bool may_bypass_gss)
1096 {
1097 	struct exp_flavor_info *f, *end = exp->ex_flavors + exp->ex_nflavors;
1098 	struct svc_xprt *xprt;
1099 
1100 	/*
1101 	 * If rqstp is NULL, this is a LOCALIO request which will only
1102 	 * ever use a filehandle/credential pair for which access has
1103 	 * been affirmed (by ACCESS or OPEN NFS requests) over the
1104 	 * wire. So there is no need for further checks here.
1105 	 */
1106 	if (!rqstp)
1107 		return nfs_ok;
1108 
1109 	xprt = rqstp->rq_xprt;
1110 
1111 	if (exp->ex_xprtsec_modes & NFSEXP_XPRTSEC_NONE) {
1112 		if (!test_bit(XPT_TLS_SESSION, &xprt->xpt_flags))
1113 			goto ok;
1114 	}
1115 	if (exp->ex_xprtsec_modes & NFSEXP_XPRTSEC_TLS) {
1116 		if (test_bit(XPT_TLS_SESSION, &xprt->xpt_flags) &&
1117 		    !test_bit(XPT_PEER_AUTH, &xprt->xpt_flags))
1118 			goto ok;
1119 	}
1120 	if (exp->ex_xprtsec_modes & NFSEXP_XPRTSEC_MTLS) {
1121 		if (test_bit(XPT_TLS_SESSION, &xprt->xpt_flags) &&
1122 		    test_bit(XPT_PEER_AUTH, &xprt->xpt_flags))
1123 			goto ok;
1124 	}
1125 	if (!may_bypass_gss)
1126 		goto denied;
1127 
1128 ok:
1129 	/* legacy gss-only clients are always OK: */
1130 	if (exp->ex_client == rqstp->rq_gssclient)
1131 		return nfs_ok;
1132 	/* ip-address based client; check sec= export option: */
1133 	for (f = exp->ex_flavors; f < end; f++) {
1134 		if (f->pseudoflavor == rqstp->rq_cred.cr_flavor)
1135 			return nfs_ok;
1136 	}
1137 	/* defaults in absence of sec= options: */
1138 	if (exp->ex_nflavors == 0) {
1139 		if (rqstp->rq_cred.cr_flavor == RPC_AUTH_NULL ||
1140 		    rqstp->rq_cred.cr_flavor == RPC_AUTH_UNIX)
1141 			return nfs_ok;
1142 	}
1143 
1144 	/* If the compound op contains a spo_must_allowed op,
1145 	 * it will be sent with integrity/protection which
1146 	 * will have to be expressly allowed on mounts that
1147 	 * don't support it
1148 	 */
1149 
1150 	if (nfsd4_spo_must_allow(rqstp))
1151 		return nfs_ok;
1152 
1153 	/* Some calls may be processed without authentication
1154 	 * on GSS exports. For example NFS2/3 calls on root
1155 	 * directory, see section 2.3.2 of rfc 2623.
1156 	 * For "may_bypass_gss" check that export has really
1157 	 * enabled some flavor with authentication (GSS or any
1158 	 * other) and also check that the used auth flavor is
1159 	 * without authentication (none or sys).
1160 	 */
1161 	if (may_bypass_gss && (
1162 	     rqstp->rq_cred.cr_flavor == RPC_AUTH_NULL ||
1163 	     rqstp->rq_cred.cr_flavor == RPC_AUTH_UNIX)) {
1164 		for (f = exp->ex_flavors; f < end; f++) {
1165 			if (f->pseudoflavor >= RPC_AUTH_DES)
1166 				return 0;
1167 		}
1168 	}
1169 
1170 denied:
1171 	return nfserr_wrongsec;
1172 }
1173 
1174 /*
1175  * Uses rq_client and rq_gssclient to find an export; uses rq_client (an
1176  * auth_unix client) if it's available and has secinfo information;
1177  * otherwise, will try to use rq_gssclient.
1178  *
1179  * Called from functions that handle requests; functions that do work on
1180  * behalf of mountd are passed a single client name to use, and should
1181  * use exp_get_by_name() or exp_find().
1182  */
1183 struct svc_export *
rqst_exp_get_by_name(struct svc_rqst * rqstp,struct path * path)1184 rqst_exp_get_by_name(struct svc_rqst *rqstp, struct path *path)
1185 {
1186 	struct svc_export *gssexp, *exp = ERR_PTR(-ENOENT);
1187 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1188 	struct cache_detail *cd = nn->svc_export_cache;
1189 
1190 	if (rqstp->rq_client == NULL)
1191 		goto gss;
1192 
1193 	/* First try the auth_unix client: */
1194 	exp = exp_get_by_name(cd, rqstp->rq_client, path, &rqstp->rq_chandle);
1195 	if (PTR_ERR(exp) == -ENOENT)
1196 		goto gss;
1197 	if (IS_ERR(exp))
1198 		return exp;
1199 	/* If it has secinfo, assume there are no gss/... clients */
1200 	if (exp->ex_nflavors > 0)
1201 		return exp;
1202 gss:
1203 	/* Otherwise, try falling back on gss client */
1204 	if (rqstp->rq_gssclient == NULL)
1205 		return exp;
1206 	gssexp = exp_get_by_name(cd, rqstp->rq_gssclient, path, &rqstp->rq_chandle);
1207 	if (PTR_ERR(gssexp) == -ENOENT)
1208 		return exp;
1209 	if (!IS_ERR(exp))
1210 		exp_put(exp);
1211 	return gssexp;
1212 }
1213 
1214 /**
1215  * rqst_exp_find - Find an svc_export in the context of a rqst or similar
1216  * @reqp:	The handle to be used to suspend the request if a cache-upcall is needed
1217  *		If NULL, missing in-cache information will result in failure.
1218  * @net:	The network namespace in which the request exists
1219  * @cl:		default auth_domain to use for looking up the export
1220  * @gsscl:	an alternate auth_domain defined using deprecated gss/krb5 format.
1221  * @fsid_type:	The type of fsid to look for
1222  * @fsidv:	The actual fsid to look up in the context of either client.
1223  *
1224  * Perform a lookup for @cl/@fsidv in the given @net for an export.  If
1225  * none found and @gsscl specified, repeat the lookup.
1226  *
1227  * Returns an export, or an error pointer.
1228  */
1229 struct svc_export *
rqst_exp_find(struct cache_req * reqp,struct net * net,struct auth_domain * cl,struct auth_domain * gsscl,int fsid_type,u32 * fsidv)1230 rqst_exp_find(struct cache_req *reqp, struct net *net,
1231 	      struct auth_domain *cl, struct auth_domain *gsscl,
1232 	      int fsid_type, u32 *fsidv)
1233 {
1234 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1235 	struct svc_export *gssexp, *exp = ERR_PTR(-ENOENT);
1236 	struct cache_detail *cd = nn->svc_export_cache;
1237 
1238 	if (!cl)
1239 		goto gss;
1240 
1241 	/* First try the auth_unix client: */
1242 	exp = exp_find(cd, cl, fsid_type, fsidv, reqp);
1243 	if (PTR_ERR(exp) == -ENOENT)
1244 		goto gss;
1245 	if (IS_ERR(exp))
1246 		return exp;
1247 	/* If it has secinfo, assume there are no gss/... clients */
1248 	if (exp->ex_nflavors > 0)
1249 		return exp;
1250 gss:
1251 	/* Otherwise, try falling back on gss client */
1252 	if (!gsscl)
1253 		return exp;
1254 	gssexp = exp_find(cd, gsscl, fsid_type, fsidv, reqp);
1255 	if (PTR_ERR(gssexp) == -ENOENT)
1256 		return exp;
1257 	if (!IS_ERR(exp))
1258 		exp_put(exp);
1259 	return gssexp;
1260 }
1261 
1262 struct svc_export *
rqst_exp_parent(struct svc_rqst * rqstp,struct path * path)1263 rqst_exp_parent(struct svc_rqst *rqstp, struct path *path)
1264 {
1265 	struct dentry *saved = dget(path->dentry);
1266 	struct svc_export *exp = rqst_exp_get_by_name(rqstp, path);
1267 
1268 	while (PTR_ERR(exp) == -ENOENT && !IS_ROOT(path->dentry)) {
1269 		struct dentry *parent = dget_parent(path->dentry);
1270 		dput(path->dentry);
1271 		path->dentry = parent;
1272 		exp = rqst_exp_get_by_name(rqstp, path);
1273 	}
1274 	dput(path->dentry);
1275 	path->dentry = saved;
1276 	return exp;
1277 }
1278 
rqst_find_fsidzero_export(struct svc_rqst * rqstp)1279 struct svc_export *rqst_find_fsidzero_export(struct svc_rqst *rqstp)
1280 {
1281 	u32 fsidv[2];
1282 
1283 	mk_fsid(FSID_NUM, fsidv, 0, 0, 0, NULL);
1284 
1285 	return rqst_exp_find(&rqstp->rq_chandle, SVC_NET(rqstp),
1286 			     rqstp->rq_client, rqstp->rq_gssclient,
1287 			     FSID_NUM, fsidv);
1288 }
1289 
1290 /*
1291  * Called when we need the filehandle for the root of the pseudofs,
1292  * for a given NFSv4 client.   The root is defined to be the
1293  * export point with fsid==0
1294  */
1295 __be32
exp_pseudoroot(struct svc_rqst * rqstp,struct svc_fh * fhp)1296 exp_pseudoroot(struct svc_rqst *rqstp, struct svc_fh *fhp)
1297 {
1298 	struct svc_export *exp;
1299 	__be32 rv;
1300 
1301 	exp = rqst_find_fsidzero_export(rqstp);
1302 	if (IS_ERR(exp))
1303 		return nfserrno(PTR_ERR(exp));
1304 	rv = fh_compose(fhp, exp, exp->ex_path.dentry, NULL);
1305 	exp_put(exp);
1306 	return rv;
1307 }
1308 
1309 static struct flags {
1310 	int flag;
1311 	char *name[2];
1312 } expflags[] = {
1313 	{ NFSEXP_READONLY, {"ro", "rw"}},
1314 	{ NFSEXP_INSECURE_PORT, {"insecure", ""}},
1315 	{ NFSEXP_ROOTSQUASH, {"root_squash", "no_root_squash"}},
1316 	{ NFSEXP_ALLSQUASH, {"all_squash", ""}},
1317 	{ NFSEXP_ASYNC, {"async", "sync"}},
1318 	{ NFSEXP_GATHERED_WRITES, {"wdelay", "no_wdelay"}},
1319 	{ NFSEXP_NOREADDIRPLUS, {"nordirplus", ""}},
1320 	{ NFSEXP_NOHIDE, {"nohide", ""}},
1321 	{ NFSEXP_CROSSMOUNT, {"crossmnt", ""}},
1322 	{ NFSEXP_NOSUBTREECHECK, {"no_subtree_check", ""}},
1323 	{ NFSEXP_NOAUTHNLM, {"insecure_locks", ""}},
1324 	{ NFSEXP_V4ROOT, {"v4root", ""}},
1325 	{ NFSEXP_PNFS, {"pnfs", ""}},
1326 	{ NFSEXP_SECURITY_LABEL, {"security_label", ""}},
1327 	{ 0, {"", ""}}
1328 };
1329 
show_expflags(struct seq_file * m,int flags,int mask)1330 static void show_expflags(struct seq_file *m, int flags, int mask)
1331 {
1332 	struct flags *flg;
1333 	int state, first = 0;
1334 
1335 	for (flg = expflags; flg->flag; flg++) {
1336 		if (flg->flag & ~mask)
1337 			continue;
1338 		state = (flg->flag & flags) ? 0 : 1;
1339 		if (*flg->name[state])
1340 			seq_printf(m, "%s%s", first++?",":"", flg->name[state]);
1341 	}
1342 }
1343 
show_secinfo_flags(struct seq_file * m,int flags)1344 static void show_secinfo_flags(struct seq_file *m, int flags)
1345 {
1346 	seq_printf(m, ",");
1347 	show_expflags(m, flags, NFSEXP_SECINFO_FLAGS);
1348 }
1349 
secinfo_flags_equal(int f,int g)1350 static bool secinfo_flags_equal(int f, int g)
1351 {
1352 	f &= NFSEXP_SECINFO_FLAGS;
1353 	g &= NFSEXP_SECINFO_FLAGS;
1354 	return f == g;
1355 }
1356 
show_secinfo_run(struct seq_file * m,struct exp_flavor_info ** fp,struct exp_flavor_info * end)1357 static int show_secinfo_run(struct seq_file *m, struct exp_flavor_info **fp, struct exp_flavor_info *end)
1358 {
1359 	int flags;
1360 
1361 	flags = (*fp)->flags;
1362 	seq_printf(m, ",sec=%d", (*fp)->pseudoflavor);
1363 	(*fp)++;
1364 	while (*fp != end && secinfo_flags_equal(flags, (*fp)->flags)) {
1365 		seq_printf(m, ":%d", (*fp)->pseudoflavor);
1366 		(*fp)++;
1367 	}
1368 	return flags;
1369 }
1370 
show_secinfo(struct seq_file * m,struct svc_export * exp)1371 static void show_secinfo(struct seq_file *m, struct svc_export *exp)
1372 {
1373 	struct exp_flavor_info *f;
1374 	struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
1375 	int flags;
1376 
1377 	if (exp->ex_nflavors == 0)
1378 		return;
1379 	f = exp->ex_flavors;
1380 	flags = show_secinfo_run(m, &f, end);
1381 	if (!secinfo_flags_equal(flags, exp->ex_flags))
1382 		show_secinfo_flags(m, flags);
1383 	while (f != end) {
1384 		flags = show_secinfo_run(m, &f, end);
1385 		show_secinfo_flags(m, flags);
1386 	}
1387 }
1388 
exp_flags(struct seq_file * m,int flag,int fsid,kuid_t anonu,kgid_t anong,struct nfsd4_fs_locations * fsloc)1389 static void exp_flags(struct seq_file *m, int flag, int fsid,
1390 		kuid_t anonu, kgid_t anong, struct nfsd4_fs_locations *fsloc)
1391 {
1392 	struct user_namespace *userns = m->file->f_cred->user_ns;
1393 
1394 	show_expflags(m, flag, NFSEXP_ALLFLAGS);
1395 	if (flag & NFSEXP_FSID)
1396 		seq_printf(m, ",fsid=%d", fsid);
1397 	if (!uid_eq(anonu, make_kuid(userns, (uid_t)-2)) &&
1398 	    !uid_eq(anonu, make_kuid(userns, 0x10000-2)))
1399 		seq_printf(m, ",anonuid=%u", from_kuid_munged(userns, anonu));
1400 	if (!gid_eq(anong, make_kgid(userns, (gid_t)-2)) &&
1401 	    !gid_eq(anong, make_kgid(userns, 0x10000-2)))
1402 		seq_printf(m, ",anongid=%u", from_kgid_munged(userns, anong));
1403 	if (fsloc && fsloc->locations_count > 0) {
1404 		char *loctype = (fsloc->migrated) ? "refer" : "replicas";
1405 		int i;
1406 
1407 		seq_printf(m, ",%s=", loctype);
1408 		seq_escape(m, fsloc->locations[0].path, ",;@ \t\n\\");
1409 		seq_putc(m, '@');
1410 		seq_escape(m, fsloc->locations[0].hosts, ",;@ \t\n\\");
1411 		for (i = 1; i < fsloc->locations_count; i++) {
1412 			seq_putc(m, ';');
1413 			seq_escape(m, fsloc->locations[i].path, ",;@ \t\n\\");
1414 			seq_putc(m, '@');
1415 			seq_escape(m, fsloc->locations[i].hosts, ",;@ \t\n\\");
1416 		}
1417 	}
1418 }
1419 
e_show(struct seq_file * m,void * p)1420 static int e_show(struct seq_file *m, void *p)
1421 {
1422 	struct cache_head *cp = p;
1423 	struct svc_export *exp = container_of(cp, struct svc_export, h);
1424 	struct cache_detail *cd = m->private;
1425 	bool export_stats = is_export_stats_file(m);
1426 
1427 	if (p == SEQ_START_TOKEN) {
1428 		seq_puts(m, "# Version 1.1\n");
1429 		if (export_stats)
1430 			seq_puts(m, "# Path Client Start-time\n#\tStats\n");
1431 		else
1432 			seq_puts(m, "# Path Client(Flags) # IPs\n");
1433 		return 0;
1434 	}
1435 
1436 	if (cache_check_rcu(cd, &exp->h, NULL))
1437 		return 0;
1438 
1439 	return svc_export_show(m, cd, cp);
1440 }
1441 
1442 const struct seq_operations nfs_exports_op = {
1443 	.start	= cache_seq_start_rcu,
1444 	.next	= cache_seq_next_rcu,
1445 	.stop	= cache_seq_stop_rcu,
1446 	.show	= e_show,
1447 };
1448 
1449 /*
1450  * Initialize the exports module.
1451  */
1452 int
nfsd_export_init(struct net * net)1453 nfsd_export_init(struct net *net)
1454 {
1455 	int rv;
1456 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1457 
1458 	dprintk("nfsd: initializing export module (net: %x).\n", net->ns.inum);
1459 
1460 	nn->svc_export_cache = cache_create_net(&svc_export_cache_template, net);
1461 	if (IS_ERR(nn->svc_export_cache))
1462 		return PTR_ERR(nn->svc_export_cache);
1463 	rv = cache_register_net(nn->svc_export_cache, net);
1464 	if (rv)
1465 		goto destroy_export_cache;
1466 
1467 	nn->svc_expkey_cache = cache_create_net(&svc_expkey_cache_template, net);
1468 	if (IS_ERR(nn->svc_expkey_cache)) {
1469 		rv = PTR_ERR(nn->svc_expkey_cache);
1470 		goto unregister_export_cache;
1471 	}
1472 	rv = cache_register_net(nn->svc_expkey_cache, net);
1473 	if (rv)
1474 		goto destroy_expkey_cache;
1475 	return 0;
1476 
1477 destroy_expkey_cache:
1478 	cache_destroy_net(nn->svc_expkey_cache, net);
1479 unregister_export_cache:
1480 	cache_unregister_net(nn->svc_export_cache, net);
1481 destroy_export_cache:
1482 	cache_destroy_net(nn->svc_export_cache, net);
1483 	return rv;
1484 }
1485 
1486 /*
1487  * Flush exports table - called when last nfsd thread is killed
1488  */
1489 void
nfsd_export_flush(struct net * net)1490 nfsd_export_flush(struct net *net)
1491 {
1492 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1493 
1494 	cache_purge(nn->svc_expkey_cache);
1495 	cache_purge(nn->svc_export_cache);
1496 }
1497 
1498 /*
1499  * Shutdown the exports module.
1500  */
1501 void
nfsd_export_shutdown(struct net * net)1502 nfsd_export_shutdown(struct net *net)
1503 {
1504 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1505 
1506 	dprintk("nfsd: shutting down export module (net: %x).\n", net->ns.inum);
1507 
1508 	cache_unregister_net(nn->svc_expkey_cache, net);
1509 	cache_unregister_net(nn->svc_export_cache, net);
1510 	cache_destroy_net(nn->svc_expkey_cache, net);
1511 	cache_destroy_net(nn->svc_export_cache, net);
1512 	svcauth_unix_purge(net);
1513 
1514 	dprintk("nfsd: export shutdown complete (net: %x).\n", net->ns.inum);
1515 }
1516