Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2856414pxk; Sun, 4 Oct 2020 15:05:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwq1USLb+8O8xlK0PGgFhvqYm7XMbadnVbz2j23InRxSKmupVdVB4ZdMiomosKwBYjBKjPH X-Received: by 2002:a17:906:1b15:: with SMTP id o21mr4127625ejg.19.1601849153816; Sun, 04 Oct 2020 15:05:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601849153; cv=none; d=google.com; s=arc-20160816; b=bRCmMoHQ5A+u+mr0JHxnFDQXRThtzCls4c1IkO9BY9KApXkZyEC9PrJ2tAaLSRjDIJ VgtSRSBhRiTSIAwLGamtWVBhqEz75WZp04p0gcKtTkLDxjfF8cviZPfdhoFeUCkXVBps D6bHWVl1G+bZz9zGCUZpkgYvZJ/B3R9sc+D/r57YN0+6mFbb7okFN/v8h7660oHy+ydc yrXQXSZw4kMXEnDXGI931cmc3jqSSTi9uLNYiCVMi/z7iNCUuhgBBz7+Lq6iKaN4l7KV h442VaLpEztW8V0579vjqfZnuMgpC2jYmiyIZGAqgriwWgQ4muAyGbe11WDkKTEJNRVK WLEA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:organization:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :ironport-sdr:ironport-sdr; bh=O6iR1Jt8PNIsP654rX9YNWtMLK1d4pn9/9JIdGqd4jo=; b=rv3Cq7yt56duG+hp3zqTnc8wfl9kqDIp8ssb/aJSDcDhSAYnIpOjMTdzKiiUl3NlbQ G59gPE7q0RLnJ+vOgcpD7Dj+l77x7wF2w/5zFzPYSSqeiG8bQ2CuAH58vAtEHvcnNgC1 AxJdrtV6tkPrrLxZ5xPJlZtGrTfSKnKj5no5cqsnTWRKRiYfEIFhJMxW3TqaLkfEtCsB qZnPpd08Ys+P+JHdJJUAY07swcr/Ou4hhPu36gvG2sLWD5WqzPNprK6pgR4smjNgd62o c2nS0TSrSS9mQ5pGSWfYWSb2GHzu1Lx3NCUFayoZL+P7wTlw1N4hbI69N5JuUfP7xPEj KVzw== 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 r7si3024eji.675.2020.10.04.15.05.30; Sun, 04 Oct 2020 15:05:53 -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 S1726688AbgJDWDM (ORCPT + 99 others); Sun, 4 Oct 2020 18:03:12 -0400 Received: from mga07.intel.com ([134.134.136.100]:14889 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726600AbgJDWDL (ORCPT ); Sun, 4 Oct 2020 18:03:11 -0400 IronPort-SDR: LEJGndDXX/lEU9pKKg6ddc5ARSGn9F0BOWh6xaYQiU/EIEiPT49fQ2t0G3jMbW6jNyObYr6lxs httzdUELN24w== X-IronPort-AV: E=McAfee;i="6000,8403,9764"; a="227444558" X-IronPort-AV: E=Sophos;i="5.77,336,1596524400"; d="scan'208";a="227444558" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Oct 2020 15:03:10 -0700 IronPort-SDR: D8WaEYIYc3W8ZM2k34p1YdK1bF/oWqAJliXPiHexrXnNuPFTPIHAGqmyfqm2ztQtLxq5Y4VJ0M ZUL7HbGi78ow== X-IronPort-AV: E=Sophos;i="5.77,336,1596524400"; d="scan'208";a="459240377" Received: from avahldie-mobl.amr.corp.intel.com (HELO localhost) ([10.249.32.74]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Oct 2020 15:03:02 -0700 Date: Mon, 5 Oct 2020 01:02:59 +0300 From: Jarkko Sakkinen To: Matthew Wilcox Cc: x86@kernel.org, linux-sgx@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Jethro Beekman , Haitao Huang , Chunyang Hui , Jordan Hand , Nathaniel McCallum , Seth Moore , Darren Kenny , Sean Christopherson , Suresh Siddha , andriy.shevchenko@linux.intel.com, asapek@google.com, bp@alien8.de, cedric.xing@intel.com, chenalexchen@google.com, conradparker@google.com, cyhanish@google.com, dave.hansen@intel.com, haitao.huang@intel.com, kai.huang@intel.com, kai.svahn@intel.com, kmoy@google.com, ludloff@google.com, luto@kernel.org, nhorman@redhat.com, puiterwijk@redhat.com, rientjes@google.com, tglx@linutronix.de, yaozhangx@google.com, mikko.ylinen@intel.com Subject: Re: [PATCH v39 11/24] x86/sgx: Add SGX enclave driver Message-ID: <20201004220259.GB43926@linux.intel.com> References: <20201003045059.665934-1-jarkko.sakkinen@linux.intel.com> <20201003045059.665934-12-jarkko.sakkinen@linux.intel.com> <20201003195440.GD20115@casper.infradead.org> <20201004215049.GA43926@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201004215049.GA43926@linux.intel.com> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Oct 05, 2020 at 12:51:00AM +0300, Jarkko Sakkinen wrote: > On Sat, Oct 03, 2020 at 08:54:40PM +0100, Matthew Wilcox wrote: > > On Sat, Oct 03, 2020 at 07:50:46AM +0300, Jarkko Sakkinen wrote: > > > + XA_STATE(xas, &encl->page_array, idx_start); > > > + > > > + /* > > > + * Disallow READ_IMPLIES_EXEC tasks as their VMA permissions might > > > + * conflict with the enclave page permissions. > > > + */ > > > + if (current->personality & READ_IMPLIES_EXEC) > > > + return -EACCES; > > > + > > > + xas_for_each(&xas, page, idx_end) > > > + if (!page || (~page->vm_max_prot_bits & vm_prot_bits)) > > > + return -EACCES; > > > > You're iterating the array without holding any lock that the XArray knows > > about. If you're OK with another thread adding/removing pages behind your > > back, or there's a higher level lock (the mmap_sem?) protecting the XArray > > from being modified while you walk it, then hold the rcu_read_lock() > > while walking the array. Otherwise you can prevent modification by > > calling xas_lock(&xas) and xas_unlock().. > > I backtracked this. The locks have been there from v21-v35. This is a > refactoring mistake in radix_tree to xarray migration happened in v36. > It's by no means intentional. > > What is shoukd take is encl->lock. > > The loop was pre-v36 like: > > idx_start = PFN_DOWN(start); > idx_end = PFN_DOWN(end - 1); > > for (idx = idx_start; idx <= idx_end; ++idx) { > mutex_lock(&encl->lock); > page = radix_tree_lookup(&encl->page_tree, idx); > mutex_unlock(&encl->lock); > > if (!page || (~page->vm_max_prot_bits & vm_prot_bits)) > return -EACCES; > } > > Looking at xarray.h and filemap.c, I'm thinking something along the > lines of: > > for (idx = idx_start; idx <= idx_end; ++idx) { > mutex_lock(&encl->lock); > page = xas_find(&xas, idx + 1); ~~~~~~~ idx > mutex_unlock(&encl->lock); > > if (!page || (~page->vm_max_prot_bits & vm_prot_bits)) > return -EACCES; > } > > Does this look about right? /Jarkko