Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2875068pxk; Tue, 15 Sep 2020 04:51:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwqaLtqEWuJSzWK8BbC2wp11/Ua3OMrtfRhgYI8wRvm3q+ENw4rTfAFrd0EOllN7VcFrFaG X-Received: by 2002:a05:6402:d09:: with SMTP id eb9mr21685708edb.219.1600170697860; Tue, 15 Sep 2020 04:51:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600170697; cv=none; d=google.com; s=arc-20160816; b=pVizWAM5APIdsHTr/ggTkfi2XhUWVWRrf4Ysc5muXyItE4m1DQWaHQ6CP+xfDt0pqr MHpE25Ael8C1qyztFZrNc8OdMtV9sFWOQ/BcTmo0niDGdLPl0XCKsUT1cse0WxZ+IuVx J/MM/QtjSjHb7tiUmoo2dW7h/e0aX5siA/rCxsmMV8aqDJnWfHlWInd1jkHYsayZ80vx JOzjn+/hHyLoFJhkHftYLZauP/M0f+aLg9N0P+gL3CoivL5LwsyFj/7yrVsUl7dJ50gJ dd/IUY9U0jM/FsD7IWui7DDpGVILorYZUpYvbjNdr4c/dovGL6AdQs2BdaS9f6oCpqcc KIZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=kyKBxcBJt11qcWFCIP3GMwrWxClKNYEvGsza00xEyNI=; b=XVyCUKR3VJY53jgA7HTm8NWiedjCgng5+guZ/xyMyPLkdTIvbXfeHrYRS3LVaj4Wyv swdn6/P/7CjkfEflhBsWSoBm92u2pDjmv/T7gDYoI9zkB1aUPxvrDwMsGjeRb+UTVRpb mteRPkHCTsVLGj07Tn3MYqqBwMh/AsLny5jsbZfuKXmNG8zE05aS6vuHqnn+Ya+0mBQP wey+nq0SO1PBPM4sSw7cFmbtWDJy6ZCIJlL6IqASch65+sMpEgNmp0ZWaMtHLHzg2Y6J IKlWX6JNvl+AJ/u3Mc6/qTibw879jmecaZIkckftuYKOVb3/KmBPdmWM+JUAxXZKOEDZ pr/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@kapsi.fi header.s=20161220 header.b=u04mglKB; 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 h4si9131759edv.181.2020.09.15.04.51.14; Tue, 15 Sep 2020 04:51:37 -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; dkim=fail header.i=@kapsi.fi header.s=20161220 header.b=u04mglKB; 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 S1726468AbgIOLob (ORCPT + 99 others); Tue, 15 Sep 2020 07:44:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726348AbgIOLdB (ORCPT ); Tue, 15 Sep 2020 07:33:01 -0400 Received: from mail.kapsi.fi (mail.kapsi.fi [IPv6:2001:67c:1be8::25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10468C0611C1; Tue, 15 Sep 2020 04:28:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=kapsi.fi; s=20161220; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=kyKBxcBJt11qcWFCIP3GMwrWxClKNYEvGsza00xEyNI=; b=u04mglKBRHeT0qPwdR31o+o27D T+Dhn2IF7uK1l1v3V8+pI9dPCt8A7M8DJ32CwKQdlxyqbQK38Wkul67xKg79V3RrVGPq/1AUxjwU+ +Uh8BTbl2qIs9SYOzhpnIbSnkadyKjpHgk8ECrxm2MaUCn0ZI2bHvqbrAYrc66dzzETiQ15ZRPpEC 6h6ti6FeDB2QVXYZU3DOTYtXyBysw3VK42mtXMwWnARtNvgzvLUL+fFKC5qgyXvmnNHYHn+za0F9w Ig7q2mIdMTTisaeKkhlzC4pfkdSXu6AEHVXpUPNfriD1IUD0jOeVEuQJ/dCt6+G1c+QpsRJMkJCCT JlUQdfSA==; Received: from 83-245-197-237.elisa-laajakaista.fi ([83.245.197.237] helo=localhost) by mail.kapsi.fi with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1kI98n-0000m6-D7; Tue, 15 Sep 2020 14:28:53 +0300 From: Jarkko Sakkinen To: x86@kernel.org, linux-sgx@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jarkko Sakkinen , Jethro Beekman , akpm@linux-foundation.org, 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, josh@joshtriplett.org, kai.huang@intel.com, kai.svahn@intel.com, kmoy@google.com, ludloff@google.com, luto@kernel.org, nhorman@redhat.com, npmccallum@redhat.com, puiterwijk@redhat.com, rientjes@google.com, sean.j.christopherson@intel.com, tglx@linutronix.de, yaozhangx@google.com Subject: [PATCH v38 17/24] x86/sgx: ptrace() support for the SGX driver Date: Tue, 15 Sep 2020 14:28:35 +0300 Message-Id: <20200915112842.897265-18-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200915112842.897265-1-jarkko.sakkinen@linux.intel.com> References: <20200915112842.897265-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 83.245.197.237 X-SA-Exim-Mail-From: jjs@kapsi.fi X-SA-Exim-Scanned: No (on mail.kapsi.fi); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add VMA callbacks for ptrace() that can be used with debug enclaves. With debug enclaves data can be read and write the memory word at a time by using ENCLS(EDBGRD) and ENCLS(EDBGWR) leaf instructions. Acked-by: Jethro Beekman Signed-off-by: Jarkko Sakkinen --- arch/x86/kernel/cpu/sgx/encl.c | 87 ++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c index 11ec2df59b54..7f8df2c8ef35 100644 --- a/arch/x86/kernel/cpu/sgx/encl.c +++ b/arch/x86/kernel/cpu/sgx/encl.c @@ -333,10 +333,97 @@ static int sgx_vma_mprotect(struct vm_area_struct *vma, return mprotect_fixup(vma, pprev, start, end, newflags); } +static int sgx_edbgrd(struct sgx_encl *encl, struct sgx_encl_page *page, + unsigned long addr, void *data) +{ + unsigned long offset = addr & ~PAGE_MASK; + int ret; + + + ret = __edbgrd(sgx_get_epc_addr(page->epc_page) + offset, data); + if (ret) + return -EIO; + + return 0; +} + +static int sgx_edbgwr(struct sgx_encl *encl, struct sgx_encl_page *page, + unsigned long addr, void *data) +{ + unsigned long offset = addr & ~PAGE_MASK; + int ret; + + ret = __edbgwr(sgx_get_epc_addr(page->epc_page) + offset, data); + if (ret) + return -EIO; + + return 0; +} + +static int sgx_vma_access(struct vm_area_struct *vma, unsigned long addr, + void *buf, int len, int write) +{ + struct sgx_encl *encl = vma->vm_private_data; + struct sgx_encl_page *entry = NULL; + char data[sizeof(unsigned long)]; + unsigned long align; + unsigned int flags; + int offset; + int cnt; + int ret = 0; + int i; + + /* If process was forked, VMA is still there but vm_private_data is set + * to NULL. + */ + if (!encl) + return -EFAULT; + + flags = atomic_read(&encl->flags); + + if (!(flags & SGX_ENCL_DEBUG) || !(flags & SGX_ENCL_INITIALIZED) || + (flags & SGX_ENCL_DEAD)) + return -EFAULT; + + for (i = 0; i < len; i += cnt) { + entry = sgx_encl_reserve_page(encl, (addr + i) & PAGE_MASK); + if (IS_ERR(entry)) { + ret = PTR_ERR(entry); + break; + } + + align = ALIGN_DOWN(addr + i, sizeof(unsigned long)); + offset = (addr + i) & (sizeof(unsigned long) - 1); + cnt = sizeof(unsigned long) - offset; + cnt = min(cnt, len - i); + + ret = sgx_edbgrd(encl, entry, align, data); + if (ret) + goto out; + + if (write) { + memcpy(data + offset, buf + i, cnt); + ret = sgx_edbgwr(encl, entry, align, data); + if (ret) + goto out; + } else + memcpy(buf + i, data + offset, cnt); + +out: + mutex_unlock(&encl->lock); + + if (ret) + break; + } + + return ret < 0 ? ret : i; +} + const struct vm_operations_struct sgx_vm_ops = { .open = sgx_vma_open, .fault = sgx_vma_fault, .mprotect = sgx_vma_mprotect, + .access = sgx_vma_access, }; /** -- 2.25.1