From: Alexey Gladkov <gladkov.alexey@gmail.com> To: LKML <linux-kernel@vger.kernel.org>, Linux Containers <containers@lists.linux-foundation.org>, Kernel Hardening <kernel-hardening@lists.openwall.com> Cc: Linus Torvalds <torvalds@linux-foundation.org>, Alexey Gladkov <legion@kernel.org>, "Eric W . Biederman" <ebiederm@xmission.com>, Christian Brauner <christian@brauner.io>, Kees Cook <keescook@chromium.org> Subject: [RFC PATCH v2 2/8] Add a reference to ucounts for each user Date: Sun, 10 Jan 2021 18:33:41 +0100 [thread overview] Message-ID: <5cef3f3b60e9cda7f4a42820ee333fa2d171a58b.1610299857.git.gladkov.alexey@gmail.com> (raw) In-Reply-To: <cover.1610299857.git.gladkov.alexey@gmail.com> Before this, only the owner of the user namespace had an entry in ucounts. This entry addressed the user in the given user namespace. Now we create such an entry in ucounts for all users in the user namespace. Each user has only one entry for each user namespace. This commit is in preparation for migrating rlimits to ucounts. Signed-off-by: Alexey Gladkov <gladkov.alexey@gmail.com> --- include/linux/cred.h | 1 + include/linux/user_namespace.h | 2 ++ kernel/cred.c | 17 +++++++++++++++-- kernel/ucount.c | 12 +++++++++++- kernel/user_namespace.c | 1 + 5 files changed, 30 insertions(+), 3 deletions(-) diff --git a/include/linux/cred.h b/include/linux/cred.h index 18639c069263..307744fcc387 100644 --- a/include/linux/cred.h +++ b/include/linux/cred.h @@ -144,6 +144,7 @@ struct cred { #endif struct user_struct *user; /* real user ID subscription */ struct user_namespace *user_ns; /* user_ns the caps and keyrings are relative to. */ + struct ucounts *ucounts; struct group_info *group_info; /* supplementary groups for euid/fsgid */ /* RCU deletion */ union { diff --git a/include/linux/user_namespace.h b/include/linux/user_namespace.h index 84fefa9247c4..483568a56f7f 100644 --- a/include/linux/user_namespace.h +++ b/include/linux/user_namespace.h @@ -102,6 +102,8 @@ bool setup_userns_sysctls(struct user_namespace *ns); void retire_userns_sysctls(struct user_namespace *ns); struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, enum ucount_type type); void dec_ucount(struct ucounts *ucounts, enum ucount_type type); +void put_ucounts(struct ucounts *ucounts); +void set_cred_ucounts(const struct cred *cred, struct user_namespace *ns, kuid_t uid); #ifdef CONFIG_USER_NS diff --git a/kernel/cred.c b/kernel/cred.c index 421b1149c651..d19e2e97092c 100644 --- a/kernel/cred.c +++ b/kernel/cred.c @@ -119,6 +119,7 @@ static void put_cred_rcu(struct rcu_head *rcu) if (cred->group_info) put_group_info(cred->group_info); free_uid(cred->user); + put_ucounts(cred->ucounts); put_user_ns(cred->user_ns); kmem_cache_free(cred_jar, cred); } @@ -144,6 +145,9 @@ void __put_cred(struct cred *cred) BUG_ON(cred == current->cred); BUG_ON(cred == current->real_cred); + BUG_ON(cred->ucounts == NULL); + BUG_ON(cred->ucounts->ns != cred->user_ns); + if (cred->non_rcu) put_cred_rcu(&cred->rcu); else @@ -271,6 +275,9 @@ struct cred *prepare_creds(void) get_uid(new->user); get_user_ns(new->user_ns); + new->ucounts = NULL; + set_cred_ucounts(new, new->user_ns, new->euid); + #ifdef CONFIG_KEYS key_get(new->session_keyring); key_get(new->process_keyring); @@ -363,6 +370,7 @@ int copy_creds(struct task_struct *p, unsigned long clone_flags) ret = create_user_ns(new); if (ret < 0) goto error_put; + set_cred_ucounts(new, new->user_ns, new->euid); } #ifdef CONFIG_KEYS @@ -485,8 +493,11 @@ int commit_creds(struct cred *new) * in set_user(). */ alter_cred_subscribers(new, 2); - if (new->user != old->user) - atomic_inc(&new->user->processes); + if (new->user != old->user || new->user_ns != old->user_ns) { + if (new->user != old->user) + atomic_inc(&new->user->processes); + set_cred_ucounts(new, new->user_ns, new->euid); + } rcu_assign_pointer(task->real_cred, new); rcu_assign_pointer(task->cred, new); if (new->user != old->user) @@ -661,6 +672,7 @@ void __init cred_init(void) /* allocate a slab in which we can store credentials */ cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred), 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL); + set_cred_ucounts(&init_cred, &init_user_ns, GLOBAL_ROOT_UID); } /** @@ -704,6 +716,7 @@ struct cred *prepare_kernel_cred(struct task_struct *daemon) get_uid(new->user); get_user_ns(new->user_ns); get_group_info(new->group_info); + set_cred_ucounts(new, new->user_ns, new->euid); #ifdef CONFIG_KEYS new->session_keyring = NULL; diff --git a/kernel/ucount.c b/kernel/ucount.c index 0f2c7c11df19..80a39073bcef 100644 --- a/kernel/ucount.c +++ b/kernel/ucount.c @@ -161,7 +161,7 @@ static struct ucounts *get_ucounts(struct user_namespace *ns, kuid_t uid) return ucounts; } -static void put_ucounts(struct ucounts *ucounts) +void put_ucounts(struct ucounts *ucounts) { unsigned long flags; @@ -175,6 +175,16 @@ static void put_ucounts(struct ucounts *ucounts) kfree(ucounts); } +void set_cred_ucounts(const struct cred *cred, struct user_namespace *ns, kuid_t uid) +{ + if (cred->ucounts) { + if (cred->ucounts->ns == ns && uid_eq(cred->ucounts->uid, uid)) + return; + put_ucounts(cred->ucounts); + } + ((struct cred *) cred)->ucounts = get_ucounts(ns, uid); +} + static inline bool atomic_inc_below(atomic_t *v, int u) { int c, old; diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c index af612945a4d0..4b8a4468d391 100644 --- a/kernel/user_namespace.c +++ b/kernel/user_namespace.c @@ -1280,6 +1280,7 @@ static int userns_install(struct nsset *nsset, struct ns_common *ns) put_user_ns(cred->user_ns); set_cred_user_ns(cred, get_user_ns(user_ns)); + set_cred_ucounts(cred, user_ns, cred->euid); return 0; } -- 2.29.2 _______________________________________________ Containers mailing list Containers@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/containers
WARNING: multiple messages have this Message-ID (diff)
From: Alexey Gladkov <gladkov.alexey@gmail.com> To: LKML <linux-kernel@vger.kernel.org>, Linux Containers <containers@lists.linux-foundation.org>, Kernel Hardening <kernel-hardening@lists.openwall.com> Cc: Alexey Gladkov <legion@kernel.org>, "Eric W . Biederman" <ebiederm@xmission.com>, Kees Cook <keescook@chromium.org>, Christian Brauner <christian@brauner.io>, Linus Torvalds <torvalds@linux-foundation.org> Subject: [RFC PATCH v2 2/8] Add a reference to ucounts for each user Date: Sun, 10 Jan 2021 18:33:41 +0100 [thread overview] Message-ID: <5cef3f3b60e9cda7f4a42820ee333fa2d171a58b.1610299857.git.gladkov.alexey@gmail.com> (raw) In-Reply-To: <cover.1610299857.git.gladkov.alexey@gmail.com> Before this, only the owner of the user namespace had an entry in ucounts. This entry addressed the user in the given user namespace. Now we create such an entry in ucounts for all users in the user namespace. Each user has only one entry for each user namespace. This commit is in preparation for migrating rlimits to ucounts. Signed-off-by: Alexey Gladkov <gladkov.alexey@gmail.com> --- include/linux/cred.h | 1 + include/linux/user_namespace.h | 2 ++ kernel/cred.c | 17 +++++++++++++++-- kernel/ucount.c | 12 +++++++++++- kernel/user_namespace.c | 1 + 5 files changed, 30 insertions(+), 3 deletions(-) diff --git a/include/linux/cred.h b/include/linux/cred.h index 18639c069263..307744fcc387 100644 --- a/include/linux/cred.h +++ b/include/linux/cred.h @@ -144,6 +144,7 @@ struct cred { #endif struct user_struct *user; /* real user ID subscription */ struct user_namespace *user_ns; /* user_ns the caps and keyrings are relative to. */ + struct ucounts *ucounts; struct group_info *group_info; /* supplementary groups for euid/fsgid */ /* RCU deletion */ union { diff --git a/include/linux/user_namespace.h b/include/linux/user_namespace.h index 84fefa9247c4..483568a56f7f 100644 --- a/include/linux/user_namespace.h +++ b/include/linux/user_namespace.h @@ -102,6 +102,8 @@ bool setup_userns_sysctls(struct user_namespace *ns); void retire_userns_sysctls(struct user_namespace *ns); struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, enum ucount_type type); void dec_ucount(struct ucounts *ucounts, enum ucount_type type); +void put_ucounts(struct ucounts *ucounts); +void set_cred_ucounts(const struct cred *cred, struct user_namespace *ns, kuid_t uid); #ifdef CONFIG_USER_NS diff --git a/kernel/cred.c b/kernel/cred.c index 421b1149c651..d19e2e97092c 100644 --- a/kernel/cred.c +++ b/kernel/cred.c @@ -119,6 +119,7 @@ static void put_cred_rcu(struct rcu_head *rcu) if (cred->group_info) put_group_info(cred->group_info); free_uid(cred->user); + put_ucounts(cred->ucounts); put_user_ns(cred->user_ns); kmem_cache_free(cred_jar, cred); } @@ -144,6 +145,9 @@ void __put_cred(struct cred *cred) BUG_ON(cred == current->cred); BUG_ON(cred == current->real_cred); + BUG_ON(cred->ucounts == NULL); + BUG_ON(cred->ucounts->ns != cred->user_ns); + if (cred->non_rcu) put_cred_rcu(&cred->rcu); else @@ -271,6 +275,9 @@ struct cred *prepare_creds(void) get_uid(new->user); get_user_ns(new->user_ns); + new->ucounts = NULL; + set_cred_ucounts(new, new->user_ns, new->euid); + #ifdef CONFIG_KEYS key_get(new->session_keyring); key_get(new->process_keyring); @@ -363,6 +370,7 @@ int copy_creds(struct task_struct *p, unsigned long clone_flags) ret = create_user_ns(new); if (ret < 0) goto error_put; + set_cred_ucounts(new, new->user_ns, new->euid); } #ifdef CONFIG_KEYS @@ -485,8 +493,11 @@ int commit_creds(struct cred *new) * in set_user(). */ alter_cred_subscribers(new, 2); - if (new->user != old->user) - atomic_inc(&new->user->processes); + if (new->user != old->user || new->user_ns != old->user_ns) { + if (new->user != old->user) + atomic_inc(&new->user->processes); + set_cred_ucounts(new, new->user_ns, new->euid); + } rcu_assign_pointer(task->real_cred, new); rcu_assign_pointer(task->cred, new); if (new->user != old->user) @@ -661,6 +672,7 @@ void __init cred_init(void) /* allocate a slab in which we can store credentials */ cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred), 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL); + set_cred_ucounts(&init_cred, &init_user_ns, GLOBAL_ROOT_UID); } /** @@ -704,6 +716,7 @@ struct cred *prepare_kernel_cred(struct task_struct *daemon) get_uid(new->user); get_user_ns(new->user_ns); get_group_info(new->group_info); + set_cred_ucounts(new, new->user_ns, new->euid); #ifdef CONFIG_KEYS new->session_keyring = NULL; diff --git a/kernel/ucount.c b/kernel/ucount.c index 0f2c7c11df19..80a39073bcef 100644 --- a/kernel/ucount.c +++ b/kernel/ucount.c @@ -161,7 +161,7 @@ static struct ucounts *get_ucounts(struct user_namespace *ns, kuid_t uid) return ucounts; } -static void put_ucounts(struct ucounts *ucounts) +void put_ucounts(struct ucounts *ucounts) { unsigned long flags; @@ -175,6 +175,16 @@ static void put_ucounts(struct ucounts *ucounts) kfree(ucounts); } +void set_cred_ucounts(const struct cred *cred, struct user_namespace *ns, kuid_t uid) +{ + if (cred->ucounts) { + if (cred->ucounts->ns == ns && uid_eq(cred->ucounts->uid, uid)) + return; + put_ucounts(cred->ucounts); + } + ((struct cred *) cred)->ucounts = get_ucounts(ns, uid); +} + static inline bool atomic_inc_below(atomic_t *v, int u) { int c, old; diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c index af612945a4d0..4b8a4468d391 100644 --- a/kernel/user_namespace.c +++ b/kernel/user_namespace.c @@ -1280,6 +1280,7 @@ static int userns_install(struct nsset *nsset, struct ns_common *ns) put_user_ns(cred->user_ns); set_cred_user_ns(cred, get_user_ns(user_ns)); + set_cred_ucounts(cred, user_ns, cred->euid); return 0; } -- 2.29.2
next prev parent reply other threads:[~2021-01-10 17:42 UTC|newest] Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-01-10 17:33 [RFC PATCH v2 0/8] Count rlimits in each user namespace Alexey Gladkov 2021-01-10 17:33 ` Alexey Gladkov 2021-01-10 17:33 ` [RFC PATCH v2 1/8] Use atomic type for ucounts reference counting Alexey Gladkov 2021-01-10 17:33 ` Alexey Gladkov 2021-01-13 16:31 ` Eric W. Biederman 2021-01-13 16:31 ` Eric W. Biederman 2021-01-13 16:31 ` Eric W. Biederman 2021-01-13 18:01 ` Kees Cook 2021-01-13 18:01 ` Kees Cook 2021-01-10 17:33 ` Alexey Gladkov [this message] 2021-01-10 17:33 ` [RFC PATCH v2 2/8] Add a reference to ucounts for each user Alexey Gladkov 2021-01-13 6:33 ` 59ebc79722: kernel_BUG_at_kernel/cred.c kernel test robot 2021-01-13 6:33 ` kernel test robot 2021-01-13 6:33 ` kernel test robot 2021-01-13 16:25 ` [RFC PATCH v2 2/8] Add a reference to ucounts for each user Eric W. Biederman 2021-01-13 16:25 ` Eric W. Biederman 2021-01-13 16:25 ` Eric W. Biederman 2021-01-10 17:33 ` [RFC PATCH v2 3/8] Increase size of ucounts to atomic_long_t Alexey Gladkov 2021-01-10 17:33 ` Alexey Gladkov 2021-01-10 17:33 ` [RFC PATCH v2 4/8] Move RLIMIT_NPROC counter to ucounts Alexey Gladkov 2021-01-10 17:33 ` Alexey Gladkov 2021-01-10 17:33 ` [RFC PATCH v2 5/8] Move RLIMIT_MSGQUEUE " Alexey Gladkov 2021-01-10 17:33 ` Alexey Gladkov 2021-01-10 17:33 ` [RFC PATCH v2 6/8] Move RLIMIT_SIGPENDING " Alexey Gladkov 2021-01-10 17:33 ` Alexey Gladkov 2021-01-10 17:33 ` [RFC PATCH v2 7/8] Move RLIMIT_MEMLOCK " Alexey Gladkov 2021-01-10 17:33 ` Alexey Gladkov 2021-01-10 17:33 ` [RFC PATCH v2 8/8] Move RLIMIT_NPROC check to the place where we increment the counter Alexey Gladkov 2021-01-10 17:33 ` Alexey Gladkov 2021-01-10 18:46 ` [RFC PATCH v2 0/8] Count rlimits in each user namespace Linus Torvalds 2021-01-10 18:46 ` Linus Torvalds 2021-01-10 18:46 ` Linus Torvalds 2021-01-11 20:17 ` Eric W. Biederman 2021-01-11 20:17 ` Eric W. Biederman 2021-01-11 20:17 ` Eric W. Biederman
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=5cef3f3b60e9cda7f4a42820ee333fa2d171a58b.1610299857.git.gladkov.alexey@gmail.com \ --to=gladkov.alexey@gmail.com \ --cc=christian@brauner.io \ --cc=containers@lists.linux-foundation.org \ --cc=ebiederm@xmission.com \ --cc=keescook@chromium.org \ --cc=kernel-hardening@lists.openwall.com \ --cc=legion@kernel.org \ --cc=linux-kernel@vger.kernel.org \ --cc=torvalds@linux-foundation.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.