From patchwork Wed Jul 24 22:52:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13741435 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 74A24C3DA61 for ; Wed, 24 Jul 2024 22:52:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 006206B008C; Wed, 24 Jul 2024 18:52:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EEC7E6B00A4; Wed, 24 Jul 2024 18:52:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D901F6B00A5; Wed, 24 Jul 2024 18:52:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id B025E6B00A3 for ; Wed, 24 Jul 2024 18:52:42 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 61D34A4ACA for ; Wed, 24 Jul 2024 22:52:42 +0000 (UTC) X-FDA: 82376147364.18.0DCDE27 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf22.hostedemail.com (Postfix) with ESMTP id 345DBC001F for ; Wed, 24 Jul 2024 22:52:39 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=DQwpSIMi; spf=pass (imf22.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721861513; 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=/p3ZJQXLxqxZi0QU2VNC5Qt4CTeV7snF007YBX98Nf0=; b=lHc2EY+BSz2wlX6DiqZT8ip35kTPaYBMS0enQBenaRBUVTeeBh096jx/kyo0KczOlfhDYT NsLZV1/53UYeC6gjKkEzUzUCJoydbR6Bh1a56qPudI7qpM0Nw3F0xPQrgkiT06hZe9gqVo 0zopRW0HOHRP4NVOSHRV15T2i0rLfxU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721861513; a=rsa-sha256; cv=none; b=phtG6ly8HTvAi8c5Buwz2wSMyKl+8baxEUlj1Qp3wsOUgOEbtkDhQKjKqS6PmfmM7unINz FRimZN8/lAK1LBh5lnTutvnErWDo4/qMi1aaUAspITvZt+SrrllTNXZ/OQ2ysVjNUZ9Upg jSMeMJWV3TmXkeX6MDnBKHY8b1g89Fw= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=DQwpSIMi; spf=pass (imf22.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 7E120CE12C2; Wed, 24 Jul 2024 22:52:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AA922C32781; Wed, 24 Jul 2024 22:52:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721861556; bh=ntgIIpn+tzkBcO08o2+RKeRyEZVireJUIPte90Dh4v8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DQwpSIMimhn7FkBAz2vr8LNKFVUQN83+hiSazx2Qt70eqWzSTOLdzhmnRy2wauKDC XprAOeoDwPz6pMgua9jNTDdtoZJnpt50jO3m0QcetBXcmIA8QAdtdsI5KaOKIjZ1AC w9vkqW0aq5enF+8I1XGYtiyOYNBVO/F+V2o/u7oBAXDrbIay1V+rNz9TI4T8pfamGA 0/u2trA0alBnBrJLjiK+Ydm9nA3nQzoZlHqXaCoZWbNDKkQOkoDFHiL2USGDbGWZRQ 9YorGVDfMlYftfv7v+SXMpkIo61qyCkdZza2jvCfu6uIaes9fzSh9ctcz/3Phbjfze Utnqd2yaZxa9A== 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, Andrii Nakryiko Subject: [PATCH v2 bpf-next 07/10] lib/buildid: harden build ID parsing logic some more Date: Wed, 24 Jul 2024 15:52:07 -0700 Message-ID: <20240724225210.545423-8-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240724225210.545423-1-andrii@kernel.org> References: <20240724225210.545423-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 345DBC001F X-Stat-Signature: 7xsuu1aooghbkizdn9xpua4oy36io8t5 X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1721861559-603710 X-HE-Meta: U2FsdGVkX19a/tv8f7fQLwU6xkhJwapE///CjMOwCxrc47jSteBO6z1gBhDncRId/nPZL9i/xuhPUn3jEkgF3qE7wsjqG5yP+XHYbPVauGHOqe1iU3Op33peGPVILTlBGvfykLNRa0iWgfyLyfBVgz/SAXzhub9qEJPGo4DmfpoRuNQYea3KBCyb+LI36zWgZcQyezqads6alxJMThABRr3LZ1R4oIE6CNzaw1dm7ydZ2nlW3dHdzdYIGsO51S76CFO0O0zNDD41fnt7Kl7h9F7vZt6eSDrW07DxuO8VxCdd6t3ST4yEWhK3Klc19WLXzpF1HdAz6T1spI9Xb29bgco63OT/BPAMRAtrkiNOodL/dman6cRcw72YGJoILOsjLnv8zATzCTdr4Dk7GQ7TLGLdlwBeT3ORX+WRA6QgpLZuR3w7ZI3fG/H2vodjvDXcG09YYZXAtYTflJ0piEsk0joeCANDku20s+q21J9x+nlTesce/nnRK/00CG5GPtCA6bB9E3nj2JkeQB8AlbzmiZ9TDxGVodOTUQyCPYedlIA7rezB+CvIXgAK+G3jBoWLNck1JETGFiOD7I/GPo5VPUGmgQVB6D7+zYfYkKeEDNx1uZ9dFLBZd2dOCB5FIDRo7cavv5dRJB4h81QBFvbj1t335s2dMqNAUjW5QBBZ4g49GzXaclTncxjbSwz3mqJQQr95O9737n6x2cEqXABtsX8haMMISHpFfsSvxrT2Z3EJZhFSo+d1vV57hwDn/fnpaKMAu49V4Exvf1UPA2Vtr9bIdaRNWyNjbWJ0dvwptrMJcK5ZtEmcrzN/olw2pCF7bX6lC7hpoixY6ctTIpRHijY4pDbf3h713tFtaQ01BYP8409IFhH9M6N94a9iKZMcVkf8LMbbPbAnyOKBnGahXIi+oHaLq7PLIgQzfcpmfXajOmVA2cnVOa5Dqetaupt5w5cG/3u2CH2EfDebCHu KrikqGgx /z50MHt0Jmr/FZ4+chWytZGZIXeTvd7ukl4ihyeqoQdVj3nUIwsZbclmHCu3mCEcA576E7B8YLgtNTMIMzQvSy3OetAwpMbA2J7cl2fLQgRZsqgR/wt3Xs+QLz3cs/Q8plL760VTe8oLs/KH1NUcdqG0SI33hv22c2ZOsAiEJ9qqrFgjt39l5TrxwI+6zVs1StpV2IQ5Qw/wOrG/RlFrHRRUHVtG7yHj5N2+2H26wdlUNOxvxVCJ//09fIxCPU9m9egvy 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 some more, adding explicit READ_ONCE() when fetching values that we then use to check correctness and various note iteration invariants. Suggested-by: Andi Kleen Signed-off-by: Andrii Nakryiko --- lib/buildid.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/buildid.c b/lib/buildid.c index 419966d88cd5..7e36a32fbb90 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -158,7 +158,7 @@ static int parse_build_id(struct freader *r, unsigned char *build_id, __u32 *siz const char note_name[] = "GNU"; const size_t note_name_sz = sizeof(note_name); u64 build_id_off, new_offs, note_end = note_offs + note_size; - u32 build_id_sz; + u32 build_id_sz, name_sz, desc_sz; const Elf32_Nhdr *nhdr; const char *data; @@ -171,14 +171,15 @@ static int parse_build_id(struct freader *r, unsigned char *build_id, __u32 *siz if (!nhdr) return r->err; - if (nhdr->n_type == BUILD_ID && - nhdr->n_namesz == note_name_sz && - !strcmp((char *)(nhdr + 1), note_name) && - nhdr->n_descsz > 0 && - nhdr->n_descsz <= BUILD_ID_SIZE_MAX) { + name_sz = READ_ONCE(nhdr->n_namesz); + desc_sz = READ_ONCE(nhdr->n_descsz); + if (READ_ONCE(nhdr->n_type) == BUILD_ID && + name_sz == note_name_sz && + !strncmp((char *)(nhdr + 1), note_name, note_name_sz) && + desc_sz > 0 && desc_sz <= BUILD_ID_SIZE_MAX) { build_id_off = note_offs + sizeof(Elf32_Nhdr) + ALIGN(note_name_sz, 4); - build_id_sz = nhdr->n_descsz; + build_id_sz = desc_sz; /* freader_fetch() will invalidate nhdr pointer */ data = freader_fetch(r, build_id_off, build_id_sz); @@ -192,8 +193,7 @@ static int parse_build_id(struct freader *r, unsigned char *build_id, __u32 *siz 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; @@ -214,7 +214,7 @@ static int get_build_id_32(struct freader *r, unsigned char *build_id, __u32 *si return r->err; /* subsequent freader_fetch() calls invalidate pointers, so remember locally */ - phnum = ehdr->e_phnum; + phnum = READ_ONCE(ehdr->e_phnum); phoff = READ_ONCE(ehdr->e_phoff); /* set upper bound on amount of segments (phdrs) we iterate */ @@ -226,8 +226,9 @@ static int get_build_id_32(struct freader *r, unsigned char *build_id, __u32 *si if (!phdr) return r->err; - if (phdr->p_type == PT_NOTE && - !parse_build_id(r, build_id, size, phdr->p_offset, phdr->p_filesz)) + if (READ_ONCE(phdr->p_type) == PT_NOTE && + !parse_build_id(r, build_id, size, + READ_ONCE(phdr->p_offset), READ_ONCE(phdr->p_filesz))) return 0; } return -EINVAL; @@ -246,7 +247,7 @@ static int get_build_id_64(struct freader *r, unsigned char *build_id, __u32 *si return r->err; /* subsequent freader_fetch() calls invalidate pointers, so remember locally */ - phnum = ehdr->e_phnum; + phnum = READ_ONCE(ehdr->e_phnum); phoff = READ_ONCE(ehdr->e_phoff); /* set upper bound on amount of segments (phdrs) we iterate */ @@ -258,8 +259,9 @@ static int get_build_id_64(struct freader *r, unsigned char *build_id, __u32 *si if (!phdr) return r->err; - if (phdr->p_type == PT_NOTE && - !parse_build_id(r, build_id, size, phdr->p_offset, phdr->p_filesz)) + if (READ_ONCE(phdr->p_type) == PT_NOTE && + !parse_build_id(r, build_id, size, + READ_ONCE(phdr->p_offset), READ_ONCE(phdr->p_filesz))) return 0; }