Skip to contentSkip to conversation

libbpf: Fix overflow detection when dumping bitfields

PATCH In review Ilya Leoshkevich 2 participants 2 messages Apr 28, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: Fix overflow detection when dumping bitfields

btf_dump test fails on s390x with the following error:

    unexpected return value dumping fs_context: actual -7 != expected 280

This happens when processing the fs_context.phase member: its type size is 4, but there are less bytes left until the end of the struct. The problem is that btf_dump_type_data_check_overflow() does not handle bitfields.

Add bitfield support; make sure that byte boundaries, which are computed from bit boundaries, are rounded up.

Fixes 920d16af9b42 ("libbpf: BTF dumper support for typed data")
---
 tools/lib/bpf/btf_dump.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
tools/lib/bpf/btf_dump.c +9 -3
index 580985ee5545..f8b538e8d753 100644
@@ -2250,9 +2250,11 @@ static int btf_dump_type_data_check_overflow(struct btf_dump *d,
 					     const struct btf_type *t,
 					     __u32 id,
 					     const void *data,
-					     __u8 bits_offset)
+					     __u8 bits_offset,
+					     __u8 bit_sz)
 {
 	__s64 size = btf__resolve_size(d->btf, id);
+	const void *end;
 
 	if (size < 0 || size >= INT_MAX) {
 		pr_warn("unexpected size [%zu] for id [%u]\n",
@@ -2280,7 +2282,11 @@ static int btf_dump_type_data_check_overflow(struct btf_dump *d,
 	case BTF_KIND_PTR:
 	case BTF_KIND_ENUM:
 	case BTF_KIND_ENUM64:
-		if (data + bits_offset / 8 + size > d->typed_dump->data_end)
+		if (bit_sz)
+			end = data + (bits_offset + bit_sz + 7) / 8;
+		else
+			end = data + (bits_offset + 7) / 8 + size;
+		if (end > d->typed_dump->data_end)
 			return -E2BIG;
 		break;
 	default:
@@ -2407,7 +2413,7 @@ static int btf_dump_dump_type_data(struct btf_dump *d,
 {
 	int size, err = 0;
 
-	size = btf_dump_type_data_check_overflow(d, t, id, data, bits_offset);
+	size = btf_dump_type_data_check_overflow(d, t, id, data, bits_offset, bit_sz);
 	if (size < 0)
 		return size;
 	err = btf_dump_type_data_check_zero(d, t, id, data, bits_offset, bit_sz);
Signature · 1 line
2.40.0
#1 lore raw
Quoted 11 lines · Ilya Leoshkevich

btf_dump test fails on s390x with the following error:

     unexpected return value dumping fs_context: actual -7 != expected 280

This happens when processing the fs_context.phase member: its type size is 4, but there are less bytes left until the end of the struct. The problem is that btf_dump_type_data_check_overflow() does not handle bitfields.

Add bitfield support; make sure that byte boundaries, which are computed from bit boundaries, are rounded up.

Ilya, Martin has submitted a patch yesterday to fix the issue:

https://lore.kernel.org/bpf/[email protected]/

Quoted 46 lines
Fixes 920d16af9b42 ("libbpf: BTF dumper support for typed data")
---
  tools/lib/bpf/btf_dump.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
tools/lib/bpf/btf_dump.c +9 -3
index 580985ee5545..f8b538e8d753 100644
@@ -2250,9 +2250,11 @@ static int btf_dump_type_data_check_overflow(struct btf_dump *d,
  					     const struct btf_type *t,
  					     __u32 id,
  					     const void *data,
-					     __u8 bits_offset)
+					     __u8 bits_offset,
+					     __u8 bit_sz)
  {
  	__s64 size = btf__resolve_size(d->btf, id);
+	const void *end;
 
  	if (size < 0 || size >= INT_MAX) {
  		pr_warn("unexpected size [%zu] for id [%u]\n",
@@ -2280,7 +2282,11 @@ static int btf_dump_type_data_check_overflow(struct btf_dump *d,
  	case BTF_KIND_PTR:
  	case BTF_KIND_ENUM:
  	case BTF_KIND_ENUM64:
-		if (data + bits_offset / 8 + size > d->typed_dump->data_end)
+		if (bit_sz)
+			end = data + (bits_offset + bit_sz + 7) / 8;
+		else
+			end = data + (bits_offset + 7) / 8 + size;
+		if (end > d->typed_dump->data_end)
  			return -E2BIG;
  		break;
  	default:
@@ -2407,7 +2413,7 @@ static int btf_dump_dump_type_data(struct btf_dump *d,
  {
  	int size, err = 0;
 
-	size = btf_dump_type_data_check_overflow(d, t, id, data, bits_offset);
+	size = btf_dump_type_data_check_overflow(d, t, id, data, bits_offset, bit_sz);
  	if (size < 0)
  		return size;
  	err = btf_dump_type_data_check_zero(d, t, id, data, bits_offset, bit_sz);