| 1 |
#! /usr/bin/stap -g v -m CVE_2016_0728 |
| 2 |
|
| 3 |
/* A systemtap emergency band-aid for CVE-2016-0728. |
| 4 |
[email protected] & [email protected] |
| 5 |
*/ |
| 6 |
|
| 7 |
probe kernel.statement("join_session_keyring@*+44") !, |
| 8 |
kernel.function("join_session_keyring").label("error2") |
| 9 |
if(cve_enabled_p) |
| 10 |
{ |
| 11 |
/* NB: if the DWARF debuginfo were more perfect, we should be able |
| 12 |
to refer directly to $keyring/$new, local variables still |
| 13 |
technically in scope. On some kernels/gcc combinations, that |
| 14 |
works fine. On others, it doesn't, so this script tries to |
| 15 |
support both. */ |
| 16 |
if (@defined($keyring)) |
| 17 |
keyring = $keyring |
| 18 |
else { |
| 19 |
if (! warned_keyrings_p++) |
| 20 |
warn("Using find_keyring_by_name $return heuristic for $keyring") |
| 21 |
keyring = keyrings[tid()] |
| 22 |
} |
| 23 |
if (@defined($new)) |
| 24 |
new = $new |
| 25 |
else { |
| 26 |
if (! warned_news_p++) |
| 27 |
warn("Using prepare_creds $return heuristic for $new") |
| 28 |
new = news[tid()] |
| 29 |
} |
| 30 |
|
| 31 |
/* The actual security band-aid payload. */ |
| 32 |
if (keyring == @cast(new,"struct cred")->session_keyring) { |
| 33 |
if (cve_trace_p) printf("%s[%d] rejoin keyring %s %p %s\n", |
| 34 |
execname(), tid(), $name$, |
| 35 |
keyring, |
| 36 |
@cast(keyring,"struct key")->usage$$) |
| 37 |
if (cve_fix_p) do_key_put(keyring) |
| 38 |
if (cve_fix_p && cve_trace_p) printf("-> %p %s\n", |
| 39 |
keyring, |
| 40 |
@cast(keyring,"struct key")->usage$$) |
| 41 |
|
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
function do_key_put(ptr) |
| 46 |
%{ |
| 47 |
if (STAP_ARG_ptr != 0) |
| 48 |
key_put ((struct key *) STAP_ARG_ptr); |
| 49 |
%} |
| 50 |
|
| 51 |
|
| 52 |
/* We cache the last $keyring value for this thread. Relying on this |
| 53 |
table instead of direct access to $keyring at the |
| 54 |
join_session_keyring function label is undesirable. This is |
| 55 |
because we don't have a very good way of keeping this table clean |
| 56 |
(to remove old entries). (Extraordinary measures could include |
| 57 |
catching thread deaths, or returns from *callers* of |
| 58 |
find_keyring_by_name.) So what we do here instead is label keyrings% |
| 59 |
as an auto-wrapping array, so *old* entries will be reused. */ |
| 60 |
global keyrings%, warned_keyrings_p = 0 |
| 61 |
probe kernel.function("find_keyring_by_name").return |
| 62 |
if(cve_enabled_p) |
| 63 |
{ |
| 64 |
keyrings[tid()] = $return |
| 65 |
} |
| 66 |
|
| 67 |
/* And same for the $new variable. :-( */ |
| 68 |
global news%, warned_news_p = 0 |
| 69 |
probe kernel.function("prepare_creds").return |
| 70 |
if(cve_enabled_p) |
| 71 |
{ |
| 72 |
news[tid()] = $return |
| 73 |
} |
| 74 |
|
| 75 |
/* Disable the automatic dumping of these globals. */ |
| 76 |
probe never |
| 77 |
{ |
| 78 |
println(keyrings[0]) |
| 79 |
println(news[0]) |
| 80 |
println(warned_keyrings_p) |
| 81 |
println(warned_news_p) |
| 82 |
} |