Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4667240iob; Sun, 8 May 2022 21:14:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzM/U2xxTOycvbffm5DbTWSNHVGD4btwM1YgdOTXF7j6TPl3wGXs5ccTLeL32bLzASIQ3q9 X-Received: by 2002:a17:90a:ac15:b0:1d9:839d:3fac with SMTP id o21-20020a17090aac1500b001d9839d3facmr24465868pjq.195.1652069687930; Sun, 08 May 2022 21:14:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652069687; cv=none; d=google.com; s=arc-20160816; b=PlZIXxvYOlGCPbIWc//tYQutVDJaFBS2OZh2jT69PcNjj9qxJB6FM6XGyXT5DILpLQ Jirb2BJSu43F+IRmNkNYUJoCHmZNcf3Pz8owyusIakN/jxKpm3upy39Wt5bLSHRl4FuU 1xPZRQ4vIQ0wWdrADne/XTFm2iPDlJXLir3yjZxX2RqryKFpmV0oANDf6EPMLJQBLguk iwRlskeNkajgQ99IZaPSEs6N3tg3HpKrvPPlCOm7Mzq7R8pjPGxB+JxPe0ZmjlI4NL8w c2J+eoOFToAa55Bqu3i+6xm+cGyRbX4i0JlCtvPBGZs3YTeyZVOCpq2ft6VLosbrApb9 cVow== 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=q1qLainXAsgwJO6wU34RTPIRFZHU781QN5N/iptifgE=; b=Jh+rLN6D3ihi5tEF2xkEbkN6EHhsqutzuSYi8dbXupr4jVdGSTfNs/LgiGYd9tQfku yCfFBXBj42bDZ4enrM3KLimEfiIzJUlfAepkkmUi8dxQIxJeCGrTIEj+rzjHluYz0xrS foBHxLEaECvxjCmfkLtmar98RuxBkcCPT7dKwloJj3zuQDulEWM9UN62xtGZIzMefl/E ZlH1eL1Y0PjzfR81jTLJ+Xhs28SGIAEA/sbYRv9hPA1NmBQe3cJRSN8J5asbnoRiwC0p ZYubYiYb7YS7JwWsdWd52a0Vfid39gPDQLD1qxPNncBBT2SAWDTGn1a72AW1Em+LH4d1 miow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WbdUXGkA; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id 12-20020a170902c10c00b0015e99deb6cdsi9382652pli.135.2022.05.08.21.14.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 May 2022 21:14:47 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WbdUXGkA; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D72DB1172DB; Sun, 8 May 2022 21:12:49 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443271AbiEFUjS (ORCPT + 99 others); Fri, 6 May 2022 16:39:18 -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: 8bit X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 @@ #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) +#define _stringify_1(x) #x +#define _stringify(x) _stringify_1(x) + #define KSYM_NAME_LEN 128 +/* A substantially bigger size than the current maximum. */ +#define KSYM_NAME_LEN_BUFFER 512 +_Static_assert( + KSYM_NAME_LEN_BUFFER == 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, unsigned long long addr, 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; - rc = fscanf(in, "%llx %c %499s\n", &addr, &type, name); + rc = fscanf(in, "%llx %c %" _stringify(KSYM_NAME_LEN_BUFFER) "s\n", &addr, &type, name); if (rc != 3) { - if (rc != EOF && fgets(name, 500, in) == NULL) + if (rc != EOF && fgets(name, sizeof(name), in) == NULL) fprintf(stderr, "Read error or end of file.\n"); return NULL; } -- 2.35.3