creds/cgroups/ns_proxy refcount_t conversions
Subsystems (4)
cover creds/cgroups/ns_proxy refcount_t conversions
I would like to reopen ages-old discussion on these conversions now. Eric had objections against them in past, namely on not providing enough security for reference counters (but we still argue it is better than overflowing atomic_t and resulting use-after-free).
However now, refcount_t has been successfully used in kernel in many places, helped to detect bugs and mistakes in logic of refcounters.
We have converted most of core kernel reference counters by now and these 3 are pretty core ones, so it would be really great to have them covered also.
The patches are fully independent and can be cherry-picked and discussed separately. More information about each conversion in each patch separately.
Elena Reshetova (3):
nsproxy: convert nsproxy.count to refcount_t
groups: convert group_info.usage to refcount_t
creds: convert cred.usage to refcount_t
include/linux/cred.h | 15 +++++++------- include/linux/nsproxy.h | 6 +++--- kernel/cred.c | 44 ++++++++++++++++++++--------------------- kernel/groups.c | 2 +- kernel/nsproxy.c | 6 +++--- net/sunrpc/auth.c | 2 +- 6 files changed, 38 insertions(+), 37 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/3 nsproxy: convert nsproxy.count 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 nsproxy.count 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 lib/refcount.c 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 nsproxy.count it might make a difference in following places:
- put_nsproxy() and switch_task_namespaces(): decrement in refcount_dec_and_test() only provides RELEASE ordering and ACQUIRE ordering on success vs. fully ordered atomic counterpart
--- include/linux/nsproxy.h | 6 +++--- kernel/nsproxy.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/nsproxy.h +3 -3
@@ -29,7 +29,7 @@ struct fs_struct;* nsproxy is copied. */ struct nsproxy { -atomic_t count; +refcount_t count;struct uts_namespace *uts_ns;struct ipc_namespace *ipc_ns;struct mnt_namespace *mnt_ns;
@@ -75,14 +75,14 @@ int __init nsproxy_cache_init(void);static inline void put_nsproxy (struct nsproxy *ns) { -if (atomic_dec_and_test (&ns->count)) { +if (refcount_dec_and_test (&ns->count)) {free_nsproxy (ns); } }static inline void get_nsproxy (struct nsproxy *ns) { -atomic_inc (&ns->count); +refcount_inc (&ns->count); }#endif
kernel/nsproxy.c +3 -3
@@ -31,7 +31,7 @@static struct kmem_cache *nsproxy_cachep;struct nsproxy init_nsproxy = { - .count =ATOMIC_INIT (1 ), + .count =REFCOUNT_INIT (1 ), .uts_ns = &init_uts_ns, #if defined(CONFIG_POSIX_MQUEUE) || defined(CONFIG_SYSVIPC) .ipc_ns = &init_ipc_ns,
@@ -52,7 +52,7 @@ static inline struct nsproxy *create_nsproxy(void) nsproxy =kmem_cache_alloc (nsproxy_cachep,GFP_KERNEL );if (nsproxy) -atomic_set (&nsproxy->count,1 ); +refcount_set (&nsproxy->count,1 );return nsproxy; }
@@ -225,7 +225,7 @@ void switch_task_namespaces(struct task_struct *p, struct nsproxy *new) p->nsproxy = new;task_unlock (p); -if (ns &&atomic_dec_and_test (&ns->count)) +if (ns &&refcount_dec_and_test (&ns->count))free_nsproxy (ns); }
Signature · 1 line
2.17.1
PATCH 2/3 groups: convert group_info.usage to refcount_t
In reply to Elena Reshetovaatomic_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 lib/refcount.c 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
--- include/linux/cred.h | 7 ++++--- kernel/cred.c | 2 +- kernel/groups.c | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-)
include/linux/cred.h +4 -3
@@ -16,6 +16,7 @@#include <linux/init.h> #include <linux/key.h> #include <linux/atomic.h> +#include <linux/refcount.h> #include <linux/uidgid.h> #include <linux/sched.h> #include <linux/sched/user.h>
@@ -27,7 +28,7 @@ struct inode;* COW Supplementary groups list */ struct group_info { -atomic_t usage; +refcount_t usage;int ngroups;kgid_t gid[0 ]; } __randomize_layout;
@@ -43,7 +44,7 @@ struct group_info {*/ static inline struct group_info *get_group_info (struct group_info *gi) { -atomic_inc (&gi->usage); +refcount_inc (&gi->usage);return gi; }
@@ -53,7 +54,7 @@ static inline struct group_info *get_group_info(struct group_info *gi)*/ #define put_group_info (group_info) \ do { \ - if (atomic_dec_and_test(&(group_info)->usage)) \ + if (refcount_dec_and_test(&(group_info)->usage)) \ groups_free(group_info); \ } while (0)
kernel/cred.c +1 -1
@@ -37,7 +37,7 @@ do { \static struct kmem_cache *cred_jar;/* init to 2 - one for init_task, one to ensure it is never freed */ -struct group_info init_groups = { .usage =ATOMIC_INIT (2 ) }; +struct group_info init_groups = { .usage =REFCOUNT_INIT (2 ) };/* * The initial credentials for the initial task
kernel/groups.c +1 -1
@@ -24,7 +24,7 @@ struct group_info *groups_alloc(int gidsetsize)if (!gi)return NULL ; -atomic_set (&gi->usage,1 ); +refcount_set (&gi->usage,1 ); gi->ngroups = gidsetsize;return gi; }
Signature · 1 line
2.17.1
PATCH 3/3 creds: convert cred.usage to refcount_t
In reply to Elena Reshetovaatomic_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 cred.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 lib/refcount.c 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 cred.usage it might make a difference
in following places:
- get_task_cred(): increment in refcount_inc_not_zero() only guarantees control dependency on success vs. fully ordered atomic counterpart
- put_cred(): decrement in refcount_dec_and_test() only provides RELEASE ordering and ACQUIRE ordering on success vs. fully ordered atomic counterpart
--- include/linux/cred.h | 8 ++++---- kernel/cred.c | 42 +++++++++++++++++++++--------------------- net/sunrpc/auth.c | 2 +- 3 files changed, 26 insertions(+), 26 deletions(-)
include/linux/cred.h +4 -4
@@ -114,7 +114,7 @@ static inline int groups_search(const struct group_info *group_info, kgid_t grp)* same context as task->real_cred. */ struct cred { -atomic_t usage; +refcount_t usage;#ifdef CONFIG_DEBUG_CREDENTIALS atomic_t subscribers;/* number of processes subscribed */ void *put_addr;
@@ -228,7 +228,7 @@ static inline bool cap_ambient_invariant_ok(const struct cred *cred)*/ static inline struct cred *get_new_cred (struct cred *cred) { -atomic_inc (&cred->usage); +refcount_inc (&cred->usage);return cred; }
@@ -259,7 +259,7 @@ static inline const struct cred *get_cred_rcu(const struct cred *cred)struct cred *nonconst_cred = (struct cred *) cred;if (!cred)return NULL ; -if (!atomic_inc_not_zero (&nonconst_cred->usage)) +if (!refcount_inc_not_zero (&nonconst_cred->usage))return NULL ;validate_creds (cred);return cred;
@@ -282,7 +282,7 @@ static inline void put_cred(const struct cred *_cred)if (cred) {validate_creds (cred); -if (atomic_dec_and_test (&(cred)->usage)) +if (refcount_dec_and_test (&(cred)->usage))__put_cred (cred); } }
kernel/cred.c +21 -21
152 lines not on this page · raw message
net/sunrpc/auth.c +1 -1
@@ -38,7 +38,7 @@ static LIST_HEAD(cred_unused);static unsigned long number_cred_unused;static struct cred machine_cred = { - .usage =ATOMIC_INIT (1 ), + .usage =REFCOUNT_INIT (1 ), #ifdef CONFIG_DEBUG_CREDENTIALS .magic =CRED_MAGIC , #endif
Signature · 1 line
2.17.1
Quoted 11 lines · Elena Reshetova
I would like to reopen ages-old discussion on these conversions now. Eric had objections against them in past, namely on not providing enough security for reference counters (but we still argue it is better than overflowing
atomic_tand resulting use-after-free).
However now,refcount_thas been successfully used in kernel in many places, helped to detect bugs and mistakes in logic of refcounters.
We have converted most of core kernel reference counters by now and these 3 are pretty core ones, so it would be really great to have them covered also.
Quoted 3 lines
The patches are fully independent and can be cherry-picked and discussed separately. More information about each conversion in each patch separately.
These look good to me. Perhaps they can go via -mm, maybe with Eric and/or Peter's ack/review? I'd really like to get these done so we can get closer to finishing the refcount_t conversions...
Thanks!
-Kees
Quoted 17 lines
Elena Reshetova (3):
nsproxy: convert nsproxy.count torefcount_t
groups: convert group_info.usage torefcount_t
creds: convert cred.usage torefcount_tinclude/linux/cred.h | 15 +++++++------- include/linux/nsproxy.h | 6 +++--- kernel/cred.c | 44 ++++++++++++++++++++--------------------- kernel/groups.c | 2 +- kernel/nsproxy.c | 6 +++--- net/sunrpc/auth.c | 2 +- 6 files changed, 38 insertions(+), 37 deletions(-)--
2.17.1
Signature · 1 line
Kees Cook