History

bpf: Refcount task stack in bpf_get_task_stack

On x86 the struct pt_regs * grabbed by task_pt_regs() points to an offset of task->stack. The pt_regs are later dereferenced in __bpf_get_stack (e.g. by user_mode() check). This can cause a fault if the task in question exits while bpf_get_task_stack is executing, as warned by task_stack_page's comment: * When accessing the stack of a non-current task that might exit, use * try_get_task_stack() instead. task_stack_page will return a pointer * that could get freed out from under you. Taking the comment's advice and using try_get_task_stack() and put_task_stack() to hold task->stack refcount, or bail early if it's already 0. Incrementing stack_refcount will ensure the task's stack sticks around while we're using its data. I noticed this bug while testing a bpf task iter similar to bpf_iter_task_stack in selftests, except mine grabbed user stack, and getting intermittent crashes, which resulted in dumps like: BUG: unable to handle page fault for address: 0000000000003fe0 \#PF: supervisor read access in kernel mode \#PF: error_code(0x0000) - not-present page RIP: 0010:__bpf_get_stack+0xd0/0x230 <snip...> Call Trace: bpf_prog_0a2be35c092cb190_get_task_stacks+0x5d/0x3ec bpf_iter_run_prog+0x24/0x81 __task_seq_show+0x58/0x80 bpf_seq_read+0xf7/0x3d0 vfs_read+0x91/0x140 ksys_read+0x59/0xd0 do_syscall_64+0x48/0x120 entry_SYSCALL_64_after_hwframe+0x44/0xa9

Dave Marchevsky committed 5 years ago

Commit 06ab1341 parent 861de021 file · +10 −2Browse files

Unified · Split

kernel/bpf/stackmap.c+10−2
@@ -517,9 +517,17 @@ const struct bpf_func_proto bpf_get_stack_proto = { · 516 unmodified lines
517517 BPF_CALL_4(bpf_get_task_stack, struct task_struct *, task, void *, buf,
518518 u32, size, u64, flags)
519519 {
520− struct pt_regs *regs = task_pt_regs(task);
520+ struct pt_regs *regs;
521+ long res;
521522
522− return __bpf_get_stack(regs, task, NULL, buf, size, flags);
523+ if (!try_get_task_stack(task))
524+ return -EFAULT;
525+
526+ regs = task_pt_regs(task);
527+ res = __bpf_get_stack(regs, task, NULL, buf, size, flags);
528+ put_task_stack(task);
529+
530+ return res;
523531 }
524532
525533 BTF_ID_LIST_SINGLE(bpf_get_task_stack_btf_ids, struct, task_struct)

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