Re: [PATCH 1/3] bpf/verifier: Log instruction patching when verbose logging is enabled

From: Daniel Borkmann
Date: Fri Nov 23 2018 - 15:10:34 EST


On 11/23/2018 07:34 PM, Ben Hutchings wrote:
> User-space does not have access to the patched eBPF code, but we
> need to be able to test that patches are being applied. Therefore
> log distinct messages for each case that requires patching.

Thanks for the patches, Ben! Above is actually not the case, e.g. privileged
admin can use something like 'bpftool prog dump xlated id <id>' and then the
BPF insns are dumped to user space for the program /after/ the verification,
so the rewrites can then be seen. test_verifier temporarily drops caps to
load and run the unprivileged cases, but we can extend the test suite to
retrieve and check the final insns after that happened. I think this would be
a nice extension to the test suite for cases like these and would also provide
better insight than verbose() statement saying that something has been
patched (but not the actual result of it).

Thanks,
Daniel

> Signed-off-by: Ben Hutchings <ben.hutchings@xxxxxxxxxxxxxxx>
> ---
> kernel/bpf/verifier.c | 13 +++++++++++++
> 1 file changed, 13 insertions(+)
>
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 4ce049cd30a3..ea4bc796e545 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -5844,6 +5844,7 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
> verbose(env, "bpf verifier is misconfigured\n");
> return -EINVAL;
> } else if (cnt) {
> + verbose(env, "patching in prologue\n");
> new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
> if (!new_prog)
> return -ENOMEM;
> @@ -5892,6 +5893,9 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
> };
>
> cnt = ARRAY_SIZE(patch);
> + verbose(env,
> + "patching in sanitization against SSB at %d\n",
> + i + delta);
> new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt);
> if (!new_prog)
> return -ENOMEM;
> @@ -5973,6 +5977,7 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
> }
> }
>
> + verbose(env, "patching explicit ctx access at %d\n", i + delta);
> new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
> if (!new_prog)
> return -ENOMEM;
> @@ -6225,6 +6230,8 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env)
> cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0);
> }
>
> + verbose(env, "patching in divide-by-zero check at %d\n",
> + i + delta);
> new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt);
> if (!new_prog)
> return -ENOMEM;
> @@ -6244,6 +6251,8 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env)
> return -EINVAL;
> }
>
> + verbose(env, "patching implicit ctx access at %d\n",
> + i + delta);
> new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
> if (!new_prog)
> return -ENOMEM;
> @@ -6307,6 +6316,8 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env)
> map)->index_mask);
> insn_buf[2] = *insn;
> cnt = 3;
> + verbose(env, "patching in tail-call bounds check at %d",
> + i + delta);
> new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
> if (!new_prog)
> return -ENOMEM;
> @@ -6342,6 +6353,8 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env)
> return -EINVAL;
> }
>
> + verbose(env, "patching in map lookup at %d",
> + i + delta);
> new_prog = bpf_patch_insn_data(env, i + delta,
> insn_buf, cnt);
> if (!new_prog)
>