Skip to content

Commit 81243ea

Browse files
Alexey Dobriyantorvalds
authored andcommitted
cred: simpler, 1D supplementary groups
Current supplementary groups code can massively overallocate memory and is implemented in a way so that access to individual gid is done via 2D array. If number of gids is <= 32, memory allocation is more or less tolerable (140/148 bytes). But if it is not, code allocates full page (!) regardless and, what's even more fun, doesn't reuse small 32-entry array. 2D array means dependent shifts, loads and LEAs without possibility to optimize them (gid is never known at compile time). All of the above is unnecessary. Switch to the usual trailing-zero-len-array scheme. Memory is allocated with kmalloc/vmalloc() and only as much as needed. Accesses become simpler (LEA 8(gi,idx,4) or even without displacement). Maximum number of gids is 65536 which translates to 256KB+8 bytes. I think kernel can handle such allocation. On my usual desktop system with whole 9 (nine) aux groups, struct group_info shrinks from 148 bytes to 44 bytes, yay! Nice side effects: - "gi->gid[i]" is shorter than "GROUP_AT(gi, i)", less typing, - fix little mess in net/ipv4/ping.c should have been using GROUP_AT macro but this point becomes moot, - aux group allocation is persistent and should be accounted as such. Link: http://lkml.kernel.org/r/20160817201927.GA2096@p183.telecom.by Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com> Cc: Vasily Kulikov <segoon@openwall.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 954f74b commit 81243ea

14 files changed

Lines changed: 46 additions & 85 deletions

File tree

‎arch/s390/kernel/compat_linux.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ static int groups16_to_user(u16 __user *grouplist, struct group_info *group_info
189189
kgid_t kgid;
190190

191191
for (i = 0; i < group_info->ngroups; i++) {
192-
kgid = GROUP_AT(group_info, i);
192+
kgid = group_info->gid[i];
193193
group = (u16)from_kgid_munged(user_ns, kgid);
194194
if (put_user(group, grouplist+i))
195195
return -EFAULT;
@@ -213,7 +213,7 @@ static int groups16_from_user(struct group_info *group_info, u16 __user *groupli
213213
if (!gid_valid(kgid))
214214
return -EINVAL;
215215

216-
GROUP_AT(group_info, i) = kgid;
216+
group_info->gid[i] = kgid;
217217
}
218218

219219
return 0;

‎drivers/staging/lustre/lustre/ptlrpc/sec.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2220,7 +2220,7 @@ int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset)
22202220
task_lock(current);
22212221
if (pud->pud_ngroups > current_ngroups)
22222222
pud->pud_ngroups = current_ngroups;
2223-
memcpy(pud->pud_groups, current_cred()->group_info->blocks[0],
2223+
memcpy(pud->pud_groups, current_cred()->group_info->gid,
22242224
pud->pud_ngroups * sizeof(__u32));
22252225
task_unlock(current);
22262226

‎fs/nfsd/auth.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp)
5555
goto oom;
5656

5757
for (i = 0; i < rqgi->ngroups; i++) {
58-
if (gid_eq(GLOBAL_ROOT_GID, GROUP_AT(rqgi, i)))
59-
GROUP_AT(gi, i) = exp->ex_anon_gid;
58+
if (gid_eq(GLOBAL_ROOT_GID, rqgi->gid[i]))
59+
gi->gid[i] = exp->ex_anon_gid;
6060
else
61-
GROUP_AT(gi, i) = GROUP_AT(rqgi, i);
61+
gi->gid[i] = rqgi->gid[i];
6262
}
6363
} else {
6464
gi = get_group_info(rqgi);

‎fs/nfsd/nfs4state.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1903,7 +1903,7 @@ static bool groups_equal(struct group_info *g1, struct group_info *g2)
19031903
if (g1->ngroups != g2->ngroups)
19041904
return false;
19051905
for (i=0; i<g1->ngroups; i++)
1906-
if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1906+
if (!gid_eq(g1->gid[i], g2->gid[i]))
19071907
return false;
19081908
return true;
19091909
}

‎fs/proc/array.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
207207
group_info = cred->group_info;
208208
for (g = 0; g < group_info->ngroups; g++)
209209
seq_put_decimal_ull(m, g ? " " : "",
210-
from_kgid_munged(user_ns, GROUP_AT(group_info, g)));
210+
from_kgid_munged(user_ns, group_info->gid[g]));
211211
put_cred(cred);
212212
/* Trailing space shouldn't have been added in the first place. */
213213
seq_putc(m, ' ');

