Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp997514imu; Fri, 11 Jan 2019 13:00:08 -0800 (PST) X-Google-Smtp-Source: ALg8bN4KyEWNRHnfB9vndvOwauzmKQYDXODpbs1DNuUMiaBCam3Fdm90CQuMEDCFTm4rzzS4OT8s X-Received: by 2002:a17:902:2f03:: with SMTP id s3mr15826955plb.277.1547240408545; Fri, 11 Jan 2019 13:00:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547240408; cv=none; d=google.com; s=arc-20160816; b=QPLSZRwotjU1CCSK5tN9zYpPRciKwEHwHRHk4+9oRhrU+Bh7+icPQIKnGReBQE0dGC 1OE6BRsAW/DNEPwaZltD3u/4q2xnxPexaa9aw0M743SFluNRIONR3xe7l+Y3UHrzL/9V gajyIAUZ1s7Gjp1UrpxQHw6T0TTkwfX/5Sn5BkN4bNlxwUhca1q04tCftFqvZ3wsGnpC +GsdOpQhMzCLRM8fW/kYjVHlGpS6CGY74uT6rRLDTXHQvE3kZfoGIXYBBRkxKOk0h3qS FeTTXrx/+7Ync1SiE9IU0xEiRKKTs8BIrNZ9JRL1j+tsZgu1C7kNXtUc+di+4cH/Y6gZ 3VRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language:in-reply-to:mime-version :user-agent:date:message-id:organization:from:references:cc:to :subject:dkim-signature; bh=UgrP4PccczJbQMOEag0r9fAS6vYvSkmF9IVyAiMhvG4=; b=0zZQiXFdbOxdI2XGQZ6MHWe+hhcC0UHm7mxMtrDwG1j8UZSl6uMgai8GUfFPH3IW8L A//r/VdbLQHt6fk5e0+0toC4SEYLNqasUx5MIvhQs5pouDzOd2mA4ZNTLkYREq0PBGNZ cQDZdzsZnZCJnj7lqihrXMAFkHvoaVSobJ9t/CcGep+5xTFsarSWnwcCsiAonK/tA5LJ j0ZOdBtHGoqXtTlZqYrOLgbYMNTrOkVw44IuMQwV7A2nAJBFWJBAwvHfbc2OP4GwWwfj DFvYeP1gDrFtOZAM4NWJO6bbmXhVR0Xr6RwIMOuREX2f+6pQHnPEeK8qD1iGN5Je1EyY u7zg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=ikMwFPCG; 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=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t5si72728010pgc.369.2019.01.11.12.59.53; Fri, 11 Jan 2019 13:00:08 -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=@oracle.com header.s=corp-2018-07-02 header.b=ikMwFPCG; 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=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733159AbfAKSXA (ORCPT + 99 others); Fri, 11 Jan 2019 13:23:00 -0500 Received: from userp2130.oracle.com ([156.151.31.86]:51888 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729711AbfAKSXA (ORCPT ); Fri, 11 Jan 2019 13:23:00 -0500 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.22/8.16.0.22) with SMTP id x0BI95QO001345; Fri, 11 Jan 2019 18:21:13 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=subject : to : cc : references : from : message-id : date : mime-version : in-reply-to : content-type; s=corp-2018-07-02; bh=UgrP4PccczJbQMOEag0r9fAS6vYvSkmF9IVyAiMhvG4=; b=ikMwFPCGN+AuSsUJZiKN/t70SgdmlW3aTkZSgMd7B1uo8+u+x8934LDvlOmxQd35VqQa 8t969CKFeM2TAjIzhBdOeFmjttnbiDXVyr0s502n8HYNBrp3yo+V7N6rwpjhs6pMy/6e K7h2xAUA0mTb6vzRSbGVs2k+jYHeGvrGdLkam9ZFYS11tquL0AvE9g7d/k2VZLfIUJ+s t8YbygfmeXBdf+mTQCMUrkiC7NixmpJC3jrVRCjApfTJmbfvTchOWyhU5TBKBJyUxlWw LkVhmaawVw1FWMt6t0PT/NqopMMSPuhrcxRDr2snu2fp+2zjlrS0SiJfcGgsd+TTaWtO /g== Received: from userv0021.oracle.com (userv0021.oracle.com [156.151.31.71]) by userp2130.oracle.com with ESMTP id 2ptm0upfk9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Jan 2019 18:21:13 +0000 Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by userv0021.oracle.com (8.14.4/8.14.4) with ESMTP id x0BILBZF008260 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Jan 2019 18:21:12 GMT Received: from abhmp0019.oracle.com (abhmp0019.oracle.com [141.146.116.25]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id x0BILAuX026106; Fri, 11 Jan 2019 18:21:11 GMT Received: from [192.168.1.44] (/24.9.64.241) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 11 Jan 2019 10:21:10 -0800 Subject: Re: [RFC PATCH v7 00/16] Add support for eXclusive Page Frame Ownership To: Dave Hansen , juergh@gmail.com, tycho@tycho.ws, jsteckli@amazon.de, ak@linux.intel.com, torvalds@linux-foundation.org, liran.alon@oracle.com, keescook@google.com, konrad.wilk@oracle.com Cc: deepa.srinivasan@oracle.com, chris.hyser@oracle.com, tyhicks@canonical.com, dwmw@amazon.co.uk, andrew.cooper3@citrix.com, jcm@redhat.com, boris.ostrovsky@oracle.com, kanth.ghatraju@oracle.com, joao.m.martins@oracle.com, jmattson@google.com, pradeep.vincent@oracle.com, john.haxby@oracle.com, tglx@linutronix.de, kirill.shutemov@linux.intel.com, hch@lst.de, steven.sistare@oracle.com, kernel-hardening@lists.openwall.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andy Lutomirski , Peter Zijlstra References: <31fe7522-0a59-94c8-663e-049e9ad2bff6@intel.com> From: Khalid Aziz Organization: Oracle Corp Message-ID: <7e3b2c4b-51ff-2027-3a53-8c798c2ca588@oracle.com> Date: Fri, 11 Jan 2019 11:21:04 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: <31fe7522-0a59-94c8-663e-049e9ad2bff6@intel.com> Content-Type: multipart/mixed; boundary="------------CF7DB4497E0E08B56DB0F0CA" Content-Language: en-US X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9133 signatures=668680 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1901110146 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is a multi-part message in MIME format. --------------CF7DB4497E0E08B56DB0F0CA Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hi Dave, Thanks for looking at this and providing feedback. On 1/10/19 4:40 PM, Dave Hansen wrote: > First of all, thanks for picking this back up. It looks to be going in= > a very positive direction! >=20 > On 1/10/19 1:09 PM, Khalid Aziz wrote: >> I implemented a solution to reduce performance penalty and >> that has had large impact. When XPFO code flushes stale TLB entries, >> it does so for all CPUs on the system which may include CPUs that >> may not have any matching TLB entries or may never be scheduled to >> run the userspace task causing TLB flush. > ... >> A rogue process can launch a ret2dir attack only from a CPU that has=20 >> dual mapping for its pages in physmap in its TLB. We can hence defer=20 >> TLB flush on a CPU until a process that would have caused a TLB >> flush is scheduled on that CPU. >=20 > This logic is a bit suspect to me. Imagine a situation where we have > two attacker processes: one which is causing page to go from > kernel->user (and be unmapped from the kernel) and a second process tha= t > *was* accessing that page. >=20 > The second process could easily have the page's old TLB entry. It coul= d > abuse that entry as long as that CPU doesn't context switch > (switch_mm_irqs_off()) or otherwise flush the TLB entry. That is an interesting scenario. Working through this scenario, physmap TLB entry for a page is flushed on the local processor when the page is allocated to userspace, in xpfo_alloc_pages(). When the userspace passes page back into kernel, that page is mapped into kernel space using a va from kmap pool in xpfo_kmap() which can be different for each new mapping of the same page. The physical page is unmapped from kernel on the way back from kernel to userspace by xpfo_kunmap(). So two processes on different CPUs sharing same physical page might not be seeing the same virtual address for that page while they are in the kernel, as long as it is an address from kmap pool. ret2dir attack relies upon being able to craft a predictable virtual address in the kernel physmap for a physical page and redirect execution to that address. Does that sound rig= ht? Now what happens if only one of these cooperating processes allocates the page, places malicious payload on that page and passes the address of this page to the other process which can deduce physmap for the page through /proc and exploit the physmap entry for the page on its CPU. That must be the scenario you are referring to. >=20 > As for where to flush the TLB... As you know, using synchronous IPIs i= s > obviously the most bulletproof from a mitigation perspective. If you > can batch the IPIs, you can get the overhead down, but you need to do > the flushes for a bunch of pages at once, which I think is what you wer= e > exploring but haven't gotten working yet. >=20 > Anything else you do will have *some* reduced mitigation value, which > isn't a deal-breaker (to me at least). Some ideas: Even without batched IPIs working reliably, I was able to measure the performance impact of this partially working solution. With just batched IPIs and no delayed TLB flushes, performance improved by a factor of 2. The 26x system time went down to 12x-13x but it was still too high and a non-starter. Combining batched IPI with delayed TLB flushes improved performance to about 1.1x as opposed to 1.33x with delayed TLB flush alone. Those numbers are very rough since the batching implementation is incomplete. >=20 > Take a look at the SWITCH_TO_KERNEL_CR3 in head_64.S. Every time that > gets called, we've (potentially) just done a user->kernel transition an= d > might benefit from flushing the TLB. We're always doing a CR3 write (o= n > Meltdown-vulnerable hardware) and it can do a full TLB flush based on i= f > X86_CR3_PCID_NOFLUSH_BIT is set. So, when you need a TLB flush, you > would set a bit that ADJUST_KERNEL_CR3 would see on the next > user->kernel transition on *each* CPU. Potentially, multiple TLB > flushes could be coalesced this way. The downside of this is that > you're exposed to the old TLB entries if a flush is needed while you ar= e > already *in* the kernel. >=20 > You could also potentially do this from C code, like in the syscall > entry code, or in sensitive places, like when you're returning from a > guest after a VMEXIT in the kvm code. >=20 Good suggestions. Thanks. I think benefit will be highest from batching TLB flushes. I see a lot of time consumed by full TLB flushes on other processors when local processor did only a limited TLB flush. I will continue to debug the batch TLB updates. -- Khalid --------------CF7DB4497E0E08B56DB0F0CA Content-Type: application/pgp-keys; name="pEpkey.asc" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="pEpkey.asc" -----BEGIN PGP PUBLIC KEY BLOCK----- mQGNBFwdSxMBDACs4wtsihnZ9TVeZBZYPzcj1sl7hz41PYvHKAq8FfBOl4yC6ghp U0FDo3h8R7ze0VGU6n5b+M6fbKvOpIYT1r02cfWsKVtcssCyNhkeeL5A5X9z5vgt QnDDhnDdNQr4GmJVwA9XPvB/Pa4wOMGz9TbepWfhsyPtWsDXjvjFLVScOorPddrL /lFhriUssPrlffmNOMKdxhqGu6saUZN2QBoYjiQnUimfUbM6rs2dcSX4SVeNwl9B 2LfyF3kRxmjk964WCrIp0A2mB7UUOizSvhr5LqzHCXyP0HLgwfRd3s6KNqb2etes FU3bINxNpYvwLCy0xOw4DYcerEyS1AasrTgh2jr3T4wtPcUXBKyObJWxr5sWx3sz /DpkJ9jupI5ZBw7rzbUfoSV3wNc5KBZhmqjSrc8G1mDHcx/B4Rv47LsdihbWkeeB PVzB9QbNqS1tjzuyEAaRpfmYrmGM2/9HNz0p2cOTsk2iXSaObx/EbOZuhAMYu4zH y744QoC+Wf08N5UAEQEA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