Skip to content

Commit d770084

Browse files
ereshetovakees
authored andcommitted
groups: Convert group_info.usage to refcount_t
atomic_t variables are currently used to implement reference counters with the following properties: - counter is initialized to 1 using atomic_set() - a resource is freed upon counter reaching zero - once counter reaches zero, its further increments aren't allowed - counter schema uses basic atomic operations (set, inc, inc_not_zero, dec_and_test, etc.) Such atomic variables should be converted to a newly provided refcount_t type and API that prevents accidental counter overflows and underflows. This is important since overflows and underflows can lead to use-after-free situation and be exploitable. The variable group_info.usage is used as pure reference counter. Convert it to refcount_t and fix up the operations. **Important note for maintainers: Some functions from refcount_t API defined in refcount.h have different memory ordering guarantees than their atomic counterparts. Please check Documentation/core-api/refcount-vs-atomic.rst for more information. Normally the differences should not matter since refcount_t provides enough guarantees to satisfy the refcounting use cases, but in some rare cases it might matter. Please double check that you don't have some undocumented memory guarantees for this variable usage. For the group_info.usage it might make a difference in following places: - put_group_info(): decrement in refcount_dec_and_test() only provides RELEASE ordering and ACQUIRE ordering on success vs. fully ordered atomic counterpart Suggested-by: Kees Cook <[email protected]> Signed-off-by: Elena Reshetova <[email protected]> Reviewed-by: David Windsor <[email protected]> Reviewed-by: Hans Liljestrand <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent 4cb2e89 commit d770084

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

include/linux/cred.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/init.h>
1313
#include <linux/key.h>
1414
#include <linux/atomic.h>
15+
#include <linux/refcount.h>
1516
#include <linux/uidgid.h>
1617
#include <linux/sched.h>
1718
#include <linux/sched/user.h>
@@ -23,7 +24,7 @@ struct inode;
2324
* COW Supplementary groups list
2425
*/
2526
struct group_info {
26-
atomic_t usage;
27+
refcount_t usage;
2728
int ngroups;
2829
kgid_t gid[];
2930
} __randomize_layout;
@@ -39,7 +40,7 @@ struct group_info {
3940
*/
4041
static inline struct group_info *get_group_info(struct group_info *gi)
4142
{
42-
atomic_inc(&gi->usage);
43+
refcount_inc(&gi->usage);
4344
return gi;
4445
}
4546

@@ -49,7 +50,7 @@ static inline struct group_info *get_group_info(struct group_info *gi)
4950
*/
5051
#define put_group_info(group_info) \
5152
do { \
52-
if (atomic_dec_and_test(&(group_info)->usage)) \
53+
if (refcount_dec_and_test(&(group_info)->usage)) \
5354
groups_free(group_info); \
5455
} while (0)
5556

kernel/cred.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ do { \
3636
static struct kmem_cache *cred_jar;
3737

3838
/* init to 2 - one for init_task, one to ensure it is never freed */
39-
static struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
39+
static struct group_info init_groups = { .usage = REFCOUNT_INIT(2) };
4040

4141
/*
4242
* The initial credentials for the initial task

kernel/groups.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct group_info *groups_alloc(int gidsetsize)
1919
if (!gi)
2020
return NULL;
2121

22-
atomic_set(&gi->usage, 1);
22+
refcount_set(&gi->usage, 1);
2323
gi->ngroups = gidsetsize;
2424
return gi;
2525
}

0 commit comments

Comments
 (0)