From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C3649C433E1 for ; Thu, 27 Aug 2020 15:21:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A83032087D for ; Thu, 27 Aug 2020 15:21:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728070AbgH0PVE (ORCPT ); Thu, 27 Aug 2020 11:21:04 -0400 Received: from mga05.intel.com ([192.55.52.43]:16015 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728234AbgH0PUz (ORCPT ); Thu, 27 Aug 2020 11:20:55 -0400 IronPort-SDR: 55Yqlb798kOgvMHQZhnXcBcTdeo+Xo1dt6DAcqiTTA9gJTKHgeL2bFtI+NYg5rfBB8jtOfPZiU TBxlyZhVzsTw== X-IronPort-AV: E=McAfee;i="6000,8403,9726"; a="241320086" X-IronPort-AV: E=Sophos;i="5.76,359,1592895600"; d="scan'208";a="241320086" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Aug 2020 08:20:53 -0700 IronPort-SDR: 2ftvRexz4LzBoob2AWoL+hj30qCYj3C4kUREAoXxllKNIiFmgRvAhAGhznSW/hfX99yOfY3vov 1w/1LiuwvBTg== X-IronPort-AV: E=Sophos;i="5.76,359,1592895600"; d="scan'208";a="337196893" Received: from sjchrist-ice.jf.intel.com (HELO sjchrist-ice) ([10.54.31.34]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Aug 2020 08:20:52 -0700 Date: Thu, 27 Aug 2020 08:20:51 -0700 From: Sean Christopherson To: Nathaniel McCallum Cc: Jarkko Sakkinen , 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: <20200827152051.GB22351@sjchrist-ice> References: <20200716135303.276442-1-jarkko.sakkinen@linux.intel.com> <20200716135303.276442-23-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-sgx-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sgx@vger.kernel.org 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; or unsigned long map_type = (flags & MAP_TYPE); if (map_type != MAP_SHARED && map_type != MAP_SHARED_VALIDATE) return -EINVAL; Side topic, there is at least one existing bug of this nature, in mm/nommu.c. I'll send a patch for that and look for any other instances of the bad pattern.