-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathdmesg_riscv_relocation.c
More file actions
120 lines (107 loc) · 4.25 KB
/
Copy pathdmesg_riscv_relocation.c
File metadata and controls
120 lines (107 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// This file is part of KASLD - https://github.com/bcoles/kasld
//
// Search kernel log for RISC-V address relocation failures.
//
// From arch/riscv/kernel/module.c:
//
// "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n"
// "%s: can not generate the GOT entry for symbol = %016llx from PC = %p\n"
//
// clang-format off
// $ dmesg | grep ffffe0
// [ 0.000000] lowmem : 0xffffffe000000000 - 0xffffffe07fe00000 (2046 MB)
// [ 90.803776] nf_tables: target ffffffe0000dbc18 can not be addressed by the 32-bit offset from PC = 000000007c954634
// [ 91.659399] nf_tables: target ffffffe0000dbc18 can not be addressed by the 32-bit offset from PC = 0000000022acd662
// [ 92.516203] nf_tables: target ffffffe0000dbc18 can not be addressed by the 32-bit offset from PC = 0000000022acd662
// [ 93.452368] nf_tables: target ffffffe0000dbc18 can not be addressed by the 32-bit offset from PC = 0000000022acd662
// [ 97.393958] nf_tables: target ffffffe0000dbc18 can not be addressed by the 32-bit offset from PC = 00000000ca60ae01
// ...
// clang-format on
//
// # grep ffffffe0000dbc18 /proc/kallsyms
// ffffffe0000dbc18 t trace_initcall_finish_cb
// ffffffe0000dbc18 T _stext
// ffffffe0000dbc18 T _text
// ffffffe0000dbc18 D __init_end
// ffffffe0000dbc18 D __per_cpu_end
//
// Leak primitive:
// Data leaked: kernel text virtual address (_stext / _text)
// Kernel subsystem: arch/riscv/kernel/module — module relocation error
// Data structure: relocation target address (kernel text virtual pointer)
// Address type: virtual (kernel text)
// Method: parsed (dmesg string)
// Status: unfixed (error message prints raw kernel pointer)
// Access check: do_syslog() → check_syslog_permissions(); gated by
// dmesg_restrict
// Source:
// https://elixir.bootlin.com/linux/v6.7/source/arch/riscv/kernel/module.c
//
// Mitigations:
// Access gated by dmesg_restrict (see dmesg.h for shared access gate
// details). Only triggered when a RISC-V kernel module has a 32-bit
// relocation that cannot reach the target. RISC-V only.
//
// Requires:
// - kernel.dmesg_restrict = 0; or CAP_SYSLOG capabilities; or
// readable /var/log/dmesg.
//
// References:
// https://elixir.bootlin.com/linux/v6.7/source/arch/riscv/kernel/module.c
// https://github.com/riscv-non-isa/riscv-asm-manual/blob/master/riscv-asm.md#assembler-relocation-functions
// ---
// <bcoles@gmail.com>
#if !defined(__riscv) && !defined(__riscv__)
#error "Architecture is not supported"
#endif
#define _GNU_SOURCE
#include "include/dmesg.h"
#include "include/kasld/api.h"
#include "include/kasld/cli.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
KASLD_EXPLAIN(
"Searches dmesg for RISC-V kernel module relocation error messages "
"that print raw kernel text virtual addresses. When a 32-bit "
"relocation overflows, the error message includes the target "
"address (e.g., _stext), which is the KASLR-adjusted kernel text "
"base. RISC-V only. Access is gated by dmesg_restrict.");
KASLD_META("method:parsed\n"
"phase:inference\n"
"discloses:virtual\n"
"sysctl:dmesg_restrict>=1\n"
"bypass:CAP_SYSLOG\n"
"fallback:/var/log/dmesg\n");
static const char *needle = ": target ";
static int on_match(const char *line, void *ctx) {
unsigned long *lowest = ctx;
const char *p = strstr(line, needle);
if (!p)
return 1;
unsigned long addr;
if (kasld_addr_parse(p + strlen(needle), 16, &addr, NULL) && addr &&
kasld_addr_is_kernel_text(addr)) {
if (!*lowest || addr < *lowest)
*lowest = addr;
}
return 1; /* keep scanning for lowest */
}
int main(void) {
unsigned long addr = 0;
kasld_info("searching dmesg for RISC-V address relocation failures ...");
int ds = dmesg_search(": target ", on_match, &addr);
if (!addr) {
if (ds < 0)
return KASLD_EXIT_NOPERM;
kasld_err("RISC-V address relocation info not found in dmesg");
return 0;
}
kasld_info("lowest leaked address: %lx", addr);
kasld_info("possible kernel base: %lx", kasld_floor_text_base(addr));
kasld_result_sample(KASLD_TYPE_VIRT, REGION_KERNEL_TEXT, addr, NULL,
CONF_PARSED);
return 0;
}