All of lore.kernel.org
 help / color / mirror / Atom feed
* [bpf PATCH] bpf: Do not allow btf_ctx_access with __int128 types
@ 2020-06-24 22:20 John Fastabend
  2020-06-25 15:01 ` Daniel Borkmann
  0 siblings, 1 reply; 5+ messages in thread
From: John Fastabend @ 2020-06-24 22:20 UTC (permalink / raw)
  To: jolsa, andrii.nakryiko, ast, daniel; +Cc: netdev, bpf, john.fastabend

To ensure btf_ctx_access() is safe the verifier checks that the BTF
arg type is an int, enum, or pointer. When the function does the
BTF arg lookup it uses the calculation 'arg = off / 8'  using the
fact that registers are 8B. This requires that the first arg is
in the first reg, the second in the second, and so on. However,
for __int128 the arg will consume two registers by default LLVM
implementation. So this will cause the arg layout assumed by the
'arg = off / 8' calculation to be incorrect.

Because __int128 is uncommon this patch applies the easiest fix and
will force int types to be sizeof(u64) or smaller so that they will
fit in a single register.

v2: remove unneeded parens per Andrii's feedback

Fixes: 9e15db66136a1 ("bpf: Implement accurate raw_tp context access via BTF")
Acked-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: John Fastabend <john.fastabend@gmail.com>
---
 include/linux/btf.h |    5 +++++
 kernel/bpf/btf.c    |    4 ++--
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/include/linux/btf.h b/include/linux/btf.h
index 5c1ea99..8b81fbb 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -82,6 +82,11 @@ static inline bool btf_type_is_int(const struct btf_type *t)
 	return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
 }
 
+static inline bool btf_type_is_small_int(const struct btf_type *t)
+{
+	return btf_type_is_int(t) && t->size <= sizeof(u64);
+}
+
 static inline bool btf_type_is_enum(const struct btf_type *t)
 {
 	return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 58c9af1..9a1a98d 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -3746,7 +3746,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
 				return false;
 
 			t = btf_type_skip_modifiers(btf, t->type, NULL);
-			if (!btf_type_is_int(t)) {
+			if (!btf_type_is_small_int(t)) {
 				bpf_log(log,
 					"ret type %s not allowed for fmod_ret\n",
 					btf_kind_str[BTF_INFO_KIND(t->info)]);
@@ -3768,7 +3768,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
 	/* skip modifiers */
 	while (btf_type_is_modifier(t))
 		t = btf_type_by_id(btf, t->type);
-	if (btf_type_is_int(t) || btf_type_is_enum(t))
+	if (btf_type_is_small_int(t) || btf_type_is_enum(t))
 		/* accessing a scalar */
 		return true;
 	if (!btf_type_is_ptr(t)) {


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [bpf PATCH] bpf: Do not allow btf_ctx_access with __int128 types
  2020-06-24 22:20 [bpf PATCH] bpf: Do not allow btf_ctx_access with __int128 types John Fastabend
@ 2020-06-25 15:01 ` Daniel Borkmann
  0 siblings, 0 replies; 5+ messages in thread
From: Daniel Borkmann @ 2020-06-25 15:01 UTC (permalink / raw)
  To: John Fastabend, jolsa, andrii.nakryiko, ast; +Cc: netdev, bpf

On 6/25/20 12:20 AM, John Fastabend wrote:
> To ensure btf_ctx_access() is safe the verifier checks that the BTF
> arg type is an int, enum, or pointer. When the function does the
> BTF arg lookup it uses the calculation 'arg = off / 8'  using the
> fact that registers are 8B. This requires that the first arg is
> in the first reg, the second in the second, and so on. However,
> for __int128 the arg will consume two registers by default LLVM
> implementation. So this will cause the arg layout assumed by the
> 'arg = off / 8' calculation to be incorrect.
> 
> Because __int128 is uncommon this patch applies the easiest fix and
> will force int types to be sizeof(u64) or smaller so that they will
> fit in a single register.
> 
> v2: remove unneeded parens per Andrii's feedback
> 
> Fixes: 9e15db66136a1 ("bpf: Implement accurate raw_tp context access via BTF")
> Acked-by: Andrii Nakryiko <andriin@fb.com>
> Signed-off-by: John Fastabend <john.fastabend@gmail.com>

