Skip to content

Commit bbe9de6

Browse files
Paul Durrantgregkh
authored andcommitted
xen-blkback: fix compatibility bug with single page rings
[ Upstream commit d75e7f6 ] Prior to commit 4a8c31a ("xen/blkback: rework connect_ring() to avoid inconsistent xenstore 'ring-page-order' set by malicious blkfront"), the behaviour of xen-blkback when connecting to a frontend was: - read 'ring-page-order' - if not present then expect a single page ring specified by 'ring-ref' - else expect a ring specified by 'ring-refX' where X is between 0 and 1 << ring-page-order This was correct behaviour, but was broken by the afforementioned commit to become: - read 'ring-page-order' - if not present then expect a single page ring (i.e. ring-page-order = 0) - expect a ring specified by 'ring-refX' where X is between 0 and 1 << ring-page-order - if that didn't work then see if there's a single page ring specified by 'ring-ref' This incorrect behaviour works most of the time but fails when a frontend that sets 'ring-page-order' is unloaded and replaced by one that does not because, instead of reading 'ring-ref', xen-blkback will read the stale 'ring-ref0' left around by the previous frontend will try to map the wrong grant reference. This patch restores the original behaviour. Fixes: 4a8c31a ("xen/blkback: rework connect_ring() to avoid inconsistent xenstore 'ring-page-order' set by malicious blkfront") Signed-off-by: Paul Durrant <pdurrant@amazon.com> Reviewed-by: Dongli Zhang <dongli.zhang@oracle.com> Reviewed-by: "Roger Pau Monné" <roger.pau@citrix.com> Link: https://lore.kernel.org/r/20210202175659.18452-1-paul@xen.org Signed-off-by: Juergen Gross <jgross@suse.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 565e7c9 commit bbe9de6

2 files changed

Lines changed: 17 additions & 22 deletions

File tree

‎drivers/block/xen-blkback/common.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ struct xen_blkif {
313313

314314
struct work_struct free_work;
315315
unsigned int nr_ring_pages;
316+
bool multi_ref;
316317
/* All rings for this device. */
317318
struct xen_blkif_ring *rings;
318319
unsigned int nr_rings;

‎drivers/block/xen-blkback/xenbus.c‎

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -998,33 +998,24 @@ static int read_per_ring_refs(struct xen_blkif_ring *ring, const char *dir)
998998
for (i = 0; i < nr_grefs; i++) {
999999
char ring_ref_name[RINGREF_NAME_LEN];
10001000

1001-
snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
1001+
if (blkif->multi_ref)
1002+
snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
1003+
else {
1004+
WARN_ON(i != 0);
1005+
snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref");
1006+
}
1007+
10021008
err = xenbus_scanf(XBT_NIL, dir, ring_ref_name,
10031009
"%u", &ring_ref[i]);
10041010

10051011
if (err != 1) {
1006-
if (nr_grefs == 1)
1007-
break;
1008-
10091012
err = -EINVAL;
10101013
xenbus_dev_fatal(dev, err, "reading %s/%s",
10111014
dir, ring_ref_name);
10121015
return err;
10131016
}
10141017
}
10151018

1016-
if (err != 1) {
1017-
WARN_ON(nr_grefs != 1);
1018-
1019-
err = xenbus_scanf(XBT_NIL, dir, "ring-ref", "%u",
1020-
&ring_ref[0]);
1021-
if (err != 1) {
1022-
err = -EINVAL;
1023-
xenbus_dev_fatal(dev, err, "reading %s/ring-ref", dir);
1024-
return err;
1025-
}
1026-
}
1027-
10281019
err = -ENOMEM;
10291020
for (i = 0; i < nr_grefs * XEN_BLKIF_REQS_PER_PAGE; i++) {
10301021
req = kzalloc(sizeof(*req), GFP_KERNEL);
@@ -1129,10 +1120,15 @@ static int connect_ring(struct backend_info *be)
11291120
blkif->nr_rings, blkif->blk_protocol, protocol,
11301121
blkif->vbd.feature_gnt_persistent ? "persistent grants" : "");
11311122

1132-
ring_page_order = xenbus_read_unsigned(dev->otherend,
1133-
"ring-page-order", 0);
1134-
1135-
if (ring_page_order > xen_blkif_max_ring_order) {
1123+
err = xenbus_scanf(XBT_NIL, dev->otherend, "ring-page-order", "%u",
1124+
&ring_page_order);
1125+
if (err != 1) {
1126+
blkif->nr_ring_pages = 1;
1127+
blkif->multi_ref = false;
1128+
} else if (ring_page_order <= xen_blkif_max_ring_order) {
1129+
blkif->nr_ring_pages = 1 << ring_page_order;
1130+
blkif->multi_ref = true;
1131+
} else {
11361132
err = -EINVAL;
11371133
xenbus_dev_fatal(dev, err,
11381134
"requested ring page order %d exceed max:%d",
@@ -1141,8 +1137,6 @@ static int connect_ring(struct backend_info *be)
11411137
return err;
11421138
}
11431139

1144-
blkif->nr_ring_pages = 1 << ring_page_order;
1145-
11461140
if (blkif->nr_rings == 1)
11471141
return read_per_ring_refs(&blkif->rings[0], dev->otherend);
11481142
else {

0 commit comments

Comments
 (0)