Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1676374pxb; Wed, 9 Feb 2022 01:55:53 -0800 (PST) X-Google-Smtp-Source: ABdhPJyzLiIrsYGtsyUA6yTpFiFEup6yIcwrPJLtpssl0gVyP0SpQN9gLWrQjx6oRoq2Xczuo3m2 X-Received: by 2002:a05:6a00:1892:: with SMTP id x18mr1457416pfh.20.1644400553616; Wed, 09 Feb 2022 01:55:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644400553; cv=none; d=google.com; s=arc-20160816; b=DkQlskr8cEYxxPUTHmv48p6bmidZH3eQlbyjmTfiS81b0EO6v50xz15vUg/ewAS3i9 AbVTnu58bZBVtRZJN3p1zqBWif6cujybQXyeQZ8qfmMX9TLE85g6y06Eul8fEanHEpn5 2a3H3immWAiR/9Ol7qX7IMQRWR0JzUXXAk37t22jKsMMrpph5upXxyYZtYRZPxFZMEVF UCD965nwaD1rX+9ZMeDD4a9CdcNQ3rI18VFBW5KmzvVe6IPrzRJBMQs9Ox94U5q6iZ6a LO+iFODDw8+oaVo9vscSl++lYVtqq2WLM9i3L09Pz1tWM3/miLjrK/siY8WyVEzm92h+ ihrw== 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=B+N2oqjhaxOGFByQfOgmbOF2697w5VbwRY9TB+98u04=; b=ET3VO/HaOtRN0u+AZDpfBdZJ6zvZ/0bT4xpTGge/FglJU6b9OUaDGn0zFhgyNysImP zJNBRCIMWXxI+NMu/+XUiQlBfW84KEH/vP46FHvQ9kiSA4MtluaPUX3M6wvc6C2Ie5u4 Ab1RMiw6ATtAjcFggCJnVqNeJV89Cd4dGvI/QfTG2zstTI682HJ9Jl+rfmxmE/ozHzfM uMA3YpZbuHdme6y+Jms3tSR98qUMY6MHxF03EOjoFEuKAgEQCclYZ+8OdEHWAiyAI2fg flx+obyC/M6YU7P7yHI7ZivnKhxW7wgQBfGHckTmrgjTWTxqaW5B6HuKRyMQD9PCHsu0 4xtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=RM2IQVu6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id r6si3549608plo.333.2022.02.09.01.55.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Feb 2022 01:55:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=RM2IQVu6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B555CE03CDB4; Wed, 9 Feb 2022 01:10:34 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245684AbiBHBKE (ORCPT + 99 others); Mon, 7 Feb 2022 20:10:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344185AbiBHAqO (ORCPT ); Mon, 7 Feb 2022 19:46:14 -0500 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E68B7C0612A4; Mon, 7 Feb 2022 16:46:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1644281173; x=1675817173; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=t+GjrwHa0XMcdV/fjG6z/cisk5uWswmvo+fb9wRZs6g=; b=RM2IQVu6mfyhm1FpgVKEqQ9lqLg1CbxlZdE86blrBMTal5JN/sILC/GS jLMC3xmI2QHeBBvzrzmqI8n/+nM2460ADGDoP/ZUX+NYt4pAt9A/3mzxh uau0NeADKmi/EOksTZVMSbc3mgrl2DJwVP/NqchqHBQfiLQQebmK5ZVYf c9EWZ8Hi4iUssfufBl9/sBDMd42olX7KKNYMWOHowiCqaPpB/9nJQf2Vx qlKuEHz4+op3uUUbHl8fEi1QiIlBINHTtzoeDe6RxbbPG0U7Z8s4D+X0L k28UgtURDcCd/hQ9dkZh7GR3mCTO/NJhCXhde7GqPrPvV0jp5JsWpqok5 Q==; X-IronPort-AV: E=McAfee;i="6200,9189,10251"; a="232407959" X-IronPort-AV: E=Sophos;i="5.88,351,1635231600"; d="scan'208";a="232407959" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Feb 2022 16:46:09 -0800 X-IronPort-AV: E=Sophos;i="5.88,351,1635231600"; d="scan'208";a="499389507" Received: from rchatre-ws.ostc.intel.com ([10.54.69.144]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Feb 2022 16:46:08 -0800 From: Reinette Chatre To: dave.hansen@linux.intel.com, jarkko@kernel.org, tglx@linutronix.de, bp@alien8.de, luto@kernel.org, mingo@redhat.com, linux-sgx@vger.kernel.org, x86@kernel.org Cc: seanjc@google.com, kai.huang@intel.com, cathy.zhang@intel.com, cedric.xing@intel.com, haitao.huang@intel.com, mark.shanahan@intel.com, hpa@zytor.com, linux-kernel@vger.kernel.org Subject: [PATCH V2 13/32] x86/sgx: Create utility to validate user provided offset and length Date: Mon, 7 Feb 2022 16:45:35 -0800 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 User provided offset and length is validated when parsing the parameters of the SGX_IOC_ENCLAVE_ADD_PAGES ioctl(). Extract this validation into a utility that can be used by the SGX2 ioctl()s that will also provide these values. Signed-off-by: Reinette Chatre --- Changes since V1: - New patch arch/x86/kernel/cpu/sgx/ioctl.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/ioctl.c b/arch/x86/kernel/cpu/sgx/ioctl.c index 7e0819a89532..6e7cc441156b 100644 --- a/arch/x86/kernel/cpu/sgx/ioctl.c +++ b/arch/x86/kernel/cpu/sgx/ioctl.c @@ -378,6 +378,26 @@ static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src, return ret; } +/* + * Ensure user provided offset and length values are valid for + * an enclave. + */ +static int sgx_validate_offset_length(struct sgx_encl *encl, + unsigned long offset, + unsigned long length) +{ + if (!IS_ALIGNED(offset, PAGE_SIZE)) + return -EINVAL; + + if (!length || length & (PAGE_SIZE - 1)) + return -EINVAL; + + if (offset + length - PAGE_SIZE >= encl->size) + return -EINVAL; + + return 0; +} + /** * sgx_ioc_enclave_add_pages() - The handler for %SGX_IOC_ENCLAVE_ADD_PAGES * @encl: an enclave pointer @@ -431,14 +451,10 @@ static long sgx_ioc_enclave_add_pages(struct sgx_encl *encl, void __user *arg) if (copy_from_user(&add_arg, arg, sizeof(add_arg))) return -EFAULT; - if (!IS_ALIGNED(add_arg.offset, PAGE_SIZE) || - !IS_ALIGNED(add_arg.src, PAGE_SIZE)) - return -EINVAL; - - if (!add_arg.length || add_arg.length & (PAGE_SIZE - 1)) + if (!IS_ALIGNED(add_arg.src, PAGE_SIZE)) return -EINVAL; - if (add_arg.offset + add_arg.length - PAGE_SIZE >= encl->size) + if (sgx_validate_offset_length(encl, add_arg.offset, add_arg.length)) return -EINVAL; if (copy_from_user(&secinfo, (void __user *)add_arg.secinfo, -- 2.25.1