signal: restore the override_rlimit logic
PATCH v2 1/1 signal: restore the override_rlimit logic
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.
v2: refactor to make the logic simpler (Eric, Oleg, Alexey)
--- 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
@@ -141,7 +141,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
@@ -419,7 +419,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
@@ -307,7 +307,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;
@@ -320,7 +321,8 @@ long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum rlimit_type type) goto 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.47.0.199.ga7371fff76-goog
Quoted 8 lines · Roman Gushchin
kernel/ucount.c +2
-1-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;
kernel/ucount.c long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum rlimit_type type) · line 328
Quoted 9 lines
kernel/ucount.c +2
-1@@ -320,7 +321,8 @@ long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum rlimit_type type) goto 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.
Quoted 20 lines · Roman Gushchin
Prior to commit
d64696905554("ReimplementRLIMIT_SIGPENDINGon top of ucounts")UCOUNT_RLIMIT_SIGPENDINGrlimit was not enforced for a class of signals. However now it's enforced unconditionally, even ifoverride_rlimitis set. This behavior change caused production issues.For example, if the limit is reached and a process receives a SIGSEGV signal,
sigqueue_allocfails 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_rlimitintoinc_rlimit_get_ucounts()and skip the comparison to max there ifoverride_rlimitis set. This effectively restores the old behavior.v2: refactor to make the logic simpler (Eric, Oleg, Alexey)
Quoted 9 lines
Fixes d64696905554 ("Reimplement RLIMIT_SIGPENDING on top of ucounts")Signed-off-by Roman Gushchin <[email protected]>Co-developed-by Andrei Vagin <[email protected]>Signed-off-by Andrei Vagin <[email protected]>Cc Kees Cook <[email protected]>Cc "Eric W. Biederman" <[email protected]>Cc Alexey Gladkov <[email protected]>Cc Oleg Nesterov <[email protected]>Cc <[email protected]>
Quoted 61 lines
--- 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@@ -141,7 +141,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@@ -419,7 +419,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@@ -307,7 +307,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;@@ -320,7 +321,8 @@ long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum rlimit_type type) goto 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.47.0.199.ga7371fff76-goog
Signature · 1 line
Rgrds, legion