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 32DDBC4167D for ; Tue, 7 Nov 2023 12:42:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235259AbjKGMmY (ORCPT ); Tue, 7 Nov 2023 07:42:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235341AbjKGMmB (ORCPT ); Tue, 7 Nov 2023 07:42:01 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6615A28FD4; Tue, 7 Nov 2023 04:29:55 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E6A89C433C8; Tue, 7 Nov 2023 12:29:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1699360195; bh=BQVry/IOuAxj1SJKlm32ooABQ+w1UXdqOMRIhasRqnM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HRf7VHDyx3IOGRw8dSYFbvsB9Tk+FYZ+ZKvo0Wu9oLgOi6EnKqmdJLdMT2h6n2q5c ldUksbjDAboRPLJAIax0e+wgQAbdns514l0eUh0KRjAwDobN3g2jURYp9xF6jtTFtZ 1sBVqAnZuw3EPVnHZe1NnWaGxww9Mevqo9b2Q31y0jPgGJekx9QHvTAabE67tvHHHT OZiqfQrDIkL8ZZc4uiZULfPzYFey9tiQuO3qxzL/yl6A4q0nCdsmYMYsuIuqep2M+X iHLSrUnerWyxJjGV8LoAop4jJRA2hWyuDwcJDymJEkxwue/A3y62mM4BYyRGQBML2T 627cYuxfibfXg== 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 5.15 04/20] string.h: add array-wrappers for (v)memdup_user() Date: Tue, 7 Nov 2023 07:28:58 -0500 Message-ID: <20231107122940.3762228-4-sashal@kernel.org> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231107122940.3762228-1-sashal@kernel.org> References: <20231107122940.3762228-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 5.15.137 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 d68097b4f600b..3b9f5abe5ee83 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