Received: by 2002:a25:1104:0:0:0:0:0 with SMTP id 4csp540922ybr; Fri, 22 May 2020 12:41:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz6nqh5sxkMfYTnCuMtORRpX7FNFBsaB8r6rDd8BteAtU3IncHWu4N6QQKtiUuCeMRkRnkv X-Received: by 2002:a17:907:420e:: with SMTP id oh22mr9224377ejb.320.1590176515626; Fri, 22 May 2020 12:41:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590176515; cv=none; d=google.com; s=arc-20160816; b=EJJNgdPa6VZnz3QSrek4oe+YW/7XeJBGE2JBSUhnOHIzfbBM4cVxFaqGEOCrk4zKYh RsIcUvfeP74zhsw+f39WCTxhjQzqoY2HyO6bEXatdjr0KGwRlQycegp5Zn1NViSDZv/x +g4Pjnr8/pj2uFHoGdFNI4X1hWNMw992iYR4RdSMDzoG2DY4aCEEa7xWIkEnpwVSJP+3 0c7IqWs5N/3GEmgr78ElEChyeQrqM2zMGU+a4S1CPODyZMByQ4x1VEKjg7nPdBs12fuS 60jI4ZCem9MQJ1GnmTOEklChqRoQyHNlthNyl0GEGpQhGQH1uaMdrp6iL4Jh0nT1X8uf uJWg== 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-disposition:mime-version:references:message-id:subject:cc :to:from:date:ironport-sdr:ironport-sdr; bh=vmq+zx8e9YwISTbJLDkTIx1Xx0UfBt8SJOjT8LVDVw4=; b=vQMoA1CJV9yd2nC44p58jPtFAQurFXeyagSlB1JjfNaJbyoUpSP+mdsgmmammThjIy MNpzOXuYW8CQbW3/ldiW0GdejMEr4xiQndAKu5eZVPlZKCezQz/HWAuoZP4DHlZVfDTo gmq4t27yOkTqPElcLa1E/xFOnlJGFmo3ROuSH9oLZfdbtNq1EcYqo+oCThQ4pIh+cE5e N94kWu5TCMSga695Z3zMBzlKVzzMLZxj5fz8t9u3tsPg2yDGum16BQ/SulGBhcr3mPZe dgLOakL7Y2+gpjPMxlxU1sjatXbC0C6yJpa5OhG3NqJBw+d9WfG7pF9qlpat9Xy/aB+T yQ7Q== 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 y43si5743501ede.404.2020.05.22.12.41.32; Fri, 22 May 2020 12:41:55 -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 S1730990AbgEVTjW (ORCPT + 99 others); Fri, 22 May 2020 15:39:22 -0400 Received: from mga17.intel.com ([192.55.52.151]:9245 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730894AbgEVTjV (ORCPT ); Fri, 22 May 2020 15:39:21 -0400 IronPort-SDR: P2Y0hb8PPAMIyFWVmjBjH/RiAjYEW2AmMogTRKNyRHa7o93+TWWF0j4LJIQYD1Uk7YFbWxkNeI a7CCLmW8a7Qg== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 May 2020 12:39:20 -0700 IronPort-SDR: Fd3haaiMcR6epj6Lwf0RAO7Vcu6h11rUPXWFv2XUJk3PogUpO/jRlywthEyJ5C35P2jd0uuh/G 2Z4tRHj71yAg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,422,1583222400"; d="scan'208";a="440966479" Received: from rpurrx-mobl1.ger.corp.intel.com (HELO localhost) ([10.252.58.10]) by orsmga005.jf.intel.com with ESMTP; 22 May 2020 12:39:14 -0700 Date: Fri, 22 May 2020 22:39:14 +0300 From: Jarkko Sakkinen To: Sean Christopherson Cc: linux-kernel@vger.kernel.org, x86@kernel.org, linux-sgx@vger.kernel.org, akpm@linux-foundation.org, dave.hansen@intel.com, nhorman@redhat.com, npmccallum@redhat.com, haitao.huang@intel.com, andriy.shevchenko@linux.intel.com, tglx@linutronix.de, kai.svahn@intel.com, bp@alien8.de, josh@joshtriplett.org, luto@kernel.org, kai.huang@intel.com, rientjes@google.com, cedric.xing@intel.com, puiterwijk@redhat.com, linux-security-module@vger.kernel.org, Suresh Siddha , Jethro Beekman , Haitao Huang , Chunyang Hui , Jordan Hand , Seth Moore Subject: Re: [PATCH v30 10/20] x86/sgx: Linux Enclave Driver Message-ID: <20200522193914.GF10319@linux.intel.com> References: <20200515004410.723949-1-jarkko.sakkinen@linux.intel.com> <20200515004410.723949-11-jarkko.sakkinen@linux.intel.com> <20200521191236.GA23043@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200521191236.GA23043@linux.intel.com> 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, May 21, 2020 at 12:12:36PM -0700, Sean Christopherson wrote: > On Fri, May 15, 2020 at 03:44:00AM +0300, Jarkko Sakkinen wrote: > > +long sgx_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) > > +{ > > + struct sgx_encl *encl = filep->private_data; > > + int ret, encl_flags; > > + > > + encl_flags = atomic_fetch_or(SGX_ENCL_IOCTL, &encl->flags); > > + if (encl_flags & SGX_ENCL_IOCTL) > > + return -EBUSY; > > + > > + if (encl_flags & SGX_ENCL_DEAD) > > + return -EFAULT; > > Returning immediately is wrong as it leaves SGX_ENCL_IOCTL set. This results > in the application seeing -EBUSY on future ioctls() instead of -EFAULT. Can be > fixed as below. Do you want me to send a formal patch on linux-sgx? > > diff --git a/arch/x86/kernel/cpu/sgx/ioctl.c b/arch/x86/kernel/cpu/sgx/ioctl.c > index 77757a74644d..df35a79e915c 100644 > --- a/arch/x86/kernel/cpu/sgx/ioctl.c > +++ b/arch/x86/kernel/cpu/sgx/ioctl.c > @@ -751,8 +751,10 @@ long sgx_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) > if (encl_flags & SGX_ENCL_IOCTL) > return -EBUSY; > > - if (encl_flags & SGX_ENCL_DEAD) > - return -EFAULT; > + if (encl_flags & SGX_ENCL_DEAD) { > + ret = -EFAULT; > + goto out; > + } > > switch (cmd) { > case SGX_IOC_ENCLAVE_CREATE: > @@ -772,6 +774,7 @@ long sgx_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) > break; > } > > +out: > atomic_andnot(SGX_ENCL_IOCTL, &encl->flags); > > return ret; > > > > + > > + switch (cmd) { > > + case SGX_IOC_ENCLAVE_CREATE: > > + ret = sgx_ioc_enclave_create(encl, (void __user *)arg); > > + break; > > + case SGX_IOC_ENCLAVE_ADD_PAGES: > > + ret = sgx_ioc_enclave_add_pages(encl, (void __user *)arg); > > + break; > > + case SGX_IOC_ENCLAVE_INIT: > > + ret = sgx_ioc_enclave_init(encl, (void __user *)arg); > > + break; > > + default: > > + ret = -ENOIOCTLCMD; > > + break; > > + } > > + > > + atomic_andnot(SGX_ENCL_IOCTL, &encl->flags); > > + > > + return ret; > > +} Thanks. Fixed in my tree: v31: * Unset SGX_ENCL_IOCTL in the error path of checking encl->flags in order to prevent leaving it set, and thus block any further ioctl calls. * Added missing cleanup_srcu_struct() call to sgx_encl_release(). * Take encl->lock in sgx_encl_add_page() in order to prevent races with the page reclaimer. /Jarkko