Received: by 2002:a05:6a10:6006:0:0:0:0 with SMTP id w6csp1553840pxa; Fri, 28 Aug 2020 16:28:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxJ+LZQP60BZ8RM++4ooNZ70JQ59jQNB9A/xWe1z1m7R53cuiMYfe7Fwq7+Joywi0KnDp1l X-Received: by 2002:a17:906:d144:: with SMTP id br4mr1090576ejb.385.1598657297328; Fri, 28 Aug 2020 16:28:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598657297; cv=none; d=google.com; s=arc-20160816; b=GDWbB5BEwERWMKVfh81rU6UeeMbJlbq/FHqsVLWGt10MXsgLq6oCFIn9Inf7siYGJA lwOBpnJo370+O0Qj5KIk2RjGU2F/+4jOxmDK5XLeEwoRfhp4foInbeMIk6oAlpofzUo+ 0ufX+aopkcY185GigQ6sO+/5PmWHIvCftXgzhtKURV2x4ETmUarFk/OTCcba4VUDjMTn 5QjAyUiKTrj1Ll3pEFYvYp9O+V7i6LoCt+CdIUEif0Z/WKFpupp1PKUwrJ5NRwhQtXXJ YrYP4YmHYBnqmSAVTNuoi6KW1oHGHB6IILrPY5jV2nYRnQhC+hr4ErUyROw2cIfjFXE8 1cxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:organization:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:ironport-sdr :ironport-sdr; bh=pActyeNP022BKMLhOTHzjrSv7lP6FfIS3UFAd8w4kCs=; b=1Glzm7HPge4H4IMGDId538IdIbpREAGRS+B4/Ci85uFESN4Lj5DzsCE+R1ffLnsVSp aSfKFabIVwBTKZNzELhrLdzfgl67ibHa+v+gnXGrRxw/HGxbeXBR/CfSLhBIIbZmD7tU qN22F7Dk8N6CTLM4xIeoX0P0rXWSldEUH5YPoflM7V46RlA/jy6F/rX2u4bYFaTbHcb3 sphgaXixMrdzzPXLACzr5NLPBJdfrQBDdL3tuzApi4RhYXPhARxGgYVIY+v9UXzkY7Di Jg/LX0jKsQMcs4X9+ZEbGMZotseXiLlnPo/sSc7CWgucAXE/tYTMGajEg6vaUadZtF22 Etqw== 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 bx22si750781ejc.239.2020.08.28.16.27.53; Fri, 28 Aug 2020 16:28:17 -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 S1726881AbgH1X1R (ORCPT + 99 others); Fri, 28 Aug 2020 19:27:17 -0400 Received: from mga05.intel.com ([192.55.52.43]:30377 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726386AbgH1X1O (ORCPT ); Fri, 28 Aug 2020 19:27:14 -0400 IronPort-SDR: 8zMilxar9NaNnNFJObuGvuorTYaF2zMK+TvZbw+n/Io+uMbWz0ggeah890dbJ5P6mKRXq1A0Hz FbIglHuwD/7A== X-IronPort-AV: E=McAfee;i="6000,8403,9727"; a="241577558" X-IronPort-AV: E=Sophos;i="5.76,365,1592895600"; d="scan'208";a="241577558" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Aug 2020 16:27:13 -0700 IronPort-SDR: aFUo5/ueJR9F9AkbOVEeok/6ZDWjzZuQWWMWoNddiPznRojLznEO7CkhtRrf9DqVg2cf22cmVs IqQ9/8dU9qog== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.76,365,1592895600"; d="scan'208";a="374214094" Received: from faerberc-mobl2.ger.corp.intel.com (HELO localhost) ([10.249.36.74]) by orsmga001.jf.intel.com with ESMTP; 28 Aug 2020 16:27:07 -0700 Date: Sat, 29 Aug 2020 02:27:06 +0300 From: Jarkko Sakkinen To: Sean Christopherson Cc: Nathaniel McCallum , X86 ML , linux-sgx@vger.kernel.org, LKML , linux-kselftest@vger.kernel.org, Andrew Morton , Andy Shevchenko , asapek@google.com, Borislav Petkov , "Xing, Cedric" , chenalexchen@google.com, Conrad Parker , cyhanish@google.com, Dave Hansen , "Huang, Haitao" , Josh Triplett , "Huang, Kai" , "Svahn, Kai" , Keith Moyer , Christian Ludloff , Andy Lutomirski , Neil Horman , Patrick Uiterwijk , David Rientjes , Thomas Gleixner , yaozhangx@google.com Subject: Re: [PATCH v36 22/24] selftests/x86: Add a selftest for SGX Message-ID: <20200828232706.GB20705@linux.intel.com> References: <20200716135303.276442-1-jarkko.sakkinen@linux.intel.com> <20200716135303.276442-23-jarkko.sakkinen@linux.intel.com> <20200827152051.GB22351@sjchrist-ice> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20200827152051.GB22351@sjchrist-ice> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Aug 27, 2020 at 08:20:51AM -0700, Sean Christopherson wrote: > On Thu, Aug 27, 2020 at 12:47:04AM -0400, Nathaniel McCallum wrote: > > > +int main(int argc, char *argv[], char *envp[]) > > > +{ > > > + struct sgx_enclave_exception exception; > > > + struct vdso_symtab symtab; > > > + Elf64_Sym *eenter_sym; > > > + uint64_t result = 0; > > > + struct encl encl; > > > + unsigned int i; > > > + void *addr; > > > + > > > + if (!encl_load("test_encl.elf", &encl)) > > > + goto err; > > > + > > > + if (!encl_measure(&encl)) > > > + goto err; > > > + > > > + if (!encl_build(&encl)) > > > + goto err; > > > + > > > + /* > > > + * An enclave consumer only must do this. > > > + */ > > > + for (i = 0; i < encl.nr_segments; i++) { > > > + struct encl_segment *seg = &encl.segment_tbl[i]; > > > + > > > + addr = mmap((void *)encl.encl_base + seg->offset, seg->size, > > > + seg->prot, MAP_SHARED | MAP_FIXED, encl.fd, 0); > > > > My patch version is a bit behind (v32), but I suspect this still > > applies. I discovered the following by accident. > > > > In the Enarx code base, this invocation succeeds: > > mmap(0x200000000000, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | > > MAP_FIXED, sgxfd, 0) > > > > However, this one fails with -EINVAL: > > mmap(0x200000000000, 0x1000, PROT_READ | PROT_WRITE, > > MAP_SHARED_VALIDATE | MAP_FIXED, sgxfd, 0) > > > > From man mmap: > > > > MAP_SHARED_VALIDATE (since Linux 4.15) > > This flag provides the same behavior as MAP_SHARED > > except that MAP_SHARED mappings ignore unknown > > flags in flags. By contrast, when creating a mapping > > using MAP_SHARED_VALIDATE, the kernel veri‐ > > fies all passed flags are known and fails the > > mapping with the error EOPNOTSUPP for unknown > > flags. This mapping type is also required to be able to > > use some mapping flags (e.g., MAP_SYNC). > > > > I can try again on a newer patch set tomorrow if need be. But the > > documentation of mmap() doesn't match the behavior I'm seeing. A brief > > look through the patch set didn't turn up anything obvious that could > > be causing this. > > This is a bug in sgx_get_unmapped_area(). EPC must be mapped SHARED, and > so MAP_PRIVATE is disallowed. The current check is: > > if (flags & MAP_PRIVATE) > return -EINVAL; > > and the base "flags" are: > > #define MAP_SHARED 0x01 /* Share changes */ > #define MAP_PRIVATE 0x02 /* Changes are private */ > #define MAP_SHARED_VALIDATE 0x03 /* share + validate extension flags */ > > which causes the SGX check to interpret MAP_SHARED_VALIDATE as MAP_PRIVATE. > The types are just that, types, not flag modifiers. So the SGX code needs > to be: > > if ((flags & MAP_TYPE) == MAP_PRIVATE) > return -EINVAL; Updated, thanks. /Jarkko