Skip to content

Commit b3b33b0

Browse files
committed
crush: pass weight vector size to map function
Pass the size of the weight vector into crush_do_rule() to ensure that we don't access values past the end. This can happen if the caller misbehaves and passes a weight vector that is smaller than max_devices. Currently the monitor tries to prevent that from happening, but this will gracefully tolerate previous bad osdmaps that got into this state. It's also a bit more defensive. Reflects ceph.git commit 5922e2c2b8335b5e46c9504349c3a55b7434c01a. Signed-off-by: Ilya Dryomov <ilya.dryomov@inktank.com> Reviewed-by: Sage Weil <sage@inktank.com>
1 parent 2b3e0c9 commit b3b33b0

3 files changed

Lines changed: 14 additions & 7 deletions

File tree

‎include/linux/crush/mapper.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ extern int crush_find_rule(const struct crush_map *map, int ruleset, int type, i
1414
extern int crush_do_rule(const struct crush_map *map,
1515
int ruleno,
1616
int x, int *result, int result_max,
17-
const __u32 *weights);
17+
const __u32 *weights, int weight_max);
1818

1919
#endif

‎net/ceph/crush/mapper.c‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,12 @@ static int crush_bucket_choose(struct crush_bucket *in, int x, int r)
264264
* true if device is marked "out" (failed, fully offloaded)
265265
* of the cluster
266266
*/
267-
static int is_out(const struct crush_map *map, const __u32 *weight, int item, int x)
267+
static int is_out(const struct crush_map *map,
268+
const __u32 *weight, int weight_max,
269+
int item, int x)
268270
{
271+
if (item >= weight_max)
272+
return 1;
269273
if (weight[item] >= 0x10000)
270274
return 0;
271275
if (weight[item] == 0)
@@ -292,7 +296,7 @@ static int is_out(const struct crush_map *map, const __u32 *weight, int item, in
292296
*/
293297
static int crush_choose(const struct crush_map *map,
294298
struct crush_bucket *bucket,
295-
const __u32 *weight,
299+
const __u32 *weight, int weight_max,
296300
int x, int numrep, int type,
297301
int *out, int outpos,
298302
int firstn, int recurse_to_leaf,
@@ -396,7 +400,7 @@ static int crush_choose(const struct crush_map *map,
396400
if (item < 0) {
397401
if (crush_choose(map,
398402
map->buckets[-1-item],
399-
weight,
403+
weight, weight_max,
400404
x, outpos+1, 0,
401405
out2, outpos,
402406
firstn, 0,
@@ -414,6 +418,7 @@ static int crush_choose(const struct crush_map *map,
414418
/* out? */
415419
if (itemtype == 0)
416420
reject = is_out(map, weight,
421+
weight_max,
417422
item, x);
418423
else
419424
reject = 0;
@@ -470,10 +475,12 @@ static int crush_choose(const struct crush_map *map,
470475
* @x: hash input
471476
* @result: pointer to result vector
472477
* @result_max: maximum result size
478+
* @weight: weight vector (for map leaves)
479+
* @weight_max: size of weight vector
473480
*/
474481
int crush_do_rule(const struct crush_map *map,
475482
int ruleno, int x, int *result, int result_max,
476-
const __u32 *weight)
483+
const __u32 *weight, int weight_max)
477484
{
478485
int result_len;
479486
int a[CRUSH_MAX_SET];
@@ -545,7 +552,7 @@ int crush_do_rule(const struct crush_map *map,
545552
j = 0;
546553
osize += crush_choose(map,
547554
map->buckets[-1-w[i]],
548-
weight,
555+
weight, weight_max,
549556
x, numrep,
550557
curstep->arg2,
551558
o+osize, j,

‎net/ceph/osdmap.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
11651165
}
11661166
r = crush_do_rule(osdmap->crush, ruleno, pps, osds,
11671167
min_t(int, pool->size, *num),
1168-
osdmap->osd_weight);
1168+
osdmap->osd_weight, osdmap->max_osd);
11691169
if (r < 0) {
11701170
pr_err("error %d from crush rule: pool %lld ruleset %d type %d"
11711171
" size %d\n", r, pgid.pool, pool->crush_ruleset,

0 commit comments

Comments
 (0)