Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1273886yba; Thu, 16 May 2019 18:12:01 -0700 (PDT) X-Google-Smtp-Source: APXvYqyX1+jDgFII7YueRn/ceSJ6ZJaiup6c/k2nInhip31xeB/wQBwNf7FMYHz3ZTf8ud0uG3sb X-Received: by 2002:a63:1512:: with SMTP id v18mr2357747pgl.69.1558055521726; Thu, 16 May 2019 18:12:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558055521; cv=none; d=google.com; s=arc-20160816; b=qtCcb5zGWM1pCp90eL1hQPGWD+i3FulZk7bOPB+9iM2ZJbeJicdJ1/q/EngvG+UX7e 2Qi061dqCMbGDqxKgxUn8OQNvqINmrg+lTLCKYX0JM1diLQJtekz/M5Sz8FZ42ihlY+d 4Dnno0j9h/QWI3IKVHX1d8YKB0icqCnHZcZ1rmm0+FcVT06GIws5JdwvBUyctLD9wnEa TbkPG5J5yBT+p6S8LbzhGiLjJmCzPJfnI/j2fBuL8tS7pM/5+RkrMF4F/rX3SE1gA/bo DtWC5uzFQyjbgk929x8HYsYSFz7d+H9GFSXXD5Jf3byZEBSmLzYTeAe605liNhogribr 5PAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=BqheBVs+f04MP1vu1vMWxW4FLSZS6CsuM/U/QdmT2tE=; b=Kqukw3gzxpUIBOc/IIAIKxIhpJMAJKGDkJwKjzeBsMAijtDtneW5IAbl0Fc4kx0+vT h1LAV0LsxYnDXpFnpTgRLGy5jBJ0M1tkkN91RgpNq7ERslW/mxjXaphKSg+v0HFmzwPg gLBMsuRX8ZFI+s7eXWbL9lKNXr9uzcYG0KnUoOCnIISteccWev5hWQnGbRzFICzhtnBd 5ZCaU0U2voI4s9vfWxFZrrnmz7BXPtDHglwVDQ3bRkqp2RPeE9lvpXXtEdKLYflnRPgP kl1MQR9Q2VZQ/XzNBdVHijd+F7TMp6R8gXbeJNKia8+xMD2eZoHpivNGUkfrvQYjqJ9h 9dFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=L1SWhJRY; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b2si6541584pgk.89.2019.05.16.18.11.46; Thu, 16 May 2019 18:12:01 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=L1SWhJRY; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727696AbfEQA0b (ORCPT + 99 others); Thu, 16 May 2019 20:26:31 -0400 Received: from mail.kernel.org ([198.145.29.99]:46148 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727052AbfEQA0a (ORCPT ); Thu, 16 May 2019 20:26:30 -0400 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id CA62321473 for ; Fri, 17 May 2019 00:26:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1558052789; bh=NztWQ67b79un61zbgNfI2N8kygb1psFx3jPAnJ9AZ9M=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=L1SWhJRYZKvea+sEKB8iBRJPEdS25Y/AC4lulser4bJgEe5UBtyCRGQcG6lkCzJto 8ehHfFQz7niGf2b4ANE/gIPmON2kGVQtua26wrVxnijcgNqx4M39criRzFKVGPefEY OBdVuJW8707vwc/N/wejcOT0J3UxPEmr5yw2jkWs= Received: by mail-wm1-f46.google.com with SMTP id t5so3730962wmh.3 for ; Thu, 16 May 2019 17:26:28 -0700 (PDT) X-Gm-Message-State: APjAAAXqz22KQAc6J7o/ibzywfA2q+hswQEe/0L750KEA7gjrSUQrJoU P/qLZMiZCY/RKzBZEFumPOK3QpgekV/bwzPbbhlbQQ== X-Received: by 2002:a1c:4107:: with SMTP id o7mr25806455wma.122.1558052787319; Thu, 16 May 2019 17:26:27 -0700 (PDT) MIME-Version: 1.0 References: <960B34DE67B9E140824F1DCDEC400C0F4E886094@ORSMSX116.amr.corp.intel.com> <6da269d8-7ebb-4177-b6a7-50cc5b435cf4@fortanix.com> <20190513102926.GD8743@linux.intel.com> <20190514104323.GA7591@linux.intel.com> <20190514204527.GC1977@linux.intel.com> <20190515013031.GF1977@linux.intel.com> <20190517000331.GD11204@linux.intel.com> In-Reply-To: <20190517000331.GD11204@linux.intel.com> From: Andy Lutomirski Date: Thu, 16 May 2019 17:26:15 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: SGX vs LSM (Re: [PATCH v20 00/28] Intel SGX1 support) To: Sean Christopherson Cc: Andy Lutomirski , James Morris , "Serge E. Hallyn" , LSM List , Paul Moore , Stephen Smalley , Eric Paris , selinux@vger.kernel.org, Jarkko Sakkinen , Jethro Beekman , "Xing, Cedric" , "Hansen, Dave" , Thomas Gleixner , "Dr. Greg" , Linus Torvalds , LKML , X86 ML , "linux-sgx@vger.kernel.org" , Andrew Morton , "nhorman@redhat.com" , "npmccallum@redhat.com" , "Ayoun, Serge" , "Katz-zamir, Shay" , "Huang, Haitao" , Andy Shevchenko , "Svahn, Kai" , Borislav Petkov , Josh Triplett , "Huang, Kai" , David Rientjes Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 16, 2019 at 5:03 PM Sean Christopherson wrote: > > On Wed, May 15, 2019 at 11:27:04AM -0700, Andy Lutomirski wrote: > > Here's a very vague proposal that's kind of like what I've been > > thinking over the past few days. The SGX inode could track, for each > > page, a "safe-to-execute" bit. When you first open /dev/sgx/enclave, > > you get a blank enclave and all pages are safe-to-execute. When you > > do the ioctl to load context (which could be code, data, or anything > > else), the kernel will check whether the *source* VMA is executable > > and, if not, mark the page of the enclave being loaded as unsafe. > > Once the enclave is initialized, the driver will clear the > > safe-to-execute bit for any page that is successfully mapped writably. > > > > The intent is that a page of the enclave is safe-to-execute if that > > page was populated from executable memory and not modified since then. > > LSMs could then enforce a policy that you can map an enclave page RX > > if the page is safe-to-execute, you can map any page you want for > > write if there are no executable mappings, and you can only map a page > > for write and execute simultaneously if you can EXECMOD permission. > > This should allow an enclave to be loaded by userspace from a file > > with EXECUTE rights. > > I'm still confused as to why you want to track execute permissions on the > enclave pages and add SGX-specific LSM hooks. Is there anything that > prevents userspace from building the enclave like any other DSO and then > copying it into enclave memory? It's entirely possible that I'm the one missing something. But here's why I think this: > I feel like I'm missing something. > > 1. Userspace loads enclave into regular memory, e.g. like a normal DSO. > All mmap(), mprotect(), etc... calls are subject to all existing > LSM policies. > > 2. Userspace opens /dev/sgx/enclave to instantiate a new enclave. > > 3. Userspace uses mmap() to allocate virtual memory for its enclave, > again subject to all existing LSM policies (sane userspaces map it RO > since the permissions eventually get tossed anyways). Is userspace actually requred to mmap() the enclave prior to EADDing things? > > 4. SGX subsystem refuses to service page faults for enclaves that have > not yet been initialized, e.g. signals SIGBUS or SIGSEGV. > > 5. Userspace invokes SGX ioctl() to copy enclave from regulary VMA to > enclave VMA. > > 6. SGX ioctl() propagates VMA protection-related flags from source VMA > to enclave VMA, e.g. invokes mprotect_fixup(). Enclave VMA(s) may > be split as part of this process. Does this also call the LSM? If so, what is it expected to do? What happens if there are different regions with different permissions on the same page? SGX has 256-byte granularity right? > > 7. At all times, mprotect() calls on the enclave VMA are subject to > existing LSM policies, i.e. it's not special cased for enclaves. I don't think the normal behavior actually works here. An enclave is always MAP_SHARED, so (with SELinux) mprotecting() to X or RX requires EXECUTE and mprotecting() to RWX requires extra permissions. But user code can also mmap() the enclave again. What is supposed to happen in that case?