Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3474079imu; Mon, 17 Dec 2018 21:56:30 -0800 (PST) X-Google-Smtp-Source: AFSGD/UnR9WkJXWUOKIzhQ/HW5KIwxe9bsaG5uIip8FTbIpCs9J5kzH1OuMHc7cJ4TyDKOcrLjIz X-Received: by 2002:a63:ea4f:: with SMTP id l15mr14388671pgk.102.1545112590938; Mon, 17 Dec 2018 21:56:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545112590; cv=none; d=google.com; s=arc-20160816; b=suljIeDCC8pSO/fzQPuH3ud5svZp/xK9CI2tdIGcV0AhJt/Id1BplMzz9AqDamOaTt dbWxRwZS9ihNMbb35h8a0Z0La0IK3LPcbZ7ZD8auzAYcHR+xrDZz0mE3VTSBAGq1vOx9 s0OiM/gPa5SvNkub8/AIMlmcfyWV0hS6U2cMkIKxNItm9scGNZ6E5bBIBfv0nRgqU8je lIZpromgP2tdGYsBUT1AB6GC3go/n4MLABuOxlIImPyswW0zpBInxQaQMicrUdiG27QE f3bHgItYoaEEXm3HCze8lJyyMNCJvMZR8XyRaHM2P0NeYPW0o6ND/p2qi6ivoHiztV6K 7O1g== 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=QQtGtKyaRa+kASyH48XCIIj0NARo6yvaw/OtMWN79qQ=; b=oAzqADagP6rvy8S4ThrkZRgv+1QD275DpJkiq9SGZz84p1hEzaaRWfhQ2d4ClVzeoE aiM6FADb3o4FVN0Gca1nZhIxQJsJjSo3x21X+/hPBOuMlsvgqs8csY+R4hDDd6I4LCdd U2tvuQAMESI2O/K2rW/SOthb450p84ePsBF+8Cg7kWW2JIGIBt9kk1yts4g6SJGAFsjX rJ7T/stBndZcNkufv0F5QXkeYzo5NYcfRDzJfcCMa9/fZqTIrsrjWjOZCI19artQ7Bo/ 8qKenmaGW8+ZO6qKUf188Tt6peWs9GVHLG7VZoGk/c+xbUHpk8GnfLvpVxlxLyWTKUfL atqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=l9J1q18f; 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 o3si12407412pll.201.2018.12.17.21.56.15; Mon, 17 Dec 2018 21:56:30 -0800 (PST) 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=l9J1q18f; 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 S1726368AbeLRFzZ (ORCPT + 99 others); Tue, 18 Dec 2018 00:55:25 -0500 Received: from mail.kernel.org ([198.145.29.99]:57286 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726314AbeLRFzZ (ORCPT ); Tue, 18 Dec 2018 00:55:25 -0500 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (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 6F0582133F for ; Tue, 18 Dec 2018 05:55:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1545112523; bh=RO+qVZqc56lHf609ezk3owYaE70TiFxtMxpkPgvQVRo=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=l9J1q18fQmFnvw+2Ia5V6jotnOcEXKeFBjejk0uCsnXOwQT9oCaCQhQDydVvPssR4 P/rPc/SACe1h+rvTnITK7F9ytJhpOLXE2gFpYUWn8DwUgBW4WkFoZlmLJgvUyVaYqy o6y9OZipAHfxPax4p5ZCiaDeC6HDrq7Ex7sfjRFE= Received: by mail-wr1-f48.google.com with SMTP id j2so14597210wrw.1 for ; Mon, 17 Dec 2018 21:55:23 -0800 (PST) X-Gm-Message-State: AA+aEWZnTYJklLTbUOefSBgdahgDNGz/U74G5MkZZoGjBSMJU4ECQZss GMhZ6pc9Big/jfWQZnlVTTrabSZ5fa0QOFTGzZrlew== X-Received: by 2002:adf:e08c:: with SMTP id c12mr12348514wri.199.1545112520053; Mon, 17 Dec 2018 21:55:20 -0800 (PST) MIME-Version: 1.0 References: <20181116010412.23967-1-jarkko.sakkinen@linux.intel.com> <20181116010412.23967-19-jarkko.sakkinen@linux.intel.com> In-Reply-To: <20181116010412.23967-19-jarkko.sakkinen@linux.intel.com> From: Andy Lutomirski Date: Mon, 17 Dec 2018 21:55:08 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v17 18/23] platform/x86: Intel SGX driver To: Jarkko Sakkinen Cc: X86 ML , Platform Driver , linux-sgx@vger.kernel.org, Dave Hansen , "Christopherson, Sean J" , nhorman@redhat.com, npmccallum@redhat.com, "Ayoun, Serge" , shay.katz-zamir@intel.com, Haitao Huang , Andy Shevchenko , Thomas Gleixner , "Svahn, Kai" , mark.shanahan@intel.com, Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Darren Hart , Andy Shevchenko , "open list:X86 ARCHITECTURE (32-BIT AND 64-BIT)" 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, Nov 15, 2018 at 5:08 PM Jarkko Sakkinen wrote: > > Intel Software Guard eXtensions (SGX) is a set of CPU instructions that > can be used by applications to set aside private regions of code and > data. The code outside the enclave is disallowed to access the memory > inside the enclave by the CPU access control. This is a very partial review. > +int sgx_encl_find(struct mm_struct *mm, unsigned long addr, > + struct vm_area_struct **vma) > +{ > + struct vm_area_struct *result; > + struct sgx_encl *encl; > + > + result = find_vma(mm, addr); > + if (!result || result->vm_ops != &sgx_vm_ops || addr < result->vm_start) > + return -EINVAL; > + > + encl = result->vm_private_data; > + *vma = result; > + > + return encl ? 0 : -ENOENT; > +} I realize that this function may go away entirely but, if you keep it: what are the locking rules? What, if anything, prevents another thread from destroying the enclave after sgx_encl_find() returns? > +static int sgx_validate_secs(const struct sgx_secs *secs, > + unsigned long ssaframesize) > +{ ... > + if (secs->attributes & SGX_ATTR_MODE64BIT) { > + if (secs->size > sgx_encl_size_max_64) > + return -EINVAL; > + } else { > + /* On 64-bit architecture allow 32-bit encls only in > + * the compatibility mode. > + */ > + if (!test_thread_flag(TIF_ADDR32)) > + return -EINVAL; > + if (secs->size > sgx_encl_size_max_32) > + return -EINVAL; > + } Why do we need the 32-bit-on-64-bit check? In general, anything that checks per-task or per-mm flags like TIF_ADDR32 is IMO likely to be problematic. You're allowing 64-bit enclaves in 32-bit tasks, so I'm guessing you could just delete the check. > + > + if (!(secs->xfrm & XFEATURE_MASK_FP) || > + !(secs->xfrm & XFEATURE_MASK_SSE) || > + (((secs->xfrm >> XFEATURE_BNDREGS) & 1) != > + ((secs->xfrm >> XFEATURE_BNDCSR) & 1)) || > + (secs->xfrm & ~sgx_xfrm_mask)) > + return -EINVAL; Do we need to check that the enclave doesn't use xfeatures that the kernel doesn't know about? Or are they all safe by design in enclave mode? > +static int sgx_encl_pm_notifier(struct notifier_block *nb, > + unsigned long action, void *data) > +{ > + struct sgx_encl *encl = container_of(nb, struct sgx_encl, pm_notifier); > + > + if (action != PM_SUSPEND_PREPARE && action != PM_HIBERNATION_PREPARE) > + return NOTIFY_DONE; Hmm. There's an argument to made that omitting this would better exercise the code that handles fully asynchronous loss of an enclave. Also, I think you're unnecessarily killing enclaves when suspend is attempted but fails. > + > +static int sgx_get_key_hash(const void *modulus, void *hash) > +{ > + struct crypto_shash *tfm; > + int ret; > + > + tfm = crypto_alloc_shash("sha256", 0, CRYPTO_ALG_ASYNC); > + if (IS_ERR(tfm)) > + return PTR_ERR(tfm); > + > + ret = __sgx_get_key_hash(tfm, modulus, hash); > + > + crypto_free_shash(tfm); > + return ret; > +} > + I'm so sorry you had to deal with this API. Once Zinc lands, you could clean this up :) > +static int sgx_encl_get(unsigned long addr, struct sgx_encl **encl) > +{ > + struct mm_struct *mm = current->mm; > + struct vm_area_struct *vma; > + int ret; > + > + if (addr & (PAGE_SIZE - 1)) > + return -EINVAL; > + > + down_read(&mm->mmap_sem); > + > + ret = sgx_encl_find(mm, addr, &vma); > + if (!ret) { > + *encl = vma->vm_private_data; > + > + if ((*encl)->flags & SGX_ENCL_SUSPEND) > + ret = SGX_POWER_LOST_ENCLAVE; > + else > + kref_get(&(*encl)->refcount); > + } Hmm. This version has explicit refcounting. > +static int sgx_mmap(struct file *file, struct vm_area_struct *vma) > +{ > + vma->vm_ops = &sgx_vm_ops; > + vma->vm_flags |= VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP | VM_IO | > + VM_DONTCOPY; > + > + return 0; > +} > + > +static unsigned long sgx_get_unmapped_area(struct file *file, > + unsigned long addr, > + unsigned long len, > + unsigned long pgoff, > + unsigned long flags) > +{ > + if (len < 2 * PAGE_SIZE || (len & (len - 1))) > + return -EINVAL; > + > + if (len > sgx_encl_size_max_64) > + return -EINVAL; > + > + if (len > sgx_encl_size_max_32 && test_thread_flag(TIF_ADDR32)) > + return -EINVAL; Generally speaking, this type of check wants to be in_compat_syscall(). But I'm not sure I understand why you need it at all. > +static void sgx_ipi_cb(void *info) > +{ > +} > + > +void sgx_flush_cpus(struct sgx_encl *encl) > +{ > + on_each_cpu_mask(mm_cpumask(encl->mm), sgx_ipi_cb, NULL, 1); > +} Please add a comment explaining what this promises to do.