History

bpf: Introduce BPF_MODIFY_RETURN

When multiple programs are attached, each program receives the return value from the previous program on the stack and the last program provides the return value to the attached function. The fmod_ret bpf programs are run after the fentry programs and before the fexit programs. The original function is only called if all the fmod_ret programs return 0 to avoid any unintended side-effects. The success value, i.e. 0 is not currently configurable but can be made so where user-space can specify it at load time. For example: int func_to_be_attached(int a, int b) { <--- do_fentry do_fmod_ret: <update ret by calling fmod_ret> if (ret != 0) goto do_fexit; original_function: <side_effects_happen_here> } <--- do_fexit The fmod_ret program attached to this function can be defined as: SEC("fmod_ret/func_to_be_attached") int BPF_PROG(func_name, int a, int b, int ret) { // This will skip the original function logic. return 1; } The first fmod_ret program is passed 0 in its return argument.

KP Singh committed 6 years ago

Commit ae240821 parent 7e639208 files · +130 −13Browse files

Showing the changes to kernel (4 of 8 files). Show all

Unified · Split

4 files
  1. kernel/bpf/btf.c +2 −1
  2. kernel/bpf/syscall.c +1 −0
  3. kernel/bpf/trampoline.c +4 −1
  4. kernel/bpf/verifier.c +1 −0
kernel/bpf/btf.c+2−1
@@ -3710,7 +3710,8 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, · 3709 unmodified lines
3710 nr_args--;3710 nr_args--;
3711 }3711 }
3712 3712
3713 if (prog->expected_attach_type == BPF_TRACE_FEXIT &&3713 if ((prog->expected_attach_type == BPF_TRACE_FEXIT ||
3714 prog->expected_attach_type == BPF_MODIFY_RETURN) &&
3714 arg == nr_args) {3715 arg == nr_args) {
3715 if (!t)3716 if (!t)
3716 /* Default prog with 5 args. 6th arg is retval. */3717 /* Default prog with 5 args. 6th arg is retval. */
kernel/bpf/syscall.c+1−0
@@ -2324,6 +2324,7 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog) · 2323 unmodified lines
2324 2324
2325 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&2325 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2326 prog->expected_attach_type != BPF_TRACE_FEXIT &&2326 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2327 prog->expected_attach_type != BPF_MODIFY_RETURN &&
2327 prog->type != BPF_PROG_TYPE_EXT) {2328 prog->type != BPF_PROG_TYPE_EXT) {
2328 err = -EINVAL;2329 err = -EINVAL;
2329 goto out_put_prog;2330 goto out_put_prog;
kernel/bpf/trampoline.c+4−1
@@ -232,7 +232,8 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr) · 231 unmodified lines
232 goto out;232 goto out;
233 }233 }
234 234
235 if (tprogs[BPF_TRAMP_FEXIT].nr_progs)235 if (tprogs[BPF_TRAMP_FEXIT].nr_progs ||
236 tprogs[BPF_TRAMP_MODIFY_RETURN].nr_progs)
236 flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME;237 flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME;
237 238
238 /* Though the second half of trampoline page is unused a task could be239 /* Though the second half of trampoline page is unused a task could be
@@ -269,6 +270,8 @@ static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(enum bpf_attach_type t) · 30 unmodified lines
269 switch (t) {270 switch (t) {
270 case BPF_TRACE_FENTRY:271 case BPF_TRACE_FENTRY:
271 return BPF_TRAMP_FENTRY;272 return BPF_TRAMP_FENTRY;
273 case BPF_MODIFY_RETURN:
274 return BPF_TRAMP_MODIFY_RETURN;
272 case BPF_TRACE_FEXIT:275 case BPF_TRACE_FEXIT:
273 return BPF_TRAMP_FEXIT;276 return BPF_TRAMP_FEXIT;
274 default:277 default:
kernel/bpf/verifier.c+1−0
@@ -9950,6 +9950,7 @@ static int check_attach_btf_id(struct bpf_verifier_env *env) · 9949 unmodified lines
9950 if (!prog_extension)9950 if (!prog_extension)
9951 return -EINVAL;9951 return -EINVAL;
9952 /* fallthrough */9952 /* fallthrough */
9953 case BPF_MODIFY_RETURN:
9953 case BPF_TRACE_FENTRY:9954 case BPF_TRACE_FENTRY:
9954 case BPF_TRACE_FEXIT:9955 case BPF_TRACE_FEXIT:
9955 if (!btf_type_is_func(t)) {9956 if (!btf_type_is_func(t)) {

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