From patchwork Tue Jul 30 20:39:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13747864 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D3792C3DA7F for ; Tue, 30 Jul 2024 20:39:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 698C06B0089; Tue, 30 Jul 2024 16:39:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 648B96B008A; Tue, 30 Jul 2024 16:39:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5101D6B008C; Tue, 30 Jul 2024 16:39:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 304E76B0089 for ; Tue, 30 Jul 2024 16:39:28 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id EB519A4932 for ; Tue, 30 Jul 2024 20:39:27 +0000 (UTC) X-FDA: 82397584374.04.83B781A Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf24.hostedemail.com (Postfix) with ESMTP id 835A118004F for ; Tue, 30 Jul 2024 20:39:25 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ZYVZiwdu; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf24.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722371962; a=rsa-sha256; cv=none; b=xNkArd+Klk5PEsriurz9YyOOVB7Nt5bk/OeiIi4mfjZBZjf0zAteNljwTMgA8YSUnDZjYS rEdV31IjxVEi5fvqU9z2uF0Fnx8OW6O/tC4JmyKjdRJf5aG3wG0tS6ha+VnxMptapwSgeK jQl+bfQm0ifQe2aFKd61ka0X3LuSMBU= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ZYVZiwdu; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf24.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722371962; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=1nuYuZZfctNbf2pcIDlRhKEUbdpWkkw3ZjiuZl5LjjM=; b=7vrZ59lihu3xJp8Ji7B1aSKw4Tbb+6J7DCvvxhofwenoGAY90Wm9j7IKrOYhPpiMa185AP LQ/D5LJ5NBnO65E3V4qgS8fLfP34qrCfwfNTKMXEmX6e55ErRD1B9IwWuyM+nHhyRqZwln QKsoYr8Wzym0ocBvB3o4VC44csTgniI= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id A9008CE1197; Tue, 30 Jul 2024 20:39:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C6F30C32782; Tue, 30 Jul 2024 20:39:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722371961; bh=ZRyyWIOIJMw57Vd+3odqRxOMUMVme1VBHFLkZo7b9vc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZYVZiwdu9FZhd7Cszk73xFZXTFp9z7q0D9Jb4gM2liZG8RTSeaBKLF/JfdS4rmEuO lJ/oObjBX08TbU4zWqTbW1L+Ux1FYemOFWSHWw6ai8DvlXdvUXW10wbDb/BsHh0+V/ sM2vFuK77AWUPzJmjeSS2KWIweCAIAPLkCIt7Xd9+xJ0mm+9yvJw66CgP66mQoSXcr WTJXklXl5GOBqey2p5ly2euAdlH3l72owAH7qaJJ3rFBOv3h2pgnWTA14wOi8logW9 2tnC5iWbCY3KwPibPVhI/3dNm81qxKntuy6CxlZ6XbBpTWBYI8FJx86gMqNYc9hQTy CfCA6W8i3poTw== From: Andrii Nakryiko To: bpf@vger.kernel.org Cc: 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, Andrii Nakryiko , stable@vger.kernel.org Subject: [PATCH v3 bpf-next 01/10] lib/buildid: harden build ID parsing logic Date: Tue, 30 Jul 2024 13:39:05 -0700 Message-ID: <20240730203914.1182569-2-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240730203914.1182569-1-andrii@kernel.org> References: <20240730203914.1182569-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 835A118004F X-Rspamd-Server: rspam01 X-Stat-Signature: d746m5k5in6sf59nu5amx49xwc8rtkqw X-HE-Tag: 1722371965-419674 X-HE-Meta: U2FsdGVkX19WKjvgkYQRmyZ8VakCdSNEmJ6WR32IbXDEQ0QWqO4EyqSTjFcOZAYO+RgfVpZjGjIGPdaQ9mudv+5kIoYz02yQ4nAE5KMuDCqi5dttfw61BfpR/naiaec0JNVOichgyicgr9rLGQum+KSvMAKtdku5mGQ2rN9tOS/YajRrVZp5u5tMshks4PBl1FXuq6MLzMupfpZQDt7+bQ6XLa9Sg9UglN+pSfuII8YWfus5sqf3A5aEdKIOY2PGJe5e1qJIPpfBqKSJldfl8KxfJu9N1CZbjmWUh2hkqglazqrHOvtMWxssqaP8aFcE33N2kpJME2Iig/HBzF2GhdIkR82e6Z5mDXH2tz8tZbKrzmJBhWkCUIGEtL1iO/cjLDdevZiacO6MB9csTAVcc+Pmhp/stmhGxAh/7UIS40Czv3HkQoNIzTO6DGTCZpeUSStKEglo67dnHkioASykJfsBSWBGVXHTH/7QPBhfbimeaeSMb6fOgd3M+87WrVAFau1IPBNWdWzo1vKNMHRkXVYqD6dpacNIM0ZNmslVpQXoIgMxlt6hvcFKd1tYlPQegU1ZUdA82XzpZDs+j00G5PZzKcoM0PZEwzFwUcG0hhzFryIuTdubbPDdcvR1G+qyburN++aeGwAl3KUVPj/iJHY7Kg9BbBmF4mCpKcklIYkgWntowhFpZUpVFAiKYpRxL4dwjGixnpf/PwETtGUtJ/h6kVf8JnOvAkmkeIDqsLiq8hnA9ZVdlqNFDi4ZIjR7XGuzRPZstcr9TZmaMf28FZ4dJk5gRBDqH7FhFwLqMHoMk6iZNqg8NROFzxM2pQwxsnpH3J5HRvqCKlhIrJPUfTQzGkkjWydF/IeVSJmw4n0RsLolos+jlIS6zV4876KEA81bqaSQsY4zjbhmC23rjNH0b8ezRQtn7nDYKIIaBG8TjBXHm3xa10KfjNWavZxvaChT0bnAJxZwJTiV16W 3RAjysEj trovksBtNMgVh6DXgOhfhxe1Y2/G9oqfJY1zBnWYoQu8HASaG+69rihuJYF29PuGvzRTnDsRFjxEhGlvuvy6zGeYEeTxJWT1XTkDeatXi2EuubKWai3mMsGM8jQbMY5TNhIhGzm5vX8hglmBCWjgM++/SYbX4Io6r+UjefC2uXbeoqqJxJSYN3zIAyHEMJibzj1v3qCSFQcu3eZGz047auVIE+wnw/1TzxRDLFIoh4avYXq0gjJwuX6+7pKrYXtgs/544+Q3k6+muljzBNqiaWMETbw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Harden build ID parsing logic, adding explicit READ_ONCE() where it's important to have a consistent value read and validated just once. 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 Cc: Jann Horn Suggested-by: Andi Kleen Fixes: bd7525dacd7e ("bpf: Move stack_map_get_build_id into lib") Signed-off-by: Andrii Nakryiko --- lib/buildid.c | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/lib/buildid.c b/lib/buildid.c index e02b5507418b..d21d86f6c19a 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -18,28 +18,29 @@ static int parse_build_id_buf(unsigned char *build_id, const void *note_start, Elf32_Word note_size) { + const char note_name[] = "GNU"; + const size_t note_name_sz = sizeof(note_name); Elf32_Word note_offs = 0, new_offs; + u32 name_sz, desc_sz; + const char *data; while (note_offs + sizeof(Elf32_Nhdr) < note_size) { Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_offs); + name_sz = READ_ONCE(nhdr->n_namesz); + desc_sz = READ_ONCE(nhdr->n_descsz); 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 && + strcmp((char *)(nhdr + 1), note_name) == 0 && + desc_sz > 0 && desc_sz <= BUILD_ID_SIZE_MAX) { + data = note_start + note_offs + 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; } - new_offs = note_offs + sizeof(Elf32_Nhdr) + - ALIGN(nhdr->n_namesz, 4) + ALIGN(nhdr->n_descsz, 4); + new_offs = note_offs + sizeof(Elf32_Nhdr) + ALIGN(name_sz, 4) + ALIGN(desc_sz, 4); if (new_offs <= note_offs) /* overflow */ break; note_offs = new_offs; @@ -71,7 +72,7 @@ static int get_build_id_32(const void *page_addr, unsigned char *build_id, { Elf32_Ehdr *ehdr = (Elf32_Ehdr *)page_addr; Elf32_Phdr *phdr; - int i; + __u32 i, phnum; /* * FIXME @@ -80,9 +81,10 @@ static int get_build_id_32(const void *page_addr, unsigned char *build_id, */ if (ehdr->e_phoff != sizeof(Elf32_Ehdr)) return -EINVAL; + + phnum = READ_ONCE(ehdr->e_phnum); /* only supports phdr that fits in one page */ - if (ehdr->e_phnum > - (PAGE_SIZE - sizeof(Elf32_Ehdr)) / sizeof(Elf32_Phdr)) + if (phnum > (PAGE_SIZE - sizeof(Elf32_Ehdr)) / sizeof(Elf32_Phdr)) return -EINVAL; phdr = (Elf32_Phdr *)(page_addr + sizeof(Elf32_Ehdr)); @@ -90,8 +92,8 @@ static int get_build_id_32(const void *page_addr, unsigned char *build_id, for (i = 0; i < ehdr->e_phnum; ++i) { if (phdr[i].p_type == PT_NOTE && !parse_build_id(page_addr, build_id, size, - page_addr + phdr[i].p_offset, - phdr[i].p_filesz)) + page_addr + READ_ONCE(phdr[i].p_offset), + READ_ONCE(phdr[i].p_filesz))) return 0; } return -EINVAL; @@ -103,7 +105,7 @@ static int get_build_id_64(const void *page_addr, unsigned char *build_id, { Elf64_Ehdr *ehdr = (Elf64_Ehdr *)page_addr; Elf64_Phdr *phdr; - int i; + __u32 i, phnum; /* * FIXME @@ -112,18 +114,19 @@ static int get_build_id_64(const void *page_addr, unsigned char *build_id, */ if (ehdr->e_phoff != sizeof(Elf64_Ehdr)) return -EINVAL; + + phnum = READ_ONCE(ehdr->e_phnum); /* only supports phdr that fits in one page */ - if (ehdr->e_phnum > - (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr)) + if (phnum > (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr)) return -EINVAL; phdr = (Elf64_Phdr *)(page_addr + sizeof(Elf64_Ehdr)); - for (i = 0; i < ehdr->e_phnum; ++i) { + for (i = 0; i < phnum; ++i) { if (phdr[i].p_type == PT_NOTE && !parse_build_id(page_addr, build_id, size, - page_addr + phdr[i].p_offset, - phdr[i].p_filesz)) + page_addr + READ_ONCE(phdr[i].p_offset), + READ_ONCE(phdr[i].p_filesz))) return 0; } return -EINVAL;