Received: by 10.223.176.5 with SMTP id f5csp88243wra; Fri, 2 Feb 2018 17:25:33 -0800 (PST) X-Google-Smtp-Source: AH8x224UXjv7mSQ8ZPAEbXee83nP6s+YDwSuKGwJfpWOfgRaU/ZqPGwClTy1ak7WUkxS126A9lZ+ X-Received: by 10.99.189.18 with SMTP id a18mr1204211pgf.22.1517621132932; Fri, 02 Feb 2018 17:25:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517621132; cv=none; d=google.com; s=arc-20160816; b=O2gnkC3nf/p2Ky7QBjLQQuRs2/GaU34KnHHObtIKRr38v7pavgorGN3yrf06HgEXG+ QpW3loiq+VeZl0MDbtNR3hNLEKvF5CLisPHZebAfbtcRTQqKe+polRtT/CPgVJRSz6CK bK1hfUxZk2YNemP80qxEb893TgNVX2KRhvsAz9YlfyUXpRqbdO5LTksX9UMmEklCETHc 9VAQWRKsSju77kZYxfr6sSy6s0fCuJ67imiK81+0RkuonlDMjnp+XmQR1nuFRsn83Ivf I5gknifuNVPjD1rPGc/CPh32T0Mxsubn3VG1/2ngVyCDJoJQie/Uby0ZL3q6rwi1Cy5E NjAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature :arc-authentication-results; bh=FsuKMOHyaMBT8Tr9Tdv4sEkWr5G9Yfa+PiZr9E6vU0M=; b=C40F+ZKKhRud5gbbpGpwShg6gxzCUx/MmoJpGEeFgekrA3jeSRrX6fYxrMr0xZdMso TUjynduTFOQa3pYHaE69W+jmPj9L2x1gKYUiY5M5cKU68D7JgOzIGmhSQ/9CU6Z8iCYt xPE0fHd0X8YNF6gCBFCNpLl6ipth6llcoO1US8kbS0Rdkr1E3GmQ6QqDNqi1hR2qiqtX 1DqCpRrFOn00ocd15s7Nw2lI+ZnP8U7IHlojVPAtbipNCGnVHeMPs7TtZsWbKq6L2UNC n9koeYbTH9T3gahBtMECHiAHUG0blCcTXFOCZRHTkEvYY4agiSN5PoHDe/wPE4Tkwz9v V51w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2017-10-26 header.b=LcJhSCt6; 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 s7-v6si2734558plp.57.2018.02.02.17.25.17; Fri, 02 Feb 2018 17:25:32 -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-2017-10-26 header.b=LcJhSCt6; 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 S1752834AbeBBXdK (ORCPT + 99 others); Fri, 2 Feb 2018 18:33:10 -0500 Received: from aserp2130.oracle.com ([141.146.126.79]:46812 "EHLO aserp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751938AbeBBXdD (ORCPT ); Fri, 2 Feb 2018 18:33:03 -0500 Received: from pps.filterd (aserp2130.oracle.com [127.0.0.1]) by aserp2130.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w12NWXYv191241; Fri, 2 Feb 2018 23:32:55 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 : content-transfer-encoding; s=corp-2017-10-26; bh=FsuKMOHyaMBT8Tr9Tdv4sEkWr5G9Yfa+PiZr9E6vU0M=; b=LcJhSCt6Ohyi9XwcYLMdAMS0hRDPzcsJJc4QhOKAh1PU4Z5WX8kq+XFYoubHvCSm4spp kNEMJBZAlIaIIO7bSm+72+C1mBIhVV4IaHatTnujncaWvXcFiKwRxhrDXTE1PVw5/znc K7ymDA76pA2IrFRsReHy6TfXaisUddVCMvqikmJyQnIxbv5Oz+CLGT3gS2JviU8cYfWZ Iz0+KwYkAGNnYXNDW3stPXlSKBJMCtREasNBGsTM/55MJgamhhP4RZLEI43CPBG8HxHt Pu7KZnab9AQfuNJnYq3z/1ZScAdRJTs643izWPwMKxXeExeuFpsemSgMA8J4CDUDFPAi Ww== Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by aserp2130.oracle.com with ESMTP id 2fvv581aaf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 02 Feb 2018 23:32:55 +0000 Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by aserv0021.oracle.com (8.14.4/8.14.4) with ESMTP id w12NWtMr032286 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Fri, 2 Feb 2018 23:32:55 GMT Received: from abhmp0018.oracle.com (abhmp0018.oracle.com [141.146.116.24]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id w12NWrVE008769; Fri, 2 Feb 2018 23:32:53 GMT Received: from dhcp-burlington7-2nd-B-east-10-152-55-162.usdhcp.oraclecorp.com (/10.152.32.65) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 02 Feb 2018 15:32:53 -0800 Subject: Re: [PATCH] xen: hypercall: fix out-of-bounds memcpy To: Arnd Bergmann , Juergen Gross Cc: Nicolas Pitre , Andi Kleen , Dan Carpenter , Jan Beulich , xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org References: <20180202153240.1190361-1-arnd@arndb.de> From: Boris Ostrovsky Message-ID: Date: Fri, 2 Feb 2018 18:33:36 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: <20180202153240.1190361-1-arnd@arndb.de> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Content-Language: en-US X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8793 signatures=668661 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-1711220000 definitions=main-1802020284 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 02/02/2018 10:32 AM, Arnd Bergmann wrote: > The legacy hypercall handlers were originally added with > a comment explaining that "copying the argument structures in > HYPERVISOR_event_channel_op() and HYPERVISOR_physdev_op() into the local > variable is sufficiently safe" and only made sure to not write > past the end of the argument structure, the checks in linux/string.h > disagree with that, when link-time optimizations are used: > > In function 'memcpy', > inlined from 'pirq_query_unmask' at drivers/xen/fallback.c:53:2, > inlined from '__startup_pirq' at drivers/xen/events/events_base.c:529:2, > inlined from 'restore_pirqs' at drivers/xen/events/events_base.c:1439:3, > inlined from 'xen_irq_resume' at drivers/xen/events/events_base.c:1581:2: > include/linux/string.h:350:3: error: call to '__read_overflow2' declared with attribute error: detected read beyond size of object passed as 2nd parameter > __read_overflow2(); > ^ > make[3]: *** [ccLujFNx.ltrans15.ltrans.o] Error 1 > make[3]: Target 'all' not remade because of errors. > lto-wrapper: fatal error: make returned 2 exit status > compilation terminated. > ld: error: lto-wrapper failed > > This changes the functions so that each argument is accessed with > exactly the correct length based on the command code. > > Fixes: cf47a83fb06e ("xen/hypercall: fix hypercall fallback code for very old hypervisors") > Signed-off-by: Arnd Bergmann > --- > drivers/xen/fallback.c | 94 ++++++++++++++++++++++++++++---------------------- > 1 file changed, 53 insertions(+), 41 deletions(-) > > diff --git a/drivers/xen/fallback.c b/drivers/xen/fallback.c > index b04fb64c5a91..eded8dd821ad 100644 > --- a/drivers/xen/fallback.c > +++ b/drivers/xen/fallback.c > @@ -7,75 +7,87 @@ > > int xen_event_channel_op_compat(int cmd, void *arg) > { > - struct evtchn_op op; > + struct evtchn_op op = { .cmd = cmd, }; > + size_t len; > int rc; > > - op.cmd = cmd; > - memcpy(&op.u, arg, sizeof(op.u)); > - rc = _hypercall1(int, event_channel_op_compat, &op); > - > switch (cmd) { > + case EVTCHNOP_bind_interdomain: > + len = sizeof(struct evtchn_bind_interdomain); > + break; > + case EVTCHNOP_bind_virq: > + len = sizeof(struct evtchn_bind_virq); > + break; > + case EVTCHNOP_bind_pirq: > + len = sizeof(struct evtchn_bind_pirq); > + break; > case EVTCHNOP_close: > + len = sizeof(struct evtchn_close); > + break; > case EVTCHNOP_send: > + len = sizeof(struct evtchn_send); > + break; > + case EVTCHNOP_alloc_unbound: > + len = sizeof(struct evtchn_alloc_unbound); > + break; > + case EVTCHNOP_bind_ipi: > + len = sizeof(struct evtchn_bind_ipi); > + break; > + case EVTCHNOP_status: > + len = sizeof(struct evtchn_status); > + break; > case EVTCHNOP_bind_vcpu: > + len = sizeof(struct evtchn_bind_vcpu); > + break; > case EVTCHNOP_unmask: > - /* no output */ > + len = sizeof(struct evtchn_unmask); > break; > - > -#define COPY_BACK(eop) \ > - case EVTCHNOP_##eop: \ > - memcpy(arg, &op.u.eop, sizeof(op.u.eop)); \ > - break > - > - COPY_BACK(bind_interdomain); > - COPY_BACK(bind_virq); > - COPY_BACK(bind_pirq); > - COPY_BACK(status); > - COPY_BACK(alloc_unbound); > - COPY_BACK(bind_ipi); > -#undef COPY_BACK > - > default: > - WARN_ON(rc != -ENOSYS); > - break; > + return -ENOSYS; > } > > + memcpy(&op.u, arg, len); > + rc = _hypercall1(int, event_channel_op_compat, &op); > + memcpy(arg, &op.u, len); We don't copy back for all commands, only those that are COPY_BACK. > + > return rc; > } > EXPORT_SYMBOL_GPL(xen_event_channel_op_compat); > > int xen_physdev_op_compat(int cmd, void *arg) > { > - struct physdev_op op; > + struct physdev_op op = { .cmd = cmd, }; > + size_t len; > int rc; > > - op.cmd = cmd; > - memcpy(&op.u, arg, sizeof(op.u)); > - rc = _hypercall1(int, physdev_op_compat, &op); > - > switch (cmd) { > case PHYSDEVOP_IRQ_UNMASK_NOTIFY: > + len = 0; > + break; > + case PHYSDEVOP_irq_status_query: > + len = sizeof(struct physdev_irq_status_query); > + break; > case PHYSDEVOP_set_iopl: > + len = sizeof(struct physdev_set_iopl); > + break; > case PHYSDEVOP_set_iobitmap: > + len = sizeof(struct physdev_set_iobitmap); > + break; > + case PHYSDEVOP_apic_read: > case PHYSDEVOP_apic_write: > - /* no output */ > + len = sizeof(struct physdev_apic); > break; > - > -#define COPY_BACK(pop, fld) \ > - case PHYSDEVOP_##pop: \ > - memcpy(arg, &op.u.fld, sizeof(op.u.fld)); \ > - break > - > - COPY_BACK(irq_status_query, irq_status_query); > - COPY_BACK(apic_read, apic_op); > - COPY_BACK(ASSIGN_VECTOR, irq_op); > -#undef COPY_BACK > - > - default: > - WARN_ON(rc != -ENOSYS); > + case PHYSDEVOP_ASSIGN_VECTOR: > + len = sizeof(struct physdev_irq); > break; > + default: > + return -ENOSYS; > } > > + memcpy(&op.u, arg, len); > + rc = _hypercall1(int, physdev_op_compat, &op); > + memcpy(arg, &op.u, len); And the same is true here. -boris > + > return rc; > } > EXPORT_SYMBOL_GPL(xen_physdev_op_compat);