Skip to content

Commit ef594c4

Browse files
committed
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/sage/ceph-client
Pull Ceph fixes from Sage Weil: "Two fixes. One is a stopgap to prevent a stack blowout when users have a deep chain of image clones. (We'll rewrite this code to be non-recursive for the next window, but in the meantime this is a simple fix that avoids a crash.) The second fixes a refcount underflow" * 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/sage/ceph-client: rbd: prevent kernel stack blow up on rbd map rbd: don't leak parent_spec in rbd_dev_probe_parent()
2 parents 37902bc + 6d69bb5 commit ef594c4

File tree

1 file changed

+39
-30
lines changed

1 file changed

+39
-30
lines changed

drivers/block/rbd.c

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ static int atomic_dec_return_safe(atomic_t *v)
9696
#define RBD_MINORS_PER_MAJOR 256
9797
#define RBD_SINGLE_MAJOR_PART_SHIFT 4
9898

99+
#define RBD_MAX_PARENT_CHAIN_LEN 16
100+
99101
#define RBD_SNAP_DEV_NAME_PREFIX "snap_"
100102
#define RBD_MAX_SNAP_NAME_LEN \
101103
(NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1))
@@ -426,7 +428,7 @@ static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf,
426428
size_t count);
427429
static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf,
428430
size_t count);
429-
static int rbd_dev_image_probe(struct rbd_device *rbd_dev, bool mapping);
431+
static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth);
430432
static void rbd_spec_put(struct rbd_spec *spec);
431433

432434
static int rbd_dev_id_to_minor(int dev_id)
@@ -5131,44 +5133,51 @@ static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev)
51315133
return ret;
51325134
}
51335135

5134-
static int rbd_dev_probe_parent(struct rbd_device *rbd_dev)
5136+
/*
5137+
* @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() ->
5138+
* rbd_dev_image_probe() recursion depth, which means it's also the
5139+
* length of the already discovered part of the parent chain.
5140+
*/
5141+
static int rbd_dev_probe_parent(struct rbd_device *rbd_dev, int depth)
51355142
{
51365143
struct rbd_device *parent = NULL;
5137-
struct rbd_spec *parent_spec;
5138-
struct rbd_client *rbdc;
51395144
int ret;
51405145

51415146
if (!rbd_dev->parent_spec)
51425147
return 0;
5143-
/*
5144-
* We need to pass a reference to the client and the parent
5145-
* spec when creating the parent rbd_dev. Images related by
5146-
* parent/child relationships always share both.
5147-
*/
5148-
parent_spec = rbd_spec_get(rbd_dev->parent_spec);
5149-
rbdc = __rbd_get_client(rbd_dev->rbd_client);
51505148

5151-
ret = -ENOMEM;
5152-
parent = rbd_dev_create(rbdc, parent_spec, NULL);
5153-
if (!parent)
5149+
if (++depth > RBD_MAX_PARENT_CHAIN_LEN) {
5150+
pr_info("parent chain is too long (%d)\n", depth);
5151+
ret = -EINVAL;
5152+
goto out_err;
5153+
}
5154+
5155+
parent = rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec,
5156+
NULL);
5157+
if (!parent) {
5158+
ret = -ENOMEM;
51545159
goto out_err;
5160+
}
5161+
5162+
/*
5163+
* Images related by parent/child relationships always share
5164+
* rbd_client and spec/parent_spec, so bump their refcounts.
5165+
*/
5166+
__rbd_get_client(rbd_dev->rbd_client);
5167+
rbd_spec_get(rbd_dev->parent_spec);
51555168

5156-
ret = rbd_dev_image_probe(parent, false);
5169+
ret = rbd_dev_image_probe(parent, depth);
51575170
if (ret < 0)
51585171
goto out_err;
5172+
51595173
rbd_dev->parent = parent;
51605174
atomic_set(&rbd_dev->parent_ref, 1);
5161-
51625175
return 0;
5176+
51635177
out_err:
5164-
if (parent) {
5165-
rbd_dev_unparent(rbd_dev);
5178+
rbd_dev_unparent(rbd_dev);
5179+
if (parent)
51665180
rbd_dev_destroy(parent);
5167-
} else {
5168-
rbd_put_client(rbdc);
5169-
rbd_spec_put(parent_spec);
5170-
}
5171-
51725181
return ret;
51735182
}
51745183

@@ -5286,7 +5295,7 @@ static void rbd_dev_image_release(struct rbd_device *rbd_dev)
52865295
* parent), initiate a watch on its header object before using that
52875296
* object to get detailed information about the rbd image.
52885297
*/
5289-
static int rbd_dev_image_probe(struct rbd_device *rbd_dev, bool mapping)
5298+
static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth)
52905299
{
52915300
int ret;
52925301

@@ -5304,7 +5313,7 @@ static int rbd_dev_image_probe(struct rbd_device *rbd_dev, bool mapping)
53045313
if (ret)
53055314
goto err_out_format;
53065315

5307-
if (mapping) {
5316+
if (!depth) {
53085317
ret = rbd_dev_header_watch_sync(rbd_dev);
53095318
if (ret) {
53105319
if (ret == -ENOENT)
@@ -5325,7 +5334,7 @@ static int rbd_dev_image_probe(struct rbd_device *rbd_dev, bool mapping)
53255334
* Otherwise this is a parent image, identified by pool, image
53265335
* and snap ids - need to fill in names for those ids.
53275336
*/
5328-
if (mapping)
5337+
if (!depth)
53295338
ret = rbd_spec_fill_snap_id(rbd_dev);
53305339
else
53315340
ret = rbd_spec_fill_names(rbd_dev);
@@ -5347,12 +5356,12 @@ static int rbd_dev_image_probe(struct rbd_device *rbd_dev, bool mapping)
53475356
* Need to warn users if this image is the one being
53485357
* mapped and has a parent.
53495358
*/
5350-
if (mapping && rbd_dev->parent_spec)
5359+
if (!depth && rbd_dev->parent_spec)
53515360
rbd_warn(rbd_dev,
53525361
"WARNING: kernel layering is EXPERIMENTAL!");
53535362
}
53545363

5355-
ret = rbd_dev_probe_parent(rbd_dev);
5364+
ret = rbd_dev_probe_parent(rbd_dev, depth);
53565365
if (ret)
53575366
goto err_out_probe;
53585367

@@ -5363,7 +5372,7 @@ static int rbd_dev_image_probe(struct rbd_device *rbd_dev, bool mapping)
53635372
err_out_probe:
53645373
rbd_dev_unprobe(rbd_dev);
53655374
err_out_watch:
5366-
if (mapping)
5375+
if (!depth)
53675376
rbd_dev_header_unwatch_sync(rbd_dev);
53685377
out_header_name:
53695378
kfree(rbd_dev->header_name);
@@ -5426,7 +5435,7 @@ static ssize_t do_rbd_add(struct bus_type *bus,
54265435
spec = NULL; /* rbd_dev now owns this */
54275436
rbd_opts = NULL; /* rbd_dev now owns this */
54285437

5429-
rc = rbd_dev_image_probe(rbd_dev, true);
5438+
rc = rbd_dev_image_probe(rbd_dev, 0);
54305439
if (rc < 0)
54315440
goto err_out_rbd_dev;
54325441

0 commit comments

Comments
 (0)