Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp103916rwb; Mon, 26 Sep 2022 09:50:51 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7p5MQyCxNy6f8LJDHjROL6+9zAmuUZIjgnyGi4yCGd4boIbi7pFX0LhTZrorIbfaDQXesS X-Received: by 2002:a17:907:1c9a:b0:782:3046:e966 with SMTP id nb26-20020a1709071c9a00b007823046e966mr18473020ejc.732.1664211051114; Mon, 26 Sep 2022 09:50:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664211051; cv=none; d=google.com; s=arc-20160816; b=Aw11VXeMljiY9u8wNc5LRx3W/OSO34EeEkqCqSauabcBorRjWwkSSDp7fQp8k7nr8L X0wXzGZr7unfKGGzfOAaxnslLbYBexkbSNCFUa5WrvU7UxZFwJG1rkBtLjsnzTKGcOYN vDSCN3KxTvXqz0T6O3POe2uTrcRkEUygpUYNkeimzaaUZ33rsRX9YN4T5fA+EzLtjsd/ wIRN4PZ/PH/EjJ1YkRf4i00nsAExPHB/7kxD0cNrEFfl+9Pxp+gt4+f1K6lSLR/yxEhI ama12sE26Prxih4R1mtZqBQLMiZViXjSxbHxfMDhGwVg8ctqZsrV67w4enGwC/1Agb9l rSiw== 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=/VcLtF7fOIU5AjRmO3bx8/4g9RXMMIh0rjrX5h+7tGo=; b=o0a0PRUPSrrKO5C/licXwDYS7IhuwU3hbOpw/jLApdpSE3G4LmK59LcbdtdMhvLxKZ 5vLcGUJb9yehbrhOZurGPvpe4+mCZJ5YEizpyWRwFI5OyvLX0kzqXOgwVVS308mepGtS cejVuUYhO6zj0DJzgGctug6ZJV3BKnhJaHUWuPXOs5JIxVcO5vHgalmN3hoFxD6zKOMx 40KzF7rq4Sjb8IkwT3aHCnKmFna8iihZMt+saljUrT8Wvp/php92QCBEVsC6Lbgf+yGX GdmGlnUmH56ZnHfGvWG3dfZgYmINxqyF/uv8tMn4rw7ENIkX+ZjSkWWPEpuZKLc3qpm2 TsMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="OqDVvX/1"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hc14-20020a170907168e00b007835897050asi674182ejc.83.2022.09.26.09.50.24; Mon, 26 Sep 2022 09:50:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="OqDVvX/1"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229944AbiIZQs0 (ORCPT + 99 others); Mon, 26 Sep 2022 12:48:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229588AbiIZQrs (ORCPT ); Mon, 26 Sep 2022 12:47:48 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CDD0515737; Mon, 26 Sep 2022 08:40:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1664206833; x=1695742833; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=a15giGE9U8MfRV7QFZGLrBfnIa+lhGBT+wMMe9HhDqQ=; b=OqDVvX/1aqyWRoYQRkvkd8LWGjAhj0makgHyCGKJH2WP4kWEfmp22oad onznlwkQObRgeVp4Zb9Egz6SubgctQgehmCZV7ZKmvtSbqS+23J7unf0g mt6qPNHzM11OgyqUcMLJMw+/mPsZaZPW7i20J50+FVMcO3OOJsOTrwf9M 8TmyEXkiOWnmreGGwSqAEcfKpxwwAJrIolvJWw89DpQPc5SqAgtrrBTPh 22GvRPzolvEt0mqIm8vpeU10+lWIC32hWcg4Ru8pcuy5wC0l6t905bcaR HVczWP91w+gHF2QpEdI1HXiJxVmc15sY9XqLcj69+3wt6/Nl+9HTP/2/A A==; X-IronPort-AV: E=McAfee;i="6500,9779,10482"; a="362890062" X-IronPort-AV: E=Sophos;i="5.93,346,1654585200"; d="scan'208";a="362890062" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Sep 2022 08:40:33 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10482"; a="763480870" X-IronPort-AV: E=Sophos;i="5.93,346,1654585200"; d="scan'208";a="763480870" Received: from bsochack-mobl2.ger.corp.intel.com (HELO paris.ger.corp.intel.com) ([10.249.128.215]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Sep 2022 08:40:24 -0700 From: Gwan-gyeong Mun To: intel-gfx@lists.freedesktop.org Cc: linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, mchehab@kernel.org, chris@chris-wilson.co.uk, matthew.auld@intel.com, thomas.hellstrom@linux.intel.com, jani.nikula@intel.com, nirmoy.das@intel.com, airlied@redhat.com, daniel@ffwll.ch, andi.shyti@linux.intel.com, andrzej.hajda@intel.com, keescook@chromium.org, mauro.chehab@linux.intel.com, linux@rasmusvillemoes.dk, vitor@massaru.org, dlatypov@google.com, ndesaulniers@google.com, trix@redhat.com, llvm@lists.linux.dev, linux-hardening@vger.kernel.org, linux-sparse@vger.kernel.org, nathan@kernel.org, gustavoars@kernel.org, luc.vanoostenryck@gmail.com Subject: [PATCH v12 2/9] overflow: Introduce check_assign() and check_assign_user_ptr() Date: Mon, 26 Sep 2022 18:39:46 +0300 Message-Id: <20220926153953.3836470-3-gwan-gyeong.mun@intel.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220926153953.3836470-1-gwan-gyeong.mun@intel.com> References: <20220926153953.3836470-1-gwan-gyeong.mun@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add check_assign() macro which performs an assigning source value into destination pointer along with an overflow check and check_assign_user_ptr() macro which performs an assigning source value into destination pointer type variable along with an overflow check. If an explicit overflow check is required while assigning to a user-space ptr, assign_user_ptr() can be used instead of u64_to_user_ptr() to assign integers into __user pointers along with an overflow check. v3: Add is_type_unsigned() macro (Mauro) Modify overflows_type() macro to consider signed data types (Mauro) Fix the problem that safe_conversion() macro always returns true v4: Fix kernel-doc markups v6: Move macro addition location so that it can be used by other than drm subsystem (Jani, Mauro, Andi) Change is_type_unsigned to is_unsigned_type to have the same name form as is_signed_type macro v8: Add check_assign() and remove safe_conversion() (Kees) Fix overflows_type() to use gcc's built-in overflow function (Andrzej) Add overflows_ptr() to allow overflow checking when assigning a value into a pointer variable (G.G.) v9: Fix overflows_type() to use __builtin_add_overflow() instead of __builtin_add_overflow_p() (Andrzej) Fix overflows_ptr() to use overflows_type() with the unsigned long type (Andrzej) v10: Remove a redundant type checking for a pointer. (Andrzej) Use updated check_add_overflow macro instead of __builtin_add_overflow (G.G) Add check_assign_user_ptr() macro and drop overflows_ptr() macro(Kees) v11: Fix incorrect type assignment between different address spaces caused by the wrong use of __user macro. (kernel test robot) Update macro description (G.G) v12: Remove overflows_type() macro here. updated overflows_type() macro will be added in a subsequent patch (G.G) Signed-off-by: Gwan-gyeong Mun Cc: Thomas Hellström Cc: Matthew Auld Cc: Nirmoy Das Cc: Jani Nikula Cc: Andi Shyti Cc: Andrzej Hajda Cc: Mauro Carvalho Chehab Cc: Kees Cook Reviewed-by: Mauro Carvalho Chehab (v5) Reviewed-by: Andrzej Hajda (v9) Acked-by: Kees Cook Reported-by: kernel test robot --- drivers/gpu/drm/i915/i915_user_extensions.c | 6 +-- include/linux/overflow.h | 44 +++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/drivers/gpu/drm/i915/i915_user_extensions.c b/drivers/gpu/drm/i915/i915_user_extensions.c index c822d0aafd2d..80ec8390b0d8 100644 --- a/drivers/gpu/drm/i915/i915_user_extensions.c +++ b/drivers/gpu/drm/i915/i915_user_extensions.c @@ -50,11 +50,11 @@ int i915_user_extensions(struct i915_user_extension __user *ext, if (err) return err; - if (get_user(next, &ext->next_extension) || - overflows_type(next, ext)) + if (get_user(next, &ext->next_extension)) return -EFAULT; - ext = u64_to_user_ptr(next); + if (check_assign_user_ptr(next, ext)) + return -EFAULT; } return 0; diff --git a/include/linux/overflow.h b/include/linux/overflow.h index 19dfdd74835e..8ccbfa46f0ed 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -5,6 +5,7 @@ #include #include #include +#include /* * We need to compute the minimum and maximum values representable in a given @@ -127,6 +128,49 @@ static inline bool __must_check __must_check_overflow(bool overflow) (*_d >> _to_shift) != _a); \ })) +/** + * check_assign - perform an assigning source value into destination pointer + * along with an overflow check. + * + * @value: source value + * @ptr: Destination pointer address + * + * Returns: + * If the value would overflow the destination, it returns true. If not return + * false. When overflow does not occur, the assigning into destination from + * value succeeds. It follows the return policy as other check_*_overflow() + * functions return non-zero as a failure. + */ +#define check_assign(value, ptr) __must_check_overflow(({ \ + check_add_overflow(0, value, ptr); \ +})) + +/** + * check_assign_user_ptr - perform an assigning source value into destination + * pointer type variable along with an overflow check + * + * @value: source value; a source value is expected to have a value of a size + * that can be stored in a pointer-type variable. + * @ptr: destination pointer type variable + * + * u64_to_user_ptr can be used in the kernel to avoid warnings about integers + * and pointers of different sizes. But u64_to_user_ptr is not performing the + * checking of overflow. If you need an explicit overflow check while + * assigning, check_assign_user_ptr() can be used to assign integers into + * pointers along with an overflow check. If ptr is not a pointer type, + * a warning message outputs while compiling. + * + * Returns: + * If the value would overflow the destination, it returns true. If not return + * false. When overflow does not occur, the assigning into ptr from value + * succeeds. It follows the return policy as other check_*_overflow() functions + * return non-zero as a failure. + */ +#define check_assign_user_ptr(value, ptr) __must_check_overflow(({ \ + uintptr_t kptr; \ + check_assign(value, &kptr) ? 1 : (({ ptr = (void __user *)kptr; }), 0); \ +})) + /** * size_mul() - Calculate size_t multiplication with saturation at SIZE_MAX * -- 2.37.1