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 1C47EEEB566 for ; Fri, 8 Sep 2023 18:21:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240403AbjIHSVg (ORCPT ); Fri, 8 Sep 2023 14:21:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343757AbjIHSUL (ORCPT ); Fri, 8 Sep 2023 14:20:11 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D00862D65; Fri, 8 Sep 2023 11:19:01 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2FB2FC116AB; Fri, 8 Sep 2023 18:18:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1694197091; bh=G/fVZIoGPqBryia/5Jy1d179ebCj3eS3YcwTvDTKY5c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QA4HzEIRcQO4VV0OObZRPk1xs5t1YCs2rttrkmPlUosWQKhvRY2BkwBAkPtB/mLFz AaQxyd8j+WfkbVoFjGRYiWg9yjvE1srDLuthQ8+ZbsiIucQHxx6wFwufw0Mnb1yBzn RY/RXqBP5nMrNsc+882kyBTn6MA3/Q1iQjsOHPEbMhI3p/06K+wiFZZ3AiD2HivwTJ grB/H4dTpy+A4VYyMVenMKM9rJWazT60CcdiUPdHbQnEmGtJIDxabeFwbM6RwBPgXi ESEv9FOrKBaImyb/iamwf9zVVIlITSHR2qb1i/Un/j3GwXltBKeCGPFZFWvea0Mh6x 1JvLGRgjdZt6g== 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.1 02/26] crypto: lrw,xts - Replace strlcpy with strscpy Date: Fri, 8 Sep 2023 14:17:40 -0400 Message-Id: <20230908181806.3460164-2-sashal@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230908181806.3460164-1-sashal@kernel.org> References: <20230908181806.3460164-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.1.52 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 8d59a66b65255..fb8892ed179f5 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 de6cbcf69bbd6..b05020657cdc8 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