History

bpf: Tighten the requirements for preallocated hash maps

The assumption that only programs attached to perf NMI events can deadlock on memory allocators is wrong. Assume the following simplified callchain: kmalloc() from regular non BPF context cache empty freelist empty lock(zone->lock); tracepoint or kprobe BPF() update_elem() lock(bucket) kmalloc() cache empty freelist empty lock(zone->lock); <- DEADLOCK There are other ways which do not involve locking to create wreckage: kmalloc() from regular non BPF context local_irq_save(); ... obj = slab_first(); kprobe() BPF() update_elem() lock(bucket) kmalloc() local_irq_save(); ... obj = slab_first(); <- Same object as above ... So preallocation _must_ be enforced for all variants of intrusive instrumentation. Unfortunately immediate enforcement would break backwards compatibility, so for now such programs still are allowed to run, but a one time warning is emitted in dmesg and the verifier emits a warning in the verifier log as well so developers are made aware about this and can fix their programs before the enforcement becomes mandatory.

Thomas Gleixner committed 6 years ago

Commit 94dacdb1 parent 8eece071 file · +28 −11Browse files

Showing the changes to kernel (1 of 1 file). Show all

Unified · Split

kernel/bpf/verifier.c+28−11
@@ -8143,26 +8143,43 @@ static bool is_tracing_prog_type(enum bpf_prog_type type) · 8142 unmodified lines
81438143 }
81448144 }
81458145
8146+static bool is_preallocated_map(struct bpf_map *map)
8147+{
8148+ if (!check_map_prealloc(map))
8149+ return false;
8150+ if (map->inner_map_meta && !check_map_prealloc(map->inner_map_meta))
8151+ return false;
8152+ return true;
8153+}
8154+
81468155 static int check_map_prog_compatibility(struct bpf_verifier_env *env,
81478156 struct bpf_map *map,
81488157 struct bpf_prog *prog)
81498158
81508159 {
8151− /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use
8152− * preallocated hash maps, since doing memory allocation
8153− * in overflow_handler can crash depending on where nmi got
8154− * triggered.
8160+ /*
8161+ * Validate that trace type programs use preallocated hash maps.
8162+ *
8163+ * For programs attached to PERF events this is mandatory as the
8164+ * perf NMI can hit any arbitrary code sequence.
8165+ *
8166+ * All other trace types using preallocated hash maps are unsafe as
8167+ * well because tracepoint or kprobes can be inside locked regions
8168+ * of the memory allocator or at a place where a recursion into the
8169+ * memory allocator would see inconsistent state.
8170+ *
8171+ * For now running such programs is allowed for backwards
8172+ * compatibility reasons, but warnings are emitted so developers are
8173+ * made aware of the unsafety and can fix their programs before this
8174+ * is enforced.
81558175 */
8156− if (prog->type == BPF_PROG_TYPE_PERF_EVENT) {
8157− if (!check_map_prealloc(map)) {
8176+ if (is_tracing_prog_type(prog->type) && !is_preallocated_map(map)) {
8177+ if (prog->type == BPF_PROG_TYPE_PERF_EVENT) {
81588178 verbose(env, "perf_event programs can only use preallocated hash map\n");
81598179 return -EINVAL;
81608180 }
8161− if (map->inner_map_meta &&
8162− !check_map_prealloc(map->inner_map_meta)) {
8163− verbose(env, "perf_event programs can only use preallocated inner hash map\n");
8164− return -EINVAL;
8165− }
8181+ WARN_ONCE(1, "trace type BPF program uses run-time allocation\n");
8182+ verbose(env, "trace type programs with run-time allocated hash maps are unsafe. Switch to preallocated hash maps.\n");
81668183 }
81678184
81688185 if ((is_tracing_prog_type(prog->type) ||

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t