From patchwork Tue Mar 7 14:04:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13163753 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 44167C678D4 for ; Tue, 7 Mar 2023 14:29:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=I5n+H//ZymhV3hqV+OkD4UmkOZmo7YQQk1TR1/U/D6g=; b=fo/lUQaU1OKc7H Sp5nNc6NAZ5O4odWwcUgVriynkeezws62Zkl918g8EN4x/+GEfOW/fJjaZtjeSMXm+jff1YUUK7YQ gkYp/pMw5Dv7TX3gCNy8634naG1PomtD5tf0r/zQRVj0EizRN1dBvcO8koKrNBgDI1cr0mp+Nt/mC KxJt7YG3Y2UUrhN5wd8kvE7BjWEN50V7kV5VOGJwqUcYnUqXIAqoNdSyNjfgfN6KxD4lE45WGZHNg zlFlng1XCyZY69d0Uc8RBgkE8UsKX4w+ZKlsy5us3bTswhTNmg9oKA1v0ro0Y5rfbxLTE/V7+276A 4kcNRpkYtpDrR3fyPZXg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pZYJ1-000nZh-BJ; Tue, 07 Mar 2023 14:28:44 +0000 Received: from dfw.source.kernel.org ([139.178.84.217]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pZXyU-000eH7-6F for linux-arm-kernel@lists.infradead.org; Tue, 07 Mar 2023 14:07:31 +0000 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 C500961465; Tue, 7 Mar 2023 14:07:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CDD83C433A1; Tue, 7 Mar 2023 14:07:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1678198048; bh=SoZODBy3e6pBd6VVEQFBkwGASrxh7Q1UeOly8/M6eFE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=E6vHoFygj0T9SHRspVnNeZkNyqEvvRmUqh2JLfhWYKsLHPiAq4RLn1hylVd6DyySr l6Bskgb3l8DhedzlzzJs0QE0QGVFMO1cLVgEHyGwPx4a1khXczJotkE6XRMpuQwlGW fg8+HMF7R0OcB71Xao+uUXf6TngveI7iZJR8ucvUmJLtdT4wE0u8b/Xpr9QffbTth7 qZbZPOj0tQEoUhBOHrg3IbRKdEK8tSHsWsIK6eoEbnlNe2QzaLjvb5BXn2gTFN6QQx ER+32leoaVp8UWAFAQ8whZUy4vsQ5z9vPbpV84AV/RnoN3dv0ZcmVi0QJHsnqB6IDp S5ToOELo5vp/w== From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: linux-arm-kernel@lists.infradead.org, Ard Biesheuvel , Catalin Marinas , Will Deacon , Marc Zyngier , Mark Rutland , Ryan Roberts , Anshuman Khandual , Kees Cook Subject: [PATCH v3 21/60] arm64: idreg-override: Avoid sprintf() for simple string concatenation Date: Tue, 7 Mar 2023 15:04:43 +0100 Message-Id: <20230307140522.2311461-22-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230307140522.2311461-1-ardb@kernel.org> References: <20230307140522.2311461-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2395; i=ardb@kernel.org; h=from:subject; bh=SoZODBy3e6pBd6VVEQFBkwGASrxh7Q1UeOly8/M6eFE=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIYXdxcjoZFmWf6zz8v36e+vaVbyFmPSMb+ZOteAwOKEv/ kPq+raOUhYGMQ4GWTFFFoHZf9/tPD1RqtZ5lizMHFYmkCEMXJwCMJGn5xn+qT54KGDTlave6FxY d8JcYOqzzpuT1a+nJlwt5t1/4PeZZwz/E8+6mO/1Ztz187VXi92UufIn1TUWHXmb5Hww8oHGhtf /uAA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230307_060730_351697_92E668B4 X-CRM114-Status: GOOD ( 18.43 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Instead of using sprintf() with the "%s.%s=" format, where the first string argument is always the same in the inner loop of match_options(), use simple memcpy() for string concatenation, and move the first copy to the outer loop. This removes the dependency on sprintf(), which will be difficult to fulfil when we move this code into the early mini C runtime. Signed-off-by: Ard Biesheuvel --- arch/arm64/kernel/idreg-override.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/arch/arm64/kernel/idreg-override.c b/arch/arm64/kernel/idreg-override.c index 476dc3f0e9d90e22..58c02fe20e1050b4 100644 --- a/arch/arm64/kernel/idreg-override.c +++ b/arch/arm64/kernel/idreg-override.c @@ -195,14 +195,15 @@ static const struct { { "nokaslr", "arm64_sw.nokaslr=1" }, }; -static int __init find_field(const char *cmdline, +static int __init find_field(const char *cmdline, char *opt, int len, const struct ftr_set_desc *reg, int f, u64 *v) { - char opt[FTR_DESC_NAME_LEN + FTR_DESC_FIELD_LEN + 2]; - int len; + int flen = strlen(reg->fields[f].name); - len = snprintf(opt, ARRAY_SIZE(opt), "%s.%s=", - reg->name, reg->fields[f].name); + // append '=' to obtain '.=' + memcpy(opt + len, reg->fields[f].name, flen); + len += flen; + opt[len++] = '='; if (memcmp(cmdline, opt, len)) return -1; @@ -212,15 +213,21 @@ static int __init find_field(const char *cmdline, static void __init match_options(const char *cmdline) { + char opt[FTR_DESC_NAME_LEN + FTR_DESC_FIELD_LEN + 2]; int i; for (i = 0; i < ARRAY_SIZE(regs); i++) { const struct ftr_set_desc *reg = prel64_to_pointer(®s[i].reg_prel); struct arm64_ftr_override *override; + int len = strlen(reg->name); int f; override = prel64_to_pointer(®->override_prel); + // set opt[] to '.' + memcpy(opt, reg->name, len); + opt[len++] = '.'; + for (f = 0; reg->fields[f].name[0] != '\0'; f++) { u64 shift = reg->fields[f].shift; u64 width = reg->fields[f].width ?: 4; @@ -228,7 +235,7 @@ static void __init match_options(const char *cmdline) bool (*filter)(u64 val); u64 v; - if (find_field(cmdline, reg, f, &v)) + if (find_field(cmdline, opt, len, reg, f, &v)) continue; /*