From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Jiri Olsa <olsajiri@gmail.com>
Cc: Andrii Nakryiko <andrii@kernel.org>,
bpf@vger.kernel.org, linux-mm@kvack.org,
akpm@linux-foundation.org, adobriyan@gmail.com,
shakeel.butt@linux.dev, hannes@cmpxchg.org, ak@linux.intel.com,
osandov@osandov.com, song@kernel.org, jannh@google.com,
linux-fsdevel@vger.kernel.org, willy@infradead.org,
stable@vger.kernel.org, Eduard Zingerman <eddyz87@gmail.com>
Subject: Re: [PATCH v7 bpf-next 01/10] lib/buildid: harden build ID parsing logic
Date: Fri, 1 Nov 2024 11:12:55 -0700 [thread overview]
Message-ID: <CAEf4BzaFd2G0HqXLSd5JbQ4HYwzTzAsAskQJNbE9hb8KuTEWTg@mail.gmail.com> (raw)
In-Reply-To: <ZyTde66MF0GUqbvB@krava>
On Fri, Nov 1, 2024 at 6:54 AM Jiri Olsa <olsajiri@gmail.com> wrote:
>
> On Thu, Aug 29, 2024 at 10:42:23AM -0700, Andrii Nakryiko wrote:
> > Harden build ID parsing logic, adding explicit READ_ONCE() where it's
> > important to have a consistent value read and validated just once.
> >
> > Also, as pointed out by Andi Kleen, we need to make sure that entire ELF
> > note is within a page bounds, so move the overflow check up and add an
> > extra note_size boundaries validation.
> >
> > Fixes tag below points to the code that moved this code into
> > lib/buildid.c, and then subsequently was used in perf subsystem, making
> > this code exposed to perf_event_open() users in v5.12+.
> >
> > Cc: stable@vger.kernel.org
> > Reviewed-by: Eduard Zingerman <eddyz87@gmail.com>
> > Reviewed-by: Jann Horn <jannh@google.com>
> > Suggested-by: Andi Kleen <ak@linux.intel.com>
> > Fixes: bd7525dacd7e ("bpf: Move stack_map_get_build_id into lib")
> > Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
> > ---
> > lib/buildid.c | 76 +++++++++++++++++++++++++++++----------------------
> > 1 file changed, 44 insertions(+), 32 deletions(-)
> >
> > diff --git a/lib/buildid.c b/lib/buildid.c
> > index e02b5507418b..26007cc99a38 100644
> > --- a/lib/buildid.c
> > +++ b/lib/buildid.c
> > @@ -18,31 +18,37 @@ static int parse_build_id_buf(unsigned char *build_id,
> > const void *note_start,
> > Elf32_Word note_size)
> > {
> > - Elf32_Word note_offs = 0, new_offs;
> > -
> > - while (note_offs + sizeof(Elf32_Nhdr) < note_size) {
> > - Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_offs);
> > + const char note_name[] = "GNU";
> > + const size_t note_name_sz = sizeof(note_name);
> > + u64 note_off = 0, new_off, name_sz, desc_sz;
> > + const char *data;
> > +
> > + while (note_off + sizeof(Elf32_Nhdr) < note_size &&
> > + note_off + sizeof(Elf32_Nhdr) > note_off /* overflow */) {
> > + Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_off);
> > +
> > + name_sz = READ_ONCE(nhdr->n_namesz);
> > + desc_sz = READ_ONCE(nhdr->n_descsz);
> > +
> > + new_off = note_off + sizeof(Elf32_Nhdr);
> > + if (check_add_overflow(new_off, ALIGN(name_sz, 4), &new_off) ||
> > + check_add_overflow(new_off, ALIGN(desc_sz, 4), &new_off) ||
> > + new_off > note_size)
> > + break;
> >
> > if (nhdr->n_type == BUILD_ID &&
> > - nhdr->n_namesz == sizeof("GNU") &&
> > - !strcmp((char *)(nhdr + 1), "GNU") &&
> > - nhdr->n_descsz > 0 &&
> > - nhdr->n_descsz <= BUILD_ID_SIZE_MAX) {
> > - memcpy(build_id,
> > - note_start + note_offs +
> > - ALIGN(sizeof("GNU"), 4) + sizeof(Elf32_Nhdr),
> > - nhdr->n_descsz);
> > - memset(build_id + nhdr->n_descsz, 0,
> > - BUILD_ID_SIZE_MAX - nhdr->n_descsz);
> > + name_sz == note_name_sz &&
> > + memcmp(nhdr + 1, note_name, note_name_sz) == 0 &&
> > + desc_sz > 0 && desc_sz <= BUILD_ID_SIZE_MAX) {
> > + data = note_start + note_off + ALIGN(note_name_sz, 4);
> > + memcpy(build_id, data, desc_sz);
> > + memset(build_id + desc_sz, 0, BUILD_ID_SIZE_MAX - desc_sz);
> > if (size)
> > - *size = nhdr->n_descsz;
> > + *size = desc_sz;
> > return 0;
> > }
>
> hi,
> this fix is causing stable kernels to return wrong build id,
> the change below seems to fix that (based on 6.6 stable)
>
> if we agree on the fix I'll send it to all affected stable trees
>
> jirka
>
>
> ---
> The parse_build_id_buf does not account Elf32_Nhdr header size
> when getting the build id data pointer and returns wrong build
> id data as result.
>
> This is problem only stable trees that merged c83a80d8b84f fix,
> the upstream build id code was refactored and returns proper
> build id.
>
> Fixes: c83a80d8b84f ("lib/buildid: harden build ID parsing logic")
> Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> ---
> lib/buildid.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/lib/buildid.c b/lib/buildid.c
> index d3bc3d0528d5..9fc46366597e 100644
> --- a/lib/buildid.c
> +++ b/lib/buildid.c
> @@ -40,7 +40,7 @@ static int parse_build_id_buf(unsigned char *build_id,
> name_sz == note_name_sz &&
> memcmp(nhdr + 1, note_name, note_name_sz) == 0 &&
> desc_sz > 0 && desc_sz <= BUILD_ID_SIZE_MAX) {
> - data = note_start + note_off + ALIGN(note_name_sz, 4);
> + data = note_start + note_off + sizeof(Elf32_Nhdr) + ALIGN(note_name_sz, 4);
ah, my screw up, sorry. LGTM
Acked-by: Andrii Nakryiko <andrii@kernel.org>
> memcpy(build_id, data, desc_sz);
> memset(build_id + desc_sz, 0, BUILD_ID_SIZE_MAX - desc_sz);
> if (size)
> --
> 2.47.0
>
next prev parent reply other threads:[~2024-11-01 18:13 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-29 17:42 [PATCH v7 bpf-next 00/10] Harden and extend ELF " Andrii Nakryiko
2024-08-29 17:42 ` [PATCH v7 bpf-next 01/10] lib/buildid: harden " Andrii Nakryiko
2024-11-01 13:54 ` Jiri Olsa
2024-11-01 18:12 ` Andrii Nakryiko [this message]
2024-08-29 17:42 ` [PATCH v7 bpf-next 02/10] lib/buildid: add single folio-based file reader abstraction Andrii Nakryiko
2024-08-29 17:42 ` [PATCH v7 bpf-next 03/10] lib/buildid: take into account e_phoff when fetching program headers Andrii Nakryiko
2024-08-29 17:42 ` [PATCH v7 bpf-next 04/10] lib/buildid: remove single-page limit for PHDR search Andrii Nakryiko
2024-08-29 17:42 ` [PATCH v7 bpf-next 05/10] lib/buildid: rename build_id_parse() into build_id_parse_nofault() Andrii Nakryiko
2024-10-14 2:50 ` Lai, Yi
2024-10-14 23:41 ` Andrii Nakryiko
2024-08-29 17:42 ` [PATCH v7 bpf-next 06/10] lib/buildid: implement sleepable build_id_parse() API Andrii Nakryiko
2024-08-29 17:42 ` [PATCH v7 bpf-next 07/10] lib/buildid: don't limit .note.gnu.build-id to the first page in ELF Andrii Nakryiko
2024-08-29 17:42 ` [PATCH v7 bpf-next 08/10] bpf: decouple stack_map_get_build_id_offset() from perf_callchain_entry Andrii Nakryiko
2024-08-29 17:42 ` [PATCH v7 bpf-next 09/10] bpf: wire up sleepable bpf_get_stack() and bpf_get_task_stack() helpers Andrii Nakryiko
2024-11-11 5:51 ` Sergey Senozhatsky
2024-11-11 17:49 ` Andrii Nakryiko
2024-11-12 1:29 ` Sergey Senozhatsky
2024-11-13 20:40 ` Andrii Nakryiko
2024-08-29 17:42 ` [PATCH v7 bpf-next 10/10] selftests/bpf: add build ID tests Andrii Nakryiko
2024-09-03 22:38 ` [PATCH v7 bpf-next 00/10] Harden and extend ELF build ID parsing logic Andrii Nakryiko
2024-09-11 0:27 ` Alexei Starovoitov
2024-09-11 17:10 ` patchwork-bot+netdevbpf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAEf4BzaFd2G0HqXLSd5JbQ4HYwzTzAsAskQJNbE9hb8KuTEWTg@mail.gmail.com \
--to=andrii.nakryiko@gmail.com \
--cc=adobriyan@gmail.com \
--cc=ak@linux.intel.com \
--cc=akpm@linux-foundation.org \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=eddyz87@gmail.com \
--cc=hannes@cmpxchg.org \
--cc=jannh@google.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=olsajiri@gmail.com \
--cc=osandov@osandov.com \
--cc=shakeel.butt@linux.dev \
--cc=song@kernel.org \
--cc=stable@vger.kernel.org \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox