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
37103710 nr_args--;
37113711 }
37123712
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) &&
37143715 arg == nr_args) {
37153716 if (!t)
37163717 /* 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
23242324
23252325 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
23262326 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2327+ prog->expected_attach_type != BPF_MODIFY_RETURN &&
23272328 prog->type != BPF_PROG_TYPE_EXT) {
23282329 err = -EINVAL;
23292330 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
232232 goto out;
233233 }
234234
235− if (tprogs[BPF_TRAMP_FEXIT].nr_progs)
235+ if (tprogs[BPF_TRAMP_FEXIT].nr_progs ||
236+ tprogs[BPF_TRAMP_MODIFY_RETURN].nr_progs)
236237 flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME;
237238
238239 /* 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
269270 switch (t) {
270271 case BPF_TRACE_FENTRY:
271272 return BPF_TRAMP_FENTRY;
273+ case BPF_MODIFY_RETURN:
274+ return BPF_TRAMP_MODIFY_RETURN;
272275 case BPF_TRACE_FEXIT:
273276 return BPF_TRAMP_FEXIT;
274277 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
99509950 if (!prog_extension)
99519951 return -EINVAL;
99529952 /* fallthrough */
9953+ case BPF_MODIFY_RETURN:
99539954 case BPF_TRACE_FENTRY:
99549955 case BPF_TRACE_FEXIT:
99559956 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