From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D7A032EAE5; Thu, 11 Apr 2024 10:01:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712829693; cv=none; b=JQo/TkQYwifRmFmOk7268A/im2oGOrnrCG0LpRmkywSRzGMX5n+kiqjpjJNJb98Sn0FZG8QtTBYyPS6huYMbAMuhcPoUYT35L6q8pkbjNKdMKzw0pS+b1w2GgS46wK67YlOAsxwC61+NZsBOPKVoAUnaWCi8ZOgPvOUgDzLXkJA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712829693; c=relaxed/simple; bh=jzCzuzIGzKnFvxvFZb7+W5cQ/7l22rZsjDaFMp6Y7Ys=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fjVWrQjP6lEOFFkGT1CUKK68YPPweiFkOPu86avUy+i0SKsk1yRAaLJbH5T6UYs9guGUcgOmJoQBNt1KKUSdFI1ZapoSKnCm95ZbXpww+Ghu+Be4eUlM64tmCYCrZHxn6zaJv/N3LGwxkGIMxDHKntDq/7wEPJYVs9EQ0MEhYvQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=NLiAimO9; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="NLiAimO9" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 60816C433F1; Thu, 11 Apr 2024 10:01:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1712829693; bh=jzCzuzIGzKnFvxvFZb7+W5cQ/7l22rZsjDaFMp6Y7Ys=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NLiAimO9Mc+FwO0wu0qTqjtpRYY+M7eKW9bLWKVN3ceDGdOM0lOIrPtw4UT4P4epg Qc+WYd4nYvLFKBuRRleAjEICzmpHdS5Y3n0QijFwGdR3mKywjAC8sc4k96dM9WYNA+ P/5giNpwLHpKhf5Wg+KaV5mREtxY5IVaWLmB9aec= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Nathan Chancellor , Kees Cook , Steffen Klassert Subject: [PATCH 4.19 072/175] xfrm: Avoid clang fortify warning in copy_to_user_tmpl() Date: Thu, 11 Apr 2024 11:54:55 +0200 Message-ID: <20240411095421.731325666@linuxfoundation.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240411095419.532012976@linuxfoundation.org> References: <20240411095419.532012976@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Nathan Chancellor commit 1a807e46aa93ebad1dfbed4f82dc3bf779423a6e upstream. After a couple recent changes in LLVM, there is a warning (or error with CONFIG_WERROR=y or W=e) from the compile time fortify source routines, specifically the memset() in copy_to_user_tmpl(). In file included from net/xfrm/xfrm_user.c:14: ... include/linux/fortify-string.h:438:4: error: call to '__write_overflow_field' declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning] 438 | __write_overflow_field(p_size_field, size); | ^ 1 error generated. While ->xfrm_nr has been validated against XFRM_MAX_DEPTH when its value is first assigned in copy_templates() by calling validate_tmpl() first (so there should not be any issue in practice), LLVM/clang cannot really deduce that across the boundaries of these functions. Without that knowledge, it cannot assume that the loop stops before i is greater than XFRM_MAX_DEPTH, which would indeed result a stack buffer overflow in the memset(). To make the bounds of ->xfrm_nr clear to the compiler and add additional defense in case copy_to_user_tmpl() is ever used in a path where ->xfrm_nr has not been properly validated against XFRM_MAX_DEPTH first, add an explicit bound check and early return, which clears up the warning. Cc: stable@vger.kernel.org Link: https://github.com/ClangBuiltLinux/linux/issues/1985 Signed-off-by: Nathan Chancellor Reviewed-by: Kees Cook Signed-off-by: Steffen Klassert Signed-off-by: Greg Kroah-Hartman --- net/xfrm/xfrm_user.c | 3 +++ 1 file changed, 3 insertions(+) --- a/net/xfrm/xfrm_user.c +++ b/net/xfrm/xfrm_user.c @@ -1703,6 +1703,9 @@ static int copy_to_user_tmpl(struct xfrm if (xp->xfrm_nr == 0) return 0; + if (xp->xfrm_nr > XFRM_MAX_DEPTH) + return -ENOBUFS; + for (i = 0; i < xp->xfrm_nr; i++) { struct xfrm_user_tmpl *up = &vec[i]; struct xfrm_tmpl *kp = &xp->xfrm_vec[i];