Applied, thanks!

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [bpf PATCH] bpf: do not allow btf_ctx_access with __int128 types
  2020-06-23 18:33 ` Andrii Nakryiko
@ 2020-06-24 22:23   ` John Fastabend
  0 siblings, 0 replies; 5+ messages in thread
From: John Fastabend @ 2020-06-24 22:23 UTC (permalink / raw)
  To: Andrii Nakryiko, John Fastabend
  Cc: Jiri Olsa, Alexei Starovoitov, Daniel Borkmann, Networking, bpf

Andrii Nakryiko wrote:
> On Tue, Jun 23, 2020 at 10:14 AM John Fastabend
> <john.fastabend@gmail.com> wrote:
> >
> > To ensure btf_ctx_access() is safe the verifier checks that the BTF
> > arg type is an int, enum, or pointer. When the function does the
> > BTF arg lookup it uses the calculation 'arg = off / 8'  using the
> > fact that registers are 8B. This requires that the first arg is
> > in the first reg, the second in the second, and so on. However,
> > for __int128 the arg will consume two registers by default LLVM
> > implementation. So this will cause the arg layout assumed by the
> > 'arg = off / 8' calculation to be incorrect.
> >
> > Because __int128 is uncommon this patch applies the easiest fix and
> > will force int types to be sizeof(u64) or smaller so that they will
> > fit in a single register.
> >
> > Fixes: 9e15db66136a1 ("bpf: Implement accurate raw_tp context access via BTF")
> > Signed-off-by: John Fastabend <john.fastabend@gmail.com>
> > ---
> 
> "small int" for u64 looks funny, but naming is hard :)
> 
> Acked-by: Andrii Nakryiko <andriin@fb.com>
> 

Fixed up the parens and sent a v2 keeping the ACK. I don't have any
better ideas for the name, let me know if you have a preference for
something else.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [bpf PATCH] bpf: do not allow btf_ctx_access with __int128 types
  2020-06-23 17:13 [bpf PATCH] bpf: do " John Fastabend
@ 2020-06-23 18:33 ` Andrii Nakryiko
  2020-06-24 22:23   ` John Fastabend
  0 siblings, 1 reply; 5+ messages in thread
From: Andrii Nakryiko @ 2020-06-23 18:33 UTC (permalink / raw)
  To: John Fastabend
  Cc: Jiri Olsa, Alexei Starovoitov, Daniel Borkmann, Networking, bpf

On Tue, Jun 23, 2020 at 10:14 AM John Fastabend
<john.fastabend@gmail.com> wrote:
>
> To ensure btf_ctx_access() is safe the verifier checks that the BTF
> arg type is an int, enum, or pointer. When the function does the
> BTF arg lookup it uses the calculation 'arg = off / 8'  using the
> fact that registers are 8B. This requires that the first arg is
> in the first reg, the second in the second, and so on. However,
> for __int128 the arg will consume two registers by default LLVM
> implementation. So this will cause the arg layout assumed by the
> 'arg = off / 8' calculation to be incorrect.
>
> Because __int128 is uncommon this patch applies the easiest fix and
> will force int types to be sizeof(u64) or smaller so that they will
> fit in a single register.
>
> Fixes: 9e15db66136a1 ("bpf: Implement accurate raw_tp context access via BTF")
> Signed-off-by: John Fastabend <john.fastabend@gmail.com>
> ---

"small int" for u64 looks funny, but naming is hard :)

Acked-by: Andrii Nakryiko <andriin@fb.com>

