bpf: don't assume build-id length is always 20 bytes
Subsystems (6)
PATCH 1/3 bpf: don't assume build-id length is always 20 bytes
Build-id length is not fixed to 20, it can be (man ld /--build-id):
* 128-bit (uuid)
* 160-bit (sha1)
* any length specified in ld --build-id=0xhexstring
To fix the issue of missing BPF_STACK_BUILD_ID_VALID for shorter build-ids, assume that build-id is somewhere in the range of 1 .. 20.
Set the remaining bytes to zero.
--- kernel/bpf/stackmap.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/bpf/stackmap.c +6 -2
@@ -180,11 +180,15 @@ static inline int stack_map_parse_build_id(void *page_addr,if (nhdr->n_type ==BPF_BUILD_ID && nhdr->n_namesz ==sizeof ("GNU" ) && - nhdr->n_descsz ==BPF_BUILD_ID_SIZE ) { + nhdr->n_descsz >0 && + nhdr->n_descsz <=BPF_BUILD_ID_SIZE ) { +__u32 len =min_t (__u32, +BPF_BUILD_ID_SIZE , nhdr->n_descsz);memcpy (build_id, note_start + note_offs +ALIGN (sizeof ("GNU" ),4 ) +sizeof (Elf32_Nhdr), -BPF_BUILD_ID_SIZE ); + len); +memset (build_id + len,0 ,BPF_BUILD_ID_SIZE - len);return 0 ; } new_offs = note_offs + sizeof(Elf32_Nhdr) +
Signature · 1 line
2.20.1.97.g81188d93c3-goog
PATCH 2/3 bpf: zero out build_id for BPF_STACK_BUILD_ID_IP
When returning BPF_STACK_BUILD_ID_IP from stack_map_get_build_id_offset, make sure that build_id field is empty. Since we are using percpu free list, there is a possibility that we might reuse some previous bpf_stack_build_id with non-zero build_id.
--- kernel/bpf/stackmap.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/bpf/stackmap.c +2 -0
@@ -315,6 +315,7 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,for (i =0 ; i < trace_nr; i++) { id_offs[i].status =BPF_STACK_BUILD_ID_IP ; id_offs[i].ip = ips[i]; +memset (id_offs[i].build_id,0 ,BPF_BUILD_ID_SIZE ); }return ; }
@@ -325,6 +326,7 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,/* per entry fall back to ips */ id_offs[i].status =BPF_STACK_BUILD_ID_IP ; id_offs[i].ip = ips[i]; +memset (id_offs[i].build_id,0 ,BPF_BUILD_ID_SIZE );continue ; } id_offs[i].offset = (vma->vm_pgoff <<PAGE_SHIFT ) + ips[i]
Signature · 1 line
2.20.1.97.g81188d93c3-goog
Re: [PATCH bpf 2/3] bpf: zero out build_id for BPF_STACK_BUILD_ID_IP
Quoted 11 lines · Stanislav Fomichev
When returning
BPF_STACK_BUILD_ID_IPfromstack_map_get_build_id_offset, make sure thatbuild_idfield is empty. Since we are using percpu free list, there is a possibility that we might reuse some previousbpf_stack_build_idwith non-zerobuild_id.Fixes 615755a77b24 ("bpf: extend stackmap to save binary_build_id+offset instead of address")Signed-off-by Stanislav Fomichev <[email protected]>
Quoted 27 lines
---
kernel/bpf/stackmap.c| 2 ++1 file changed, 2 insertions(+)kernel/bpf/stackmap.c +2
-0@@ -315,6 +315,7 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,for (i =0 ; i < trace_nr; i++) { id_offs[i].status =BPF_STACK_BUILD_ID_IP ; id_offs[i].ip = ips[i]; +memset (id_offs[i].build_id,0 ,BPF_BUILD_ID_SIZE ); }return ; }@@ -325,6 +326,7 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,/* per entry fall back to ips */ id_offs[i].status =BPF_STACK_BUILD_ID_IP ; id_offs[i].ip = ips[i]; +memset (id_offs[i].build_id,0 ,BPF_BUILD_ID_SIZE );continue ; } id_offs[i].offset = (vma->vm_pgoff <<PAGE_SHIFT ) + ips[i]Signature · 1 line
2.20.1.97.g81188d93c3-goog
PATCH 3/3 selftests/bpf: retry tests that expect build-id
In reply to Stanislav FomichevWhile running test_progs in a loop I found out that I'm sometimes hitting "Didn't find expected build ID from the map" error.
Looking at stack_map_get_build_id_offset() it seems that it is racy (by design) and can sometimes return BPF_STACK_BUILD_ID_IP (i.e. can't trylock current->mm->mmap_sem).
Let's retry this test a single time.
--- tools/testing/selftests/bpf/test_progs.c | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
tools/testing/selftests/bpf/test_progs.c +30 -0
@@ -1188,7 +1188,9 @@ static void test_stacktrace_build_id(void)int i, j;struct bpf_stack_build_id id_offs[PERF_MAX_STACK_DEPTH ];int build_id_matches =0 ; +int retry =1 ; +retry: err =bpf_prog_load (file,BPF_PROG_TYPE_TRACEPOINT , &obj, &prog_fd);if (CHECK (err,"prog_load" ,"err %d errno %d\n" , err, errno)) goto out;
@@ -1301,6 +1303,19 @@ static void test_stacktrace_build_id(void) previous_key = key; }while (bpf_map_get_next_key (stackmap_fd, &previous_key, &key) ==0 ); +/* stack_map_get_build_id_offset() is racy and sometimes can return +* BPF_STACK_BUILD_ID_IP instead of BPF_STACK_BUILD_ID_VALID; +* try it one more time. +*/ +if (build_id_matches <1 && retry--) { +ioctl (pmu_fd,PERF_EVENT_IOC_DISABLE ); +close (pmu_fd); +bpf_object__close (obj); +printf ("%s:WARN:Didn't find expected build ID from the map, retrying\n" , + __func__); + goto retry; + } +if (CHECK (build_id_matches <1 ,"build id match" ,"Didn't find expected build ID from the map\n" )) goto disable_pmu;
@@ -1341,7 +1356,9 @@ static void test_stacktrace_build_id_nmi(void)int i, j;struct bpf_stack_build_id id_offs[PERF_MAX_STACK_DEPTH ];int build_id_matches =0 ; +int retry =1 ; +retry: err =bpf_prog_load (file,BPF_PROG_TYPE_PERF_EVENT , &obj, &prog_fd);if (CHECK (err,"prog_load" ,"err %d errno %d\n" , err, errno))return ;
@@ -1436,6 +1453,19 @@ static void test_stacktrace_build_id_nmi(void) previous_key = key; }while (bpf_map_get_next_key (stackmap_fd, &previous_key, &key) ==0 ); +/* stack_map_get_build_id_offset() is racy and sometimes can return +* BPF_STACK_BUILD_ID_IP instead of BPF_STACK_BUILD_ID_VALID; +* try it one more time. +*/ +if (build_id_matches <1 && retry--) { +ioctl (pmu_fd,PERF_EVENT_IOC_DISABLE ); +close (pmu_fd); +bpf_object__close (obj); +printf ("%s:WARN:Didn't find expected build ID from the map, retrying\n" , + __func__); + goto retry; + } +if (CHECK (build_id_matches <1 ,"build id match" ,"Didn't find expected build ID from the map\n" )) goto disable_pmu;
Signature · 1 line
2.20.1.97.g81188d93c3-goog
Re: [PATCH bpf 3/3] selftests/bpf: retry tests that expect build-id
Quoted 10 lines · Stanislav Fomichev
While running
test_progsin a loop I found out that I'm sometimes hitting "Didn't find expected build ID from the map" error.
Looking atstack_map_get_build_id_offset()it seems that it is racy (by design) and can sometimes returnBPF_STACK_BUILD_ID_IP(i.e. can't trylockcurrent->mm->mmap_sem).
Quoted 4 lines
Let's retry this test a single time.
Fixes 13790d1cc72c ("bpf: add selftest for stackmap with build_id in NMI context")Signed-off-by Stanislav Fomichev <[email protected]>
Quoted 71 lines
---
tools/testing/selftests/bpf/test_progs.c| 30 ++++++++++++++++++++++++1 file changed, 30 insertions(+)tools/testing/selftests/bpf/test_progs.c +30
-0@@ -1188,7 +1188,9 @@ static void test_stacktrace_build_id(void)int i, j;struct bpf_stack_build_id id_offs[PERF_MAX_STACK_DEPTH ];int build_id_matches =0 ; +int retry =1 ; +retry: err =bpf_prog_load (file,BPF_PROG_TYPE_TRACEPOINT , &obj, &prog_fd);if (CHECK (err,"prog_load" ,"err %d errno %d\n" , err, errno)) goto out;@@ -1301,6 +1303,19 @@ static void test_stacktrace_build_id(void) previous_key = key; }while (bpf_map_get_next_key (stackmap_fd, &previous_key, &key) ==0 ); +/* stack_map_get_build_id_offset() is racy and sometimes can return +* BPF_STACK_BUILD_ID_IP instead of BPF_STACK_BUILD_ID_VALID; +* try it one more time. +*/ +if (build_id_matches <1 && retry--) { +ioctl (pmu_fd,PERF_EVENT_IOC_DISABLE ); +close (pmu_fd); +bpf_object__close (obj); +printf ("%s:WARN:Didn't find expected build ID from the map, retrying\n" , + __func__); + goto retry; + } +if (CHECK (build_id_matches <1 ,"build id match" ,"Didn't find expected build ID from the map\n" )) goto disable_pmu;@@ -1341,7 +1356,9 @@ static void test_stacktrace_build_id_nmi(void)int i, j;struct bpf_stack_build_id id_offs[PERF_MAX_STACK_DEPTH ];int build_id_matches =0 ; +int retry =1 ; +retry: err =bpf_prog_load (file,BPF_PROG_TYPE_PERF_EVENT , &obj, &prog_fd);if (CHECK (err,"prog_load" ,"err %d errno %d\n" , err, errno))return ;@@ -1436,6 +1453,19 @@ static void test_stacktrace_build_id_nmi(void) previous_key = key; }while (bpf_map_get_next_key (stackmap_fd, &previous_key, &key) ==0 ); +/* stack_map_get_build_id_offset() is racy and sometimes can return +* BPF_STACK_BUILD_ID_IP instead of BPF_STACK_BUILD_ID_VALID; +* try it one more time. +*/ +if (build_id_matches <1 && retry--) { +ioctl (pmu_fd,PERF_EVENT_IOC_DISABLE ); +close (pmu_fd); +bpf_object__close (obj); +printf ("%s:WARN:Didn't find expected build ID from the map, retrying\n" , + __func__); + goto retry; + } +if (CHECK (build_id_matches <1 ,"build id match" ,"Didn't find expected build ID from the map\n" )) goto disable_pmu;Signature · 1 line
2.20.1.97.g81188d93c3-goog
Quoted 24 lines · Stanislav Fomichev
Build-id length is not fixed to 20, it can be (
man ld/--build-id):
* 128-bit (uuid)
* 160-bit (sha1)
* any length specified in ld --build-id=0xhexstringTo fix the issue of missing
BPF_STACK_BUILD_ID_VALIDfor shorter build-ids, assume that build-id is somewhere in the range of 1 .. 20.
Set the remaining bytes to zero.Fixes 615755a77b24 ("bpf: extend stackmap to save binary_build_id+offset instead of address")Signed-off-by Stanislav Fomichev <[email protected]>---
kernel/bpf/stackmap.c| 8 ++++++--1 file changed, 6 insertions(+), 2 deletions(-)kernel/bpf/stackmap.c +0
-0
kernel/bpf/stackmap.c static inline int stack_map_parse_build_id(void *page_addr, · line 186
Quoted 8 lines
kernel/bpf/stackmap.c +4
-1@@ -180,11 +180,15 @@ static inline int stack_map_parse_build_id(void *page_addr,if (nhdr->n_type ==BPF_BUILD_ID && nhdr->n_namesz ==sizeof ("GNU" ) && - nhdr->n_descsz ==BPF_BUILD_ID_SIZE ) { + nhdr->n_descsz >0 && + nhdr->n_descsz <=BPF_BUILD_ID_SIZE ) { +__u32 len =min_t (__u32, +BPF_BUILD_ID_SIZE , nhdr->n_descsz);
Given the check above, we only need len = nhdr->n_descsz, right?
Other than this,
Thanks for the fix!
Quoted 12 lines
kernel/bpf/stackmap.c +2
-1memcpy (build_id, note_start + note_offs +ALIGN (sizeof ("GNU" ),4 ) +sizeof (Elf32_Nhdr), -BPF_BUILD_ID_SIZE ); + len); +memset (build_id + len,0 ,BPF_BUILD_ID_SIZE - len);return 0 ; } new_offs = note_offs + sizeof(Elf32_Nhdr) +Signature · 1 line
2.20.1.97.g81188d93c3-goog
Quoted 26 lines · Song Liu
Quoted 26 lines · Stanislav Fomichev
Build-id length is not fixed to 20, it can be (
man ld/--build-id):
* 128-bit (uuid)
* 160-bit (sha1)
* any length specified in ld --build-id=0xhexstringTo fix the issue of missing
BPF_STACK_BUILD_ID_VALIDfor shorter build-ids, assume that build-id is somewhere in the range of 1 .. 20.
Set the remaining bytes to zero.Fixes 615755a77b24 ("bpf: extend stackmap to save binary_build_id+offset instead of address")Signed-off-by Stanislav Fomichev <[email protected]>---
kernel/bpf/stackmap.c| 8 ++++++--1 file changed, 6 insertions(+), 2 deletions(-)kernel/bpf/stackmap.c +0
-0@@ -180,11 +180,15 @@ static inline int stack_map_parse_build_id(void *page_addr,
Quoted 9 lines
kernel/bpf/stackmap.c static inline int stack_map_parse_build_id(void *page_addr, · line 186
Quoted 7 lines
kernel/bpf/stackmap.c +4
-1if (nhdr->n_type ==BPF_BUILD_ID && nhdr->n_namesz ==sizeof ("GNU" ) && - nhdr->n_descsz ==BPF_BUILD_ID_SIZE ) { + nhdr->n_descsz >0 && + nhdr->n_descsz <=BPF_BUILD_ID_SIZE ) { +__u32 len =min_t (__u32, +BPF_BUILD_ID_SIZE , nhdr->n_descsz);Given the check above, we only need len =
nhdr->n_descsz, right?
Ah, correct, I'll fix in v2.
I initially had without if (nhdr->n_descsz <= BPF_BUILD_ID_SIZE) and clamped it here, but then decided that clamping is probably bad as well.
Quoted 20 lines
Other than this,
Acked-by Song Liu <[email protected]>Thanks for the fix!
Quoted 12 lines
kernel/bpf/stackmap.c +2
-2memcpy (build_id, note_start + note_offs +ALIGN (sizeof ("GNU" ),4 ) +sizeof (Elf32_Nhdr), -BPF_BUILD_ID_SIZE ); + len); +memset (build_id + len,0 ,BPF_BUILD_ID_SIZE - len);return 0 ; } new_offs = note_offs + sizeof(Elf32_Nhdr) + --2.20.1.97.g81188d93c3-goog
PATCH v2 1/3 bpf: don't assume build-id length is always 20 bytes
In reply to Song LiuBuild-id length is not fixed to 20, it can be (man ld /--build-id):
* 128-bit (uuid)
* 160-bit (sha1)
* any length specified in ld --build-id=0xhexstring
To fix the issue of missing BPF_STACK_BUILD_ID_VALID for shorter build-ids, assume that build-id is somewhere in the range of 1 .. 20.
Set the remaining bytes to zero.
v2:
* don't introduce new "len = min(BPF_BUILD_ID_SIZE, nhdr->n_descsz)", we already know that nhdr->n_descsz <= BPF_BUILD_ID_SIZE if we enter this 'if' condition
--- kernel/bpf/stackmap.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
kernel/bpf/stackmap.c +5 -2
@@ -180,11 +180,14 @@ static inline int stack_map_parse_build_id(void *page_addr,if (nhdr->n_type ==BPF_BUILD_ID && nhdr->n_namesz ==sizeof ("GNU" ) && - nhdr->n_descsz ==BPF_BUILD_ID_SIZE ) { + nhdr->n_descsz >0 && + nhdr->n_descsz <=BPF_BUILD_ID_SIZE ) {memcpy (build_id, note_start + note_offs +ALIGN (sizeof ("GNU" ),4 ) +sizeof (Elf32_Nhdr), -BPF_BUILD_ID_SIZE ); + nhdr->n_descsz); +memset (build_id + nhdr->n_descsz,0 , +BPF_BUILD_ID_SIZE - nhdr->n_descsz);return 0 ; } new_offs = note_offs + sizeof(Elf32_Nhdr) +
Signature · 1 line
2.20.1.97.g81188d93c3-goog
Quoted 13 lines · Stanislav Fomichev
Build-id length is not fixed to 20, it can be (
man ld/--build-id):
* 128-bit (uuid)
* 160-bit (sha1)
* any length specified in ld --build-id=0xhexstringTo fix the issue of missing
BPF_STACK_BUILD_ID_VALIDfor shorter build-ids, assume that build-id is somewhere in the range of 1 .. 20.
Set the remaining bytes to zero.
Quoted 7 lines
v2:
* don't introduce new "len =min(BPF_BUILD_ID_SIZE, nhdr->n_descsz)", we already know thatnhdr->n_descsz<=BPF_BUILD_ID_SIZEif we enter this 'if' conditionFixes 615755a77b24 ("bpf: extend stackmap to save binary_build_id+offset instead of address")Signed-off-by Stanislav Fomichev <[email protected]>
Quoted 28 lines
---
kernel/bpf/stackmap.c| 7 +++++--1 file changed, 5 insertions(+), 2 deletions(-)kernel/bpf/stackmap.c +5
-2@@ -180,11 +180,14 @@ static inline int stack_map_parse_build_id(void *page_addr,if (nhdr->n_type ==BPF_BUILD_ID && nhdr->n_namesz ==sizeof ("GNU" ) && - nhdr->n_descsz ==BPF_BUILD_ID_SIZE ) { + nhdr->n_descsz >0 && + nhdr->n_descsz <=BPF_BUILD_ID_SIZE ) {memcpy (build_id, note_start + note_offs +ALIGN (sizeof ("GNU" ),4 ) +sizeof (Elf32_Nhdr), -BPF_BUILD_ID_SIZE ); + nhdr->n_descsz); +memset (build_id + nhdr->n_descsz,0 , +BPF_BUILD_ID_SIZE - nhdr->n_descsz);return 0 ; } new_offs = note_offs + sizeof(Elf32_Nhdr) +Signature · 1 line
2.20.1.97.g81188d93c3-goog
Daniel Borkmann
maintainerQuoted 9 lines · Stanislav Fomichev
Build-id length is not fixed to 20, it can be (
man ld/--build-id):
* 128-bit (uuid)
* 160-bit (sha1)
* any length specified in ld --build-id=0xhexstringTo fix the issue of missing
BPF_STACK_BUILD_ID_VALIDfor shorter build-ids, assume that build-id is somewhere in the range of 1 .. 20.
Set the remaining bytes to zero.
Quoted 7 lines
v2:
* don't introduce new "len =min(BPF_BUILD_ID_SIZE, nhdr->n_descsz)", we already know thatnhdr->n_descsz<=BPF_BUILD_ID_SIZEif we enter this 'if' conditionFixes 615755a77b24 ("bpf: extend stackmap to save binary_build_id+offset instead of address")Signed-off-by Stanislav Fomichev <[email protected]>
Hmm, looks like rest of the v2 series didn't make it to the list. Please double check; just in case simply resend the full v2 set so it properly ends up in patchwork.
Thanks,
Daniel
Quoted 15 lines · Daniel Borkmann
Quoted 14 lines · Stanislav Fomichev
Build-id length is not fixed to 20, it can be (
man ld/--build-id):
* 128-bit (uuid)
* 160-bit (sha1)
* any length specified in ld --build-id=0xhexstringTo fix the issue of missing
BPF_STACK_BUILD_ID_VALIDfor shorter build-ids, assume that build-id is somewhere in the range of 1 .. 20.
Set the remaining bytes to zero.v2:
* don't introduce new "len =min(BPF_BUILD_ID_SIZE, nhdr->n_descsz)", we already know thatnhdr->n_descsz<=BPF_BUILD_ID_SIZEif we enter this 'if' condition
Quoted 6 lines
Quoted 2 lines
Fixes 615755a77b24 ("bpf: extend stackmap to save binary_build_id+offset instead of address")Signed-off-by Stanislav Fomichev <[email protected]>Hmm, looks like rest of the v2 series didn't make it to the list. Please double check; just in case simply resend the full v2 set so it properly ends up in patchwork.
Oh, I didn't send the first two patches because I didn't change them, I'll resend full v2 series in a moment.
Quoted 3 lines
Thanks,
Daniel