Skip to contentSkip to conversation

bpf: btf: Fix endianness of bitfields

PATCH Superseded Okash Khawaja 4 participants 10 messages Jul 9, 2018 – Jul 10, 2018

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover bpf: btf: Fix endianness of bitfields

Hi,

This patch fixes endianness bug in btf_int_bits_seq_show(). Jakub Kicinski pointed out in a separate patch review for bpftool ("bpf: btf:
add btf print functionality") that parsing of bitfield might not work on big endian machine. Similar parsing is performed in btf_int_bits_seq_show() and this patch fixes it.

Thanks,
Okash

#1 lore raw

PATCH 1/1 bpf: btf: Fix bitfield extraction for big endian

When extracting bitfield from a number, btf_int_bits_seq_show() builds a mask and accesses least significant byte of the number in a way specific to little-endian. This patch fixes that by checking endianness of the machine and then shifting left and right the unneeded bits.

Thanks to Martin Lau for the help in navigating potential pitfalls when dealing with endianess and for the final solution.

Fixes b00b8daec828 ("bpf: btf: Add pretty print capability for data with BTF type info")
---
 kernel/bpf/btf.c |   32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)
kernel/bpf/btf.c +15 -17
@@ -162,6 +162,8 @@
 #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
 #define BITS_ROUNDUP_BYTES(bits) \
 	(BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
+const int one = 1;
+#define is_big_endian() ((*(char *)&one) == 0)
 
 #define BTF_INFO_MASK 0x0f00ffff
 #define BTF_INT_MASK 0x0fffffff
@@ -991,16 +993,13 @@ static void btf_int_bits_seq_show(const
 				  void *data, u8 bits_offset,
 				  struct seq_file *m)
 {
+	u8 left_shift_bits, right_shift_bits;
 	u32 int_data = btf_type_int(t);
 	u16 nr_bits = BTF_INT_BITS(int_data);
 	u16 total_bits_offset;
 	u16 nr_copy_bytes;
 	u16 nr_copy_bits;
-	u8 nr_upper_bits;
-	union {
-		u64 u64_num;
-		u8  u8_nums[8];
-	} print_num;
+	u64 print_num;
 
 	total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data);
 	data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
@@ -1008,21 +1007,20 @@ static void btf_int_bits_seq_show(const
 	nr_copy_bits = nr_bits + bits_offset;
 	nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits);
 
-	print_num.u64_num = 0;
-	memcpy(&print_num.u64_num, data, nr_copy_bytes);
-
-	/* Ditch the higher order bits */
-	nr_upper_bits = BITS_PER_BYTE_MASKED(nr_copy_bits);
-	if (nr_upper_bits) {
-		/* We need to mask out some bits of the upper byte. */
-		u8 mask = (1 << nr_upper_bits) - 1;
-
-		print_num.u8_nums[nr_copy_bytes - 1] &= mask;
+	print_num = 0;
+	memcpy(&print_num, data, nr_copy_bytes);
+	if (is_big_endian()) {
+		left_shift_bits = bits_offset;
+		right_shift_bits = BITS_PER_U64 - nr_bits;
+	} else {
+		left_shift_bits = BITS_PER_U64 - nr_copy_bits;
+		right_shift_bits = BITS_PER_U64 - nr_bits;
 	}
 
-	print_num.u64_num >>= bits_offset;
+	print_num <<= left_shift_bits;
+	print_num >>= right_shift_bits;
 
-	seq_printf(m, "0x%llx", print_num.u64_num);
+	seq_printf(m, "0x%llx", print_num);
 }
 
 static void btf_int_seq_show(const struct btf *btf, const struct btf_type *t,
#2 lore raw

Re: [PATCH bpf 1/1] bpf: btf: Fix bitfield extraction for big endian

Quoted 18 lines · Okash Khawaja

When extracting bitfield from a number, btf_int_bits_seq_show() builds a mask and accesses least significant byte of the number in a way specific to little-endian. This patch fixes that by checking endianness of the machine and then shifting left and right the unneeded bits.

Thanks to Martin Lau for the help in navigating potential pitfalls when dealing with endianess and for the final solution.

Fixes b00b8daec828 ("bpf: btf: Add pretty print capability for data with BTF type info")
---
 kernel/bpf/btf.c |   32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)

kernel/bpf/btf.c line 166

Quoted 5 lines
kernel/bpf/btf.c +2 -0
@@ -162,6 +162,8 @@
 #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
 #define BITS_ROUNDUP_BYTES(bits) \
 	(BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
+const int one = 1;
+#define is_big_endian() ((*(char *)&one) == 0)

Can the __BIG_ENDIAN be reused here?

Quoted 56 lines
kernel/bpf/btf.c +13 -17
 #define BTF_INFO_MASK 0x0f00ffff
 #define BTF_INT_MASK 0x0fffffff
@@ -991,16 +993,13 @@ static void btf_int_bits_seq_show(const
 				  void *data, u8 bits_offset,
 				  struct seq_file *m)
 {
+	u8 left_shift_bits, right_shift_bits;
 	u32 int_data = btf_type_int(t);
 	u16 nr_bits = BTF_INT_BITS(int_data);
 	u16 total_bits_offset;
 	u16 nr_copy_bytes;
 	u16 nr_copy_bits;
-	u8 nr_upper_bits;
-	union {
-		u64 u64_num;
-		u8  u8_nums[8];
-	} print_num;
+	u64 print_num;
 
 	total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data);
 	data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
@@ -1008,21 +1007,20 @@ static void btf_int_bits_seq_show(const
 	nr_copy_bits = nr_bits + bits_offset;
 	nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits);
 
-	print_num.u64_num = 0;
-	memcpy(&print_num.u64_num, data, nr_copy_bytes);
-
-	/* Ditch the higher order bits */
-	nr_upper_bits = BITS_PER_BYTE_MASKED(nr_copy_bits);
-	if (nr_upper_bits) {
-		/* We need to mask out some bits of the upper byte. */
-		u8 mask = (1 << nr_upper_bits) - 1;
-
-		print_num.u8_nums[nr_copy_bytes - 1] &= mask;
+	print_num = 0;
+	memcpy(&print_num, data, nr_copy_bytes);
+	if (is_big_endian()) {
+		left_shift_bits = bits_offset;
+		right_shift_bits = BITS_PER_U64 - nr_bits;
+	} else {
+		left_shift_bits = BITS_PER_U64 - nr_copy_bits;
+		right_shift_bits = BITS_PER_U64 - nr_bits;
 	}
 
-	print_num.u64_num >>= bits_offset;
+	print_num <<= left_shift_bits;
+	print_num >>= right_shift_bits;
 
-	seq_printf(m, "0x%llx", print_num.u64_num);
+	seq_printf(m, "0x%llx", print_num);
 }
 
 static void btf_int_seq_show(const struct btf *btf, const struct btf_type *t,
#3 lore raw

Re: [PATCH bpf 1/1] bpf: btf: Fix bitfield extraction for big endian

In reply to Okash Khawaja
Quoted 27 lines · Okash Khawaja

When extracting bitfield from a number, btf_int_bits_seq_show() builds a mask and accesses least significant byte of the number in a way specific to little-endian. This patch fixes that by checking endianness of the machine and then shifting left and right the unneeded bits.

Thanks to Martin Lau for the help in navigating potential pitfalls when dealing with endianess and for the final solution.

Fixes b00b8daec828 ("bpf: btf: Add pretty print capability for data with BTF type info")
---
 kernel/bpf/btf.c |   32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)
kernel/bpf/btf.c +2 -0
@@ -162,6 +162,8 @@
 #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
 #define BITS_ROUNDUP_BYTES(bits) \
 	(BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
+const int one = 1;
+#define is_big_endian() ((*(char *)&one) == 0)
 
 #define BTF_INFO_MASK 0x0f00ffff
 #define BTF_INT_MASK 0x0fffffff

kernel/bpf/btf.c static void btf_int_bits_seq_show(const · line 996

Quoted 4 lines
kernel/bpf/btf.c +1 -0
@@ -991,16 +993,13 @@ static void btf_int_bits_seq_show(const
 				  void *data, u8 bits_offset,
 				  struct seq_file *m)
 {
+	u8 left_shift_bits, right_shift_bits;

Nit.
Although only max 64 bit int is allowed now (ensured by btf_int_check_meta), it is better to use u16 such that it will be consistent to BTF_INT_BITS.

Quoted 26 lines
kernel/bpf/btf.c +1 -13
 	u32 int_data = btf_type_int(t);
 	u16 nr_bits = BTF_INT_BITS(int_data);
 	u16 total_bits_offset;
 	u16 nr_copy_bytes;
 	u16 nr_copy_bits;
-	u8 nr_upper_bits;
-	union {
-		u64 u64_num;
-		u8  u8_nums[8];
-	} print_num;
+	u64 print_num;
 
 	total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data);
 	data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
@@ -1008,21 +1007,20 @@ static void btf_int_bits_seq_show(const
 	nr_copy_bits = nr_bits + bits_offset;
 	nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits);
 
-	print_num.u64_num = 0;
-	memcpy(&print_num.u64_num, data, nr_copy_bytes);
-
-	/* Ditch the higher order bits */
-	nr_upper_bits = BITS_PER_BYTE_MASKED(nr_copy_bits);
-	if (nr_upper_bits) {
-		/* We need to mask out some bits of the upper byte. */
-		u8 mask = (1 << nr_upper_bits) - 1;

kernel/bpf/btf.c static void btf_int_bits_seq_show(const · line 1017

Quoted 10 lines
kernel/bpf/btf.c +8 -2
-
-		print_num.u8_nums[nr_copy_bytes - 1] &= mask;
+	print_num = 0;
+	memcpy(&print_num, data, nr_copy_bytes);
+	if (is_big_endian()) {
+		left_shift_bits = bits_offset;
+		right_shift_bits = BITS_PER_U64 - nr_bits;
+	} else {
+		left_shift_bits = BITS_PER_U64 - nr_copy_bits;
+		right_shift_bits = BITS_PER_U64 - nr_bits;

Nit.
right_shift_bits is the same for both cases. Lets simplify it.

Quoted 12 lines
kernel/bpf/btf.c +3 -2
 	}
 
-	print_num.u64_num >>= bits_offset;
+	print_num <<= left_shift_bits;
+	print_num >>= right_shift_bits;
 
-	seq_printf(m, "0x%llx", print_num.u64_num);
+	seq_printf(m, "0x%llx", print_num);
 }
 
 static void btf_int_seq_show(const struct btf *btf, const struct btf_type *t,
maintainer
#4 lore raw

Re: [PATCH bpf 1/1] bpf: btf: Fix bitfield extraction for big endian

Quoted 16 lines · Martin KaFai Lau
Quoted 15 lines · Okash Khawaja

When extracting bitfield from a number, btf_int_bits_seq_show() builds a mask and accesses least significant byte of the number in a way specific to little-endian. This patch fixes that by checking endianness of the machine and then shifting left and right the unneeded bits.

Thanks to Martin Lau for the help in navigating potential pitfalls when dealing with endianess and for the final solution.

Fixes b00b8daec828 ("bpf: btf: Add pretty print capability for data with BTF type info")
---
 kernel/bpf/btf.c |   32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)
Quoted 8 lines

kernel/bpf/btf.c line 166

Quoted 8 lines
kernel/bpf/btf.c +2 -0
@@ -162,6 +162,8 @@
 #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
 #define BITS_ROUNDUP_BYTES(bits) \
 	(BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
+const int one = 1;
+#define is_big_endian() ((*(char *)&one) == 0)

Also here, in the kernel archs provide proper definitions.

Quoted 62 lines

kernel/bpf/btf.c static void btf_int_bits_seq_show(const · line 996

Quoted 7 lines
kernel/bpf/btf.c +1 -0
 #define BTF_INFO_MASK 0x0f00ffff
 #define BTF_INT_MASK 0x0fffffff
@@ -991,16 +993,13 @@ static void btf_int_bits_seq_show(const
 				  void *data, u8 bits_offset,
 				  struct seq_file *m)
 {
+	u8 left_shift_bits, right_shift_bits;

Nit.
Although only max 64 bit int is allowed now (ensured by btf_int_check_meta), it is better to use u16 such that it will be consistent to BTF_INT_BITS.

Quoted 26 lines
kernel/bpf/btf.c +1 -13
 	u32 int_data = btf_type_int(t);
 	u16 nr_bits = BTF_INT_BITS(int_data);
 	u16 total_bits_offset;
 	u16 nr_copy_bytes;
 	u16 nr_copy_bits;
-	u8 nr_upper_bits;
-	union {
-		u64 u64_num;
-		u8  u8_nums[8];
-	} print_num;
+	u64 print_num;
 
 	total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data);
 	data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
@@ -1008,21 +1007,20 @@ static void btf_int_bits_seq_show(const
 	nr_copy_bits = nr_bits + bits_offset;
 	nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits);
 
-	print_num.u64_num = 0;
-	memcpy(&print_num.u64_num, data, nr_copy_bytes);
-
-	/* Ditch the higher order bits */
-	nr_upper_bits = BITS_PER_BYTE_MASKED(nr_copy_bits);
-	if (nr_upper_bits) {
-		/* We need to mask out some bits of the upper byte. */
-		u8 mask = (1 << nr_upper_bits) - 1;

kernel/bpf/btf.c static void btf_int_bits_seq_show(const · line 1017

Quoted 10 lines
kernel/bpf/btf.c +8 -2
-
-		print_num.u8_nums[nr_copy_bytes - 1] &= mask;
+	print_num = 0;
+	memcpy(&print_num, data, nr_copy_bytes);
+	if (is_big_endian()) {
+		left_shift_bits = bits_offset;
+		right_shift_bits = BITS_PER_U64 - nr_bits;
+	} else {
+		left_shift_bits = BITS_PER_U64 - nr_copy_bits;
+		right_shift_bits = BITS_PER_U64 - nr_bits;

Nit.
right_shift_bits is the same for both cases. Lets simplify it.

Quoted 12 lines
kernel/bpf/btf.c +3 -2
 	}
 
-	print_num.u64_num >>= bits_offset;
+	print_num <<= left_shift_bits;
+	print_num >>= right_shift_bits;
 
-	seq_printf(m, "0x%llx", print_num.u64_num);
+	seq_printf(m, "0x%llx", print_num);
 }
 
 static void btf_int_seq_show(const struct btf *btf, const struct btf_type *t,
#5 lore raw

Re: [PATCH bpf 1/1] bpf: btf: Fix bitfield extraction for big endian

Quoted 17 lines · Daniel Borkmann
Quoted 16 lines · Martin KaFai Lau
Quoted 15 lines · Okash Khawaja

When extracting bitfield from a number, btf_int_bits_seq_show() builds a mask and accesses least significant byte of the number in a way specific to little-endian. This patch fixes that by checking endianness of the machine and then shifting left and right the unneeded bits.

Thanks to Martin Lau for the help in navigating potential pitfalls when dealing with endianess and for the final solution.

Fixes b00b8daec828 ("bpf: btf: Add pretty print capability for data with BTF type info")
---
 kernel/bpf/btf.c |   32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)
Quoted 10 lines
Quoted 8 lines

kernel/bpf/btf.c line 166

Quoted 8 lines
kernel/bpf/btf.c +2 -0
@@ -162,6 +162,8 @@
 #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
 #define BITS_ROUNDUP_BYTES(bits) \
 	(BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
+const int one = 1;
+#define is_big_endian() ((*(char *)&one) == 0)

Also here, in the kernel archs provide proper definitions.

Is this the __BIG_ENDIAN #define or are there better ways to check that?

Thanks,
Okash

maintainer
#6 lore raw

Re: [PATCH bpf 1/1] bpf: btf: Fix bitfield extraction for big endian

Quoted 28 lines · Okash Khawaja
Quoted 17 lines · Daniel Borkmann
Quoted 16 lines · Martin KaFai Lau
Quoted 15 lines · Okash Khawaja

When extracting bitfield from a number, btf_int_bits_seq_show() builds a mask and accesses least significant byte of the number in a way specific to little-endian. This patch fixes that by checking endianness of the machine and then shifting left and right the unneeded bits.

Thanks to Martin Lau for the help in navigating potential pitfalls when dealing with endianess and for the final solution.

Fixes b00b8daec828 ("bpf: btf: Add pretty print capability for data with BTF type info")
---
 kernel/bpf/btf.c |   32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)
Quoted 10 lines
Quoted 8 lines

kernel/bpf/btf.c line 166

Quoted 8 lines
kernel/bpf/btf.c +2 -0
@@ -162,6 +162,8 @@
 #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
 #define BITS_ROUNDUP_BYTES(bits) \
 	(BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
+const int one = 1;
+#define is_big_endian() ((*(char *)&one) == 0)

Also here, in the kernel archs provide proper definitions.

Quoted 1 line

Is this the __BIG_ENDIAN #define or are there better ways to check that?

Given this deals with bitfields, should be __{BIG,LITTLE}_ENDIAN_BITFIELD.

#7 lore raw

RE: [PATCH bpf 1/1] bpf: btf: Fix bitfield extraction for big endian

In reply to Martin KaFai Lau

From: Martin KaFai Lau

Quoted 26 lines

Sent: 09 July 2018 19:33

Quoted 24 lines · Okash Khawaja

When extracting bitfield from a number, btf_int_bits_seq_show() builds a mask and accesses least significant byte of the number in a way specific to little-endian. This patch fixes that by checking endianness of the machine and then shifting left and right the unneeded bits.

Thanks to Martin Lau for the help in navigating potential pitfalls when dealing with endianess and for the final solution.

Fixes b00b8daec828 ("bpf: btf: Add pretty print capability for data with BTF type info")
---
 kernel/bpf/btf.c |   32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)
kernel/bpf/btf.c +2 -0
@@ -162,6 +162,8 @@
 #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
 #define BITS_ROUNDUP_BYTES(bits) \
 	(BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
+const int one = 1;
+#define is_big_endian() ((*(char *)&one) == 0)
 
Quoted 10 lines

kernel/bpf/btf.c static void btf_int_bits_seq_show(const · line 996

Quoted 7 lines
kernel/bpf/btf.c +1 -0
 #define BTF_INFO_MASK 0x0f00ffff
 #define BTF_INT_MASK 0x0fffffff
@@ -991,16 +993,13 @@ static void btf_int_bits_seq_show(const
 				  void *data, u8 bits_offset,
 				  struct seq_file *m)
 {
+	u8 left_shift_bits, right_shift_bits;

Nit.
Although only max 64 bit int is allowed now (ensured by btf_int_check_meta), it is better to use u16 such that it will be consistent to BTF_INT_BITS.

Double-nit.

Use 'int' or 'unsigned int'.
Sub-word arithmetic will require extra instructions on almost everything except x86.

David

#8 lore raw

Re: [PATCH bpf 1/1] bpf: btf: Fix bitfield extraction for big endian

Quoted 38 lines · David Laight

From: Martin KaFai Lau

Quoted 26 lines

Sent: 09 July 2018 19:33

Quoted 24 lines · Okash Khawaja

When extracting bitfield from a number, btf_int_bits_seq_show() builds a mask and accesses least significant byte of the number in a way specific to little-endian. This patch fixes that by checking endianness of the machine and then shifting left and right the unneeded bits.

Thanks to Martin Lau for the help in navigating potential pitfalls when dealing with endianess and for the final solution.

Fixes b00b8daec828 ("bpf: btf: Add pretty print capability for data with BTF type info")
---
 kernel/bpf/btf.c |   32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)
kernel/bpf/btf.c +2 -0
@@ -162,6 +162,8 @@
 #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
 #define BITS_ROUNDUP_BYTES(bits) \
 	(BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
+const int one = 1;
+#define is_big_endian() ((*(char *)&one) == 0)
 
Quoted 10 lines

kernel/bpf/btf.c static void btf_int_bits_seq_show(const · line 996

Quoted 7 lines
kernel/bpf/btf.c +1 -0
 #define BTF_INFO_MASK 0x0f00ffff
 #define BTF_INT_MASK 0x0fffffff
@@ -991,16 +993,13 @@ static void btf_int_bits_seq_show(const
 				  void *data, u8 bits_offset,
 				  struct seq_file *m)
 {
+	u8 left_shift_bits, right_shift_bits;

Nit.
Although only max 64 bit int is allowed now (ensured by btf_int_check_meta), it is better to use u16 such that it will be consistent to BTF_INT_BITS.

Quoted 5 lines

Double-nit.

Use 'int' or 'unsigned int'.
Sub-word arithmetic will require extra instructions on almost everything except x86.

I would prefer to keep it as u16 which is the max width that is allowed for this field in the wire format. Keeping the usage consistent can avoid accidentally incorrect offsetting or writing wrong data out in other cases.

maintainer
#9 lore raw

Re: [PATCH bpf 1/1] bpf: btf: Fix bitfield extraction for big endian

On 07/10/2018 07:18 PM, Martin KaFai Lau wrote:
[...]

Quoted 4 lines

I would prefer to keep it as u16 which is the max width that is allowed for this field in the wire format. Keeping the usage consistent can avoid accidentally incorrect offsetting or writing wrong data out in other cases.

+1