Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3996500imu; Tue, 18 Dec 2018 07:30:59 -0800 (PST) X-Google-Smtp-Source: AFSGD/VdFdP5PlPgYj+c/voGIUbnFx93fYyWdiyIl3ePyftEZq8sGsnxKRVZTLw+lRuTpCDoUUJG X-Received: by 2002:a17:902:d891:: with SMTP id b17mr17180141plz.80.1545147059456; Tue, 18 Dec 2018 07:30:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545147059; cv=none; d=google.com; s=arc-20160816; b=amFbE01zkR6Y2iPhSWkHh2mk21WPFSEvrFEjGOq7I9c/zJckUpZnz8lcH/yjT9A4s0 JnhH1gi7K/pvjjPLVWTcMeiKJihY2viDs2C1xg+juZ7/Kjtyje6Te/f3+bBWskJ6MJZn DbHJbRYi9Jqa+/cGRC6LJF36nSuaccKEAC7HTUvxIsl0Zr/zEv6bZiiIrCixvL2/IQ0F 50ZF8dSE6maACSZTx0SFUAjX4NVyacGFZMzT1sLnzxhM/pVTuh0ASq09szevA1wDg4PB 6gdJX/GTNcnYKiSeCt280T3WSUYsd5wrWsE9K/AaTjAhBziTVPrB+7EfvqIjs2AFMOCp cX4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:cc:references:to :subject:dkim-signature; bh=niJuTMJE7sALPAy3HHGbpxLpeTOx3yliOv3/M/tLOPg=; b=T7PdZdesTSekfJNT4F3rIVW+l6zDc3Xz2XeWdCL3bDORtqMQcvePea3J4xJYxwdFBL Qh8CtczKXpKQCXl8JzJsG197Gq3GyNdYtwF4eliyjucKf+YT6XjSxRKLoZzLr8rolyy4 enOu4e5ias2Tb21Fps6W4k7oyD1/ZSBoFoJv8G50Va1/0iKBbL+s1e22ZmJjlHeHakhu biYcmcr1MJosXNhr6wHyWvnGI7A/aCbjjN5GGlXs4neeuM4ioVh/QBpmi47N2Q8LjCJw 5+DGvpaMx2qHuyXmOGlHrxEB1uWh3s5vwGvCOGfmRwtmsxIXJSxaNnRdizwgfdpW/NG0 fQGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=sJ0cZmWE; 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 u69si14549682pfj.219.2018.12.18.07.30.43; Tue, 18 Dec 2018 07:30:59 -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=sJ0cZmWE; 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 S1726893AbeLRP3e (ORCPT + 99 others); Tue, 18 Dec 2018 10:29:34 -0500 Received: from userp2120.oracle.com ([156.151.31.85]:52406 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726690AbeLRP3e (ORCPT ); Tue, 18 Dec 2018 10:29:34 -0500 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.22/8.16.0.22) with SMTP id wBIFTTp3041264; Tue, 18 Dec 2018 15:29:29 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=subject : to : references : cc : from : message-id : date : mime-version : in-reply-to : content-type : content-transfer-encoding; s=corp-2018-07-02; bh=niJuTMJE7sALPAy3HHGbpxLpeTOx3yliOv3/M/tLOPg=; b=sJ0cZmWEB20erYrQQH5ulJ3EgwKCh4RYk7ZHpJFoVM3HNmJ0TCJFHi6wOA6BMau633jR exBmBld75fGil+qbWkwSwpO79XK+yOfnBQSMVKgdryV+FDH2YIs2a/MDdM3ntlJSfStP ofbXAp9U0WIr/g1RYF8MCsmXIznnDmZ23dhp+s/tU1YJLFtTJ8Gl2LRQC/msbBNSBaQh m/u7rzAQMSHBqFveMh6KdlbxkbSg38EG+ld+DC+XUANyRy/bt+13NRUi1TNJxC7oWaRa FusLP+YcWG6mxKR0nfmMsNwSZH/C99C2Z+yXrsIiTEqrvAisSM0+sKLW4k/NGoeYYoNf vw== Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by userp2120.oracle.com with ESMTP id 2pct8qv0t9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Dec 2018 15:29:29 +0000 Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserv0021.oracle.com (8.14.4/8.14.4) with ESMTP id wBIFTSUd002740 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Dec 2018 15:29:28 GMT Received: from abhmp0014.oracle.com (abhmp0014.oracle.com [141.146.116.20]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id wBIFTSTN021431; Tue, 18 Dec 2018 15:29:28 GMT Received: from [192.168.2.8] (/1.202.67.146) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 18 Dec 2018 07:29:27 -0800 Subject: Re: [Xen-devel] [PATCH v2 1/1] xen/blkback: rework connect_ring() to avoid inconsistent xenstore 'ring-page-order' set by malicious blkfront To: =?UTF-8?Q?Roger_Pau_Monn=c3=a9?= References: <1545094538-31426-1-git-send-email-dongli.zhang@oracle.com> <20181218093300.qfslnloxooqnu2im@mac> <51ea8086-e5fd-b1db-f935-101160ca9359@oracle.com> <20181218151331.iwkq3roqwigf3e2r@mac> Cc: axboe@kernel.dk, konrad.wilk@oracle.com, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, paul.durrant@citrix.com, xen-devel@lists.xenproject.org From: Dongli Zhang Message-ID: Date: Tue, 18 Dec 2018 23:29:16 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.3.0 MIME-Version: 1.0 In-Reply-To: <20181218151331.iwkq3roqwigf3e2r@mac> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9111 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-1812180131 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 12/18/2018 11:13 PM, Roger Pau Monn? wrote: > On Tue, Dec 18, 2018 at 07:31:59PM +0800, Dongli Zhang wrote: >> Hi Roger, >> >> On 12/18/2018 05:33 PM, Roger Pau Monn? wrote: >>> On Tue, Dec 18, 2018 at 08:55:38AM +0800, Dongli Zhang wrote: >>>> The xenstore 'ring-page-order' is used globally for each blkback queue and >>>> therefore should be read from xenstore only once. However, it is obtained >>>> in read_per_ring_refs() which might be called multiple times during the >>>> initialization of each blkback queue. >>>> >>>> If the blkfront is malicious and the 'ring-page-order' is set in different >>>> value by blkfront every time before blkback reads it, this may end up at >>>> the "WARN_ON(i != (XEN_BLKIF_REQS_PER_PAGE * blkif->nr_ring_pages));" in >>>> xen_blkif_disconnect() when frontend is destroyed. >>>> >>>> This patch reworks connect_ring() to read xenstore 'ring-page-order' only >>>> once. >>>> >>>> Signed-off-by: Dongli Zhang >>>> --- >>>> Changed since v1: >>>> * change the order of xenstore read in read_per_ring_refs(suggested by Roger Pau Monne) >>>> * use xenbus_read_unsigned() in connect_ring() (suggested by Roger Pau Monne) >>>> >>>> drivers/block/xen-blkback/xenbus.c | 70 ++++++++++++++++++++++---------------- >>>> 1 file changed, 40 insertions(+), 30 deletions(-) >>>> >>>> diff --git a/drivers/block/xen-blkback/xenbus.c b/drivers/block/xen-blkback/xenbus.c >>>> index a4bc74e..7178f0f 100644 >>>> --- a/drivers/block/xen-blkback/xenbus.c >>>> +++ b/drivers/block/xen-blkback/xenbus.c >>>> @@ -926,7 +926,7 @@ static int read_per_ring_refs(struct xen_blkif_ring *ring, const char *dir) >>>> int err, i, j; >>>> struct xen_blkif *blkif = ring->blkif; >>>> struct xenbus_device *dev = blkif->be->dev; >>>> - unsigned int ring_page_order, nr_grefs, evtchn; >>>> + unsigned int nr_grefs, evtchn; >>>> >>>> err = xenbus_scanf(XBT_NIL, dir, "event-channel", "%u", >>>> &evtchn); >>>> @@ -936,43 +936,38 @@ static int read_per_ring_refs(struct xen_blkif_ring *ring, const char *dir) >>>> return err; >>>> } >>>> >>>> - err = xenbus_scanf(XBT_NIL, dev->otherend, "ring-page-order", "%u", >>>> - &ring_page_order); >>>> - if (err != 1) { >>>> - err = xenbus_scanf(XBT_NIL, dir, "ring-ref", "%u", &ring_ref[0]); >>>> - if (err != 1) { >>>> + nr_grefs = blkif->nr_ring_pages; >>>> + WARN_ON(!nr_grefs); >>>> + >>>> + for (i = 0; i < nr_grefs; i++) { >>>> + char ring_ref_name[RINGREF_NAME_LEN]; >>>> + >>>> + snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i); >>>> + err = xenbus_scanf(XBT_NIL, dir, ring_ref_name, >>>> + "%u", &ring_ref[i]); >>>> + >>>> + if (err != 1 && (i || (!i && nr_grefs > 1))) { >>> >>> AFAICT the above condition can be simplified as "err != 1 && >>> nr_grefs". >>> >>>> err = -EINVAL; >>> >>> There's no point in setting err here... >>> >>>> - xenbus_dev_fatal(dev, err, "reading %s/ring-ref", dir); >>>> + xenbus_dev_fatal(dev, err, "reading %s/%s", >>>> + dir, ring_ref_name); >>>> return err; >>> >>> ...since you can just return -EINVAL (same applies to the other >>> instance below). >> >> I would like to confirm if I would keep the err = -EINVAL in below because most >> of the below code is copied from original implementation without modification. >> >> There is no err set by xenbus_read_unsigned(). > > Right, but instead of doing: > > err = -EINVAL; > return err; > > You can just do: > > return -EINVAL; > > Which is one line shorter :). However, for the "ring-page-order" case, the err used in xenbus_dev_fatal() is not set as xenbus_read_unsigned() does not return any err? For "ring-page-order", I would still need to set err = -EINVAL with extra one line of code? > >> + ring_page_order = xenbus_read_unsigned(dev->otherend, >> + "ring-page-order", 0); >> + >> + if (ring_page_order > xen_blkif_max_ring_order) { >> + err = -EINVAL; >> + xenbus_dev_fatal(dev, err, >> + "requested ring page order %d exceed max:%d", >> + ring_page_order, >> + xen_blkif_max_ring_order); >> + return err; >> + } >> + >> + be->blkif->nr_ring_pages = 1 << ring_page_order; >> >> >> For the rest, I would do something like: >> >> + err = xenbus_scanf(XBT_NIL, dir, ring_ref_name, >> + "%u", &ring_ref[i]); >> + >> + if (err != 1 && nr_grefs > 1) { >> + xenbus_dev_fatal(dev, err, "reading %s/%s", >> + dir, ring_ref_name); >> + return -EINVAL; >> + } >> >> >> Thank you very much! > > Thanks! > Dongli Zhang