Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp78856pxf; Tue, 30 Mar 2021 20:07:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxpFXXDebEtxoq6SKdWNOn//qCEi3JaypKnWnPR9zJJUZ5xToHXzo6LGVCmLWHHi5Qkddab X-Received: by 2002:a05:6402:1d19:: with SMTP id dg25mr1073570edb.218.1617160049763; Tue, 30 Mar 2021 20:07:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617160049; cv=none; d=google.com; s=arc-20160816; b=uUnm7sMLl/S5/K+0snbGt69Lbq1c1hWNxFZBCDlHAw5kRE5o/GzpE2plNOnZwOpiZd LDg63JPwn8CLZcEpb7kjw7vdOqTye+aSK7m8fR5ALgtsfLroE5UGpkTjeqCchYjGt/9D MbpHbE075/NppHdPvFi40GRh1jBnaeDxxXrmMvSSOawukxhgKzLppLlq2iafTWJhPq4U VT60V9xUhOCiYb8ux9UF8m6RgLQ3UQ89M8r9LolpOhzv/gYj5JRA24u62pzr3SOwG41t 52WW+PIoNO49ut+kECKMpknXxa1ZVDMfCtPVfRU53HG/1ydG90luj3ldz/6QuZcxxEus HDzA== 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=xY3ms9y+N39U80obnwVhTs0YwJmwUA+v7N5J1rpQp20=; b=TL1s/0mcpTvaMxnHUcWwel5SbqRO0+bwEUbXrnOY6zBko3LA+hYGzANfsFBWKtx7V7 2TNPz/jhK9vJMvjQkX4LpBH0qrpM9SjkObWygt0hz8gYKCYjkwXUwn0GSvRBI5SmcBqF DqFOdBomEBmXp/fgbY291cllnZAZGkm4+LMirFYcss0ijtMaCxQk6pbWBybgupIUZ7z8 wR5t2yEGNuDUrJW72ehTwrVlzS3EYTiUa8NE5rGSNu92Oi92xNrKrKaxWmBpq35/3pcC TG4IAyqCLdrlDuK27PlMIBAWrDtQU+KYf18Ws/efjBM/vX4abEATBJlwjTDFBOZmAVK5 +w4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=lmjOvoqa; 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 fx16si531932ejb.342.2021.03.30.20.07.07; Tue, 30 Mar 2021 20:07:29 -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=lmjOvoqa; 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 S233492AbhCaDFl (ORCPT + 99 others); Tue, 30 Mar 2021 23:05:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233299AbhCaDFY (ORCPT ); Tue, 30 Mar 2021 23:05:24 -0400 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D3DF2C061574 for ; Tue, 30 Mar 2021 20:05:24 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id l123so13513849pfl.8 for ; Tue, 30 Mar 2021 20:05:24 -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=lmjOvoqa3MT6JcIM+QEhEIsHtotCoIPKKexdWYKQqYyx2IGJG163S6/LE4XDAfvg4I E3mtf3yNWOcZhhF+xLfFipTdkXNK741kUDOjODfhLfFzLtgBvJrAfvFwhDck2W6tAhoO jqsnhchpIpCO4t/Ja6EuwJ1giAVGm4a53ngec= 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=VldxB9/f3yX2M+kStRQIvOnFvNvJPt3ch3PGsu5JUB2gX8iC4gr00NaZhIo8/0Ye8L 8+Kas6GccgAJmJOd9hBVS0N3fjMY0KjQNfOAS2MPcxrWtoMOWFiD5b5UMAAD325+0yYL yP0WyUuM3bFohKmbcml68MijTP8sdSob9P7XmTE8IyaGNxftclsiIlkAc1rKBHqJm77j XvWLSx/eKqr5t2OHlFyG/y+j3jU23NFrrmolh4fA/xIzAFZevvFMf1XkX3p3BKGLxR6l pVcCKng9evHmidxcBUza70cNfvGLFMGP6Vb1Zt8Wmkf9YZu3nIfAHoSoUor3G9Fnlf8T 2T8A== X-Gm-Message-State: AOAM530jkbXMjc/oYiU3AmZcmPXgOr2yt75OCXtDw5t4gjXospV/Ym2V EAM8SX4lZtPAbjEJTn/YJ4GoSuZKBHMTiw== X-Received: by 2002:a63:f258:: with SMTP id d24mr1135388pgk.174.1617159924457; Tue, 30 Mar 2021 20:05:24 -0700 (PDT) Received: from smtp.gmail.com ([2620:15c:202:201:c8c2:b814:df0f:253f]) by smtp.gmail.com with ESMTPSA id c6sm389024pfj.99.2021.03.30.20.05.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Mar 2021 20:05:24 -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 v3 01/12] buildid: Add API to parse build ID out of buffer Date: Tue, 30 Mar 2021 20:05:09 -0700 Message-Id: <20210331030520.3816265-2-swboyd@chromium.org> X-Mailer: git-send-email 2.31.0.291.g576ba9dcdaf-goog In-Reply-To: <20210331030520.3816265-1-swboyd@chromium.org> References: <20210331030520.3816265-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