Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1863253pxk; Sun, 13 Sep 2020 19:59:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyvrM/miRHN5GvhsgBt/G7legYQFiy/s/7R+V2ip1PtOCuql1oYM6SlISRLupXjGCixR7Ok X-Received: by 2002:a50:8c24:: with SMTP id p33mr15202366edp.330.1600052363049; Sun, 13 Sep 2020 19:59:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600052363; cv=none; d=google.com; s=arc-20160816; b=az5YLKRcSd9WKC44n6QpbnjpGxJv0F7kUsV3tBQByyiDwa+snpTAJDW7lJwTDDGIEq +gVhb9szUl+vo2MSz7rPUvmb0PAjvrD4vBlA2RI2ysK4tC1NH+SCQNbjCt2GoS1jyhG2 j3bVoJKjAVW0UjZOymtKvWk6X1fGEXrUI7ScJBRvxV5YmNz23XeW/sj7BLepU3uEvh7B 4eH7uF6ojGIrJHb1ZkYQNuU9MeiTzhQsdDDss5aAvXSTghkqkAMgiUrJ9CQDHGbe0wGg rdXuMhw0a/zZO6XipV2ZR8b+wvd66db2jf2c8hjATO7iW7VH+UNS6vegb+h3g7wUKUW3 9BXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to:message-id :organization:from:content-transfer-encoding:mime-version:date :references:subject:cc:to:ironport-sdr:ironport-sdr; bh=CB/zc/gsYSVjHdKG0qOm2zApPwfrIUeqs8LtEFL1x1g=; b=HBvBm/DvwmOmlt/L/ceFrv2m+YULeT3mx48adzwXBVnRmtwR0aUrtn3Y1NqrhC9Lhq zrjQ5QWb/tw+XczXMsg7Dv5xVTMESRTsE1ifE5bnyY3V3I+qZYQCgBOrL1c9rL/cDboo qflNBvNUJtUQI/1kCGBH6tlH3aA478+pQBg38dRq/DdkaDnOrmK2cipXYXIB3cBcmt4V W0pp63sj3v+YnyR8v9PIbPd0PEWOuG/ZVq4/JT/5ZF3CSsTSs0p2lMWkYg2q/Dd6VMZv +83JOZM9guCwZEpFj32fEQnY95E+ifBRTFo0pW91XQDxhAzgxsC2SU/8Z/Jf4YzjyoAc bnFQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ch15si6703017ejb.52.2020.09.13.19.59.00; Sun, 13 Sep 2020 19:59:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726003AbgINC6Y (ORCPT + 99 others); Sun, 13 Sep 2020 22:58:24 -0400 Received: from mga02.intel.com ([134.134.136.20]:52278 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725973AbgINC6X (ORCPT ); Sun, 13 Sep 2020 22:58:23 -0400 IronPort-SDR: WdaufW36xhTT6IPdlA4rhfBE1797fDWts78a0cvstnId1pTOi/uCkMXm1zTpWbX2NvQWvhXRO8 LBSVExqNeDfA== X-IronPort-AV: E=McAfee;i="6000,8403,9743"; a="146711122" X-IronPort-AV: E=Sophos;i="5.76,424,1592895600"; d="scan'208";a="146711122" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Sep 2020 19:58:23 -0700 IronPort-SDR: ZvIZFL/bHVCv8UvIZS+Iob+W9lvFpmkTusX/l6h9f93ONO1KiEMq4AuK9wv8/ddLA0f+1AAAzx 7cJzFJflrOpA== X-IronPort-AV: E=Sophos;i="5.76,424,1592895600"; d="scan'208";a="482166211" Received: from hhuan26-mobl1.amr.corp.intel.com (HELO mqcpg7oapc828.gar.corp.intel.com) ([10.255.35.58]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-SHA; 13 Sep 2020 19:58:19 -0700 Content-Type: text/plain; charset=iso-8859-15; format=flowed; delsp=yes To: x86@kernel.org, linux-sgx@vger.kernel.org, "Jarkko Sakkinen" Cc: linux-kernel@vger.kernel.org, "Jethro Beekman" , "Chunyang Hui" , "Jordan Hand" , "Nathaniel McCallum" , "Seth Moore" , "Darren Kenny" , "Sean Christopherson" , "Suresh Siddha" , akpm@linux-foundation.org, andriy.shevchenko@linux.intel.com, asapek@google.com, bp@alien8.de, cedric.xing@intel.com, chenalexchen@google.com, conradparker@google.com, cyhanish@google.com, dave.hansen@intel.com, haitao.huang@intel.com, josh@joshtriplett.org, kai.huang@intel.com, kai.svahn@intel.com, kmoy@google.com, ludloff@google.com, luto@kernel.org, nhorman@redhat.com, puiterwijk@redhat.com, rientjes@google.com, tglx@linutronix.de, yaozhangx@google.com Subject: Re: [PATCH v37 13/24] x86/sgx: Add SGX_IOC_ENCLAVE_ADD_PAGES References: <20200911124019.42178-1-jarkko.sakkinen@linux.intel.com> <20200911124019.42178-14-jarkko.sakkinen@linux.intel.com> Date: Sun, 13 Sep 2020 21:56:03 -0500 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: "Haitao Huang" Organization: Intel Corp Message-ID: In-Reply-To: <20200911124019.42178-14-jarkko.sakkinen@linux.intel.com> User-Agent: Opera Mail/1.0 (Win32) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 11 Sep 2020 07:40:08 -0500, Jarkko Sakkinen wrote: ... > +/** > + * sgx_ioc_enclave_add_pages() - The handler for > %SGX_IOC_ENCLAVE_ADD_PAGES > + * @encl: an enclave pointer > + * @arg: a user pointer to a struct sgx_enclave_add_pages instance > + * > + * Add one or more pages to an uninitialized enclave, and optionally > extend the > + * measurement with the contents of the page. The SECINFO and > measurement mask > + * are applied to all pages. > + * > + * A SECINFO for a TCS is required to always contain zero permissions > because > + * CPU silently zeros them. Allowing anything else would cause a > mismatch in > + * the measurement. > + * > + * mmap()'s protection bits are capped by the page permissions. For > each page > + * address, the maximum protection bits are computed with the following > + * heuristics: > + * > + * 1. A regular page: PROT_R, PROT_W and PROT_X match the SECINFO > permissions. > + * 2. A TCS page: PROT_R | PROT_W. > + * > + * mmap() is not allowed to surpass the minimum of the maximum > protection bits > + * within the given address range. > + * > + * If ENCLS opcode fails, that effectively means that EPC has been > invalidated. > + * When this happens the enclave is destroyed and -EIO is returned to > the > + * caller. > + * > + * Return: > + * length of the data processed on success, > + * -EACCES if an executable source page is located in a noexec > partition, > + * -EIO if either ENCLS[EADD] or ENCLS[EEXTEND] fails > + * -errno otherwise > + */ > +static long sgx_ioc_enclave_add_pages(struct sgx_encl *encl, void > __user *arg) > +{ > + struct sgx_enclave_add_pages addp; > + struct sgx_secinfo secinfo; > + unsigned long c; > + int ret; > + > + if ((atomic_read(&encl->flags) & SGX_ENCL_INITIALIZED) || > + !(atomic_read(&encl->flags) & SGX_ENCL_CREATED)) > + return -EINVAL; > + > + if (copy_from_user(&addp, arg, sizeof(addp))) > + return -EFAULT; > + > + if (!IS_ALIGNED(addp.offset, PAGE_SIZE) || > + !IS_ALIGNED(addp.src, PAGE_SIZE)) > + return -EINVAL; > + > + if (!(access_ok(addp.src, PAGE_SIZE))) > + return -EFAULT; > + > + if (addp.length & (PAGE_SIZE - 1)) > + return -EINVAL; > + > + if (addp.offset + addp.length - PAGE_SIZE >= encl->size) > + return -EINVAL; > + > + if (copy_from_user(&secinfo, (void __user *)addp.secinfo, > + sizeof(secinfo))) > + return -EFAULT; > + > + if (sgx_validate_secinfo(&secinfo)) > + return -EINVAL; > + > + for (c = 0 ; c < addp.length; c += PAGE_SIZE) { > + if (c == SGX_MAX_ADD_PAGES_LENGTH || signal_pending(current)) { > + ret = c; > + break; > + } > + > + if (need_resched()) > + cond_resched(); > + > + ret = sgx_encl_add_page(encl, addp.src + c, addp.offset + c, > + addp.length - c, &secinfo, addp.flags); no need passing addp.length - c? > + if (ret) > + break; Some error cases here are fatal and should be passed back to user space so that it would not retry. > + } > + > + if (copy_to_user(arg, &addp, sizeof(addp))) > + return -EFAULT; This copy no longer needed? > + return c; > +} > + Thanks Haitao