* [PATCH 0/3] creds/cgroups/ns_proxy refcount_t conversions @ 2019-03-06 11:05 Elena Reshetova 2019-03-06 11:05 ` [PATCH 1/3] nsproxy: convert nsproxy.count to refcount_t Elena Reshetova ` (3 more replies) 0 siblings, 4 replies; 6+ messages in thread From: Elena Reshetova @ 2019-03-06 11:05 UTC (permalink / raw) To: ebiederm; +Cc: serue, keescook, peterz, linux-kernel, 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_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(-) -- 2.17.1 ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 1/3] nsproxy: convert nsproxy.count to refcount_t 2019-03-06 11:05 [PATCH 0/3] creds/cgroups/ns_proxy refcount_t conversions Elena Reshetova @ 2019-03-06 11:05 ` Elena Reshetova 2019-03-06 11:05 ` [PATCH 2/3] groups: convert group_info.usage " Elena Reshetova ` (2 subsequent siblings) 3 siblings, 0 replies; 6+ messages in thread From: Elena Reshetova @ 2019-03-06 11:05 UTC (permalink / raw) To: ebiederm; +Cc: serue, keescook, peterz, linux-kernel, Elena Reshetova 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 Suggested-by: Kees Cook <keescook@chromium.org> Reviewed-by: David Windsor <dwindsor@gmail.com> Reviewed-by: Hans Liljestrand <ishkamiel@gmail.com> Signed-off-by: Elena Reshetova <elena.reshetova@intel.com> --- include/linux/nsproxy.h | 6 +++--- kernel/nsproxy.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/linux/nsproxy.h b/include/linux/nsproxy.h index 2ae1b1a4d84d..90f09ffca20f 100644 --- a/include/linux/nsproxy.h +++ b/include/linux/nsproxy.h @@ -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 diff --git a/kernel/nsproxy.c b/kernel/nsproxy.c index f6c5d330059a..5bfe69194621 100644 --- a/kernel/nsproxy.c +++ b/kernel/nsproxy.c @@ -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); } -- 2.17.1 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 2/3] groups: convert group_info.usage to refcount_t 2019-03-06 11:05 [PATCH 0/3] creds/cgroups/ns_proxy refcount_t conversions Elena Reshetova 2019-03-06 11:05 ` [PATCH 1/3] nsproxy: convert nsproxy.count to refcount_t Elena Reshetova @ 2019-03-06 11:05 ` Elena Reshetova 2019-03-06 11:05 ` [PATCH 3/3] creds: convert cred.usage " Elena Reshetova 2019-03-07 17:43 ` [PATCH 0/3] creds/cgroups/ns_proxy refcount_t conversions Kees Cook 3 siblings, 0 replies; 6+ messages in thread From: Elena Reshetova @ 2019-03-06 11:05 UTC (permalink / raw) To: ebiederm; +Cc: serue, keescook, peterz, linux-kernel, Elena Reshetova 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 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 Suggested-by: Kees Cook <keescook@chromium.org> Reviewed-by: David Windsor <dwindsor@gmail.com> Reviewed-by: Hans Liljestrand <ishkamiel@gmail.com> Signed-off-by: Elena Reshetova <elena.reshetova@intel.com> --- include/linux/cred.h | 7 ++++--- kernel/cred.c | 2 +- kernel/groups.c | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/include/linux/cred.h b/include/linux/cred.h index ddd45bb74887..cad4b415ae90 100644 --- a/include/linux/cred.h +++ b/include/linux/cred.h @@ -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) diff --git a/kernel/cred.c b/kernel/cred.c index 45d77284aed0..49d23bd45a23 100644 --- a/kernel/cred.c +++ b/kernel/cred.c @@ -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 diff --git a/kernel/groups.c b/kernel/groups.c index daae2f2dc6d4..e8194bbf0dbe 100644 --- a/kernel/groups.c +++ b/kernel/groups.c @@ -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; } -- 2.17.1 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 3/3] creds: convert cred.usage to refcount_t 2019-03-06 11:05 [PATCH 0/3] creds/cgroups/ns_proxy refcount_t conversions Elena Reshetova 2019-03-06 11:05 ` [PATCH 1/3] nsproxy: convert nsproxy.count to refcount_t Elena Reshetova 2019-03-06 11:05 ` [PATCH 2/3] groups: convert group_info.usage " Elena Reshetova @ 2019-03-06 11:05 ` Elena Reshetova 2019-03-07 17:43 ` [PATCH 0/3] creds/cgroups/ns_proxy refcount_t conversions Kees Cook 3 siblings, 0 replies; 6+ messages in thread From: Elena Reshetova @ 2019-03-06 11:05 UTC (permalink / raw) To: ebiederm; +Cc: serue, keescook, peterz, linux-kernel, Elena Reshetova 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 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 Suggested-by: Kees Cook <keescook@chromium.org> Reviewed-by: David Windsor <dwindsor@gmail.com> Reviewed-by: Hans Liljestrand <ishkamiel@gmail.com> Signed-off-by: Elena Reshetova <elena.reshetova@intel.com> --- include/linux/cred.h | 8 ++++---- kernel/cred.c | 42 +++++++++++++++++++++--------------------- net/sunrpc/auth.c | 2 +- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/include/linux/cred.h b/include/linux/cred.h index cad4b415ae90..ee993333a39e 100644 --- a/include/linux/cred.h +++ b/include/linux/cred.h @@ -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); } } diff --git a/kernel/cred.c b/kernel/cred.c index 49d23bd45a23..ceb2f8a2399e 100644 --- a/kernel/cred.c +++ b/kernel/cred.c @@ -43,7 +43,7 @@ struct group_info init_groups = { .usage = REFCOUNT_INIT(2) }; * The initial credentials for the initial task */ struct cred init_cred = { - .usage = ATOMIC_INIT(4), + .usage = REFCOUNT_INIT(4), #ifdef CONFIG_DEBUG_CREDENTIALS .subscribers = ATOMIC_INIT(2), .magic = CRED_MAGIC, @@ -102,17 +102,17 @@ static void put_cred_rcu(struct rcu_head *rcu) #ifdef CONFIG_DEBUG_CREDENTIALS if (cred->magic != CRED_MAGIC_DEAD || - atomic_read(&cred->usage) != 0 || + refcount_read(&cred->usage) != 0 || read_cred_subscribers(cred) != 0) panic("CRED: put_cred_rcu() sees %p with" " mag %x, put %p, usage %d, subscr %d\n", cred, cred->magic, cred->put_addr, - atomic_read(&cred->usage), + refcount_read(&cred->usage), read_cred_subscribers(cred)); #else - if (atomic_read(&cred->usage) != 0) + if (refcount_read(&cred->usage) != 0) panic("CRED: put_cred_rcu() sees %p with usage %d\n", - cred, atomic_read(&cred->usage)); + cred, refcount_read(&cred->usage)); #endif security_cred_free(cred); @@ -136,10 +136,10 @@ static void put_cred_rcu(struct rcu_head *rcu) void __put_cred(struct cred *cred) { kdebug("__put_cred(%p{%d,%d})", cred, - atomic_read(&cred->usage), + refcount_read(&cred->usage), read_cred_subscribers(cred)); - BUG_ON(atomic_read(&cred->usage) != 0); + BUG_ON(refcount_read(&cred->usage) != 0); #ifdef CONFIG_DEBUG_CREDENTIALS BUG_ON(read_cred_subscribers(cred) != 0); cred->magic = CRED_MAGIC_DEAD; @@ -160,7 +160,7 @@ void exit_creds(struct task_struct *tsk) struct cred *cred; kdebug("exit_creds(%u,%p,%p,{%d,%d})", tsk->pid, tsk->real_cred, tsk->cred, - atomic_read(&tsk->cred->usage), + refcount_read(&tsk->cred->usage), read_cred_subscribers(tsk->cred)); cred = (struct cred *) tsk->real_cred; @@ -214,7 +214,7 @@ struct cred *cred_alloc_blank(void) if (!new) return NULL; - atomic_set(&new->usage, 1); + refcount_set(&new->usage, 1); #ifdef CONFIG_DEBUG_CREDENTIALS new->magic = CRED_MAGIC; #endif @@ -260,7 +260,7 @@ struct cred *prepare_creds(void) old = task->cred; memcpy(new, old, sizeof(struct cred)); - atomic_set(&new->usage, 1); + refcount_set(&new->usage, 1); set_cred_subscribers(new, 0); get_group_info(new->group_info); get_uid(new->user); @@ -337,7 +337,7 @@ int copy_creds(struct task_struct *p, unsigned long clone_flags) get_cred(p->cred); alter_cred_subscribers(p->cred, 2); kdebug("share_creds(%p{%d,%d})", - p->cred, atomic_read(&p->cred->usage), + p->cred, refcount_read(&p->cred->usage), read_cred_subscribers(p->cred)); atomic_inc(&p->cred->user->processes); return 0; @@ -428,7 +428,7 @@ int commit_creds(struct cred *new) const struct cred *old = task->real_cred; kdebug("commit_creds(%p{%d,%d})", new, - atomic_read(&new->usage), + refcount_read(&new->usage), read_cred_subscribers(new)); BUG_ON(task->cred != old); @@ -437,7 +437,7 @@ int commit_creds(struct cred *new) validate_creds(old); validate_creds(new); #endif - BUG_ON(atomic_read(&new->usage) < 1); + BUG_ON(refcount_read(&new->usage) < 1); get_cred(new); /* we will require a ref for the subj creds too */ @@ -502,13 +502,13 @@ EXPORT_SYMBOL(commit_creds); void abort_creds(struct cred *new) { kdebug("abort_creds(%p{%d,%d})", new, - atomic_read(&new->usage), + refcount_read(&new->usage), read_cred_subscribers(new)); #ifdef CONFIG_DEBUG_CREDENTIALS BUG_ON(read_cred_subscribers(new) != 0); #endif - BUG_ON(atomic_read(&new->usage) < 1); + BUG_ON(refcount_read(&new->usage) < 1); put_cred(new); } EXPORT_SYMBOL(abort_creds); @@ -525,7 +525,7 @@ const struct cred *override_creds(const struct cred *new) const struct cred *old = current->cred; kdebug("override_creds(%p{%d,%d})", new, - atomic_read(&new->usage), + refcount_read(&new->usage), read_cred_subscribers(new)); validate_creds(old); @@ -536,7 +536,7 @@ const struct cred *override_creds(const struct cred *new) alter_cred_subscribers(old, -1); kdebug("override_creds() = %p{%d,%d}", old, - atomic_read(&old->usage), + refcount_read(&old->usage), read_cred_subscribers(old)); return old; } @@ -554,7 +554,7 @@ void revert_creds(const struct cred *old) const struct cred *override = current->cred; kdebug("revert_creds(%p{%d,%d})", old, - atomic_read(&old->usage), + refcount_read(&old->usage), read_cred_subscribers(old)); validate_creds(old); @@ -667,7 +667,7 @@ struct cred *prepare_kernel_cred(struct task_struct *daemon) validate_creds(old); *new = *old; - atomic_set(&new->usage, 1); + refcount_set(&new->usage, 1); set_cred_subscribers(new, 0); get_uid(new->user); get_user_ns(new->user_ns); @@ -778,7 +778,7 @@ static void dump_invalid_creds(const struct cred *cred, const char *label, printk(KERN_ERR "CRED: ->magic=%x, put_addr=%p\n", cred->magic, cred->put_addr); printk(KERN_ERR "CRED: ->usage=%d, subscr=%d\n", - atomic_read(&cred->usage), + refcount_read(&cred->usage), read_cred_subscribers(cred)); printk(KERN_ERR "CRED: ->*uid = { %d,%d,%d,%d }\n", from_kuid_munged(&init_user_ns, cred->uid), @@ -852,7 +852,7 @@ void validate_creds_for_do_exit(struct task_struct *tsk) { kdebug("validate_creds_for_do_exit(%p,%p{%d,%d})", tsk->real_cred, tsk->cred, - atomic_read(&tsk->cred->usage), + refcount_read(&tsk->cred->usage), read_cred_subscribers(tsk->cred)); __validate_process_creds(tsk, __FILE__, __LINE__); diff --git a/net/sunrpc/auth.c b/net/sunrpc/auth.c index e7861026b9e5..9538bd5c87af 100644 --- a/net/sunrpc/auth.c +++ b/net/sunrpc/auth.c @@ -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 -- 2.17.1 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 0/3] creds/cgroups/ns_proxy refcount_t conversions 2019-03-06 11:05 [PATCH 0/3] creds/cgroups/ns_proxy refcount_t conversions Elena Reshetova ` (2 preceding siblings ...) 2019-03-06 11:05 ` [PATCH 3/3] creds: convert cred.usage " Elena Reshetova @ 2019-03-07 17:43 ` Kees Cook 3 siblings, 0 replies; 6+ messages in thread From: Kees Cook @ 2019-03-07 17:43 UTC (permalink / raw) To: Elena Reshetova; +Cc: Eric W. Biederman, serue, Peter Zijlstra, LKML On Wed, Mar 6, 2019 at 3:05 AM Elena Reshetova <elena.reshetova@intel.com> wrote: > > 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. 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 > > 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(-) > > -- > 2.17.1 > -- Kees Cook ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 0/3] Convert nsproxy, groups, and creds to refcount_t @ 2020-06-12 18:34 Kees Cook 2020-06-12 18:34 ` [PATCH 2/3] groups: convert group_info.usage " Kees Cook 0 siblings, 1 reply; 6+ messages in thread From: Kees Cook @ 2020-06-12 18:34 UTC (permalink / raw) To: Peter Zijlstra Cc: Kees Cook, Xiaoming Ni, David Windsor, Hans Liljestrand, Elena Reshetova, Paul Moore, edumazet, paulmck, David Howells, shakeelb, James Morris, alex.huangjianhui, dylix.dailei, chenzefeng2, linux-kernel This series was never applied[1], and was recently pointed out as missing[2]. If someone has a tree for this, please take it. Otherwise, please Ack and I'll send it to Linus. Thanks! -Kees [1] https://lore.kernel.org/lkml/20190306110549.7628-1-elena.reshetova@intel.com/ [2] https://lore.kernel.org/lkml/1591957695-118312-1-git-send-email-nixiaoming@huawei.com/ 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 | 7 +++---- kernel/cred.c | 44 ++++++++++++++++++++--------------------- kernel/groups.c | 2 +- kernel/nsproxy.c | 6 +++--- net/sunrpc/auth.c | 2 +- 6 files changed, 38 insertions(+), 38 deletions(-) -- 2.25.1 ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 2/3] groups: convert group_info.usage to refcount_t 2020-06-12 18:34 [PATCH 0/3] Convert nsproxy, groups, and creds to refcount_t Kees Cook @ 2020-06-12 18:34 ` Kees Cook 0 siblings, 0 replies; 6+ messages in thread From: Kees Cook @ 2020-06-12 18:34 UTC (permalink / raw) To: Peter Zijlstra Cc: Kees Cook, Elena Reshetova, David Windsor, Hans Liljestrand, Xiaoming Ni, Paul Moore, edumazet, paulmck, David Howells, shakeelb, James Morris, alex.huangjianhui, dylix.dailei, chenzefeng2, linux-kernel From: Elena Reshetova <elena.reshetova@intel.com> 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 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 Suggested-by: Kees Cook <keescook@chromium.org> Signed-off-by: Elena Reshetova <elena.reshetova@intel.com> Reviewed-by: David Windsor <dwindsor@gmail.com> Reviewed-by: Hans Liljestrand <ishkamiel@gmail.com> Link: https://lore.kernel.org/r/20190306110549.7628-3-elena.reshetova@intel.com Signed-off-by: Kees Cook <keescook@chromium.org> --- include/linux/cred.h | 7 ++++--- kernel/cred.c | 2 +- kernel/groups.c | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/include/linux/cred.h b/include/linux/cred.h index 18639c069263..32be0daf5a32 100644 --- a/include/linux/cred.h +++ b/include/linux/cred.h @@ -12,6 +12,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> @@ -23,7 +24,7 @@ struct inode; * COW Supplementary groups list */ struct group_info { - atomic_t usage; + refcount_t usage; int ngroups; kgid_t gid[0]; } __randomize_layout; @@ -39,7 +40,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; } @@ -49,7 +50,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) diff --git a/kernel/cred.c b/kernel/cred.c index 421b1149c651..ed95117d07c4 100644 --- a/kernel/cred.c +++ b/kernel/cred.c @@ -33,7 +33,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 diff --git a/kernel/groups.c b/kernel/groups.c index 6ee6691f6839..22f2892dbaf3 100644 --- a/kernel/groups.c +++ b/kernel/groups.c @@ -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; } -- 2.25.1 ^ permalink raw reply related [flat|nested] 6+ messages in thread
end of thread, other threads:[~2020-06-12 18:35 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2019-03-06 11:05 [PATCH 0/3] creds/cgroups/ns_proxy refcount_t conversions Elena Reshetova 2019-03-06 11:05 ` [PATCH 1/3] nsproxy: convert nsproxy.count to refcount_t Elena Reshetova 2019-03-06 11:05 ` [PATCH 2/3] groups: convert group_info.usage " Elena Reshetova 2019-03-06 11:05 ` [PATCH 3/3] creds: convert cred.usage " Elena Reshetova 2019-03-07 17:43 ` [PATCH 0/3] creds/cgroups/ns_proxy refcount_t conversions Kees Cook 2020-06-12 18:34 [PATCH 0/3] Convert nsproxy, groups, and creds to refcount_t Kees Cook 2020-06-12 18:34 ` [PATCH 2/3] groups: convert group_info.usage " Kees Cook
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).