FAILED: patch "[PATCH] ucount: take the rlimit reference before checking the limit" failed to apply to 5.15-stable tree
Sasha Levin
maintainerThe patch below does not apply to the 5.15-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.15.y
git checkout FETCH_HEAD
git cherry-pick -x b14fcabd34e02c40f72d3cd1e9bd4fd18df40ff1
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '[email protected]' --subject-prefix 'PATCH 5.15.y' 'HEAD^..'
Thanks,
Sasha
------------------ original commit in Linus's tree ------------------
From b14fcabd34e02c40f72d3cd1e9bd4fd18df40ff1 Mon Sep 17 00:00:00 2001
From: Nickolai Zeldovich <[email protected]>
Date: Mon, 19 Oct 2026 14:59:13 -0400
Subject: [PATCH] ucount: take the rlimit reference before checking the limit
inc_rlimit_get_ucounts() takes the reference that pins a ucounts level while its rlimit count is non-zero only in the walk whose increment is the 0 -> 1 transition, and only after that walk passed the limit check. A walk that raises the count from zero and then fails the limit backs out without the reference, while a concurrent walk that raised it past one keeps its increment and never takes one either:
CPU0 (obeys the limit) CPU1 (overrides it)
new = add_return(1) -> 1
new = add_return(1) -> 2
new != 1, no get_ucounts() new > max -> dec_unwind
sub_return(1) -> 1 (no put)
The count is 1 with no reference held. When CPU1 drops its charge, dec_rlimit_put_ucounts() takes the count to 0 and puts a reference nobody took, which frees the live struct ucounts, and the user_namespace it pins, with kfree_rcu(). The mirror interleaving leaks the reference instead.
An unprivileged user reaches this on a kernel with CONFIG_USER_NS=y: in a user namespace with RLIMIT_SIGPENDING 0, one process taking synchronous SIGSEGVs (forced, so override_rlimit is set) races a second process in the same namespace and uid that queues real-time signals, which obey the limit. The two hold different sighand->siglock but charge the same per-namespace SIGPENDING ucounts. Unpatched, with rcuref debugging:
rcuref - imbalanced put()
WARNING: lib/rcuref.c:266 at rcuref_put_slowpath+0xf4/0x120 put_ucounts+0x5a/0x180 do_dec_rlimit_put_ucounts+0x70/0x90 __sigqueue_free+0x33/0xc0 get_signal+0x56f/0xd20 arch_do_signal_or_restart+0x87/0x380 irqentry_exit+0x243/0x650 asm_exc_page_fault+0x26/0x30
Take the reference on the 0 -> 1 transition before the limit is checked, so the walk that raises the count from zero owns that level's reference even when its request is rejected, and unwind a rejected level through do_dec_rlimit_put_ucounts(), which releases the reference when the count returns to 0. The get_ucounts() failure path is unchanged. With the patch the same interleaving, forced some 80000 times in a KASAN build, produces no warning and no KASAN report.
--- kernel/ucount.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)
kernel/ucount.c +12 -9
@@ -308,23 +308,26 @@ long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum rlimit_type type,for (iter = ucounts; iter; iter = iter->ns->ucounts) {long new =atomic_long_add_return (1 , &iter->rlimit[type]); -if (new <0 || new > max) +/* +* Take the extra reference on the 0 -> 1 transition before the +* limit check: a rejected walk must not leave a concurrent one +* holding the surviving increment with no reference. +*/ +if (new ==1 && !get_ucounts (iter)) goto dec_unwind; +if (new <0 || new > max) { +/* undo iter too, releasing the reference taken above */ +do_dec_rlimit_put_ucounts (ucounts, iter->ns->ucounts, type); +return 0 ; + }if (iter == ucounts) ret = new;if (!override_rlimit) max =get_userns_rlimit_max (iter->ns, type); -/* -* Grab an extra ucount reference for the caller when -* the rlimit count was previously 0. -*/ -if (new !=1 ) -continue ; -if (!get_ucounts (iter)) - goto dec_unwind; }return ret; dec_unwind: +/* get_ucounts() failed for iter: undo iter without a put */ dec =atomic_long_sub_return (1 , &iter->rlimit[type]);WARN_ON_ONCE (dec <0 );do_dec_rlimit_put_ucounts (ucounts, iter, type);
Signature · 1 line
2.53.0
Quoted 5 lines · Sasha Levin
The patch below does not apply to the 5.15-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
Quoted 8 lines
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.15.y
git checkoutFETCH_HEAD
git cherry-pick -xb14fcabd34e02c40f72d3cd1e9bd4fd18df40ff1
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '[email protected]' --subject-prefix 'PATCH 5.15.y' 'HEAD^..'
The 5.15-stable is too old for this change. The branch does not contain commits that separate ucounts and rlimits.
9e05e5c7ee87 ("signal: restore the override_rlimit logic") de399236e240 ("ucounts: Split rlimit and ucount values and max values") (and most likely there are still a few more.)
I don't think it's worth backporting these changes.
Quoted 112 lines
Thanks,
Sasha------------------ original commit in Linus's tree ------------------
From
b14fcabd34e02c40f72d3cd1e9bd4fd18df40ff1Mon Sep 17 00:00:00 2001
From: Nickolai Zeldovich <[email protected]>
Date: Mon, 19 Oct 2026 14:59:13 -0400
Subject: [PATCH] ucount: take the rlimit reference before checking the limit
inc_rlimit_get_ucounts()takes the reference that pins a ucounts level while its rlimit count is non-zero only in the walk whose increment is the 0 -> 1 transition, and only after that walk passed the limit check. A walk that raises the count from zero and then fails the limit backs out without the reference, while a concurrent walk that raised it past one keeps its increment and never takes one either:CPU0 (obeys the limit) CPU1 (overrides it)
new =add_return(1)-> 1new = add_return(1) -> 2 new != 1, no get_ucounts()new > max ->
dec_unwind
sub_return(1)-> 1 (no put)The count is 1 with no reference held. When CPU1 drops its charge,
dec_rlimit_put_ucounts()takes the count to 0 and puts a reference nobody took, which frees the live struct ucounts, and theuser_namespaceit pins, withkfree_rcu(). The mirror interleaving leaks the reference instead.An unprivileged user reaches this on a kernel with
CONFIG_USER_NS=y: in a user namespace withRLIMIT_SIGPENDING0, one process taking synchronous SIGSEGVs (forced, sooverride_rlimitis set) races a second process in the same namespace and uid that queues real-time signals, which obey the limit. The two hold differentsighand->siglockbut charge the same per-namespace SIGPENDING ucounts. Unpatched, with rcuref debugging:rcuref - imbalanced
put()WARNING: lib/rcuref.c:266 at rcuref_put_slowpath+0xf4/0x120 put_ucounts+0x5a/0x180 do_dec_rlimit_put_ucounts+0x70/0x90 __sigqueue_free+0x33/0xc0 get_signal+0x56f/0xd20 arch_do_signal_or_restart+0x87/0x380 irqentry_exit+0x243/0x650 asm_exc_page_fault+0x26/0x30Take the reference on the 0 -> 1 transition before the limit is checked, so the walk that raises the count from zero owns that level's reference even when its request is rejected, and unwind a rejected level through
do_dec_rlimit_put_ucounts(), which releases the reference when the count returns to 0. Theget_ucounts()failure path is unchanged. With the patch the same interleaving, forced some 80000 times in a KASAN build, produces no warning and no KASAN report.Fixes 15bc01effefe ("ucounts: Fix signal ucount refcounting")Signed-off-by Nickolai Zeldovich <[email protected]>Acked-by Alexey Gladkov <[email protected]>Signed-off-by Christian Brauner (Amutable) <[email protected]>--- kernel/ucount.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)kernel/ucount.c +12
-9@@ -308,23 +308,26 @@ long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum rlimit_type type,for (iter = ucounts; iter; iter = iter->ns->ucounts) {long new =atomic_long_add_return (1 , &iter->rlimit[type]); -if (new <0 || new > max) +/* +* Take the extra reference on the 0 -> 1 transition before the +* limit check: a rejected walk must not leave a concurrent one +* holding the surviving increment with no reference. +*/ +if (new ==1 && !get_ucounts (iter)) goto dec_unwind; +if (new <0 || new > max) { +/* undo iter too, releasing the reference taken above */ +do_dec_rlimit_put_ucounts (ucounts, iter->ns->ucounts, type); +return 0 ; + }if (iter == ucounts) ret = new;if (!override_rlimit) max =get_userns_rlimit_max (iter->ns, type); -/* -* Grab an extra ucount reference for the caller when -* the rlimit count was previously 0. -*/ -if (new !=1 ) -continue ; -if (!get_ucounts (iter)) - goto dec_unwind; }return ret; dec_unwind: +/* get_ucounts() failed for iter: undo iter without a put */ dec =atomic_long_sub_return (1 , &iter->rlimit[type]);WARN_ON_ONCE (dec <0 );do_dec_rlimit_put_ucounts (ucounts, iter, type);Signature · 1 line
2.53.0
Signature · 1 line
Rgrds, legion
Sasha Levin
maintainerPATCH 1/4 ucounts: Use atomic_long_sub_return for clarity
In reply to Sasha LevinFrom: "Eric W. Biederman" <[email protected]>
[ Upstream commit 32342701b4ba57a6fd77e8aca2f65f68c0fa1da6 ]
Decrement ucounts using atomic_long_sub_return to make it clear the point is for the ucount to decrease.
Not a big deal but it should make it easier to catch bugs.
Stable-dep-of: b14fcabd34e0 ("ucount: take the rlimit reference before checking the limit")
--- kernel/ucount.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/ucount.c +3 -3
@@ -279,7 +279,7 @@ bool dec_rlimit_ucounts(struct ucounts *ucounts, enum ucount_type type, long v)struct ucounts *iter;long new =-1 ;/* Silence compiler warning */ for (iter = ucounts; iter; iter = iter->ns->ucounts) { -long dec =atomic_long_add_return (-v, &iter->ucount[type]); +long dec =atomic_long_sub_return (v, &iter->ucount[type]);WARN_ON_ONCE (dec <0 );if (iter == ucounts) new = dec;
@@ -292,7 +292,7 @@ static void do_dec_rlimit_put_ucounts(struct ucounts *ucounts, {struct ucounts *iter, *next;for (iter = ucounts; iter != last; iter = next) { -long dec =atomic_long_add_return (-1 , &iter->ucount[type]); +long dec =atomic_long_sub_return (1 , &iter->ucount[type]);WARN_ON_ONCE (dec <0 ); next = iter->ns->ucounts;if (dec ==0 )
@@ -330,7 +330,7 @@ long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum ucount_type type) }return ret; dec_unwind: - dec =atomic_long_add_return (-1 , &iter->ucount[type]); + dec =atomic_long_sub_return (1 , &iter->ucount[type]);WARN_ON_ONCE (dec <0 );do_dec_rlimit_put_ucounts (ucounts, iter, type);return 0 ;
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/4 ucounts: Split rlimit and ucount values and max values
From: Alexey Gladkov <[email protected]>
[ Upstream commit de399236e240743ad2dd10d719c37b97ddf31996 ]
Since the semantics of maximum rlimit values are different, it would be better not to mix ucount and rlimit values. This will prevent the error of using inc_count/dec_ucount for rlimit parameters.
This patch also renames the functions to emphasize the lack of connection between rlimit and ucount.
v3:
- Fix BUG:KASAN:use-after-free_in_dec_ucount.
v2:
- Fix the array-index-out-of-bounds that was found by the lkp project.
Stable-dep-of: b14fcabd34e0 ("ucount: take the rlimit reference before checking the limit")
--- fs/exec.c | 2 +- fs/proc/array.c | 2 +- include/linux/user_namespace.h | 35 +++++++++++++++++++++------------- kernel/fork.c | 12 ++++++------ kernel/sys.c | 2 +- kernel/ucount.c | 34 +++++++++++++++------------------ kernel/user_namespace.c | 10 +++++----- 7 files changed, 51 insertions(+), 46 deletions(-)
fs/exec.c +1 -1
@@ -1914,7 +1914,7 @@ static int do_execveat_common(int fd, struct filename *filename,* whether NPROC limit is still exceeded. */ if ((current->flags &PF_NPROC_EXCEEDED ) && -is_ucounts_overlimit (current_ucounts (),UCOUNT_RLIMIT_NPROC ,rlimit (RLIMIT_NPROC ))) { +is_rlimit_overlimit (current_ucounts (),UCOUNT_RLIMIT_NPROC ,rlimit (RLIMIT_NPROC ))) { retval = -EAGAIN ; goto out_ret; }
fs/proc/array.c +1 -1
@@ -274,7 +274,7 @@ static inline void task_sig(struct seq_file *m, struct task_struct *p)collect_sigign_sigcatch (p, &ignored, &caught); num_threads =get_nr_threads (p);rcu_read_lock ();/* FIXME: is this correct? */ - qsize =get_ucounts_value (task_ucounts (p),UCOUNT_RLIMIT_SIGPENDING ); + qsize =get_rlimit_value (task_ucounts (p),UCOUNT_RLIMIT_SIGPENDING );rcu_read_unlock (); qlim =task_rlimit (p,RLIMIT_SIGPENDING );unlock_task_sighand (p, &flags);
include/linux/user_namespace.h +22 -13
@@ -54,15 +54,17 @@ enum ucount_type { UCOUNT_FANOTIFY_GROUPS, UCOUNT_FANOTIFY_MARKS, #endif + UCOUNT_COUNTS, +}; + +enum rlimit_type { UCOUNT_RLIMIT_NPROC, UCOUNT_RLIMIT_MSGQUEUE, UCOUNT_RLIMIT_SIGPENDING, UCOUNT_RLIMIT_MEMLOCK, - UCOUNT_COUNTS, + UCOUNT_RLIMIT_COUNTS, }; -#define MAX_PER_NAMESPACE_UCOUNTS UCOUNT_RLIMIT_NPROC - struct user_namespace { struct uid_gid_map uid_map; struct uid_gid_map gid_map;
@@ -99,6 +101,7 @@ struct user_namespace {#endif struct ucounts *ucounts;long ucount_max[UCOUNT_COUNTS ]; +long rlimit_max[UCOUNT_RLIMIT_COUNTS ]; } __randomize_layout;struct ucounts {
@@ -107,6 +110,7 @@ struct ucounts {kuid_t uid;atomic_t count;atomic_long_t ucount[UCOUNT_COUNTS ]; +atomic_long_t rlimit[UCOUNT_RLIMIT_COUNTS ]; };extern struct user_namespace init_user_ns;
@@ -120,21 +124,26 @@ struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid);struct ucounts *__must_check get_ucounts (struct ucounts *ucounts);void put_ucounts (struct ucounts *ucounts); -static inline long get_ucounts_value (struct ucounts *ucounts,enum ucount_type type) +static inline long get_rlimit_value (struct ucounts *ucounts,enum rlimit_type type) { -return atomic_long_read (&ucounts->ucount[type]); +return atomic_long_read (&ucounts->rlimit[type]); } -long inc_rlimit_ucounts (struct ucounts *ucounts,enum ucount_type type,long v); -bool dec_rlimit_ucounts (struct ucounts *ucounts,enum ucount_type type,long v); -long inc_rlimit_get_ucounts (struct ucounts *ucounts,enum ucount_type type); -void dec_rlimit_put_ucounts (struct ucounts *ucounts,enum ucount_type type); -bool is_ucounts_overlimit (struct ucounts *ucounts,enum ucount_type type,unsigned long max); +long inc_rlimit_ucounts (struct ucounts *ucounts,enum rlimit_type type,long v); +bool dec_rlimit_ucounts (struct ucounts *ucounts,enum rlimit_type type,long v); +long inc_rlimit_get_ucounts (struct ucounts *ucounts,enum rlimit_type type); +void dec_rlimit_put_ucounts (struct ucounts *ucounts,enum rlimit_type type); +bool is_rlimit_overlimit (struct ucounts *ucounts,enum rlimit_type type,unsigned long max); + +static inline long get_userns_rlimit_max (struct user_namespace *ns,enum rlimit_type type) +{ +return READ_ONCE (ns->rlimit_max[type]); +} -static inline void set_rlimit_ucount_max (struct user_namespace *ns, -enum ucount_type type,unsigned long max) +static inline void set_userns_rlimit_max (struct user_namespace *ns, +enum rlimit_type type,unsigned long max) { - ns->ucount_max[type] = max <=LONG_MAX ? max :LONG_MAX ; + ns->rlimit_max[type] = max <=LONG_MAX ? max :LONG_MAX ; }#ifdef CONFIG_USER_NS
kernel/fork.c +6 -6
@@ -850,13 +850,13 @@ void __init fork_init(void) init_task.signal->rlim[RLIMIT_SIGPENDING ] = init_task.signal->rlim[RLIMIT_NPROC ]; -for (i =0 ; i <MAX_PER_NAMESPACE_UCOUNTS ; i++) +for (i =0 ; i <UCOUNT_COUNTS ; i++) init_user_ns.ucount_max[i] = max_threads/2 ; -set_rlimit_ucount_max (&init_user_ns,UCOUNT_RLIMIT_NPROC ,RLIM_INFINITY ); -set_rlimit_ucount_max (&init_user_ns,UCOUNT_RLIMIT_MSGQUEUE ,RLIM_INFINITY ); -set_rlimit_ucount_max (&init_user_ns,UCOUNT_RLIMIT_SIGPENDING ,RLIM_INFINITY ); -set_rlimit_ucount_max (&init_user_ns,UCOUNT_RLIMIT_MEMLOCK ,RLIM_INFINITY ); +set_userns_rlimit_max (&init_user_ns,UCOUNT_RLIMIT_NPROC ,RLIM_INFINITY ); +set_userns_rlimit_max (&init_user_ns,UCOUNT_RLIMIT_MSGQUEUE ,RLIM_INFINITY ); +set_userns_rlimit_max (&init_user_ns,UCOUNT_RLIMIT_SIGPENDING ,RLIM_INFINITY ); +set_userns_rlimit_max (&init_user_ns,UCOUNT_RLIMIT_MEMLOCK ,RLIM_INFINITY );#ifdef CONFIG_VMAP_STACK cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "fork:vm_stack_cache",
@@ -2176,7 +2176,7 @@ static __latent_entropy struct task_struct *copy_process( goto bad_fork_free; retval = -EAGAIN ; -if (is_ucounts_overlimit (task_ucounts (p),UCOUNT_RLIMIT_NPROC ,rlimit (RLIMIT_NPROC ))) { +if (is_rlimit_overlimit (task_ucounts (p),UCOUNT_RLIMIT_NPROC ,rlimit (RLIMIT_NPROC ))) {if (p->real_cred->user !=INIT_USER && !capable (CAP_SYS_RESOURCE ) && !capable (CAP_SYS_ADMIN )) goto bad_fork_cleanup_count;
kernel/sys.c +1 -1
@@ -489,7 +489,7 @@ static void flag_nproc_exceeded(struct cred *new)* for programs doing set*uid()+execve() by harmlessly deferring the * failure to the execve() stage. */ -if (is_ucounts_overlimit (new->ucounts,UCOUNT_RLIMIT_NPROC ,rlimit (RLIMIT_NPROC )) && +if (is_rlimit_overlimit (new->ucounts,UCOUNT_RLIMIT_NPROC ,rlimit (RLIMIT_NPROC )) && new->user !=INIT_USER ) current->flags |=PF_NPROC_EXCEEDED ;else
kernel/ucount.c +15 -19
@@ -87,10 +87,6 @@ static struct ctl_table user_table[] = {UCOUNT_ENTRY ("max_fanotify_groups" ), UCOUNT_ENTRY("max_fanotify_marks"), #endif - { }, - { }, - { }, - { }, { } };#endif /* CONFIG_SYSCTL */
@@ -257,29 +253,29 @@ void dec_ucount(struct ucounts *ucounts, enum ucount_type type)put_ucounts (ucounts); } -long inc_rlimit_ucounts (struct ucounts *ucounts,enum ucount_type type,long v) +long inc_rlimit_ucounts (struct ucounts *ucounts,enum rlimit_type type,long v) {struct ucounts *iter;long max =LONG_MAX ;long ret =0 ;for (iter = ucounts; iter; iter = iter->ns->ucounts) { -long new =atomic_long_add_return (v, &iter->ucount[type]); +long new =atomic_long_add_return (v, &iter->rlimit[type]);if (new <0 || new > max) ret =LONG_MAX ;else if (iter == ucounts) ret = new; - max =READ_ONCE (iter->ns->ucount_max[type]); + max =get_userns_rlimit_max (iter->ns, type); }return ret; } -bool dec_rlimit_ucounts (struct ucounts *ucounts,enum ucount_type type,long v) +bool dec_rlimit_ucounts (struct ucounts *ucounts,enum rlimit_type type,long v) {struct ucounts *iter;long new =-1 ;/* Silence compiler warning */ for (iter = ucounts; iter; iter = iter->ns->ucounts) { -long dec =atomic_long_sub_return (v, &iter->ucount[type]); +long dec =atomic_long_sub_return (v, &iter->rlimit[type]);WARN_ON_ONCE (dec <0 );if (iter == ucounts) new = dec;
@@ -288,11 +284,11 @@ bool dec_rlimit_ucounts(struct ucounts *ucounts, enum ucount_type type, long v) }static void do_dec_rlimit_put_ucounts (struct ucounts *ucounts, -struct ucounts *last,enum ucount_type type) +struct ucounts *last,enum rlimit_type type) {struct ucounts *iter, *next;for (iter = ucounts; iter != last; iter = next) { -long dec =atomic_long_sub_return (1 , &iter->ucount[type]); +long dec =atomic_long_sub_return (1 , &iter->rlimit[type]);WARN_ON_ONCE (dec <0 ); next = iter->ns->ucounts;if (dec ==0 )
@@ -300,12 +296,12 @@ static void do_dec_rlimit_put_ucounts(struct ucounts *ucounts, } } -void dec_rlimit_put_ucounts (struct ucounts *ucounts,enum ucount_type type) +void dec_rlimit_put_ucounts (struct ucounts *ucounts,enum rlimit_type type) {do_dec_rlimit_put_ucounts (ucounts,NULL , type); } -long inc_rlimit_get_ucounts (struct ucounts *ucounts,enum ucount_type type) +long inc_rlimit_get_ucounts (struct ucounts *ucounts,enum rlimit_type type) {/* Caller must hold a reference to ucounts */ struct ucounts *iter;
@@ -313,12 +309,12 @@ long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum ucount_type type)long dec, ret =0 ;for (iter = ucounts; iter; iter = iter->ns->ucounts) { -long new =atomic_long_add_return (1 , &iter->ucount[type]); +long new =atomic_long_add_return (1 , &iter->rlimit[type]);if (new <0 || new > max) goto dec_unwind;if (iter == ucounts) ret = new; - max =READ_ONCE (iter->ns->ucount_max[type]); + max =get_userns_rlimit_max (iter->ns, type);/* * Grab an extra ucount reference for the caller when * the rlimit count was previously 0.
@@ -330,23 +326,23 @@ long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum ucount_type type) }return ret; dec_unwind: - dec =atomic_long_sub_return (1 , &iter->ucount[type]); + dec =atomic_long_sub_return (1 , &iter->rlimit[type]);WARN_ON_ONCE (dec <0 );do_dec_rlimit_put_ucounts (ucounts, iter, type);return 0 ; } -bool is_ucounts_overlimit (struct ucounts *ucounts,enum ucount_type type,unsigned long rlimit) +bool is_rlimit_overlimit (struct ucounts *ucounts,enum rlimit_type type,unsigned long rlimit) {struct ucounts *iter;long max = rlimit;if (rlimit >LONG_MAX ) max =LONG_MAX ;for (iter = ucounts; iter; iter = iter->ns->ucounts) { -long val =get_ucounts_value (iter, type); +long val =get_rlimit_value (iter, type);if (val <0 || val > max)return true; - max =READ_ONCE (iter->ns->ucount_max[type]); + max =get_userns_rlimit_max (iter->ns, type); }return false; }
kernel/user_namespace.c +5 -5
@@ -131,13 +131,13 @@ int create_user_ns(struct cred *new) ns->owner = owner; ns->group = group;INIT_WORK (&ns->work, free_user_ns); -for (i =0 ; i <MAX_PER_NAMESPACE_UCOUNTS ; i++) { +for (i =0 ; i <UCOUNT_COUNTS ; i++) { ns->ucount_max[i] =INT_MAX ; } -set_rlimit_ucount_max (ns,UCOUNT_RLIMIT_NPROC ,enforced_nproc_rlimit ()); -set_rlimit_ucount_max (ns,UCOUNT_RLIMIT_MSGQUEUE ,rlimit (RLIMIT_MSGQUEUE )); -set_rlimit_ucount_max (ns,UCOUNT_RLIMIT_SIGPENDING ,rlimit (RLIMIT_SIGPENDING )); -set_rlimit_ucount_max (ns,UCOUNT_RLIMIT_MEMLOCK ,rlimit (RLIMIT_MEMLOCK )); +set_userns_rlimit_max (ns,UCOUNT_RLIMIT_NPROC ,enforced_nproc_rlimit ()); +set_userns_rlimit_max (ns,UCOUNT_RLIMIT_MSGQUEUE ,rlimit (RLIMIT_MSGQUEUE )); +set_userns_rlimit_max (ns,UCOUNT_RLIMIT_SIGPENDING ,rlimit (RLIMIT_SIGPENDING )); +set_userns_rlimit_max (ns,UCOUNT_RLIMIT_MEMLOCK ,rlimit (RLIMIT_MEMLOCK )); ns->ucounts = ucounts;/* Inherit USERNS_SETGROUPS_ALLOWED from our parent */
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/4 signal: restore the override_rlimit logic
In reply to Sasha LevinFrom: Roman Gushchin <[email protected]>
[ Upstream commit 9e05e5c7ee8758141d2db7e8fea2cab34500c6ed ]
Prior to commit d64696905554 ("Reimplement RLIMIT_SIGPENDING on top of ucounts") UCOUNT_RLIMIT_SIGPENDING rlimit was not enforced for a class of signals. However now it's enforced unconditionally, even if override_rlimit is set. This behavior change caused production issues.
For example, if the limit is reached and a process receives a SIGSEGV signal, sigqueue_alloc fails to allocate the necessary resources for the signal delivery, preventing the signal from being delivered with siginfo. This prevents the process from correctly identifying the fault address and handling the error. From the user-space perspective, applications are unaware that the limit has been reached and that the siginfo is effectively 'corrupted'. This can lead to unpredictable behavior and crashes, as we observed with java applications.
Fix this by passing override_rlimit into inc_rlimit_get_ucounts() and skip the comparison to max there if override_rlimit is set. This effectively restores the old behavior.
override_rlimit is set. This behavior change caused production issues.
For example, if the limit is reached and a process receives a SIGSEGV signal, sigqueue_alloc fails to allocate the necessary resources for the signal delivery, preventing the signal from being delivered with siginfo.
Stable-dep-of: b14fcabd34e0 ("ucount: take the rlimit reference before checking the limit")
--- include/linux/user_namespace.h | 3 ++- kernel/signal.c | 3 ++- kernel/ucount.c | 6 ++++-- 3 files changed, 8 insertions(+), 4 deletions(-)
include/linux/user_namespace.h +2 -1
@@ -131,7 +131,8 @@ static inline long get_rlimit_value(struct ucounts *ucounts, enum rlimit_type tylong inc_rlimit_ucounts (struct ucounts *ucounts,enum rlimit_type type,long v);bool dec_rlimit_ucounts (struct ucounts *ucounts,enum rlimit_type type,long v); -long inc_rlimit_get_ucounts (struct ucounts *ucounts,enum rlimit_type type); +long inc_rlimit_get_ucounts (struct ucounts *ucounts,enum rlimit_type type, +bool override_rlimit);void dec_rlimit_put_ucounts (struct ucounts *ucounts,enum rlimit_type type);bool is_rlimit_overlimit (struct ucounts *ucounts,enum rlimit_type type,unsigned long max);
kernel/signal.c +2 -1
@@ -426,7 +426,8 @@ __sigqueue_alloc(int sig, struct task_struct *t, gfp_t gfp_flags,*/ rcu_read_lock (); ucounts =task_ucounts (t); - sigpending =inc_rlimit_get_ucounts (ucounts,UCOUNT_RLIMIT_SIGPENDING ); + sigpending =inc_rlimit_get_ucounts (ucounts,UCOUNT_RLIMIT_SIGPENDING , + override_rlimit);rcu_read_unlock ();if (!sigpending)return NULL ;
kernel/ucount.c +4 -2
@@ -301,7 +301,8 @@ void dec_rlimit_put_ucounts(struct ucounts *ucounts, enum rlimit_type type)do_dec_rlimit_put_ucounts (ucounts,NULL , type); } -long inc_rlimit_get_ucounts (struct ucounts *ucounts,enum rlimit_type type) +long inc_rlimit_get_ucounts (struct ucounts *ucounts,enum rlimit_type type, +bool override_rlimit) {/* Caller must hold a reference to ucounts */ struct ucounts *iter;
@@ -314,7 +315,8 @@ long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum rlimit_type type) goto dec_unwind;if (iter == ucounts) ret = new; - max =get_userns_rlimit_max (iter->ns, type); +if (!override_rlimit) + max =get_userns_rlimit_max (iter->ns, type);/* * Grab an extra ucount reference for the caller when * the rlimit count was previously 0.
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 4/4 ucount: take the rlimit reference before checking the limit
In reply to Sasha LevinFrom: Nickolai Zeldovich <[email protected]>
[ Upstream commit b14fcabd34e02c40f72d3cd1e9bd4fd18df40ff1 ]
inc_rlimit_get_ucounts() takes the reference that pins a ucounts level while its rlimit count is non-zero only in the walk whose increment is the 0 -> 1 transition, and only after that walk passed the limit check. A walk that raises the count from zero and then fails the limit backs out without the reference, while a concurrent walk that raised it past one keeps its increment and never takes one either:
CPU0 (obeys the limit) CPU1 (overrides it)
new = add_return(1) -> 1
new = add_return(1) -> 2
new != 1, no get_ucounts() new > max -> dec_unwind
sub_return(1) -> 1 (no put)
The count is 1 with no reference held. When CPU1 drops its charge, dec_rlimit_put_ucounts() takes the count to 0 and puts a reference nobody took, which frees the live struct ucounts, and the user_namespace it pins, with kfree_rcu(). The mirror interleaving leaks the reference instead.
An unprivileged user reaches this on a kernel with CONFIG_USER_NS=y: in a user namespace with RLIMIT_SIGPENDING 0, one process taking synchronous SIGSEGVs (forced, so override_rlimit is set) races a second process in the same namespace and uid that queues real-time signals, which obey the limit. The two hold different sighand->siglock but charge the same per-namespace SIGPENDING ucounts. Unpatched, with rcuref debugging:
rcuref - imbalanced put()
WARNING: lib/rcuref.c:266 at rcuref_put_slowpath+0xf4/0x120 put_ucounts+0x5a/0x180 do_dec_rlimit_put_ucounts+0x70/0x90 __sigqueue_free+0x33/0xc0 get_signal+0x56f/0xd20 arch_do_signal_or_restart+0x87/0x380 irqentry_exit+0x243/0x650 asm_exc_page_fault+0x26/0x30
Take the reference on the 0 -> 1 transition before the limit is checked, so the walk that raises the count from zero owns that level's reference even when its request is rejected, and unwind a rejected level through do_dec_rlimit_put_ucounts(), which releases the reference when the count returns to 0. The get_ucounts() failure path is unchanged. With the patch the same interleaving, forced some 80000 times in a KASAN build, produces no warning and no KASAN report.
--- kernel/ucount.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)
kernel/ucount.c +12 -9
@@ -311,23 +311,26 @@ long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum rlimit_type type,for (iter = ucounts; iter; iter = iter->ns->ucounts) {long new =atomic_long_add_return (1 , &iter->rlimit[type]); -if (new <0 || new > max) +/* +* Take the extra reference on the 0 -> 1 transition before the +* limit check: a rejected walk must not leave a concurrent one +* holding the surviving increment with no reference. +*/ +if (new ==1 && !get_ucounts (iter)) goto dec_unwind; +if (new <0 || new > max) { +/* undo iter too, releasing the reference taken above */ +do_dec_rlimit_put_ucounts (ucounts, iter->ns->ucounts, type); +return 0 ; + }if (iter == ucounts) ret = new;if (!override_rlimit) max =get_userns_rlimit_max (iter->ns, type); -/* -* Grab an extra ucount reference for the caller when -* the rlimit count was previously 0. -*/ -if (new !=1 ) -continue ; -if (!get_ucounts (iter)) - goto dec_unwind; }return ret; dec_unwind: +/* get_ucounts() failed for iter: undo iter without a put */ dec =atomic_long_sub_return (1 , &iter->rlimit[type]);WARN_ON_ONCE (dec <0 );do_dec_rlimit_put_ucounts (ucounts, iter, type);
Signature · 1 line
2.53.0