Skip to content

Commit eb8c507

Browse files
0x7f454c46kuba-moo
authored andcommitted
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]>
1 parent 19833ae commit eb8c507

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
@@ -224,9 +224,10 @@ extern bool arch_jump_label_transform_queue(struct jump_entry *entry,
224224
enum jump_label_type type);
225225
extern void arch_jump_label_transform_apply(void);
226226
extern int jump_label_text_reserved(void *start, void *end);
227-
extern void static_key_slow_inc(struct static_key *key);
227+
extern bool static_key_slow_inc(struct static_key *key);
228+
extern bool static_key_fast_inc_not_disabled(struct static_key *key);
228229
extern void static_key_slow_dec(struct static_key *key);
229-
extern void static_key_slow_inc_cpuslocked(struct static_key *key);
230+
extern bool static_key_slow_inc_cpuslocked(struct static_key *key);
230231
extern void static_key_slow_dec_cpuslocked(struct static_key *key);
231232
extern int static_key_count(struct static_key *key);
232233
extern void static_key_enable(struct static_key *key);
@@ -278,11 +279,23 @@ static __always_inline bool static_key_true(struct static_key *key)
278279
return false;
279280
}
280281

281-
static inline void static_key_slow_inc(struct static_key *key)
282+
static inline bool static_key_fast_inc_not_disabled(struct static_key *key)
282283
{
284+
int v;
285+
283286
STATIC_KEY_CHECK_USE(key);
284-
atomic_inc(&key->enabled);
287+
/*
288+
* Prevent key->enabled getting negative to follow the same semantics
289+
* as for CONFIG_JUMP_LABEL=y, see kernel/jump_label.c comment.
290+
*/
291+
v = atomic_read(&key->enabled);
292+
do {
293+
if (v < 0 || (v + 1) < 0)
294+
return false;
295+
} while (!likely(atomic_try_cmpxchg(&key->enabled, &v, v + 1)));
296+
return true;
285297
}
298+
#define static_key_slow_inc(key) static_key_fast_inc_not_disabled(key)
286299

287300
static inline void static_key_slow_dec(struct static_key *key)
288301
{

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)