From nobody Fri May 8 09:11:03 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C8575C433EF for ; Fri, 6 May 2022 20:35:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443197AbiEFUjU (ORCPT ); Fri, 6 May 2022 16:39:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1443204AbiEFUjL (ORCPT ); Fri, 6 May 2022 16:39:11 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8602BEB3; Fri, 6 May 2022 13:35:25 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 2054460B07; Fri, 6 May 2022 20:35:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CDD7CC385A8; Fri, 6 May 2022 20:35:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1651869324; bh=65ko2VgV4hklZgR4OxvVNIdExSe/uakUcSdJsONNAww=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WbdUXGkAtWpHCKf0pJ9f44irXNKgkwGL/vSSu7EhqpaEr1EAZSqdwDUOvtagzrM6j fJJIHezSbfv0TnxsDShnCwU/nLIfU50bTnQh3R48nX0NnGrQ2dQCBLupkySMeGyAY2 3pdPAdU8UV40zh16yPZWhkqBLu8vTThFHLq0thAKqynF5C7AiE06bP6NPPVMCWMByF /zHYTxJaGu4hXdzqQo/zJnrBz5VFR8/HejGH8gcmqFzYmMvZNBvqufADUb+A/YbIYo 1P9s9NrVKIDSYrKL2pLlvz3eAwYuSnAbfkXKItYQMPyA+6iXkWl602Eupk19DPG1Dm dUFxCqft/b1Dg== From: Miguel Ojeda To: Boqun Feng , Miguel Ojeda , Kees Cook , Alex Gaynor , Wedson Almeida Filho , Changbin Du , linux-kernel@vger.kernel.org Cc: rust-for-linux@vger.kernel.org Subject: [PATCH v2 1/3] kallsyms: avoid hardcoding the buffer size Date: Fri, 6 May 2022 22:34:22 +0200 Message-Id: <20220506203443.24721-2-ojeda@kernel.org> In-Reply-To: <20220506203443.24721-1-ojeda@kernel.org> References: <20220506203443.24721-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Boqun Feng This makes it easier to update the size later on. Furthermore, a static assert is added to ensure both are updated when that happens. The relationship used is one that keeps the new size (512+1) close to the original buffer size (500). Reviewed-by: Kees Cook Signed-off-by: Boqun Feng Co-developed-by: Miguel Ojeda Signed-off-by: Miguel Ojeda --- v2: - Collected Reviewed-by. - Use `sizeof(name)` in one instance, suggested by Kees Cook. scripts/kallsyms.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 8caabddf817c..82d6508bdf29 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -27,8 +27,18 @@ =20 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) =20 +#define _stringify_1(x) #x +#define _stringify(x) _stringify_1(x) + #define KSYM_NAME_LEN 128 =20 +/* A substantially bigger size than the current maximum. */ +#define KSYM_NAME_LEN_BUFFER 512 +_Static_assert( + KSYM_NAME_LEN_BUFFER =3D=3D KSYM_NAME_LEN * 4, + "Please keep KSYM_NAME_LEN_BUFFER in sync with KSYM_NAME_LEN" +); + struct sym_entry { unsigned long long addr; unsigned int len; @@ -197,15 +207,15 @@ static void check_symbol_range(const char *sym, unsig= ned long long addr, =20 static struct sym_entry *read_symbol(FILE *in) { - char name[500], type; + char name[KSYM_NAME_LEN_BUFFER+1], type; unsigned long long addr; unsigned int len; struct sym_entry *sym; int rc; =20 - rc =3D fscanf(in, "%llx %c %499s\n", &addr, &type, name); + rc =3D fscanf(in, "%llx %c %" _stringify(KSYM_NAME_LEN_BUFFER) "s\n", &ad= dr, &type, name); if (rc !=3D 3) { - if (rc !=3D EOF && fgets(name, 500, in) =3D=3D NULL) + if (rc !=3D EOF && fgets(name, sizeof(name), in) =3D=3D NULL) fprintf(stderr, "Read error or end of file.\n"); return NULL; } --=20 2.35.3 From nobody Fri May 8 09:11:03 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E41A0C433F5 for ; Fri, 6 May 2022 20:35:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443309AbiEFUjg (ORCPT ); Fri, 6 May 2022 16:39:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38050 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1443176AbiEFUjU (ORCPT ); Fri, 6 May 2022 16:39:20 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B39FB12AE8; Fri, 6 May 2022 13:35:35 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 3CF8DB83993; Fri, 6 May 2022 20:35:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 81EE3C385A8; Fri, 6 May 2022 20:35:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1651869333; bh=BfaelvsE9otW4HAecF8ygPRcE3Y5bqRUzQDPTs9Sn3c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IejeAEzxtxG1qUtJMS/r4CKjWA25UAMFuSezsgmIAJsnXjXdznpcJPUfnww3Gv4bz XTyE7VLmyEu4lTmdCnj1hncLoQU74t/jrPBk4vJ1xfYzRBY/TaoZKhjhBMz7s+JOMs FAucOlRnCXqic9ZaL8KrTkvfQkda+6Lp5snrgQNRb8oSSqWTWCVOr8VCflmDi3fLxP EfreD8Zgt+hFwqxltT22yLEbPqnvukEEnir59Tc4itgtJDgb6HUaEIHJ9AZltNXLNZ /g9J8UmJ6pNbmlID1p4Evd48GR8NXvWPwDpnDPGGgDudTORp5nMfGmVY4O19xXiw50 CczQZfWKV/YZQ== From: Miguel Ojeda To: Kees Cook , Bixuan Cui , Randy Dunlap , Miroslav Benes , Matthew Wilcox , David Vernet , Nick Desaulniers , Jiri Olsa , Miguel Ojeda , Stephen Boyd , Boqun Feng , Wedson Almeida Filho , Gary Guo , Changbin Du , linux-kernel@vger.kernel.org Cc: rust-for-linux@vger.kernel.org, Alex Gaynor Subject: [PATCH v2 2/3] kallsyms: support "big" kernel symbols Date: Fri, 6 May 2022 22:34:23 +0200 Message-Id: <20220506203443.24721-3-ojeda@kernel.org> In-Reply-To: <20220506203443.24721-1-ojeda@kernel.org> References: <20220506203443.24721-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Rust symbols can become quite long due to namespacing introduced by modules, types, traits, generics, etc. Increasing to 255 is not enough in some cases, and therefore we need to introduce longer lengths to the symbol table. In order to avoid increasing all lengths to 2 bytes (since most of them are small, including many Rust ones), we use ULEB128 to keep smaller symbols in 1 byte, with the rest in 2 bytes. Reviewed-by: Kees Cook Co-developed-by: Alex Gaynor Signed-off-by: Alex Gaynor Co-developed-by: Wedson Almeida Filho Signed-off-by: Wedson Almeida Filho Co-developed-by: Gary Guo Signed-off-by: Gary Guo Co-developed-by: Boqun Feng Signed-off-by: Boqun Feng Co-developed-by: Matthew Wilcox Signed-off-by: Matthew Wilcox Signed-off-by: Miguel Ojeda --- v2: - Collected Reviewed-by. kernel/kallsyms.c | 26 ++++++++++++++++++++++---- scripts/kallsyms.c | 29 ++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index 79f2eb617a62..e8d2262ef2d2 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -69,12 +69,20 @@ static unsigned int kallsyms_expand_symbol(unsigned int= off, data =3D &kallsyms_names[off]; len =3D *data; data++; + off++; + + /* If MSB is 1, it is a "big" symbol, so needs an additional byte. */ + if ((len & 0x80) !=3D 0) { + len =3D (len & 0x7F) | (*data << 7); + data++; + off++; + } =20 /* * Update the offset to return the offset for the next symbol on * the compressed stream. */ - off +=3D len + 1; + off +=3D len; =20 /* * For every byte on the compressed symbol data, copy the table @@ -127,7 +135,7 @@ static char kallsyms_get_symbol_type(unsigned int off) static unsigned int get_symbol_offset(unsigned long pos) { const u8 *name; - int i; + int i, len; =20 /* * Use the closest marker we have. We have markers every 256 positions, @@ -141,8 +149,18 @@ static unsigned int get_symbol_offset(unsigned long po= s) * so we just need to add the len to the current pointer for every * symbol we wish to skip. */ - for (i =3D 0; i < (pos & 0xFF); i++) - name =3D name + (*name) + 1; + for (i =3D 0; i < (pos & 0xFF); i++) { + len =3D *name; + + /* + * If MSB is 1, it is a "big" symbol, so we need to look into + * the next byte (and skip it, too). + */ + if ((len & 0x80) !=3D 0) + len =3D ((len & 0x7F) | (name[1] << 7)) + 1; + + name =3D name + len + 1; + } =20 return name - kallsyms_names; } diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 82d6508bdf29..7e99799aa7b9 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -480,12 +480,35 @@ static void write_src(void) if ((i & 0xFF) =3D=3D 0) markers[i >> 8] =3D off; =20 - printf("\t.byte 0x%02x", table[i]->len); + /* There cannot be any symbol of length zero. */ + if (table[i]->len =3D=3D 0) { + fprintf(stderr, "kallsyms failure: " + "unexpected zero symbol length\n"); + exit(EXIT_FAILURE); + } + + /* Only lengths that fit in up-to-two-byte ULEB128 are supported. */ + if (table[i]->len > 0x3FFF) { + fprintf(stderr, "kallsyms failure: " + "unexpected huge symbol length\n"); + exit(EXIT_FAILURE); + } + + /* Encode length with ULEB128. */ + if (table[i]->len <=3D 0x7F) { + /* Most symbols use a single byte for the length. */ + printf("\t.byte 0x%02x", table[i]->len); + off +=3D table[i]->len + 1; + } else { + /* "Big" symbols use two bytes. */ + printf("\t.byte 0x%02x, 0x%02x", + (table[i]->len & 0x7F) | 0x80, + (table[i]->len >> 7) & 0x7F); + off +=3D table[i]->len + 2; + } for (k =3D 0; k < table[i]->len; k++) printf(", 0x%02x", table[i]->sym[k]); printf("\n"); - - off +=3D table[i]->len + 1; } printf("\n"); =20 --=20 2.35.3 From nobody Fri May 8 09:11:03 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E4ACCC4332F for ; Fri, 6 May 2022 20:36:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443300AbiEFUjq (ORCPT ); Fri, 6 May 2022 16:39:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1443262AbiEFUjn (ORCPT ); Fri, 6 May 2022 16:39:43 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE9AF13F26; Fri, 6 May 2022 13:35:54 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 958B4B83993; Fri, 6 May 2022 20:35:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 44B5BC385A9; Fri, 6 May 2022 20:35:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1651869352; bh=3kPhcKaDX/Q+2C1Refp+7gz4Jh/EZkT9U6Y5DeG8zHM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EpY6GvAv2zKwPBid1bffm6mOZGnHWgr0qQdBgKHGiz7jrAC0hVk7t257LF8qRmj9k s0UAWzMlltrC5KhBgUxvmyzDKJDyqyVrhTn5TO3fx3uwWtmVgUr8csoLXWIgEKzYj1 XYrkJAoxusE0ZWliYA6AADMnxt+1UK+SNA/Ha0a7ro51z/voI7jFmknRzHIgR//fcs uWB+1+19v7OK7MKi056I5XaN1zqB5VWxAXk4jB1lEVA3vSmtn0R3DUCKrZKKPm6amv 2rNEMVrEaDR8vaRFW5/d2s1m3CekHNXG7cwMw4vSdTN42brodD6QfpPSc1rdxRDcOj jh0cvEWB8FeZQ== From: Miguel Ojeda To: Josh Poimboeuf , Jiri Kosina , Miroslav Benes , Petr Mladek , Joe Lawrence , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Andrew Morton , Kefeng Wang , Sergey Senozhatsky , Kees Cook , Miguel Ojeda , Stephen Boyd , Christophe Leroy , Boqun Feng , Alex Gaynor , Gary Guo , Changbin Du , Wedson Almeida Filho , Adrian Hunter , "Gustavo A. R. Silva" , linux-kernel@vger.kernel.org, live-patching@vger.kernel.org, linux-perf-users@vger.kernel.org Cc: rust-for-linux@vger.kernel.org Subject: [PATCH v2 3/3] kallsyms: increase maximum kernel symbol length to 512 Date: Fri, 6 May 2022 22:34:24 +0200 Message-Id: <20220506203443.24721-4-ojeda@kernel.org> In-Reply-To: <20220506203443.24721-1-ojeda@kernel.org> References: <20220506203443.24721-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Rust symbols can become quite long due to namespacing introduced by modules, types, traits, generics, etc. For instance, the following code: pub mod my_module { pub struct MyType; pub struct MyGenericType(T); pub trait MyTrait { fn my_method() -> u32; } impl MyTrait for MyGenericType { fn my_method() -> u32 { 42 } } } generates a symbol of length 96 when using the upcoming v0 mangling scheme: _RNvXNtCshGpAVYOtgW1_7example9my_moduleINtB2_13MyGenericTypeNtB2_6MyTyp= eENtB2_7MyTrait9my_method At the moment, Rust symbols may reach up to 300 in length. Setting 512 as the maximum seems like a reasonable choice to keep some headroom. Reviewed-by: Kees Cook Reviewed-by: Petr Mladek Co-developed-by: Alex Gaynor Signed-off-by: Alex Gaynor Co-developed-by: Wedson Almeida Filho Signed-off-by: Wedson Almeida Filho Co-developed-by: Gary Guo Signed-off-by: Gary Guo Co-developed-by: Boqun Feng Signed-off-by: Boqun Feng Signed-off-by: Miguel Ojeda --- v2: - Collected Reviewed-bys. include/linux/kallsyms.h | 2 +- kernel/livepatch/core.c | 4 ++-- scripts/kallsyms.c | 4 ++-- tools/include/linux/kallsyms.h | 2 +- tools/lib/perf/include/perf/event.h | 2 +- tools/lib/symbol/kallsyms.h | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/linux/kallsyms.h b/include/linux/kallsyms.h index ce1bd2fbf23e..e5ad6e31697d 100644 --- a/include/linux/kallsyms.h +++ b/include/linux/kallsyms.h @@ -15,7 +15,7 @@ =20 #include =20 -#define KSYM_NAME_LEN 128 +#define KSYM_NAME_LEN 512 #define KSYM_SYMBOL_LEN (sizeof("%s+%#lx/%#lx [%s %s]") + \ (KSYM_NAME_LEN - 1) + \ 2*(BITS_PER_LONG*3/10) + (MODULE_NAME_LEN - 1) + \ diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index bc475e62279d..ec06ce59d728 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -213,7 +213,7 @@ static int klp_resolve_symbols(Elf_Shdr *sechdrs, const= char *strtab, * we use the smallest/strictest upper bound possible (56, based on * the current definition of MODULE_NAME_LEN) to prevent overflows. */ - BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN !=3D 128); + BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN !=3D 512); =20 relas =3D (Elf_Rela *) relasec->sh_addr; /* For each rela in this klp relocation section */ @@ -227,7 +227,7 @@ static int klp_resolve_symbols(Elf_Shdr *sechdrs, const= char *strtab, =20 /* Format: .klp.sym.sym_objname.sym_name,sympos */ cnt =3D sscanf(strtab + sym->st_name, - ".klp.sym.%55[^.].%127[^,],%lu", + ".klp.sym.%55[^.].%511[^,],%lu", sym_objname, sym_name, &sympos); if (cnt !=3D 3) { pr_err("symbol %s has an incorrectly formatted name\n", diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 7e99799aa7b9..275044b840dc 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -30,10 +30,10 @@ #define _stringify_1(x) #x #define _stringify(x) _stringify_1(x) =20 -#define KSYM_NAME_LEN 128 +#define KSYM_NAME_LEN 512 =20 /* A substantially bigger size than the current maximum. */ -#define KSYM_NAME_LEN_BUFFER 512 +#define KSYM_NAME_LEN_BUFFER 2048 _Static_assert( KSYM_NAME_LEN_BUFFER =3D=3D KSYM_NAME_LEN * 4, "Please keep KSYM_NAME_LEN_BUFFER in sync with KSYM_NAME_LEN" diff --git a/tools/include/linux/kallsyms.h b/tools/include/linux/kallsyms.h index efb6c3f5f2a9..5a37ccbec54f 100644 --- a/tools/include/linux/kallsyms.h +++ b/tools/include/linux/kallsyms.h @@ -6,7 +6,7 @@ #include #include =20 -#define KSYM_NAME_LEN 128 +#define KSYM_NAME_LEN 512 =20 struct module; =20 diff --git a/tools/lib/perf/include/perf/event.h b/tools/lib/perf/include/p= erf/event.h index e7758707cadd..116a80c31675 100644 --- a/tools/lib/perf/include/perf/event.h +++ b/tools/lib/perf/include/perf/event.h @@ -95,7 +95,7 @@ struct perf_record_throttle { }; =20 #ifndef KSYM_NAME_LEN -#define KSYM_NAME_LEN 256 +#define KSYM_NAME_LEN 512 #endif =20 struct perf_record_ksymbol { diff --git a/tools/lib/symbol/kallsyms.h b/tools/lib/symbol/kallsyms.h index 72ab9870454b..542f9b059c3b 100644 --- a/tools/lib/symbol/kallsyms.h +++ b/tools/lib/symbol/kallsyms.h @@ -7,7 +7,7 @@ #include =20 #ifndef KSYM_NAME_LEN -#define KSYM_NAME_LEN 256 +#define KSYM_NAME_LEN 512 #endif =20 static inline u8 kallsyms2elf_binding(char type) --=20 2.35.3