From mboxrd@z Thu Jan 1 00:00:00 1970 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 A9BCEEEB562 for ; Fri, 8 Sep 2023 19:12:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241287AbjIHTMY (ORCPT ); Fri, 8 Sep 2023 15:12:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232600AbjIHTMV (ORCPT ); Fri, 8 Sep 2023 15:12:21 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CCFDB180; Fri, 8 Sep 2023 12:12:15 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 447C3C116AE; Fri, 8 Sep 2023 18:15:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1694196960; bh=Px6YTqWVrT9O5rjNMNUtYYowvE2PAQ3QfWU57k78G78=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sOlSJjCSVOTY0AVUiL+qk8vpc5EI03kv+xHqUENLHXBrKWHVTV9fkojW1IsuqGkUq 43bjYc+UR+nEVSU0JPrXSpPnOujLaxIOl9AWCqwxEPsMbKA8f8Ykm+5bta6HkIqb7i trmNntPFWzLpTh8UVOL38aaCd+XZcTIa81vFAxOgutusQsDQnGYbBdDcbHrjtDrM/w Gi4IPrsAxoYzhXIJtzCZ29RmRofwfMJkB4Odog6TgT9Jm8RKT1LPVHh9a4w/fvgEAW nV8eYjqsFdd9KWljqpwlGideR0rXJFDgW0/9WzqME4zjKJRcbMzVQFlh+qOBBAzhhK osZbXYd9hVYaQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Azeem Shaikh , Kees Cook , Herbert Xu , Sasha Levin , davem@davemloft.net, linux-crypto@vger.kernel.org Subject: [PATCH AUTOSEL 6.4 02/41] crypto: lrw,xts - Replace strlcpy with strscpy Date: Fri, 8 Sep 2023 14:15:16 -0400 Message-Id: <20230908181555.3459640-2-sashal@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230908181555.3459640-1-sashal@kernel.org> References: <20230908181555.3459640-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.4.15 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Azeem Shaikh [ Upstream commit babb80b3ecc6f40c962e13c654ebcd27f25ee327 ] strlcpy() reads the entire source buffer first. This read may exceed the destination size limit. This is both inefficient and can lead to linear read overflows if a source string is not NUL-terminated [1]. In an effort to remove strlcpy() completely [2], replace strlcpy() here with strscpy(). Direct replacement is safe here since return value of -errno is used to check for truncation instead of sizeof(dest). [1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy [2] https://github.com/KSPP/linux/issues/89 Signed-off-by: Azeem Shaikh Reviewed-by: Kees Cook Signed-off-by: Herbert Xu Signed-off-by: Sasha Levin --- crypto/lrw.c | 6 +++--- crypto/xts.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/crypto/lrw.c b/crypto/lrw.c index 1b0f76ba3eb5e..59260aefed280 100644 --- a/crypto/lrw.c +++ b/crypto/lrw.c @@ -357,10 +357,10 @@ static int lrw_create(struct crypto_template *tmpl, struct rtattr **tb) * cipher name. */ if (!strncmp(cipher_name, "ecb(", 4)) { - unsigned len; + int len; - len = strlcpy(ecb_name, cipher_name + 4, sizeof(ecb_name)); - if (len < 2 || len >= sizeof(ecb_name)) + len = strscpy(ecb_name, cipher_name + 4, sizeof(ecb_name)); + if (len < 2) goto err_free_inst; if (ecb_name[len - 1] != ')') diff --git a/crypto/xts.c b/crypto/xts.c index 09be909a6a1aa..548b302c6c6a0 100644 --- a/crypto/xts.c +++ b/crypto/xts.c @@ -396,10 +396,10 @@ static int xts_create(struct crypto_template *tmpl, struct rtattr **tb) * cipher name. */ if (!strncmp(cipher_name, "ecb(", 4)) { - unsigned len; + int len; - len = strlcpy(ctx->name, cipher_name + 4, sizeof(ctx->name)); - if (len < 2 || len >= sizeof(ctx->name)) + len = strscpy(ctx->name, cipher_name + 4, sizeof(ctx->name)); + if (len < 2) goto err_free_inst; if (ctx->name[len - 1] != ')') -- 2.40.1