Skip to content

Commit bdcf0a4

Browse files
trbeckertorvalds
authored andcommitted
kernel: make groups_sort calling a responsibility group_info allocators
In testing, we found that nfsd threads may call set_groups in parallel for the same entry cached in auth.unix.gid, racing in the call of groups_sort, corrupting the groups for that entry and leading to permission denials for the client. This patch: - Make groups_sort globally visible. - Move the call to groups_sort to the modifiers of group_info - Remove the call to groups_sort from set_groups Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Thiago Rafael Becker <[email protected]> Reviewed-by: Matthew Wilcox <[email protected]> Reviewed-by: NeilBrown <[email protected]> Acked-by: "J. Bruce Fields" <[email protected]> Cc: Al Viro <[email protected]> Cc: Martin Schwidefsky <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 1f704fd commit bdcf0a4

File tree

8 files changed

+13
-2
lines changed

8 files changed

+13
-2
lines changed

arch/s390/kernel/compat_linux.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ COMPAT_SYSCALL_DEFINE2(s390_setgroups16, int, gidsetsize, u16 __user *, grouplis
263263
return retval;
264264
}
265265

266+
groups_sort(group_info);
266267
retval = set_current_groups(group_info);
267268
put_group_info(group_info);
268269

fs/nfsd/auth.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp)
6060
gi->gid[i] = exp->ex_anon_gid;
6161
else
6262
gi->gid[i] = rqgi->gid[i];
63+
64+
/* Each thread allocates its own gi, no race */
65+
groups_sort(gi);
6366
}
6467
} else {
6568
gi = get_group_info(rqgi);

include/linux/cred.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ extern int set_current_groups(struct group_info *);
8383
extern void set_groups(struct cred *, struct group_info *);
8484
extern int groups_search(const struct group_info *, kgid_t);
8585
extern bool may_setgroups(void);
86+
extern void groups_sort(struct group_info *);
8687

8788
/*
8889
* The security context of a task

kernel/groups.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ static int gid_cmp(const void *_a, const void *_b)
8686
return gid_gt(a, b) - gid_lt(a, b);
8787
}
8888

89-
static void groups_sort(struct group_info *group_info)
89+
void groups_sort(struct group_info *group_info)
9090
{
9191
sort(group_info->gid, group_info->ngroups, sizeof(*group_info->gid),
9292
gid_cmp, NULL);
9393
}
94+
EXPORT_SYMBOL(groups_sort);
9495

9596
/* a simple bsearch */
9697
int groups_search(const struct group_info *group_info, kgid_t grp)
@@ -122,7 +123,6 @@ int groups_search(const struct group_info *group_info, kgid_t grp)
122123
void set_groups(struct cred *new, struct group_info *group_info)
123124
{
124125
put_group_info(new->group_info);
125-
groups_sort(group_info);
126126
get_group_info(group_info);
127127
new->group_info = group_info;
128128
}
@@ -206,6 +206,7 @@ SYSCALL_DEFINE2(setgroups, int, gidsetsize, gid_t __user *, grouplist)
206206
return retval;
207207
}
208208

209+
groups_sort(group_info);
209210
retval = set_current_groups(group_info);
210211
put_group_info(group_info);
211212

kernel/uid16.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ SYSCALL_DEFINE2(setgroups16, int, gidsetsize, old_gid_t __user *, grouplist)
192192
return retval;
193193
}
194194

195+
groups_sort(group_info);
195196
retval = set_current_groups(group_info);
196197
put_group_info(group_info);
197198

net/sunrpc/auth_gss/gss_rpc_xdr.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ static int gssx_dec_linux_creds(struct xdr_stream *xdr,
231231
goto out_free_groups;
232232
creds->cr_group_info->gid[i] = kgid;
233233
}
234+
groups_sort(creds->cr_group_info);
234235

235236
return 0;
236237
out_free_groups:

net/sunrpc/auth_gss/svcauth_gss.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ static int rsc_parse(struct cache_detail *cd,
481481
goto out;
482482
rsci.cred.cr_group_info->gid[i] = kgid;
483483
}
484+
groups_sort(rsci.cred.cr_group_info);
484485

485486
/* mech name */
486487
len = qword_get(&mesg, buf, mlen);

net/sunrpc/svcauth_unix.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ static int unix_gid_parse(struct cache_detail *cd,
520520
ug.gi->gid[i] = kgid;
521521
}
522522

523+
groups_sort(ug.gi);
523524
ugp = unix_gid_lookup(cd, uid);
524525
if (ugp) {
525526
struct cache_head *ch;
@@ -819,6 +820,7 @@ svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
819820
kgid_t kgid = make_kgid(&init_user_ns, svc_getnl(argv));
820821
cred->cr_group_info->gid[i] = kgid;
821822
}
823+
groups_sort(cred->cr_group_info);
822824
if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
823825
*authp = rpc_autherr_badverf;
824826
return SVC_DENIED;

0 commit comments

Comments
 (0)