-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathfdt_facts.c
More file actions
38 lines (34 loc) · 1.62 KB
/
Copy pathfdt_facts.c
File metadata and controls
38 lines (34 loc) · 1.62 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
// This file is part of KASLD - https://github.com/bcoles/kasld
//
// Emit SF_FDT_KASLR_SEED: the FDT /chosen/kaslr-seed value (riscv64), which a
// rule turns into a deterministic text-base pin on non-EFI boots.
// ---
// <bcoles@gmail.com>
#include "include/kasld/api.h"
#include "include/kasld/kaslr_default.h"
KASLD_EXPLAIN("Reads the flattened device tree /chosen/kaslr-seed and emits it "
"as a scalar fact. On non-EFI riscv64 the kernel derives its "
"KASLR offset deterministically from this seed. No privileges.");
KASLD_META("method:parsed\n"
"phase:inference\n"
"discloses:facts\n");
int main(void) {
unsigned long seed = (unsigned long)kasld_read_fdt_kaslr_seed();
if (!seed)
return 0;
#if defined(__riscv) && __riscv_xlen == 64
/* setup_vm() seeds KASLR from the Zkr `seed` CSR FIRST and only falls back to
* the FDT /chosen/kaslr-seed when Zkr returns 0. On a Zkr-capable CPU the FDT
* cell may therefore be present and non-zero yet have played NO part in the
* slot selection, so the seed does not determine the base. The only consumer,
* riscv64_fdt_kaslr_seed, turns this fact into a deterministic slot pin /
* ceiling on Q_VIRT_IMAGE_BASE — feeding it a seed the kernel did not use
* would place a guaranteed constraint that excludes the true base. Suppress
* the fact unless the CPU lacks 'zkr' (the helper fails closed: an unreadable
* /proc/cpuinfo counts as present). This mirrors riscv64_no_seed's guard. */
if (kasld_cpu_feature_zkr_present())
return 0;
#endif
kasld_emit_scalar(SF_FDT_KASLR_SEED, seed, CONF_PARSED);
return 0;
}