‎include/linux/cred.h‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,10 @@ struct inode;
2626
/*
2727
* COW Supplementary groups list
2828
*/
29-
#define NGROUPS_SMALL 32
30-
#define NGROUPS_PER_BLOCK ((unsigned int)(PAGE_SIZE / sizeof(kgid_t)))
31-
3229
struct group_info {
3330
atomic_t usage;
3431
int ngroups;
35-
int nblocks;
36-
kgid_t small_block[NGROUPS_SMALL];
37-
kgid_t *blocks[0];
32+
kgid_t gid[0];
3833
};
3934

4035
/**
@@ -88,10 +83,6 @@ extern void set_groups(struct cred *, struct group_info *);
8883
extern int groups_search(const struct group_info *, kgid_t);
8984
extern bool may_setgroups(void);
9085

91-
/* access the groups "array" with this macro */
92-
#define GROUP_AT(gi, i) \
93-
((gi)->blocks[(i) / NGROUPS_PER_BLOCK][(i) % NGROUPS_PER_BLOCK])
94-
9586
/*
9687
* The security context of a task
9788
*

‎kernel/groups.c‎

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,31 @@
77
#include <linux/security.h>
88
#include <linux/syscalls.h>
99
#include <linux/user_namespace.h>
10+
#include <linux/vmalloc.h>
1011
#include <asm/uaccess.h>
1112

1213
struct group_info *groups_alloc(int gidsetsize)
1314
{
14-
struct group_info *group_info;
15-
int nblocks;
16-
int i;
17-
18-
nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
19-
/* Make sure we always allocate at least one indirect block pointer */
20-
nblocks = nblocks ? : 1;
21-
group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
22-
if (!group_info)
15+
struct group_info *gi;
16+
unsigned int len;
17+
18+
len = sizeof(struct group_info) + sizeof(kgid_t) * gidsetsize;
19+
gi = kmalloc(len, GFP_KERNEL_ACCOUNT|__GFP_NOWARN|__GFP_NORETRY);
20+
if (!gi)
21+
gi = __vmalloc(len, GFP_KERNEL_ACCOUNT|__GFP_HIGHMEM, PAGE_KERNEL);
22+
if (!gi)
2323
return NULL;
24-
group_info->ngroups = gidsetsize;
25-
group_info->nblocks = nblocks;
26-
atomic_set(&group_info->usage, 1);
27-
28-
if (gidsetsize <= NGROUPS_SMALL)
29-
group_info->blocks[0] = group_info->small_block;
30-
else {
31-
for (i = 0; i < nblocks; i++) {
32-
kgid_t *b;
33-
b = (void *)__get_free_page(GFP_USER);
34-
if (!b)
35-
goto out_undo_partial_alloc;
36-
group_info->blocks[i] = b;
37-
}
38-
}
39-
return group_info;
4024

41-
out_undo_partial_alloc:
42-
while (--i >= 0) {
43-
free_page((unsigned long)group_info->blocks[i]);
44-
}
45-
kfree(group_info);
46-
return NULL;
25+
atomic_set(&gi->usage, 1);
26+
gi->ngroups = gidsetsize;
27+
return gi;
4728
}
4829

4930
EXPORT_SYMBOL(groups_alloc);
5031

5132
void groups_free(struct group_info *group_info)
5233
{
53-
if (group_info->blocks[0] != group_info->small_block) {
54-
int i;
55-
for (i = 0; i < group_info->nblocks; i++)
56-
free_page((unsigned long)group_info->blocks[i]);
57-
}
58-
kfree(group_info);
34+
kvfree(group_info);
5935
}
6036

