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 032D4C4332F for ; Tue, 7 Nov 2023 12:56:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234654AbjKGM46 (ORCPT ); Tue, 7 Nov 2023 07:56:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233904AbjKGM4j (ORCPT ); Tue, 7 Nov 2023 07:56:39 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89EDAA5C4; Tue, 7 Nov 2023 04:28:22 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0DD5CC433C8; Tue, 7 Nov 2023 12:28:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1699360102; bh=ESxXRXCSO5pStj63urMcbLh1AJ70GP2zRoE5T49Q8D0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ac9fdRh2qUNy2FHb2NzVzkDzeB78xS7XJxsVjCWyGYpsvR8yTUZLoGgkv81AaMRtF ZQD9xNsRKy5dg4wfT6jvBPr4WFN1uSDlKk0cCMnduiR/GwhEledOKUKhi9/ylNR0ho /k/L2iYaMTSQXIW0QRCHSP3wqLj9wobX1T9j8McLIu0FRunHCcPps5WbTK8GTkwBeT 4hI3h2uS1oiiumfH4Q31hOzEp4DPdp4dG/ueUuZ998R1RZbOVN70X37q2KjPpsxX51 5BRc8yy07FwWJZRAT8uher7CrxMNST1uM7vw7MayCwwAp8FW7ETwTUZ5b6n48LAGB3 ZUcbWBLZIirKg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Philipp Stanner , David Airlie , Andy Shevchenko , Kees Cook , Zack Rusin , Sasha Levin , linux-hardening@vger.kernel.org Subject: [PATCH AUTOSEL 6.1 07/25] string.h: add array-wrappers for (v)memdup_user() Date: Tue, 7 Nov 2023 07:26:46 -0500 Message-ID: <20231107122745.3761613-7-sashal@kernel.org> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231107122745.3761613-1-sashal@kernel.org> References: <20231107122745.3761613-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.1.61 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Philipp Stanner [ Upstream commit 313ebe47d75558511aa1237b6e35c663b5c0ec6f ] Currently, user array duplications are sometimes done without an overflow check. Sometimes the checks are done manually; sometimes the array size is calculated with array_size() and sometimes by calculating n * size directly in code. Introduce wrappers for arrays for memdup_user() and vmemdup_user() to provide a standardized and safe way for duplicating user arrays. This is both for new code as well as replacing usage of (v)memdup_user() in existing code that uses, e.g., n * size to calculate array sizes. Suggested-by: David Airlie Signed-off-by: Philipp Stanner Reviewed-by: Andy Shevchenko Reviewed-by: Kees Cook Reviewed-by: Zack Rusin Signed-off-by: Dave Airlie Link: https://patchwork.freedesktop.org/patch/msgid/20230920123612.16914-3-pstanner@redhat.com Signed-off-by: Sasha Levin --- include/linux/string.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/include/linux/string.h b/include/linux/string.h index cf7607b321027..b1c944a6dbe61 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -5,7 +5,9 @@ #include /* for inline */ #include /* for size_t */ #include /* for NULL */ +#include /* for ERR_PTR() */ #include /* for E2BIG */ +#include /* for check_mul_overflow() */ #include #include @@ -14,6 +16,44 @@ extern void *memdup_user(const void __user *, size_t); extern void *vmemdup_user(const void __user *, size_t); extern void *memdup_user_nul(const void __user *, size_t); +/** + * memdup_array_user - duplicate array from user space + * @src: source address in user space + * @n: number of array members to copy + * @size: size of one array member + * + * Return: an ERR_PTR() on failure. Result is physically + * contiguous, to be freed by kfree(). + */ +static inline void *memdup_array_user(const void __user *src, size_t n, size_t size) +{ + size_t nbytes; + + if (check_mul_overflow(n, size, &nbytes)) + return ERR_PTR(-EOVERFLOW); + + return memdup_user(src, nbytes); +} + +/** + * vmemdup_array_user - duplicate array from user space + * @src: source address in user space + * @n: number of array members to copy + * @size: size of one array member + * + * Return: an ERR_PTR() on failure. Result may be not + * physically contiguous. Use kvfree() to free. + */ +static inline void *vmemdup_array_user(const void __user *src, size_t n, size_t size) +{ + size_t nbytes; + + if (check_mul_overflow(n, size, &nbytes)) + return ERR_PTR(-EOVERFLOW); + + return vmemdup_user(src, nbytes); +} + /* * Include machine specific inline routines */ -- 2.42.0