|
12 | 12 |
|
13 | 13 | extern struct static_call_site __start_static_call_sites[], |
14 | 14 | __stop_static_call_sites[]; |
| 15 | +extern struct static_call_tramp_key __start_static_call_tramp_key[], |
| 16 | + __stop_static_call_tramp_key[]; |
15 | 17 |
|
16 | 18 | static bool static_call_initialized; |
17 | 19 |
|
@@ -323,10 +325,59 @@ static int __static_call_mod_text_reserved(void *start, void *end) |
323 | 325 | return ret; |
324 | 326 | } |
325 | 327 |
|
| 328 | +static unsigned long tramp_key_lookup(unsigned long addr) |
| 329 | +{ |
| 330 | + struct static_call_tramp_key *start = __start_static_call_tramp_key; |
| 331 | + struct static_call_tramp_key *stop = __stop_static_call_tramp_key; |
| 332 | + struct static_call_tramp_key *tramp_key; |
| 333 | + |
| 334 | + for (tramp_key = start; tramp_key != stop; tramp_key++) { |
| 335 | + unsigned long tramp; |
| 336 | + |
| 337 | + tramp = (long)tramp_key->tramp + (long)&tramp_key->tramp; |
| 338 | + if (tramp == addr) |
| 339 | + return (long)tramp_key->key + (long)&tramp_key->key; |
| 340 | + } |
| 341 | + |
| 342 | + return 0; |
| 343 | +} |
| 344 | + |
326 | 345 | static int static_call_add_module(struct module *mod) |
327 | 346 | { |
328 | | - return __static_call_init(mod, mod->static_call_sites, |
329 | | - mod->static_call_sites + mod->num_static_call_sites); |
| 347 | + struct static_call_site *start = mod->static_call_sites; |
| 348 | + struct static_call_site *stop = start + mod->num_static_call_sites; |
| 349 | + struct static_call_site *site; |
| 350 | + |
| 351 | + for (site = start; site != stop; site++) { |
| 352 | + unsigned long addr = (unsigned long)static_call_key(site); |
| 353 | + unsigned long key; |
| 354 | + |
| 355 | + /* |
| 356 | + * Is the key is exported, 'addr' points to the key, which |
| 357 | + * means modules are allowed to call static_call_update() on |
| 358 | + * it. |
| 359 | + * |
| 360 | + * Otherwise, the key isn't exported, and 'addr' points to the |
| 361 | + * trampoline so we need to lookup the key. |
| 362 | + * |
| 363 | + * We go through this dance to prevent crazy modules from |
| 364 | + * abusing sensitive static calls. |
| 365 | + */ |
| 366 | + if (!kernel_text_address(addr)) |
| 367 | + continue; |
| 368 | + |
| 369 | + key = tramp_key_lookup(addr); |
| 370 | + if (!key) { |
| 371 | + pr_warn("Failed to fixup __raw_static_call() usage at: %ps\n", |
| 372 | + static_call_addr(site)); |
| 373 | + return -EINVAL; |
| 374 | + } |
| 375 | + |
| 376 | + site->key = (key - (long)&site->key) | |
| 377 | + (site->key & STATIC_CALL_SITE_FLAGS); |
| 378 | + } |
| 379 | + |
| 380 | + return __static_call_init(mod, start, stop); |
330 | 381 | } |
331 | 382 |
|
332 | 383 | static void static_call_del_module(struct module *mod) |
|
0 commit comments