6137
EXPORT_SYMBOL(groups_free);
@@ -70,7 +46,7 @@ static int groups_to_user(gid_t __user *grouplist,
7046

7147
for (i = 0; i < count; i++) {
7248
gid_t gid;
73-
gid = from_kgid_munged(user_ns, GROUP_AT(group_info, i));
49+
gid = from_kgid_munged(user_ns, group_info->gid[i]);
7450
if (put_user(gid, grouplist+i))
7551
return -EFAULT;
7652
}
@@ -95,7 +71,7 @@ static int groups_from_user(struct group_info *group_info,
9571
if (!gid_valid(kgid))
9672
return -EINVAL;
9773

98-
GROUP_AT(group_info, i) = kgid;
74+
group_info->gid[i] = kgid;
9975
}
10076
return 0;
10177
}
@@ -115,15 +91,14 @@ static void groups_sort(struct group_info *group_info)
11591
for (base = 0; base < max; base++) {
11692
int left = base;
11793
int right = left + stride;
118-
kgid_t tmp = GROUP_AT(group_info, right);
94+
kgid_t tmp = group_info->gid[right];
11995

120-
while (left >= 0 && gid_gt(GROUP_AT(group_info, left), tmp)) {
121-
GROUP_AT(group_info, right) =
122-
GROUP_AT(group_info, left);
96+
while (left >= 0 && gid_gt(group_info->gid[left], tmp)) {
97+
group_info->gid[right] = group_info->gid[left];
12398
right = left;
12499
left -= stride;
125100
}
126-
GROUP_AT(group_info, right) = tmp;
101+
group_info->gid[right] = tmp;
127102
}
128103
stride /= 3;
129104
}
@@ -141,9 +116,9 @@ int groups_search(const struct group_info *group_info, kgid_t grp)
141116
right = group_info->ngroups;
142117
while (left < right) {
143118
unsigned int mid = (left+right)/2;
144-
if (gid_gt(grp, GROUP_AT(group_info, mid)))
119+
if (gid_gt(grp, group_info->gid[mid]))
145120
left = mid + 1;
146-
else if (gid_lt(grp, GROUP_AT(group_info, mid)))
121+
else if (gid_lt(grp, group_info->gid[mid]))
147122
right = mid;
148123
else
149124
return 1;

‎kernel/uid16.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static int groups16_to_user(old_gid_t __user *grouplist,
117117
kgid_t kgid;
118118

119119
for (i = 0; i < group_info->ngroups; i++) {
120-
kgid = GROUP_AT(group_info, i);
120+
kgid = group_info->gid[i];
121121
group = high2lowgid(from_kgid_munged(user_ns, kgid));
122122
if (put_user(group, grouplist+i))
123123
return -EFAULT;
@@ -142,7 +142,7 @@ static int groups16_from_user(struct group_info *group_info,
142142
if (!gid_valid(kgid))
143143
return -EINVAL;
144144

145-
GROUP_AT(group_info, i) = kgid;
145+
group_info->gid[i] = kgid;
146146
}
147147

148148
return 0;

‎net/ipv4/ping.c‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ int ping_init_sock(struct sock *sk)
258258
struct net *net = sock_net(sk);
259259
kgid_t group = current_egid();
260260
struct group_info *group_info;
261-
int i, j, count;
261+
int i;
262262
kgid_t low, high;
263263
int ret = 0;
264264

@@ -270,16 +270,11 @@ int ping_init_sock(struct sock *sk)
270270
return 0;
271271

272272
group_info = get_current_groups();
273-
count = group_info->ngroups;
274-
for (i = 0; i < group_info->nblocks; i++) {
275-
int cp_count = min_t(int, NGROUPS_PER_BLOCK, count);
276-
for (j = 0; j < cp_count; j++) {
277-
kgid_t gid = group_info->blocks[i][j];
278-
if (gid_lte(low, gid) && gid_lte(gid, high))
279-
goto out_release_group;
280-
}
273+
for (i = 0; i < group_info->ngroups; i++) {
274+
kgid_t gid = group_info->gid[i];
281275

282-
count -= cp_count;
276+
if (gid_lte(low, gid) && gid_lte(gid, high))
277+
goto out_release_group;
283278
}
284279

285280
ret = -EACCES;

‎net/sunrpc/auth_generic.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ generic_match(struct auth_cred *acred, struct rpc_cred *cred, int flags)
176176
if (gcred->acred.group_info->ngroups != acred->group_info->ngroups)
177177
goto out_nomatch;
178178
for (i = 0; i < gcred->acred.group_info->ngroups; i++) {
179-
if (!gid_eq(GROUP_AT(gcred->acred.group_info, i),
180-
GROUP_AT(acred->group_info, i)))
179+
if (!gid_eq(gcred->acred.group_info->gid[i],
180+
acred->group_info->gid[i]))
181181
goto out_nomatch;
182182
}
183183
out_match:

0 commit comments

Comments
 (0)