Linux Audio

Check our new training course

Loading...
v3.15
 
 1/* Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> */
 2
 3#include <linux/sched.h>
 4#include <linux/user_namespace.h>
 5#include "nfsd.h"
 6#include "auth.h"
 7
 8int nfsexp_flags(struct svc_rqst *rqstp, struct svc_export *exp)
 9{
10	struct exp_flavor_info *f;
11	struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
12
13	for (f = exp->ex_flavors; f < end; f++) {
14		if (f->pseudoflavor == rqstp->rq_cred.cr_flavor)
15			return f->flags;
16	}
17	return exp->ex_flags;
18
19}
20
21int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp)
22{
23	struct group_info *rqgi;
24	struct group_info *gi;
25	struct cred *new;
26	int i;
27	int flags = nfsexp_flags(rqstp, exp);
28	int ret;
29
30	validate_process_creds();
31
32	/* discard any old override before preparing the new set */
33	revert_creds(get_cred(current->real_cred));
34	new = prepare_creds();
35	if (!new)
36		return -ENOMEM;
37
38	new->fsuid = rqstp->rq_cred.cr_uid;
39	new->fsgid = rqstp->rq_cred.cr_gid;
40
41	rqgi = rqstp->rq_cred.cr_group_info;
42
43	if (flags & NFSEXP_ALLSQUASH) {
44		new->fsuid = exp->ex_anon_uid;
45		new->fsgid = exp->ex_anon_gid;
46		gi = groups_alloc(0);
47		if (!gi)
48			goto oom;
49	} else if (flags & NFSEXP_ROOTSQUASH) {
50		if (uid_eq(new->fsuid, GLOBAL_ROOT_UID))
51			new->fsuid = exp->ex_anon_uid;
52		if (gid_eq(new->fsgid, GLOBAL_ROOT_GID))
53			new->fsgid = exp->ex_anon_gid;
54
55		gi = groups_alloc(rqgi->ngroups);
56		if (!gi)
57			goto oom;
58
59		for (i = 0; i < rqgi->ngroups; i++) {
60			if (gid_eq(GLOBAL_ROOT_GID, GROUP_AT(rqgi, i)))
61				GROUP_AT(gi, i) = exp->ex_anon_gid;
62			else
63				GROUP_AT(gi, i) = GROUP_AT(rqgi, i);
64		}
 
 
 
65	} else {
66		gi = get_group_info(rqgi);
67	}
68
69	if (uid_eq(new->fsuid, INVALID_UID))
70		new->fsuid = exp->ex_anon_uid;
71	if (gid_eq(new->fsgid, INVALID_GID))
72		new->fsgid = exp->ex_anon_gid;
73
74	set_groups(new, gi);
75	put_group_info(gi);
76
77	if (!uid_eq(new->fsuid, GLOBAL_ROOT_UID))
78		new->cap_effective = cap_drop_nfsd_set(new->cap_effective);
79	else
80		new->cap_effective = cap_raise_nfsd_set(new->cap_effective,
81							new->cap_permitted);
82	validate_process_creds();
83	put_cred(override_creds(new));
84	put_cred(new);
85	validate_process_creds();
86	return 0;
87
88oom:
89	ret = -ENOMEM;
90	abort_creds(new);
91	return ret;
92}
93
v5.14.15
 1// SPDX-License-Identifier: GPL-2.0
 2/* Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> */
 3
 4#include <linux/sched.h>
 
 5#include "nfsd.h"
 6#include "auth.h"
 7
 8int nfsexp_flags(struct svc_rqst *rqstp, struct svc_export *exp)
 9{
10	struct exp_flavor_info *f;
11	struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
12
13	for (f = exp->ex_flavors; f < end; f++) {
14		if (f->pseudoflavor == rqstp->rq_cred.cr_flavor)
15			return f->flags;
16	}
17	return exp->ex_flags;
18
19}
20
21int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp)
22{
23	struct group_info *rqgi;
24	struct group_info *gi;
25	struct cred *new;
26	int i;
27	int flags = nfsexp_flags(rqstp, exp);
 
28
29	validate_process_creds();
30
31	/* discard any old override before preparing the new set */
32	revert_creds(get_cred(current_real_cred()));
33	new = prepare_creds();
34	if (!new)
35		return -ENOMEM;
36
37	new->fsuid = rqstp->rq_cred.cr_uid;
38	new->fsgid = rqstp->rq_cred.cr_gid;
39
40	rqgi = rqstp->rq_cred.cr_group_info;
41
42	if (flags & NFSEXP_ALLSQUASH) {
43		new->fsuid = exp->ex_anon_uid;
44		new->fsgid = exp->ex_anon_gid;
45		gi = groups_alloc(0);
46		if (!gi)
47			goto oom;
48	} else if (flags & NFSEXP_ROOTSQUASH) {
49		if (uid_eq(new->fsuid, GLOBAL_ROOT_UID))
50			new->fsuid = exp->ex_anon_uid;
51		if (gid_eq(new->fsgid, GLOBAL_ROOT_GID))
52			new->fsgid = exp->ex_anon_gid;
53
54		gi = groups_alloc(rqgi->ngroups);
55		if (!gi)
56			goto oom;
57
58		for (i = 0; i < rqgi->ngroups; i++) {
59			if (gid_eq(GLOBAL_ROOT_GID, rqgi->gid[i]))
60				gi->gid[i] = exp->ex_anon_gid;
61			else
62				gi->gid[i] = rqgi->gid[i];
63		}
64
65		/* Each thread allocates its own gi, no race */
66		groups_sort(gi);
67	} else {
68		gi = get_group_info(rqgi);
69	}
70
71	if (uid_eq(new->fsuid, INVALID_UID))
72		new->fsuid = exp->ex_anon_uid;
73	if (gid_eq(new->fsgid, INVALID_GID))
74		new->fsgid = exp->ex_anon_gid;
75
76	set_groups(new, gi);
77	put_group_info(gi);
78
79	if (!uid_eq(new->fsuid, GLOBAL_ROOT_UID))
80		new->cap_effective = cap_drop_nfsd_set(new->cap_effective);
81	else
82		new->cap_effective = cap_raise_nfsd_set(new->cap_effective,
83							new->cap_permitted);
84	validate_process_creds();
85	put_cred(override_creds(new));
86	put_cred(new);
87	validate_process_creds();
88	return 0;
89
90oom:
 
91	abort_creds(new);
92	return -ENOMEM;
93}
94