Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp4034292pxk; Tue, 22 Sep 2020 08:45:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzP6OrnHgGQnjpBvtaxh5t9DF6mPrYA/5gLrYeLX+3QZ5lJi/D1/ykmv+NteKYW8VdW695E X-Received: by 2002:a17:906:cd0d:: with SMTP id oz13mr5594717ejb.212.1600789558180; Tue, 22 Sep 2020 08:45:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600789558; cv=none; d=google.com; s=arc-20160816; b=bxwrtQCZlXnzm+kKjCEd456LU87UwM3CjelF4IQHJuVnUbq4oqbSw9+HPgOAgrQ2GY rytGMbr1uMqYaz/bnRH+iRm61e04phuxgVuzsvYmrwsUEaAJ1BEPDMsDNy/S2POkwqyc 6x7P6ml7OgX2CpvDbdqz4JHkRpQ482lzYCljToOAHIIpYSvjIurc4Tx8wEEUmSreth0l iBFIzlGlVivhAc1bCx8g3Rl7MyLvogVwJxq0e/RyJdoDjCEzOYWfn62gM16PQruEBjF1 L99npivkpqtKImOfN/q1kHj9QwaEsuek9UJSuERQqh/gbMtHA3GBUPE5kPO8PSVLcK/w 3ZHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=ofmUrseAbycCQWoSGsPSBEr7sPcQODqh36PCqwBgOQE=; b=zAckXl/KEQzLp1zKtLqqaf9tVHQ2GL57LxieECSwf4XmkW7qUAawbjMaA0+65jQDg9 rWL791XsUvA3hWZULO1+mes0AQjaC+tw/6lMWNCZochfuF6QNAaYyBx0CDLAuWpjgwjN ayvnI1U9APtjurvMPtPpsdPvm/buMYNA1Qxetp/Drl1Wk76hYKrWGI70GfAmWL12n0C3 C4zOX3jC8s2XSn5vMEZD6XU1E8FpSIScny/gfvVLL8nv0VD7Ryq/8Bfs//CvF0GBiA4L hmUdzhoYB5EIvuptbb/g60UJs+rTrLqWDlHpN//Ha4uO+btk4Mr9Z7yraAXCVRU1m6u/ 18HQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@alien8.de header.s=dkim header.b=ot2mqXZ8; 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=pass (p=NONE sp=NONE dis=NONE) header.from=alien8.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c14si3805656edj.174.2020.09.22.08.45.34; Tue, 22 Sep 2020 08:45:58 -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=pass header.i=@alien8.de header.s=dkim header.b=ot2mqXZ8; 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=pass (p=NONE sp=NONE dis=NONE) header.from=alien8.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726721AbgIVPob (ORCPT + 99 others); Tue, 22 Sep 2020 11:44:31 -0400 Received: from mail.skyhub.de ([5.9.137.197]:45438 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726566AbgIVPob (ORCPT ); Tue, 22 Sep 2020 11:44:31 -0400 Received: from zn.tnic (p200300ec2f0bfb00281bee649a1894f0.dip0.t-ipconnect.de [IPv6:2003:ec:2f0b:fb00:281b:ee64:9a18:94f0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id AB6641EC00EC; Tue, 22 Sep 2020 17:44:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1600789469; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references; bh=ofmUrseAbycCQWoSGsPSBEr7sPcQODqh36PCqwBgOQE=; b=ot2mqXZ8ahJI5pqNoMHX56jTHjhYaeu3jczmnUSz7PsVyifLgzuVMFY0/aPFwYQAevE0NF 9OPHMeuCl227HhPaIdM9Uy0g/9tNSlMM+oadQBgE/9f4/8ycLpwpoNUc7DaEyOuRRGbDjp vN5YXLlyCyVVspH2kmvDVKR+oWCnT94= Date: Tue, 22 Sep 2020 17:44:24 +0200 From: Borislav Petkov To: Jarkko Sakkinen Cc: x86@kernel.org, linux-sgx@vger.kernel.org, linux-kernel@vger.kernel.org, Jethro Beekman , akpm@linux-foundation.org, andriy.shevchenko@linux.intel.com, asapek@google.com, 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: Re: [PATCH v38 17/24] x86/sgx: ptrace() support for the SGX driver Message-ID: <20200922154424.GL22660@zn.tnic> References: <20200915112842.897265-1-jarkko.sakkinen@linux.intel.com> <20200915112842.897265-18-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20200915112842.897265-18-jarkko.sakkinen@linux.intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > Subject: Re: [PATCH v38 17/24] x86/sgx: ptrace() support for the SGX driver ... x86/sgx: Add ptrace() support... subject needs a verb. On Tue, Sep 15, 2020 at 02:28:35PM +0300, Jarkko Sakkinen wrote: > 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 I think you wanna say here "... data can be read and/or written a memory word at a time by using..." > 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; > +} I know those are supposed to correspond to the ENCLS leafs but the function names are totally unreadable. I guess you could name them sgx_encl_dbg_read sgx_encl_dbg_write and leave the lowlevel helpers like the insn names. > +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. > + */ Kernel comments style is: /* * A sentence ending with a full-stop. * Another sentence. ... * More sentences. ... */ > + 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 } else { > + memcpy(buf + i, data + offset, cnt); } Put the else branch in {} too. -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette