Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp139568pxy; Tue, 20 Apr 2021 14:51:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxZFsFCDG2LqO2XBQZ86/MkfDtciInChSEouNHtA0f5pavbERtGDplM/6xy0c22+vMq5G9T X-Received: by 2002:aa7:d9d9:: with SMTP id v25mr21737643eds.83.1618955496008; Tue, 20 Apr 2021 14:51:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618955496; cv=none; d=google.com; s=arc-20160816; b=v0czK5+00xD8PMo0/MddM2TE2pMwj0ruYTDgbRq1qAzwlfk+WFmtv7vepENY6aW9MX mhoJ2/KKRDZ1SjDGik06QiQX2s6ji4ozLKANAl2k9inzg5vObcRqJYB7j3bfLIH594lw zVsbzPg9vvtybmO69P9Kp6ZIvy6scfcANuNoV9TW3lhKt1IDz2v1eNvhjOx8i8PE8mVt cKTTRFzS2S2jvvS6ZWCLiEmkW/Lj/HyCJn3iyC6En2mYR89teEcI3hJqtJxEiChyhbZu +hmMMsaMo8sEq9vPzspdPHg6ZvamP+5SVAXz8x8g7wxDdcuPue6Ir3VOy/M/DV80YteC 3ocw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ZbspQKLdATEoVgdZs/IrxjMWjbvi9Biurj+hlH6bsBM=; b=hEBYuKy14jDIC6md5Jkoc7tmt9mAEBm5kJfRlenppEUpMdCa1ZTWnBkF0sm8X2ZcwR ooMkTUIs93O9OJin3Y+74Z1JTeyfWi/UFH1Ki9hBNLzL3g3LRVPsnsDEgMuTOOzChYTW oxc/URr39q+aP7/Ht/lW55HOrA0G6hcRvAgSbSJY1au6KEBDq3eYbqNZBG+XdoZUBLN4 J3AK7Hyfuo+PQco3L2Ge7Q8KUYtD/LS1oMEkr7Cev5kvt/AvgyKPjg0r2MIxxbTL81aD XyPpkjWXBNMwdY13VSx5lxv2SLpeqd8MigHLPa7k2Cuail6U07mbsvc6Wgb9ADGWJ+A4 OY/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=liiXtsG9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c7si48140ejx.166.2021.04.20.14.51.11; Tue, 20 Apr 2021 14:51:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=liiXtsG9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234225AbhDTVuq (ORCPT + 99 others); Tue, 20 Apr 2021 17:50:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234187AbhDTVuj (ORCPT ); Tue, 20 Apr 2021 17:50:39 -0400 Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BAAB6C06174A for ; Tue, 20 Apr 2021 14:50:06 -0700 (PDT) Received: by mail-pl1-x635.google.com with SMTP id g16so3578229plq.3 for ; Tue, 20 Apr 2021 14:50:06 -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=ZbspQKLdATEoVgdZs/IrxjMWjbvi9Biurj+hlH6bsBM=; b=liiXtsG9lsia9ZJtipouuR4tCJuKg+BF2IFmXQ+qus2I+gUJdi4kjcFZXWclCbnuAm QE1UdHbDnlPCdlidqTfSIiVs7WnInM0img+HRNSzfWOJUHHGAwFNmQ1YD2qXPpr67x6I cS+P3jomrpnXqqm+oTmftnfqKtrFIKnPKl4zo= 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=ZbspQKLdATEoVgdZs/IrxjMWjbvi9Biurj+hlH6bsBM=; b=cU78tc+JjklrNxxkkiLjtyL/z1xk7pcukVIxYSsSBzUcXUiudP+rd6BOTGInv8HTDm ZO3eiROK5MvVgZeR3M+Be9fGOLvmx42glczbxXPWhLsJ0MwOUfeLLP9WFLe3Oj5+RrrL YJ4ulYRGfvBR1j6s7YJkolqM8HN6nJ5tIkc/E97AdPpdcv9zj0SHoOgaGoZfjt/mpvSN QP+pm9kUhFk3AMM8rEIt/s0by+Qf3iJM34AlhGIu8NGHxBjdfGQhI4pI6AS4b0UAi48a xHcmptJCtaI6x96KHEUbnMSErcKXQL8HJ+W5urzEVMC2s304x0yM2476LGEqYz2CkZzD 7nzQ== X-Gm-Message-State: AOAM531IKcu3VPEgGTr7g3RtyEm0B9qdwewCZrZjy+pL+lSFCRXyEPeP 2Zi1CPmWFpN6y377jueSBIb8uw== X-Received: by 2002:a17:902:7788:b029:e9:11:5334 with SMTP id o8-20020a1709027788b02900e900115334mr30799987pll.70.1618955406371; Tue, 20 Apr 2021 14:50:06 -0700 (PDT) Received: from smtp.gmail.com ([2620:15c:202:201:bf8b:4b8b:2315:3719]) by smtp.gmail.com with ESMTPSA id c21sm31481pfo.91.2021.04.20.14.50.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Apr 2021 14:50:06 -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 v5 02/13] buildid: Add API to parse build ID out of buffer Date: Tue, 20 Apr 2021 14:49:52 -0700 Message-Id: <20210420215003.3510247-3-swboyd@chromium.org> X-Mailer: git-send-email 2.31.1.368.gbe11c130af-goog In-Reply-To: <20210420215003.3510247-1-swboyd@chromium.org> References: <20210420215003.3510247-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 e014636ec3eb..6aea1c4e5e85 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); @@ -50,9 +43,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) @@ -148,3 +159,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