Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp1385284rdb; Wed, 20 Sep 2023 07:49:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHiTmthXaxHiijzzdiIDyP1zlOCgd8kpm1gzJdmGdL7eahzicIC1LJshlrnbjlQIvBNkOlD X-Received: by 2002:a17:90b:3a84:b0:269:439d:8c3f with SMTP id om4-20020a17090b3a8400b00269439d8c3fmr2660012pjb.22.1695221354353; Wed, 20 Sep 2023 07:49:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695221354; cv=none; d=google.com; s=arc-20160816; b=YpUpXOhL+maM9HLksQDgBAelIuV38bT9XMoHXDksiNYnGqHx6pW4Z9Jd2OLOt8KKNQ jhcocp/sZFrSu/REU/O2A2lw7RLOfGDP6beegQOVgAi9tJiu2XATRgBeRLvO3d//grxO d4Ffxm3DnA5JAtsRqmTo3jtp+REoRjx6VZ09zia3iWPfJrCc5nhG3Cp5WL6I6a7UpwOd TKiA6XXjdBjF4fM/tOVEb1vPqTUJzVd8Y9rVYL/xbJu/GcTeIghUAfQLRPU3XMxtcCsy cAmZF+PTrvbDJqWCjJFXe6zrARL5R8ME2M0Wd7G9e5LSp/glhJKKYd3e5aYWRnFCMDQN RWYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=OJRnSvDMQADh3Wr31msjxxHMdDRAXuX2nElLtMGFtqc=; fh=dlXnl2/h0N1OeQGp8MieJrMIHWCkT16vgcz0nXgbgqE=; b=NJ+CaV4e12RaKfK4CGH5Ipcf/C+WseOilUphUMHVWup+pfnvKdUo9yr0zXNi26jZG/ IbuxH9FCy9HN4S26T5TQb8VXMLvYsgsQTtojCCp1xWYwdUDFzocKlFv2aMRM/sfg4uli gLfAa2wlFbcFbyP/1kUCQBZKHC203pqLQcuVon1sY6GTse9RPHQTxScX7Hzr7UtNYGDo aXxV9GZP+n4SfHYcTXWGU0zxkr2WtknQTJr9YfBweKKOUcUrHxumH3fb+sYRWF6w0DpL p12yxE/FH6itolwzseAj/bI7zmYuZD4gHb9GaGFvYG9ymQ/9YfxB1lbNXkGAuU0KqhDR nGcw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MPqOrPin; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id br10-20020a17090b0f0a00b00274c5572179si1683034pjb.2.2023.09.20.07.49.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Sep 2023 07:49:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MPqOrPin; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id AE16180EFC62; Wed, 20 Sep 2023 05:37:39 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235973AbjITMhj (ORCPT + 99 others); Wed, 20 Sep 2023 08:37:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235977AbjITMhg (ORCPT ); Wed, 20 Sep 2023 08:37:36 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1BAA3AD for ; Wed, 20 Sep 2023 05:36:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695213404; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OJRnSvDMQADh3Wr31msjxxHMdDRAXuX2nElLtMGFtqc=; b=MPqOrPinGAIlwW9zbKvBuoiC5cUV1gkN9gmQA/exWlTN3jYn5voTpXGWX50+v3+2UakWPH RAhVQyvCZ70g+yio6sYh/ZF1CKNQYkUX5BZoo6teuHlJD+ANZwJXEI2yGFmWrizNkPLEjX H4MHQGW4rXc6KBMy9JJ/y1Xx1c5FsK0= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-554-7ALJkzjOOQaSZyuL75OXHQ-1; Wed, 20 Sep 2023 08:36:43 -0400 X-MC-Unique: 7ALJkzjOOQaSZyuL75OXHQ-1 Received: by mail-ej1-f71.google.com with SMTP id a640c23a62f3a-9ade76f4a44so68123466b.0 for ; Wed, 20 Sep 2023 05:36:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695213402; x=1695818202; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OJRnSvDMQADh3Wr31msjxxHMdDRAXuX2nElLtMGFtqc=; b=eGJEunD4eC44MgMBDdPo9SX3vq60+aio33NM2PcqDKD/Iz6gvVGB3+jbVX/kJOmt1B dDR88DgCLX8lAu/PU3pDPY/p9r0vJ8/fCYgdxSuaKZQ8Mrl3XNymxgxDQWQb9S7vRyrP WOEVLpeLo7RpGtZPciO7EhysSxYe/O/mnkT/7Qz1SiXP3t+nAm7+Ox21EZ9Slvu30ZG6 Wj1S/OCM7v2MS5nHqDr0YnD9QORpJAkP3Q5+pq8hUxanhC5cMTCWlrMCW2FDCujNbhTQ y+RxZ5qUCoriFTJutMKjWre6nHnyvWsGHqzq3fVd+cnNDbrCcyH1llPd92hHoi7eKYV7 CGnw== X-Gm-Message-State: AOJu0Yxnq/lwdfrEBb+ZtaBqdXtxI7JgbnGjWDJYrrTGp2Dtnkn7jwNh DfiRLB9w3jkfqleVVFuzwvPxbDV1cKQE0aqhhViN73mmC0d8w3QoOEsHvb+nG83ANOi1lO/0fiD KwuhwyO9DdwK+/SHuXoSBdegU X-Received: by 2002:a17:906:196:b0:9a5:9f3c:961f with SMTP id 22-20020a170906019600b009a59f3c961fmr1815595ejb.3.1695213401728; Wed, 20 Sep 2023 05:36:41 -0700 (PDT) X-Received: by 2002:a17:906:196:b0:9a5:9f3c:961f with SMTP id 22-20020a170906019600b009a59f3c961fmr1815563ejb.3.1695213401348; Wed, 20 Sep 2023 05:36:41 -0700 (PDT) Received: from fedorinator.. ([2a01:599:906:7772:a505:d891:dcff:9565]) by smtp.gmail.com with ESMTPSA id a18-20020a170906191200b0098e42bef736sm9348305eje.176.2023.09.20.05.36.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Sep 2023 05:36:37 -0700 (PDT) From: Philipp Stanner To: Kees Cook , Andy Shevchenko , Eric Biederman , Christian Brauner , David Disseldorp , Luis Chamberlain , Siddh Raman Pant , Nick Alcock , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Zack Rusin Cc: VMware Graphics Reviewers , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, kexec@lists.infradead.org, linux-hardening@vger.kernel.org, Philipp Stanner , David Airlie , Andy Shevchenko Subject: [PATCH v3 1/5] string.h: add array-wrappers for (v)memdup_user() Date: Wed, 20 Sep 2023 14:36:09 +0200 Message-ID: <20230920123612.16914-3-pstanner@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230920123612.16914-2-pstanner@redhat.com> References: <20230920123612.16914-2-pstanner@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Wed, 20 Sep 2023 05:37:39 -0700 (PDT) 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 --- include/linux/string.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/include/linux/string.h b/include/linux/string.h index dbfc66400050..debf4ef1098f 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.41.0