auth.c 2.1 KB
Newer Older
1
/* Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> */
L
Linus Torvalds 已提交
2 3

#include <linux/sched.h>
4
#include <linux/user_namespace.h>
5
#include "nfsd.h"
H
Harvey Harrison 已提交
6
#include "auth.h"
L
Linus Torvalds 已提交
7

J
J. Bruce Fields 已提交
8
int nfsexp_flags(struct svc_rqst *rqstp, struct svc_export *exp)
9 10 11 12 13
{
	struct exp_flavor_info *f;
	struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;

	for (f = exp->ex_flavors; f < end; f++) {
14
		if (f->pseudoflavor == rqstp->rq_cred.cr_flavor)
15 16 17 18 19 20
			return f->flags;
	}
	return exp->ex_flags;

}

L
Linus Torvalds 已提交
21 22
int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp)
{
D
David Howells 已提交
23 24 25
	struct group_info *rqgi;
	struct group_info *gi;
	struct cred *new;
L
Linus Torvalds 已提交
26
	int i;
27
	int flags = nfsexp_flags(rqstp, exp);
L
Linus Torvalds 已提交
28 29
	int ret;

30 31
	validate_process_creds();

32 33
	/* discard any old override before preparing the new set */
	revert_creds(get_cred(current->real_cred));
D
David Howells 已提交
34 35 36 37 38 39 40 41 42
	new = prepare_creds();
	if (!new)
		return -ENOMEM;

	new->fsuid = rqstp->rq_cred.cr_uid;
	new->fsgid = rqstp->rq_cred.cr_gid;

	rqgi = rqstp->rq_cred.cr_group_info;

43
	if (flags & NFSEXP_ALLSQUASH) {
D
David Howells 已提交
44 45 46
		new->fsuid = exp->ex_anon_uid;
		new->fsgid = exp->ex_anon_gid;
		gi = groups_alloc(0);
47 48
		if (!gi)
			goto oom;
49
	} else if (flags & NFSEXP_ROOTSQUASH) {
D
David Howells 已提交
50 51 52 53
		if (!new->fsuid)
			new->fsuid = exp->ex_anon_uid;
		if (!new->fsgid)
			new->fsgid = exp->ex_anon_gid;
L
Linus Torvalds 已提交
54

D
David Howells 已提交
55 56 57 58 59
		gi = groups_alloc(rqgi->ngroups);
		if (!gi)
			goto oom;

		for (i = 0; i < rqgi->ngroups; i++) {
60 61
			if (gid_eq(GLOBAL_ROOT_GID, GROUP_AT(rqgi, i)))
				GROUP_AT(gi, i) = make_kgid(&init_user_ns, exp->ex_anon_gid);
D
David Howells 已提交
62 63 64
			else
				GROUP_AT(gi, i) = GROUP_AT(rqgi, i);
		}
L
Linus Torvalds 已提交
65
	} else {
D
David Howells 已提交
66
		gi = get_group_info(rqgi);
L
Linus Torvalds 已提交
67
	}
D
David Howells 已提交
68 69 70 71 72 73 74 75

	if (new->fsuid == (uid_t) -1)
		new->fsuid = exp->ex_anon_uid;
	if (new->fsgid == (gid_t) -1)
		new->fsgid = exp->ex_anon_gid;

	ret = set_groups(new, gi);
	put_group_info(gi);
D
David Howells 已提交
76
	if (ret < 0)
D
David Howells 已提交
77 78
		goto error;

D
David Howells 已提交
79
	if (new->fsuid)
D
David Howells 已提交
80 81 82 83
		new->cap_effective = cap_drop_nfsd_set(new->cap_effective);
	else
		new->cap_effective = cap_raise_nfsd_set(new->cap_effective,
							new->cap_permitted);
84
	validate_process_creds();
85
	put_cred(override_creds(new));
86
	put_cred(new);
87
	validate_process_creds();
88
	return 0;
D
David Howells 已提交
89 90 91 92 93

oom:
	ret = -ENOMEM;
error:
	abort_creds(new);
L
Linus Torvalds 已提交
94 95
	return ret;
}
96