Skip to content

Commit 9442f2b

Browse files
committed
jump_label: Prevent key->enabled int overflow
JIRA: https://issues.redhat.com/browse/RHEL-68940 commit eb8c507 Author: Dmitry Safonov <[email protected]> Date: Wed, 23 Nov 2022 17:38:55 +0000 jump_label: Prevent key->enabled int overflow 1. With CONFIG_JUMP_LABEL=n static_key_slow_inc() doesn't have any protection against key->enabled refcounter overflow. 2. With CONFIG_JUMP_LABEL=y static_key_slow_inc_cpuslocked() still may turn the refcounter negative as (v + 1) may overflow. key->enabled is indeed a ref-counter as it's documented in multiple places: top comment in jump_label.h, Documentation/staging/static-keys.rst, etc. As -1 is reserved for static key that's in process of being enabled, functions would break with negative key->enabled refcount: - for CONFIG_JUMP_LABEL=n negative return of static_key_count() breaks static_key_false(), static_key_true() - the ref counter may become 0 from negative side by too many static_key_slow_inc() calls and lead to use-after-free issues. These flaws result in that some users have to introduce an additional mutex and prevent the reference counter from overflowing themselves, see bpf_enable_runtime_stats() checking the counter against INT_MAX / 2. Prevent the reference counter overflow by checking if (v + 1) > 0. Change functions API to return whether the increment was successful. Signed-off-by: Dmitry Safonov <[email protected]> Acked-by: Jakub Kicinski <[email protected]> Acked-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Jakub Kicinski <[email protected]> Signed-off-by: Waiman Long <[email protected]>
1 parent fcbf663 commit 9442f2b

File tree

2 files changed

+61
-16
lines changed

2 files changed

+61
-16
lines changed

include/linux/jump_label.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,10 @@ extern bool arch_jump_label_transform_queue(struct jump_entry *entry,
229229
enum jump_label_type type);
230230
extern void arch_jump_label_transform_apply(void);
231231
extern int jump_label_text_reserved(void *start, void *end);
232-
extern void static_key_slow_inc(struct static_key *key);
232+
extern bool static_key_slow_inc(struct static_key *key);
233+
extern bool static_key_fast_inc_not_disabled(struct static_key *key);
233234
extern void static_key_slow_dec(struct static_key *key);
234-
extern void static_key_slow_inc_cpuslocked(struct static_key *key);
235+
extern bool static_key_slow_inc_cpuslocked(struct static_key *key);
235236
extern void static_key_slow_dec_cpuslocked(struct static_key *key);
236237
extern int static_key_count(struct static_key *key);
237238
extern void static_key_enable(struct static_key *key);
@@ -283,11 +284,23 @@ static __always_inline bool static_key_true(struct static_key *key)
283284
return false;
284285
}
285286

286-
static inline void static_key_slow_inc(struct static_key *key)
287+
static inline bool static_key_fast_inc_not_disabled(struct static_key *key)
287288
{
289+
int v;
290+
288291
STATIC_KEY_CHECK_USE(key);
289-
atomic_inc(&key->enabled);
292+
/*
293+
* Prevent key->enabled getting negative to follow the same semantics
294+
* as for CONFIG_JUMP_LABEL=y, see kernel/jump_label.c comment.
295+
*/
296+
v = atomic_read(&key->enabled);
297+
do {
298+
if (v < 0 || (v + 1) < 0)
299+
return false;
300+
} while (!likely(atomic_try_cmpxchg(&key->enabled, &v, v + 1)));
301+
return true;
290302
}
303+
#define static_key_slow_inc(key) static_key_fast_inc_not_disabled(key)
291304

292305
static inline void static_key_slow_dec(struct static_key *key)
293306
{

kernel/jump_label.c

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,40 @@ int static_key_count(struct static_key *key)
113113
}
114114
EXPORT_SYMBOL_GPL(static_key_count);
115115

116-
void static_key_slow_inc_cpuslocked(struct static_key *key)
116+
/*
117+
* static_key_fast_inc_not_disabled - adds a user for a static key
118+
* @key: static key that must be already enabled
119+
*
120+
* The caller must make sure that the static key can't get disabled while
121+
* in this function. It doesn't patch jump labels, only adds a user to
122+
* an already enabled static key.
123+
*
124+
* Returns true if the increment was done. Unlike refcount_t the ref counter
125+
* is not saturated, but will fail to increment on overflow.
126+
*/
127+
bool static_key_fast_inc_not_disabled(struct static_key *key)
117128
{
129+
int v;
130+
118131
STATIC_KEY_CHECK_USE(key);
132+
/*
133+
* Negative key->enabled has a special meaning: it sends
134+
* static_key_slow_inc() down the slow path, and it is non-zero
135+
* so it counts as "enabled" in jump_label_update(). Note that
136+
* atomic_inc_unless_negative() checks >= 0, so roll our own.
137+
*/
138+
v = atomic_read(&key->enabled);
139+
do {
140+
if (v <= 0 || (v + 1) < 0)
141+
return false;
142+
} while (!likely(atomic_try_cmpxchg(&key->enabled, &v, v + 1)));
143+
144+
return true;
145+
}
146+
EXPORT_SYMBOL_GPL(static_key_fast_inc_not_disabled);
147+
148+
bool static_key_slow_inc_cpuslocked(struct static_key *key)
149+
{
119150
lockdep_assert_cpus_held();
120151

121152
/*
@@ -124,15 +155,9 @@ void static_key_slow_inc_cpuslocked(struct static_key *key)
124155
* jump_label_update() process. At the same time, however,
125156
* the jump_label_update() call below wants to see
126157
* static_key_enabled(&key) for jumps to be updated properly.
127-
*
128-
* So give a special meaning to negative key->enabled: it sends
129-
* static_key_slow_inc() down the slow path, and it is non-zero
130-
* so it counts as "enabled" in jump_label_update(). Note that
131-
* atomic_inc_unless_negative() checks >= 0, so roll our own.
132158
*/
133-
for (int v = atomic_read(&key->enabled); v > 0; )
134-
if (likely(atomic_try_cmpxchg(&key->enabled, &v, v + 1)))
135-
return;
159+
if (static_key_fast_inc_not_disabled(key))
160+
return true;
136161

137162
jump_label_lock();
138163
if (atomic_read(&key->enabled) == 0) {
@@ -144,16 +169,23 @@ void static_key_slow_inc_cpuslocked(struct static_key *key)
144169
*/
145170
atomic_set_release(&key->enabled, 1);
146171
} else {
147-
atomic_inc(&key->enabled);
172+
if (WARN_ON_ONCE(!static_key_fast_inc_not_disabled(key))) {
173+
jump_label_unlock();
174+
return false;
175+
}
148176
}
149177
jump_label_unlock();
178+
return true;
150179
}
151180

152-
void static_key_slow_inc(struct static_key *key)
181+
bool static_key_slow_inc(struct static_key *key)
153182
{
183+
bool ret;
184+
154185
cpus_read_lock();
155-
static_key_slow_inc_cpuslocked(key);
186+
ret = static_key_slow_inc_cpuslocked(key);
156187
cpus_read_unlock();
188+
return ret;
157189
}
158190
EXPORT_SYMBOL_GPL(static_key_slow_inc);
159191

0 commit comments

Comments
 (0)