diff mbox series

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

Message ID 20181123183455.qjokyt6zpa2yck6s@xylophone.i.decadent.org.uk
State Changes Requested, archived
Delegated to: BPF Maintainers
Headers show
Series bpf: Test defence against SSB exploitation | expand

Commit Message

Ben Hutchings Nov. 23, 2018, 6:34 p.m. UTC
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.

Signed-off-by: Ben Hutchings <ben.hutchings@codethink.co.uk>
---
 kernel/bpf/verifier.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

Comments

Daniel Borkmann Nov. 23, 2018, 8:10 p.m. UTC | #1
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@codethink.co.uk>
> ---
>  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)
>
Ben Hutchings Nov. 29, 2018, 3:55 p.m. UTC | #2
On Fri, 2018-11-23 at 21:10 +0100, Daniel Borkmann wrote:
> 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.

Oh that's good.

> 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).

Agreed; I'll look into doing this instead.

Ben.
diff mbox series

Patch

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)