From patchwork Wed Mar 2 06:51:51 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Razvan Cojocaru X-Patchwork-Id: 8478181 Return-Path: X-Original-To: patchwork-xen-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 277B7C0553 for ; Wed, 2 Mar 2016 06:55:23 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 3BB122037C for ; Wed, 2 Mar 2016 06:55:22 +0000 (UTC) Received: from lists.xen.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 521112035B for ; Wed, 2 Mar 2016 06:55:21 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xen.org) by lists.xen.org with esmtp (Exim 4.84) (envelope-from ) id 1ab0df-0001Vz-C9; Wed, 02 Mar 2016 06:52:03 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.84) (envelope-from ) id 1ab0de-0001Vt-W2 for xen-devel@lists.xen.org; Wed, 02 Mar 2016 06:52:03 +0000 Received: from [85.158.139.211] by server-11.bemta-5.messagelabs.com id 6D/72-02987-29D86D65; Wed, 02 Mar 2016 06:52:02 +0000 X-Env-Sender: rcojocaru@bitdefender.com X-Msg-Ref: server-11.tower-206.messagelabs.com!1456901521!13889388!1 X-Originating-IP: [91.199.104.161] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 8.11; banners=-,-,- X-VirusChecked: Checked Received: (qmail 12844 invoked from network); 2 Mar 2016 06:52:01 -0000 Received: from mx01.bbu.dsd.mx.bitdefender.com (HELO mx01.bbu.dsd.mx.bitdefender.com) (91.199.104.161) by server-11.tower-206.messagelabs.com with DHE-RSA-AES128-GCM-SHA256 encrypted SMTP; 2 Mar 2016 06:52:01 -0000 Received: (qmail 7057 invoked from network); 2 Mar 2016 08:52:00 +0200 Received: from unknown (HELO mx-sr.buh.bitdefender.com) (10.17.80.103) by mx01.bbu.dsd.mx.bitdefender.com with AES256-GCM-SHA384 encrypted SMTP; 2 Mar 2016 08:52:00 +0200 Received: from smtp01.buh.bitdefender.com (unknown [10.17.80.75]) by mx-sr.buh.bitdefender.com (Postfix) with ESMTP id D13437FBFC for ; Wed, 2 Mar 2016 08:51:59 +0200 (EET) Received: (qmail 1345 invoked from network); 2 Mar 2016 08:51:59 +0200 Received: from xen.dsd.ro (HELO xen.dsd.bitdefender.biz) (rcojocaru@bitdefender.com@10.10.14.109) by smtp01.buh.bitdefender.com with AES128-SHA256 encrypted SMTP; 2 Mar 2016 08:51:58 +0200 From: Razvan Cojocaru To: xen-devel@lists.xen.org Date: Wed, 2 Mar 2016 08:51:51 +0200 Message-Id: <1456901511-22065-1-git-send-email-rcojocaru@bitdefender.com> X-Mailer: git-send-email 1.9.1 X-BitDefender-Scanner: Clean, Agent: BitDefender qmail 3.1.4 on smtp01.buh.bitdefender.com, sigver: 7.64690 X-BitDefender-Spam: No (0) X-BitDefender-SpamStamp: Build: [Engines: 2.15.6.743, Dats: 415856, Stamp: 3], Multi: [Enabled, t: (0.000007, 0.002858)], BW: [Enabled, t: (0.000006,0.000001)], RBL DNSBL: [Disabled], APM: [Enabled, Score: 500, t: (0.005183), Flags: BDD9BED7; NN_SEE_MY_PHOTO_ADN; NN_NO_CONTENT_TYPE; NN_ADULT_16_SLT; NN_LEGIT_SUMM_400_WORDS; NN_LEGIT_BITDEFENDER; NN_LEGIT_S_SQARE_BRACKETS; NN_LEGIT_MAILING_LIST_TO], SGN: [Enabled, t: (0.008299)], URL: [Enabled, t: (0.000018)], RTDA: [Enabled, t: (0.014841), Hit: No, Details: v2.3.3; Id: 2m1ghaq.1ac9nt89q.411e1], total: 0(775) X-BitDefender-CF-Stamp: none Cc: wei.liu2@citrix.com, ian.jackson@eu.citrix.com, Razvan Cojocaru , stefano.stabellini@eu.citrix.com Subject: [Xen-devel] [PATCH] libxc: Have xc_translate_foreign_address() set errno properly X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently it's possible for xc_translate_foreign_address() to fail and errno still be set to success. This patch fixes the issue. Based on the first half of Don Slutz' patch: http://lists.xen.org/archives/html/xen-devel/2014-03/msg03720.html Signed-off-by: Razvan Cojocaru --- tools/libxc/xc_pagetab.c | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/tools/libxc/xc_pagetab.c b/tools/libxc/xc_pagetab.c index ec97890..f7dc30b 100644 --- a/tools/libxc/xc_pagetab.c +++ b/tools/libxc/xc_pagetab.c @@ -34,9 +34,14 @@ unsigned long xc_translate_foreign_address(xc_interface *xch, uint32_t dom, int size, level, pt_levels = 2; void *map; - if (xc_domain_getinfo(xch, dom, 1, &dominfo) != 1 + if (xc_domain_getinfo(xch, dom, 1, &dominfo) != 1 || dominfo.domid != dom) + { + if (errno == 0) + errno = EINVAL; + return 0; + } /* What kind of paging are we dealing with? */ if (dominfo.hvm) { @@ -44,7 +49,12 @@ unsigned long xc_translate_foreign_address(xc_interface *xch, uint32_t dom, if (xc_domain_hvm_getcontext_partial(xch, dom, HVM_SAVE_CODE(CPU), vcpu, &ctx, sizeof ctx) != 0) + { + if (errno == 0) + errno = EINVAL; + return 0; + } if (!(ctx.cr0 & CR0_PG)) return virt >> PAGE_SHIFT; pt_levels = (ctx.msr_efer&EFER_LMA) ? 4 : (ctx.cr4&CR4_PAE) ? 3 : 2; @@ -53,9 +63,19 @@ unsigned long xc_translate_foreign_address(xc_interface *xch, uint32_t dom, unsigned int gwidth; vcpu_guest_context_any_t ctx; if (xc_vcpu_getcontext(xch, dom, vcpu, &ctx) != 0) + { + if (errno == 0) + errno = EINVAL; + return 0; + } if (xc_domain_get_guest_width(xch, dom, &gwidth) != 0) + { + if (errno == 0) + errno = EINVAL; + return 0; + } if (gwidth == 8) { pt_levels = 4; paddr = (uint64_t)xen_cr3_to_pfn_x86_64(ctx.x64.ctrlreg[3]) @@ -84,12 +104,22 @@ unsigned long xc_translate_foreign_address(xc_interface *xch, uint32_t dom, paddr += ((virt & mask) >> (xc_ffs64(mask) - 1)) * size; map = xc_map_foreign_range(xch, dom, PAGE_SIZE, PROT_READ, paddr >>PAGE_SHIFT); - if (!map) + if (!map) + { + if (errno == 0) + errno = EINVAL; + return 0; + } memcpy(&pte, map + (paddr & (PAGE_SIZE - 1)), size); munmap(map, PAGE_SIZE); - if (!(pte & 1)) + if (!(pte & 1)) + { + if (errno == 0) + errno = EADDRNOTAVAIL; + return 0; + } paddr = pte & 0x000ffffffffff000ull; if (level == 2 && (pte & PTE_PSE)) { mask = ((mask ^ ~-mask) >> 1); /* All bits below first set bit */