From nobody Sat Nov 30 01:00:05 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1620900217; cv=none; d=zohomail.com; s=zohoarc; b=fNkUTTDVjViayDTQY8G4MJ5/6BHcNaG/L0OvNvCrrUat2uuZFEVv60O5C0JouhB4mYcXVAK9ACEXmNpzRo7So5/nExoPpHlWBbSSvMYPmTInWAHFZhTH765wWZNZkvXScQ1xQZTdnZS4QFoKjZUM7TGkZG8owxTAWkzvB1esqjY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620900217; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=+55VMtjzppNIHp9ZFTWUzvbF11WwY57Fyil6Rw9VV3g=; b=ek3xBBdVqEsK6SIBG2vVAs8xPHn6MQ7vFEZcrVjjQ+wxJgw/pSSipAOWu22GleIk/4jTj/m5nrOgfCz8ZbYoZBwfJIemnTmxoyYkc0WXpfppYjg01RWtNCoMQ7hYg63Mafx3OGvyLTifWrtwfIyK7yb9XM53LqipprK+Kam/rnc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1620900217182186.99321307911634; Thu, 13 May 2021 03:03:37 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.126811.238345 (Exim 4.92) (envelope-from ) id 1lh8Bg-0005zi-Dh; Thu, 13 May 2021 10:03:24 +0000 Received: by outflank-mailman (output) from mailman id 126811.238345; Thu, 13 May 2021 10:03:24 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lh8Bg-0005zL-8S; Thu, 13 May 2021 10:03:24 +0000 Received: by outflank-mailman (input) for mailman id 126811; Thu, 13 May 2021 10:03:22 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lh8Be-0003vg-3U for xen-devel@lists.xenproject.org; Thu, 13 May 2021 10:03:22 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id a743bf04-aef4-409d-a481-7e626ae6bc5f; Thu, 13 May 2021 10:03:07 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 30CD0B021; Thu, 13 May 2021 10:03:06 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: a743bf04-aef4-409d-a481-7e626ae6bc5f X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1620900186; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+55VMtjzppNIHp9ZFTWUzvbF11WwY57Fyil6Rw9VV3g=; b=WIbclcgSxN14F4SQTwhdICOoy/oKAZ9BoqgICnTIhIpgk6Erd4l4kEsUzugP1UaejvjNbV SF+VCrj+ofIatBefBM2F1ruGfq4tOuGWV2bH5lzn6RI0YeWS3mPXlGIxGVYjtjjZvVYhSc QH0d4mMri26CSMZx7ZAGaRASXWH8GmQ= From: Juergen Gross To: xen-devel@lists.xenproject.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Juergen Gross , Konrad Rzeszutek Wilk , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Boris Ostrovsky , Stefano Stabellini , Jens Axboe Subject: [PATCH 4/8] xen/blkfront: don't trust the backend response data blindly Date: Thu, 13 May 2021 12:02:58 +0200 Message-Id: <20210513100302.22027-5-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210513100302.22027-1-jgross@suse.com> References: <20210513100302.22027-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) Content-Type: text/plain; charset="utf-8" Today blkfront will trust the backend to send only sane response data. In order to avoid privilege escalations or crashes in case of malicious backends verify the data to be within expected limits. Especially make sure that the response always references an outstanding request. Introduce a new state of the ring BLKIF_STATE_ERROR which will be switched to in case an inconsistency is being detected. Signed-off-by: Juergen Gross --- drivers/block/xen-blkfront.c | 62 +++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/drivers/block/xen-blkfront.c b/drivers/block/xen-blkfront.c index c6a05de4f15f..aa0f159829b4 100644 --- a/drivers/block/xen-blkfront.c +++ b/drivers/block/xen-blkfront.c @@ -80,6 +80,7 @@ enum blkif_state { BLKIF_STATE_DISCONNECTED, BLKIF_STATE_CONNECTED, BLKIF_STATE_SUSPENDED, + BLKIF_STATE_ERROR, }; =20 struct grant { @@ -89,6 +90,7 @@ struct grant { }; =20 enum blk_req_status { + REQ_PROCESSING, REQ_WAITING, REQ_DONE, REQ_ERROR, @@ -543,7 +545,7 @@ static unsigned long blkif_ring_get_request(struct blkf= ront_ring_info *rinfo, =20 id =3D get_id_from_freelist(rinfo); rinfo->shadow[id].request =3D req; - rinfo->shadow[id].status =3D REQ_WAITING; + rinfo->shadow[id].status =3D REQ_PROCESSING; rinfo->shadow[id].associated_id =3D NO_ASSOCIATED_ID; =20 rinfo->shadow[id].req.u.rw.id =3D id; @@ -572,6 +574,7 @@ static int blkif_queue_discard_req(struct request *req,= struct blkfront_ring_inf =20 /* Copy the request to the ring page. */ *final_ring_req =3D *ring_req; + rinfo->shadow[id].status =3D REQ_WAITING; =20 return 0; } @@ -847,8 +850,11 @@ static int blkif_queue_rw_req(struct request *req, str= uct blkfront_ring_info *ri =20 /* Copy request(s) to the ring page. */ *final_ring_req =3D *ring_req; - if (unlikely(require_extra_req)) + rinfo->shadow[id].status =3D REQ_WAITING; + if (unlikely(require_extra_req)) { *final_extra_ring_req =3D *extra_ring_req; + rinfo->shadow[extra_id].status =3D REQ_WAITING; + } =20 if (new_persistent_gnts) gnttab_free_grant_references(setup.gref_head); @@ -1420,8 +1426,8 @@ static enum blk_req_status blkif_rsp_to_req_status(in= t rsp) static int blkif_get_final_status(enum blk_req_status s1, enum blk_req_status s2) { - BUG_ON(s1 =3D=3D REQ_WAITING); - BUG_ON(s2 =3D=3D REQ_WAITING); + BUG_ON(s1 < REQ_DONE); + BUG_ON(s2 < REQ_DONE); =20 if (s1 =3D=3D REQ_ERROR || s2 =3D=3D REQ_ERROR) return BLKIF_RSP_ERROR; @@ -1454,7 +1460,7 @@ static bool blkif_completion(unsigned long *id, s->status =3D blkif_rsp_to_req_status(bret->status); =20 /* Wait the second response if not yet here. */ - if (s2->status =3D=3D REQ_WAITING) + if (s2->status < REQ_DONE) return false; =20 bret->status =3D blkif_get_final_status(s->status, @@ -1574,10 +1580,16 @@ static irqreturn_t blkif_interrupt(int irq, void *d= ev_id) spin_lock_irqsave(&rinfo->ring_lock, flags); again: rp =3D rinfo->ring.sring->rsp_prod; + if (RING_RESPONSE_PROD_OVERFLOW(&rinfo->ring, rp)) { + pr_alert("%s: illegal number of responses %u\n", + info->gd->disk_name, rp - rinfo->ring.rsp_cons); + goto err; + } rmb(); /* Ensure we see queued responses up to 'rp'. */ =20 for (i =3D rinfo->ring.rsp_cons; i !=3D rp; i++) { unsigned long id; + unsigned int op; =20 RING_COPY_RESPONSE(&rinfo->ring, i, &bret); id =3D bret.id; @@ -1588,14 +1600,28 @@ static irqreturn_t blkif_interrupt(int irq, void *d= ev_id) * look in get_id_from_freelist. */ if (id >=3D BLK_RING_SIZE(info)) { - WARN(1, "%s: response to %s has incorrect id (%ld)\n", - info->gd->disk_name, op_name(bret.operation), id); - /* We can't safely get the 'struct request' as - * the id is busted. */ - continue; + pr_alert("%s: response has incorrect id (%ld)\n", + info->gd->disk_name, id); + goto err; } + if (rinfo->shadow[id].status !=3D REQ_WAITING) { + pr_alert("%s: response references no pending request\n", + info->gd->disk_name); + goto err; + } + + rinfo->shadow[id].status =3D REQ_PROCESSING; req =3D rinfo->shadow[id].request; =20 + op =3D rinfo->shadow[id].req.operation; + if (op =3D=3D BLKIF_OP_INDIRECT) + op =3D rinfo->shadow[id].req.u.indirect.indirect_op; + if (bret.operation !=3D op) { + pr_alert("%s: response has wrong operation (%u instead of %u)\n", + info->gd->disk_name, bret.operation, op); + goto err; + } + if (bret.operation !=3D BLKIF_OP_DISCARD) { /* * We may need to wait for an extra response if the @@ -1620,7 +1646,8 @@ static irqreturn_t blkif_interrupt(int irq, void *dev= _id) case BLKIF_OP_DISCARD: if (unlikely(bret.status =3D=3D BLKIF_RSP_EOPNOTSUPP)) { struct request_queue *rq =3D info->rq; - printk(KERN_WARNING "blkfront: %s: %s op failed\n", + + pr_warn_ratelimited("blkfront: %s: %s op failed\n", info->gd->disk_name, op_name(bret.operation)); blkif_req(req)->error =3D BLK_STS_NOTSUPP; info->feature_discard =3D 0; @@ -1632,13 +1659,13 @@ static irqreturn_t blkif_interrupt(int irq, void *d= ev_id) case BLKIF_OP_FLUSH_DISKCACHE: case BLKIF_OP_WRITE_BARRIER: if (unlikely(bret.status =3D=3D BLKIF_RSP_EOPNOTSUPP)) { - printk(KERN_WARNING "blkfront: %s: %s op failed\n", + pr_warn_ratelimited("blkfront: %s: %s op failed\n", info->gd->disk_name, op_name(bret.operation)); blkif_req(req)->error =3D BLK_STS_NOTSUPP; } if (unlikely(bret.status =3D=3D BLKIF_RSP_ERROR && rinfo->shadow[id].req.u.rw.nr_segments =3D=3D 0)) { - printk(KERN_WARNING "blkfront: %s: empty %s op failed\n", + pr_warn_ratelimited("blkfront: %s: empty %s op failed\n", info->gd->disk_name, op_name(bret.operation)); blkif_req(req)->error =3D BLK_STS_NOTSUPP; } @@ -1653,8 +1680,8 @@ static irqreturn_t blkif_interrupt(int irq, void *dev= _id) case BLKIF_OP_READ: case BLKIF_OP_WRITE: if (unlikely(bret.status !=3D BLKIF_RSP_OKAY)) - dev_dbg(&info->xbdev->dev, "Bad return from blkdev data " - "request: %x\n", bret.status); + dev_dbg_ratelimited(&info->xbdev->dev, + "Bad return from blkdev data request: %x\n", bret.status); =20 break; default: @@ -1680,6 +1707,11 @@ static irqreturn_t blkif_interrupt(int irq, void *de= v_id) spin_unlock_irqrestore(&rinfo->ring_lock, flags); =20 return IRQ_HANDLED; + + err: + info->connected =3D BLKIF_STATE_ERROR; + pr_alert("%s disabled for further use\n", info->gd->disk_name); + return IRQ_HANDLED; } =20 =20 --=20 2.26.2