>  include/linux/btf.h |    5 +++++
>  kernel/bpf/btf.c    |    4 ++--
>  2 files changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/include/linux/btf.h b/include/linux/btf.h
> index 5c1ea99..35642f6 100644
> --- a/include/linux/btf.h
> +++ b/include/linux/btf.h
> @@ -82,6 +82,11 @@ static inline bool btf_type_is_int(const struct btf_type *t)
>         return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
>  }
>
> +static inline bool btf_type_is_small_int(const struct btf_type *t)
> +{
> +       return btf_type_is_int(t) && (t->size <= sizeof(u64));

nit: unnecessary (), () are usually used to disambiguate | and &  vs
|| and &&; this is not the case, though.

> +}
> +
>  static inline bool btf_type_is_enum(const struct btf_type *t)
>  {
>         return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index 58c9af1..9a1a98d 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -3746,7 +3746,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
>                                 return false;
>
>                         t = btf_type_skip_modifiers(btf, t->type, NULL);
> -                       if (!btf_type_is_int(t)) {
> +                       if (!btf_type_is_small_int(t)) {
>                                 bpf_log(log,
>                                         "ret type %s not allowed for fmod_ret\n",
>                                         btf_kind_str[BTF_INFO_KIND(t->info)]);
> @@ -3768,7 +3768,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
>         /* skip modifiers */
>         while (btf_type_is_modifier(t))
>                 t = btf_type_by_id(btf, t->type);
> -       if (btf_type_is_int(t) || btf_type_is_enum(t))
> +       if (btf_type_is_small_int(t) || btf_type_is_enum(t))
>                 /* accessing a scalar */
>                 return true;
>         if (!btf_type_is_ptr(t)) {
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [bpf PATCH] bpf: do not allow btf_ctx_access with __int128 types
@ 2020-06-23 17:13 John Fastabend
  2020-06-23 18:33 ` Andrii Nakryiko
  0 siblings, 1 reply; 5+ messages in thread
From: John Fastabend @ 2020-06-23 17:13 UTC (permalink / raw)
  To: jolsa, ast, daniel; +Cc: netdev, bpf, john.fastabend

To ensure btf_ctx_access() is safe the verifier checks that the BTF
arg type is an int, enum, or pointer. When the function does the
BTF arg lookup it uses the calculation 'arg = off / 8'  using the
fact that registers are 8B. This requires that the first arg is
in the first reg, the second in the second, and so on. However,
for __int128 the arg will consume two registers by default LLVM
implementation. So this will cause the arg layout assumed by the
'arg = off / 8' calculation to be incorrect.

Because __int128 is uncommon this patch applies the easiest fix and
will force int types to be sizeof(u64) or smaller so that they will
fit in a single register.

Fixes: 9e15db66136a1 ("bpf: Implement accurate raw_tp context access via BTF")
Signed-off-by: John Fastabend <john.fastabend@gmail.com>
---
 include/linux/btf.h |    5 +++++
 kernel/bpf/btf.c    |    4 ++--
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/include/linux/btf.h b/include/linux/btf.h
index 5c1ea99..35642f6 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -82,6 +82,11 @@ static inline bool btf_type_is_int(const struct btf_type *t)
 	return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
 }
 
+static inline bool btf_type_is_small_int(const struct btf_type *t)
+{
+	return btf_type_is_int(t) && (t->size <= sizeof(u64));
+}
+
 static inline bool btf_type_is_enum(const struct btf_type *t)
 {
 	return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 58c9af1..9a1a98d 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -3746,7 +3746,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
 				return false;
 
 			t = btf_type_skip_modifiers(btf, t->type, NULL);
-			if (!btf_type_is_int(t)) {
+			if (!btf_type_is_small_int(t)) {
 				bpf_log(log,
 					"ret type %s not allowed for fmod_ret\n",
 					btf_kind_str[BTF_INFO_KIND(t->info)]);
@@ -3768,7 +3768,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
 	/* skip modifiers */
 	while (btf_type_is_modifier(t))
 		t = btf_type_by_id(btf, t->type);
-	if (btf_type_is_int(t) || btf_type_is_enum(t))
+	if (btf_type_is_small_int(t) || btf_type_is_enum(t))
 		/* accessing a scalar */
 		return true;
 	if (!btf_type_is_ptr(t)) {


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-06-25 15:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-24 22:20 [bpf PATCH] bpf: Do not allow btf_ctx_access with __int128 types John Fastabend
2020-06-25 15:01 ` Daniel Borkmann
  -- strict thread matches above, loose matches on Subject: below --
2020-06-23 17:13 [bpf PATCH] bpf: do " John Fastabend
2020-06-23 18:33 ` Andrii Nakryiko
2020-06-24 22:23   ` John Fastabend

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.