All of lore.kernel.org
 help / color / mirror / Atom feed
From: "jarkko@kernel.org" <jarkko@kernel.org>
To: Zhiquan Li <zhiquan1.li@intel.com>
Cc: "Huang, Kai" <kai.huang@intel.com>,
	"linux-sgx@vger.kernel.org" <linux-sgx@vger.kernel.org>,
	"Luck, Tony" <tony.luck@intel.com>,
	"Hansen, Dave" <dave.hansen@intel.com>,
	"dave.hansen@linux.intel.com" <dave.hansen@linux.intel.com>,
	"tglx@linutronix.de" <tglx@linutronix.de>,
	"Du, Fan" <fan.du@intel.com>, "Christopherson,,
	Sean" <seanjc@google.com>, "Zhang, Cathy" <cathy.zhang@intel.com>,
	"bp@suse.de" <bp@suse.de>
Subject: Re: [PATCH v9 3/3] x86/sgx: Fine grained SGX MCA behavior for virtualization
Date: Tue, 1 Nov 2022 02:46:00 +0200	[thread overview]
Message-ID: <Y2BsSCM1BWlsqPcK@kernel.org> (raw)
In-Reply-To: <e8f1e391-0362-33b7-aaf6-45a685eba0a4@intel.com>

On Mon, Oct 24, 2022 at 09:32:13AM +0800, Zhiquan Li wrote:
> 
> 
> On 2022/10/24 04:39, jarkko@kernel.org wrote:
> >> As you can see if the EPC page has already been populated at a given index of
> >> one virtual EPC instance, the current fault handler just assumes the mapping is
> >> already there and returns success immediately.  This causes a bug when one
> >> virtual EPC instance is shared by multi processes via fork(): if the EPC page at
> >> one index is already populated by the parent process, when the child accesses
> >> the same page using different virtual address, the fault handler just returns
> >> success w/o actually setting up the mapping for the child, resulting in endless
> >> page fault.
> >>
> >> This needs to be fixed in no matter what way.
> > I think you mean that vm_insert_pfn() does not happen for child because
> > of early return? I did not understand the part about "different virtual
> > addresses", as it is the same mapping.
> > 
> 
> If userspace do something like this, the child will get "different
> virtual address":
> 
>   ... parent run enclave within VM ...
>   if (fork() == 0) {
>       int *caddr = mmap(NULL, 4096, PROT_READ, MAP_SHARED, vepc_fd, 0);
>       printf("child: %d\n", caddr[0]);
>   }
> 
> 
> - "vepc_fd" is inherited from parent which had opened /dev/sgx_vepc.
> - mmap() will create a VMA in child with "different virtual addresses".
> - "caddr[0]" will cause a page fault as it's a new mapping.
> 
> 1. Then kernel will run into the code snippet referenced by Kai.
> 2. The early return 0 will result in sgx_vepc_fault() return
>    "VM_FAULT_NOPAGE".
> 3. This return value will make the condition as true at
>    function do_user_addr_fault()
> 
>    if (likely(!(fault & VM_FAULT_ERROR)))
>        return;
> 
> 4. Since this page fault has not been handled and "$RIP" is still the
>    original value, it will result in the same page fault again.  Namely,
>    it's an endless page fault.
> 
> But the problem is neither the early return in __sgx_vepc_fault() nor
> the return of VM_FAULT_NOPAGE at sgx_vepc_fault().  The root cause has
> been highlighted by Kai, one virtual EPC instance
> can only be mmap()-ed by the process which opens /dev/sgx_vepc.
> 
> In fact, to share a virtual EPC instance in userspace doesn't make any
> sense.  Even though it can be shared by child, the virtual EPC page
> cannot be used by child correctly.

OK, makes sense, thanks for the explanation!

Why would we want to enforce for user space not to do this, even
if it does cause malfunctioning program?

BR, Jarkko

  reply	other threads:[~2022-11-01  0:46 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-20  6:39 [PATCH v9 0/3] x86/sgx: fine grained SGX MCA behavior Zhiquan Li
2022-09-20  6:39 ` [PATCH v9 1/3] x86/sgx: Rename the owner field of struct sgx_epc_page as encl_owner Zhiquan Li
2022-09-20  6:39 ` [PATCH v9 2/3] x86/sgx: Introduce union with vepc_vaddr field for virtualization case Zhiquan Li
2022-10-10 23:10   ` Dave Hansen
2022-10-11  5:49     ` Zhiquan Li
2022-10-11 13:57       ` Dave Hansen
2022-10-12  4:42         ` Zhiquan Li
2022-10-12 11:17           ` Huang, Kai
2022-09-20  6:39 ` [PATCH v9 3/3] x86/sgx: Fine grained SGX MCA behavior for virtualization Zhiquan Li
2022-10-10 23:20   ` Dave Hansen
2022-10-11  4:44     ` Zhiquan Li
2022-10-11 14:04   ` Dave Hansen
2022-10-12  5:09     ` Zhiquan Li
2022-10-12 11:01       ` Huang, Kai
2022-10-12 11:54         ` jarkko
2022-10-12 20:56           ` Huang, Kai
2022-10-13  2:05         ` Zhiquan Li
2022-10-12 14:36       ` Dave Hansen
2022-10-13 14:40         ` Zhiquan Li
2022-10-13 15:39           ` Dave Hansen
2022-10-14  5:42             ` Zhiquan Li
2022-10-14  5:41               ` Dave Hansen
2022-10-13 15:44           ` Dave Hansen
2022-10-13 21:49             ` Huang, Kai
2022-10-13 22:02               ` Dave Hansen
2022-10-13 22:15                 ` Huang, Kai
2022-10-13 22:28                   ` Dave Hansen
2022-10-13 23:40                     ` Huang, Kai
2022-10-13 23:57                       ` Dave Hansen
2022-10-14  0:19                         ` Huang, Kai
2022-10-19 10:59                           ` Huang, Kai
2022-10-23 20:39                             ` jarkko
2022-10-24  1:32                               ` Zhiquan Li
2022-11-01  0:46                                 ` jarkko [this message]
2022-11-02  1:38                                   ` Zhiquan Li
2022-11-07 11:36                                     ` jarkko
2022-11-07 12:19                                       ` Zhiquan Li
2022-11-04 10:17                                   ` Huang, Kai
2022-11-04 16:26                                     ` Sean Christopherson
2022-11-04 16:34                                       ` Dave Hansen
2022-11-07  8:55                                         ` Huang, Kai
2022-11-07  8:54                                       ` Huang, Kai
2022-10-24 22:23                               ` Huang, Kai
2022-11-01  0:53                                 ` jarkko
2022-09-29  8:05 ` [PATCH v9 0/3] x86/sgx: fine grained SGX MCA behavior Zhiquan Li
2022-10-08  2:29 ` Zhiquan Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y2BsSCM1BWlsqPcK@kernel.org \
    --to=jarkko@kernel.org \
    --cc=bp@suse.de \
    --cc=cathy.zhang@intel.com \
    --cc=dave.hansen@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=fan.du@intel.com \
    --cc=kai.huang@intel.com \
    --cc=linux-sgx@vger.kernel.org \
    --cc=seanjc@google.com \
    --cc=tglx@linutronix.de \
    --cc=tony.luck@intel.com \
    --cc=zhiquan1.li@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.