From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 45EE9C433E0 for ; Wed, 24 Mar 2021 02:05:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 13AA061A07 for ; Wed, 24 Mar 2021 02:05:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234737AbhCXCFE (ORCPT ); Tue, 23 Mar 2021 22:05:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58190 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234727AbhCXCEs (ORCPT ); Tue, 23 Mar 2021 22:04:48 -0400 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C2C5C0613D8 for ; Tue, 23 Mar 2021 19:04:48 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id e14so7100033plj.2 for ; Tue, 23 Mar 2021 19:04:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xY3ms9y+N39U80obnwVhTs0YwJmwUA+v7N5J1rpQp20=; b=knGpVpXleUibqy4hwhI57hS8MPFATE4kjo8jhzaOPPuPirCzEuzRVam4bUpTNEIFEE a0rjc8P6K++2N1jWAk1LO/uj9M4AWEc/zJpTnJ/+g0kYhDuBv7rnsQJmRYJtx6g1aKwF qUBsg10q06i7D/S8irKuJOTCMxxRIZua7T+HE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xY3ms9y+N39U80obnwVhTs0YwJmwUA+v7N5J1rpQp20=; b=Q60GLf1FXFKSYy/GLZAaHiBJ/0sKdNmHX3W6hfMC5nI8lkmo+eDDYhZSb4FZ4sh1db LgcnEcLooJAtK8+58ySBPU3Io4Sgp+tkzTWNlMpdSnmOE8UmiOw4NWweucAQuD+e2FJb tJSM+8tZGjnDGtBNfLJTqahRU1ieoUcnyf5PUpDWvVA44q/Ixif6oIvnh+jlECbQ5o6g E6m83KEqmZcu0u7595Et8rgdSGvClLZJbxC6zbKyt/tla52DZCi13r5VJvVzUUtIuDiE DhOFsS3Uq1Kq2ejIRMeiE1h+L9qLdiEZP7tGdCHwoJGKCSXa277ctDNDZaE698gFlEQF nOww== X-Gm-Message-State: AOAM533wOBd0T8cw2GuPtQ6YwgmSOKyfenv0Qw1gkCNgdZ8VhgrExrMU QL3r6SX9KDyXJmKArbUVp4azdw== X-Google-Smtp-Source: ABdhPJyEoUzZpcJ9ymHyud/OqsBa7tkSyyRPVpKd9ptDiYN6Y82huNe1EuGU673I4L52itVqlIN5YQ== X-Received: by 2002:a17:902:d706:b029:e6:90aa:24e0 with SMTP id w6-20020a170902d706b02900e690aa24e0mr1248989ply.42.1616551487637; Tue, 23 Mar 2021 19:04:47 -0700 (PDT) Received: from smtp.gmail.com ([2620:15c:202:201:84ac:62f7:16a8:ccc7]) by smtp.gmail.com with ESMTPSA id b3sm389441pjg.41.2021.03.23.19.04.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Mar 2021 19:04:47 -0700 (PDT) From: Stephen Boyd To: Andrew Morton Cc: linux-kernel@vger.kernel.org, Jiri Olsa , Alexei Starovoitov , Jessica Yu , Evan Green , Hsin-Yi Wang Subject: [PATCH v2 01/12] buildid: Add API to parse build ID out of buffer Date: Tue, 23 Mar 2021 19:04:32 -0700 Message-Id: <20210324020443.1815557-2-swboyd@chromium.org> X-Mailer: git-send-email 2.31.0.291.g576ba9dcdaf-goog In-Reply-To: <20210324020443.1815557-1-swboyd@chromium.org> References: <20210324020443.1815557-1-swboyd@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add an API that can parse the build ID out of a buffer, instead of a vma, to support printing a kernel module's build ID for stack traces. Cc: Jiri Olsa Cc: Alexei Starovoitov Cc: Jessica Yu Cc: Evan Green Cc: Hsin-Yi Wang Signed-off-by: Stephen Boyd --- include/linux/buildid.h | 1 + lib/buildid.c | 50 ++++++++++++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/include/linux/buildid.h b/include/linux/buildid.h index 40232f90db6e..ebce93f26d06 100644 --- a/include/linux/buildid.h +++ b/include/linux/buildid.h @@ -8,5 +8,6 @@ int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id, __u32 *size); +int build_id_parse_buf(const void *buf, unsigned char *build_id, u32 buf_size); #endif diff --git a/lib/buildid.c b/lib/buildid.c index 6156997c3895..010ab0674cb9 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -2,30 +2,23 @@ #include #include +#include #include #define BUILD_ID 3 + /* * Parse build id from the note segment. This logic can be shared between * 32-bit and 64-bit system, because Elf32_Nhdr and Elf64_Nhdr are * identical. */ -static inline int parse_build_id(void *page_addr, - unsigned char *build_id, - __u32 *size, - void *note_start, - Elf32_Word note_size) +static int parse_build_id_buf(unsigned char *build_id, + __u32 *size, + const void *note_start, + Elf32_Word note_size) { Elf32_Word note_offs = 0, new_offs; - /* check for overflow */ - if (note_start < page_addr || note_start + note_size < note_start) - return -EINVAL; - - /* only supports note that fits in the first page */ - if (note_start + note_size > page_addr + PAGE_SIZE) - return -EINVAL; - while (note_offs + sizeof(Elf32_Nhdr) < note_size) { Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_offs); @@ -49,9 +42,27 @@ static inline int parse_build_id(void *page_addr, break; note_offs = new_offs; } + return -EINVAL; } +static inline int parse_build_id(void *page_addr, + unsigned char *build_id, + __u32 *size, + void *note_start, + Elf32_Word note_size) +{ + /* check for overflow */ + if (note_start < page_addr || note_start + note_size < note_start) + return -EINVAL; + + /* only supports note that fits in the first page */ + if (note_start + note_size > page_addr + PAGE_SIZE) + return -EINVAL; + + return parse_build_id_buf(build_id, size, note_start, note_size); +} + /* Parse build ID from 32-bit ELF */ static int get_build_id_32(void *page_addr, unsigned char *build_id, __u32 *size) @@ -147,3 +158,16 @@ int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id, put_page(page); return ret; } + +/** + * build_id_parse_buf - Get build ID from a buffer + * @buf: Elf note section(s) to parse + * @buf_size: Size of @buf in bytes + * @build_id: Build ID parsed from @buf, at least BUILD_ID_SIZE_MAX long + * + * Return: 0 on success, -EINVAL otherwise + */ +int build_id_parse_buf(const void *buf, unsigned char *build_id, u32 buf_size) +{ + return parse_build_id_buf(build_id, NULL, buf, buf_size); +} -- https://chromeos.dev