From nobody Sat Apr 27 10:06:34 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; arc=fail (BodyHash is different from the expected one) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1567519374575800.3248194831108; Tue, 3 Sep 2019 07:02:54 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1i59NE-0006MJ-Hr; Tue, 03 Sep 2019 14:01:32 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1i59NC-0006MD-H2 for xen-devel@lists.xenproject.org; Tue, 03 Sep 2019 14:01:30 +0000 Received: from EUR02-AM5-obe.outbound.protection.outlook.com (unknown [40.107.0.113]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 53248532-ce53-11e9-a337-bc764e2007e4; Tue, 03 Sep 2019 14:01:29 +0000 (UTC) Received: from AM7PR02MB5750.eurprd02.prod.outlook.com (10.141.171.8) by AM7PR02MB5798.eurprd02.prod.outlook.com (10.141.172.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2220.20; Tue, 3 Sep 2019 14:01:27 +0000 Received: from AM7PR02MB5750.eurprd02.prod.outlook.com ([fe80::d422:432c:51e0:2d74]) by AM7PR02MB5750.eurprd02.prod.outlook.com ([fe80::d422:432c:51e0:2d74%4]) with mapi id 15.20.2220.022; Tue, 3 Sep 2019 14:01:27 +0000 X-Inumbo-ID: 53248532-ce53-11e9-a337-bc764e2007e4 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cuMlOtp4JN66Ni01riGItnD2GYmX5jpqHQ1+ph/ZJfhM3YhKVT0bBUmtxLV3Ez/JHg5amJlvWpNfaey8XflYZn5kwEHOPnbXwCe9f/8fum7u7gtbVzNVuN9sfpfZsFU8tyXzoph2tZvnXTW/MnsZew91nI5iFTu/HqhfWgeRypfxC3myDY1urpXWAs23QttaGmgv8rhk/jwE+2SborPAAiW7Sv92nqN449wAYsAPsrf8V4THYmK0PL79uwonIh4Tp1G36i1q1PA4vGdKfhx68/ZZtpUeJghKfKP0lbOhGxeanxU1HHUpw2lpfZkohtQXnA6cpla0TQ6dF/1mZ/E+ug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ag1IajAbK/Hqo4QKFbVnw7TQS2mbl3pys1dNBpzEy6k=; b=CTx6i/mQWqxLy+J4/xrUa6WE1aghM7SZcCC6G3MdAYKF6u3kGd9QEZegKcDMHXyPUqLd2pTEIc+gWwoYpiOuwZlQMFjPDz1LJqX214fvhC8SFZskj3XvdLyg4006d44VWFO/kOFMnkwZoCY9n09GEVRdpm+rAo7erYoOlMRDzQbKyQSvPoGFk/GpPr/IYoPw0aEekJQGea1UV4hpCdU6UEA9g2OSKs6KV+BW+dU/onpN7w4Sqfx83jszdnsbAZfnaXRscdComRBXvPpmdYb3RgTbLbS6S2+ETFAMJ26yGRZosPe3IbH+ZPqTIcRwhBlmEgsj1vxl5Lj2lB9tlEvSiQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=bitdefender.com; dmarc=pass action=none header.from=bitdefender.com; dkim=pass header.d=bitdefender.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bitdefender.onmicrosoft.com; s=selector2-bitdefender-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ag1IajAbK/Hqo4QKFbVnw7TQS2mbl3pys1dNBpzEy6k=; b=rVXJ7QxsumaghmvPk6Mz5oKtlT4r88XK5/ekN78tFzvBjwLMywjJRne0JRG+JHCmH9cd1m0EE5BLx6GLuBD033pHhEYcChP+yMSWYtYTGMWxmNBpyQk8lQ44SONsFNGDyEq6g5J1arjh6YSoU5PwzDjMRjJaN7KyO5AqkuRcyEU= From: Alexandru Stefan ISAILA To: "xen-devel@lists.xenproject.org" Thread-Topic: [PATCH v8] x86/emulate: Send vm_event from emulate Thread-Index: AQHVYmAUwzAuH5cfgUOJUZVAs8B/2Q== Date: Tue, 3 Sep 2019 14:01:27 +0000 Message-ID: <20190903140112.18806-1-aisaila@bitdefender.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: PR2P264CA0020.FRAP264.PROD.OUTLOOK.COM (2603:10a6:101::32) To AM7PR02MB5750.eurprd02.prod.outlook.com (2603:10a6:20b:102::8) authentication-results: spf=none (sender IP is ) smtp.mailfrom=aisaila@bitdefender.com; x-ms-exchange-messagesentrepresentingtype: 1 x-mailer: git-send-email 2.17.1 x-originating-ip: [91.199.104.6] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 02fdcc14-076d-497d-3d8e-08d73077367f x-microsoft-antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600166)(711020)(4605104)(1401327)(2017052603328)(7193020); SRVR:AM7PR02MB5798; x-ms-traffictypediagnostic: AM7PR02MB5798:|AM7PR02MB5798:|AM7PR02MB5798: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:9508; x-forefront-prvs: 01494FA7F7 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(366004)(136003)(376002)(396003)(39860400002)(346002)(199004)(189003)(50226002)(478600001)(2501003)(99286004)(52116002)(316002)(6116002)(64756008)(66446008)(71200400001)(66556008)(25786009)(66946007)(14454004)(4326008)(6486002)(5660300002)(66066001)(6916009)(86362001)(71190400001)(66476007)(1076003)(54906003)(102836004)(386003)(6506007)(53936002)(305945005)(5640700003)(6436002)(6512007)(2906002)(256004)(14444005)(26005)(8676002)(476003)(81156014)(186003)(2351001)(486006)(81166006)(2616005)(36756003)(8936002)(3846002)(7736002)(309714004); DIR:OUT; SFP:1102; SCL:1; SRVR:AM7PR02MB5798; H:AM7PR02MB5750.eurprd02.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; Received-SPF: none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; received-spf: None (protection.outlook.com: bitdefender.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: uAtql9Tgh/ZK3/HyDQql6JgEYFaE4ufjUxUx37xHm7i+Vok+ezCyVjDrtfGTRTkpjOEhiY9vciD6Rxnn2MDujsyHc7zk/3JfgJOJtwyaP4si0TxgtO/lqTic+/FeWrZI200LN/sgRQ2CjuQeIBrE+dWXC31wvMYpwq23Xt5IVAkq4UchNa6riooBFdsor/DgBEIfU3r9rtZiklQnW0HRWtqgCltp9w5pPIPO9F+vCVGUe/oz/snJCKobp4EWVqFhaHN2aFke99/oJ7HkJ8vA2opNifoEIFKk9CAsHkdyQjKPa4LBm7OJXyY/gtaGf1fXlGPFfR3eIYQHx/k9g4jWraC7M1o8v3BleA1vNUiEjmKE6FSPz4Pfr5ecYznQsMM21BrHLjJCYAP4hYNYNmdbDdWfq/h6OMmA9V7Ti5E7mYc= MIME-Version: 1.0 X-OriginatorOrg: bitdefender.com X-MS-Exchange-CrossTenant-Network-Message-Id: 02fdcc14-076d-497d-3d8e-08d73077367f X-MS-Exchange-CrossTenant-originalarrivaltime: 03 Sep 2019 14:01:27.1125 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 487baf29-f1da-469a-9221-243f830c36f3 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: osMnAVoIzutbY2XRFmre5kQKsa0M55XohVhRlKeUk+hPFJ/mTVz8NDCZZJqSKSpmmoUT51o9DykvCJD3deQY+CtIBfNMT5+bJp9QOvzMcI4= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM7PR02MB5798 Subject: [Xen-devel] [PATCH v8] x86/emulate: Send vm_event from emulate X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Petre Ovidiu PIRCALABU , "tamas@tklengyel.com" , "wl@xen.org" , Razvan COJOCARU , "george.dunlap@eu.citrix.com" , "andrew.cooper3@citrix.com" , "paul.durrant@citrix.com" , "jbeulich@suse.com" , Alexandru Stefan ISAILA , "roger.pau@citrix.com" Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) A/D bit writes (on page walks) can be considered benign by an introspection agent, so receiving vm_events for them is a pessimization. We try here to optimize by filtering these events out. Currently, we are fully emulating the instruction at RIP when the hardware = sees an EPT fault with npfec.kind !=3D npfec_kind_with_gla. This is, however, incorrect, because the instruction at RIP might legitimately cause an EPT fault of its own while accessing a _different_ page from the original o= ne, where A/D were set. The solution is to perform the whole emulation, while ignoring EPT restrict= ions for the walk part, and taking them into account for the "actual" emulating = of the instruction at RIP. When we send out a vm_event, we don't want the emul= ation to complete, since in that case we won't be able to veto whatever it is doi= ng. That would mean that we can't actually prevent any malicious activity, inst= ead we'd only be able to report on it. When we see a "send-vm_event" case while emulating, we need to first send t= he event out and then suspend the emulation (return X86EMUL_RETRY). After the emulation stops we'll call hvm_vm_event_do_resume() again after t= he introspection agent treats the event and resumes the guest. There, the instruction at RIP will be fully emulated (with the EPT ignored) if the introspection application allows it, and the guest will continue to run past the instruction. A common example is if the hardware exits because of an EPT fault caused by= a page walk, p2m_mem_access_check() decides if it is going to send a vm_event. If the vm_event was sent and it would be treated so it runs the instruction at RIP, that instruction might also hit a protected page and provoke a vm_e= vent. Now if npfec.kind !=3D npfec_kind_with_gla and d->arch.monitor.inguest_page= fault_disabled is true then we are in the page walk case and we can do this emulation opti= mization and emulate the page walk while ignoring the EPT, but don't ignore the EPT = for the emulation of the actual instruction. In the first case we would have 2 EPT events, in the second case we would h= ave 1 EPT event if the instruction at the RIP triggers an EPT event. We use hvmemul_map_linear_addr() to intercept r/w access and __hvm_copy() to intercept exec access. hvm_emulate_send_vm_event() can return false if there was no violation, if there was an error from monitor_traps() or p2m_get_mem_access(). Returning false if p2m_get_mem_access() fails if the entry was not found in the EPT in which case it is unrestricted. NOTE: hvm_emulate_send_vm_event() assumes the caller will check arch.vm_event->send_event Signed-off-by: Alexandru Isaila --- Changes since V7: - Change commit message - Fix indentation - Remove send_event =3D false - Remove goto out from the send_vm_event check - Init err with null and remove the err =3D NULL from hvmemul_map_linear_addr() - Check err after the loop - Add assert for send_event in hvm_emulate_send_vm_event() --- xen/arch/x86/hvm/emulate.c | 85 ++++++++++++++++++++++++++++--- xen/arch/x86/hvm/hvm.c | 8 +++ xen/arch/x86/mm/mem_access.c | 1 + xen/include/asm-x86/hvm/emulate.h | 4 ++ xen/include/asm-x86/vm_event.h | 2 + 5 files changed, 94 insertions(+), 6 deletions(-) diff --git a/xen/arch/x86/hvm/emulate.c b/xen/arch/x86/hvm/emulate.c index e4b3f330a8..4ab113ef58 100644 --- a/xen/arch/x86/hvm/emulate.c +++ b/xen/arch/x86/hvm/emulate.c @@ -12,9 +12,11 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -531,6 +533,72 @@ static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, return hvmemul_do_io_addr(1, mmio_gpa, reps, size, dir, df, ram_gpa); } =20 +/* + * Send memory access vm_events based on pfec. Returns true if the event w= as + * sent and false for p2m_get_mem_access() error, no violation and event s= end + * error. Assumes the caller will check arch.vm_event->send_event. + * + * NOTE: p2m_get_mem_access() can fail if the entry was not found in the E= PT + * (in which case access to it is unrestricted, so no violations can occur= ). + * In this cases it is fine to continue the emulation. + */ +bool hvm_emulate_send_vm_event(unsigned long gla, gfn_t gfn, + uint32_t pfec) +{ + xenmem_access_t access; + vm_event_request_t req =3D {}; + paddr_t gpa =3D (gfn_to_gaddr(gfn) | (gla & ~PAGE_MASK)); + + ASSERT(current->arch.vm_event->send_event); + + current->arch.vm_event->send_event =3D false; + + if ( p2m_get_mem_access(current->domain, gfn, &access, + altp2m_vcpu_idx(current)) !=3D 0 ) + return false; + + switch ( access ) + { + case XENMEM_access_x: + case XENMEM_access_rx: + if ( pfec & PFEC_write_access ) + req.u.mem_access.flags =3D MEM_ACCESS_R | MEM_ACCESS_W; + break; + + case XENMEM_access_w: + case XENMEM_access_rw: + if ( pfec & PFEC_insn_fetch ) + req.u.mem_access.flags =3D MEM_ACCESS_X; + break; + + case XENMEM_access_r: + case XENMEM_access_n: + if ( pfec & PFEC_write_access ) + req.u.mem_access.flags |=3D MEM_ACCESS_R | MEM_ACCESS_W; + if ( pfec & PFEC_insn_fetch ) + req.u.mem_access.flags |=3D MEM_ACCESS_X; + break; + + case XENMEM_access_wx: + case XENMEM_access_rwx: + case XENMEM_access_rx2rw: + case XENMEM_access_n2rwx: + case XENMEM_access_default: + break; + } + + if ( !req.u.mem_access.flags ) + return false; /* no violation */ + + req.reason =3D VM_EVENT_REASON_MEM_ACCESS; + req.u.mem_access.gfn =3D gfn_x(gfn); + req.u.mem_access.flags |=3D MEM_ACCESS_FAULT_WITH_GLA | MEM_ACCESS_GLA= _VALID; + req.u.mem_access.gla =3D gla; + req.u.mem_access.offset =3D gpa & ~PAGE_MASK; + + return monitor_traps(current, true, &req) >=3D 0; +} + /* * Map the frame(s) covering an individual linear access, for writeable * access. May return NULL for MMIO, or ERR_PTR(~X86EMUL_*) for other err= ors @@ -544,10 +612,11 @@ static void *hvmemul_map_linear_addr( struct hvm_emulate_ctxt *hvmemul_ctxt) { struct vcpu *curr =3D current; - void *err, *mapping; + void *err =3D NULL, *mapping; unsigned int nr_frames =3D ((linear + bytes - !!bytes) >> PAGE_SHIFT) - (linear >> PAGE_SHIFT) + 1; unsigned int i; + gfn_t gfn; =20 /* * mfn points to the next free slot. All used slots have a page refer= ence @@ -582,7 +651,7 @@ static void *hvmemul_map_linear_addr( ASSERT(mfn_x(*mfn) =3D=3D 0); =20 res =3D hvm_translate_get_page(curr, addr, true, pfec, - &pfinfo, &page, NULL, &p2mt); + &pfinfo, &page, &gfn, &p2mt); =20 switch ( res ) { @@ -596,7 +665,6 @@ static void *hvmemul_map_linear_addr( goto out; =20 case HVMTRANS_bad_gfn_to_mfn: - err =3D NULL; goto out; =20 case HVMTRANS_gfn_paged_out: @@ -619,14 +687,19 @@ static void *hvmemul_map_linear_addr( } =20 if ( p2mt =3D=3D p2m_ioreq_server ) - { - err =3D NULL; goto out; - } =20 ASSERT(p2mt =3D=3D p2m_ram_logdirty || !p2m_is_readonly(p2mt)); } + + if ( curr->arch.vm_event && + curr->arch.vm_event->send_event && + hvm_emulate_send_vm_event(addr, gfn, pfec) ) + err =3D ERR_PTR(~X86EMUL_RETRY); } + /* Check if eny vm_event was sent */ + if ( err ) + goto out; =20 /* Entire access within a single frame? */ if ( nr_frames =3D=3D 1 ) diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 2b8189946b..727f195359 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -3224,6 +3224,14 @@ static enum hvm_translation_result __hvm_copy( return HVMTRANS_bad_gfn_to_mfn; } =20 + if ( unlikely(v->arch.vm_event) && + v->arch.vm_event->send_event && + hvm_emulate_send_vm_event(addr, gfn, pfec) ) + { + put_page(page); + return HVMTRANS_gfn_paged_out; + } + p =3D (char *)__map_domain_page(page) + (addr & ~PAGE_MASK); =20 if ( flags & HVMCOPY_to_guest ) diff --git a/xen/arch/x86/mm/mem_access.c b/xen/arch/x86/mm/mem_access.c index 0144f92b98..c0faa57db1 100644 --- a/xen/arch/x86/mm/mem_access.c +++ b/xen/arch/x86/mm/mem_access.c @@ -214,6 +214,7 @@ bool p2m_mem_access_check(paddr_t gpa, unsigned long gl= a, d->arch.monitor.inguest_pagefault_disabled && npfec.kind !=3D npfec_kind_with_gla ) /* don't send a mem_event */ { + v->arch.vm_event->send_event =3D true; hvm_emulate_one_vm_event(EMUL_KIND_NORMAL, TRAP_invalid_op, X86_EV= ENT_NO_EC); =20 return true; diff --git a/xen/include/asm-x86/hvm/emulate.h b/xen/include/asm-x86/hvm/em= ulate.h index b39a1a0331..3682efd90b 100644 --- a/xen/include/asm-x86/hvm/emulate.h +++ b/xen/include/asm-x86/hvm/emulate.h @@ -80,6 +80,10 @@ struct segment_register *hvmemul_get_seg_reg( enum x86_segment seg, struct hvm_emulate_ctxt *hvmemul_ctxt); int hvm_emulate_one_mmio(unsigned long mfn, unsigned long gla); +bool hvm_emulate_send_vm_event( + unsigned long gla, + gfn_t gfn, + uint32_t pfec); =20 static inline bool handle_mmio(void) { diff --git a/xen/include/asm-x86/vm_event.h b/xen/include/asm-x86/vm_event.h index 23e655710b..66db9e1e25 100644 --- a/xen/include/asm-x86/vm_event.h +++ b/xen/include/asm-x86/vm_event.h @@ -36,6 +36,8 @@ struct arch_vm_event { bool set_gprs; /* A sync vm_event has been sent and we're not done handling it. */ bool sync_event; + /* Send mem access events from emulator */ + bool send_event; }; =20 int vm_event_init_domain(struct domain *d); --=20 2.17.1 _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel