From patchwork Fri Nov 11 17:11:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13040635 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 5B4D2C4332F for ; Fri, 11 Nov 2022 17:23:09 +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=YB7eL06ZRqsMJAq81OkPRSkCN4NnQEVLWM24ZfTAqMM=; b=Yj11ON6fPcvQDl DQ+h1UXUW3jn0hDrleZR8MftpPjjCQAEDWDUybN2X9/H9u4Kgb/S7c/VkEmQEgaTTm8vb1Gt3H+ST Uiy07vn0ddcqJ0W+iRUP53K4acJRYzAU3iqHsVu+09U0aPFtHGK2vTL4nDeNxud14JD/WfbFl/RmI jcNoO3HDJ0OrRQ8NjBdbxENkGPP7JL6KTjTGhRoBvT0Z/HCvMrAh3e4R8T/GFgVz8RRk5ZkzXpY/m 5cY+H6zELjJUwHLSBTMClhU2PboN8FlJnt3mE98R+J6a5703X9cdH9rGbVnpPgTejJ8qbBEmEzYzY Aeg2hN/JdulDY0bam2gA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1otXj8-00HCmb-CP; Fri, 11 Nov 2022 17:22:03 +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 1otXaH-00H7mV-Tq for linux-arm-kernel@lists.infradead.org; Fri, 11 Nov 2022 17:12:55 +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 8257962068; Fri, 11 Nov 2022 17:12:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 14C0FC43146; Fri, 11 Nov 2022 17:12:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668186772; bh=A2WyRGZ5o48qgcumyUSsq0JAEwTt/8p5p7vznI25Q3s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DWcpftWW5DB0E5zj/yeT3Zp7BzNCs9jFUq6QArkB9Wv+QjWwbpwLr3n2bwvCPiiOH ZmxkO/uMOocW+CBNQT8bWsl+SElU7kgqw4rhZKT2Yvp2gShkLfeNSgLK0BykgXjAZP PutHBaz3FO5JAQqwAL5Yc6Bb1as9F5ABtKfdE59S1KNriU+dorWMnJup+fEuTxvZ29 2eSnCk+jaGu+Hknm2h2pRS0KPDLIJ3TIdQ67UMuA16C2fyyoO1IWrUYy1fGAH90/WX CdH5T+5zA4UmYnwUCZ2ygipDoBTSAv5X9IGU9IFBxvjXcncLtFLU36rox5PO9rWGrR ZhTgrk/DFOR2w== From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: Ard Biesheuvel , Marc Zyngier , Will Deacon , Mark Rutland , Kees Cook , Catalin Marinas , Mark Brown , Anshuman Khandual Subject: [PATCH v7 15/33] arm64: idreg-override: Avoid sprintf() for simple string concatenation Date: Fri, 11 Nov 2022 18:11:43 +0100 Message-Id: <20221111171201.2088501-16-ardb@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221111171201.2088501-1-ardb@kernel.org> References: <20221111171201.2088501-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2329; i=ardb@kernel.org; h=from:subject; bh=A2WyRGZ5o48qgcumyUSsq0JAEwTt/8p5p7vznI25Q3s=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBjboJBs9NxBIY4U0OBxs89a4tMyVnsT5ycStjAz9mG uIfHnyuJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCY26CQQAKCRDDTyI5ktmPJFRZDA CO0nHCENwlSc22mvfRKldu23OV2e+HFsqDxPftQ0XTSDEKjHTX0aqs1Wpde+zy/t32eZkwK3zJFgvj jbIeW1OMN65pbYnsb7CqjBlGDpP6s0b8O2PhuZSFKJH34kg7R49Dp13TsHxzDC+7npFtJ2WiWMoV2O FCNRWL4Ls+xW3AqIdzZn4QKWij3ni5Ky8cHXeXHsuh8xdtGSJgVnob25Tag9Jf84LAVFwjBp6hGLqZ TPQMWJMoRUctmAZfDfy2cHQJx9QlDXLW2cTA7j025sbhZjfPiKDS+OweYxLktsjexvz0Gq7fLCTdUX 9ibIW2pttZpU8hu/sZni74YKVTDwwUNrmctGMCAXtP+Fjpd1/WaS83NQbNB0mDYMKo7qOO7XT+OS+7 NS1HdwDjqqf133mn0ooM5m1QRchyau7lQ2wwbnjNOwF/5uiKFpmFXadKbVCphYbJLjESFh07wfWZ8L EYG9YSGUZoGhLgXgIdunoH1B2/H8hsrmzwVYhdpyBjQfE= 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-20221111_091254_103415_650AC2A3 X-CRM114-Status: GOOD ( 17.82 ) 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 97ec832d87d44f64..a5299aa1d1711adc 100644 --- a/arch/arm64/kernel/idreg-override.c +++ b/arch/arm64/kernel/idreg-override.c @@ -166,14 +166,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; @@ -190,12 +191,18 @@ static const void * __init get_filter(const struct ftr_set_desc *reg, int idx) 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 = offset_to_ptr(®s[i]); + int len = strlen(reg->name); int f; + // 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; @@ -203,7 +210,7 @@ static void __init match_options(const char *cmdline) bool (*filter)(u64) = get_filter(reg, f); u64 v; - if (find_field(cmdline, reg, f, &v)) + if (find_field(cmdline, opt, len, reg, f, &v)) continue; /*