libceph: reject zero bucket types in crush_decode
PATCH v4 1/2 libceph: reject zero bucket types in crush_decode
From: Douya Le <[email protected]>
CRUSH bucket type 0 is reserved for devices. The mapper relies on that invariant and uses type 0 to identify leaf devices.
If crush_decode() accepts a bucket with type 0, a malformed CRUSH map can make the mapper treat a negative bucket ID as a device and pass it to is_out(), causing an out-of-bounds access when indexing the OSD weight array with a negative value.
Reject zero bucket types while decoding the CRUSH map so the invalid state never reaches the mapper.
Define the reserved device type as a standalone named constant instead of mixing it into the bucket algorithm enum.
---
Changes in v4:
- define CRUSH_ITEM_TYPE_DEVICE as a standalone constant instead of
putting it in the bucket algorithm enum
- move the remaining type-0-to-constant cleanups into a separate patch
- v3 Link: https://lore.kernel.org/all/[email protected]/
Changes in v3:
- use CRUSH_BUCKET_DEVICE instead of CRUSH_ITEM_TYPE_DEVICE
- v2 Link: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- use CRUSH_ITEM_TYPE_DEVICE instead of hardcoded 0
- v1 Link: https://lore.kernel.org/all/4ad15b49fbbae00d86dfe12348bf94d45aa60ac2.1779949116.git.ldy3087146292@gmail.com/
--- include/linux/crush/crush.h | 4 +++- net/ceph/osdmap.c | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-)
include/linux/crush/crush.h +3 -1
@@ -110,6 +110,8 @@ struct crush_rule {* straw O(n) better better * straw2 O(n) optimal optimal */ +#define CRUSH_ITEM_TYPE_DEVICE 0 +enum {CRUSH_BUCKET_UNIFORM =1 ,CRUSH_BUCKET_LIST =2 ,
@@ -130,7 +132,7 @@ extern const char *crush_bucket_alg_name(int alg);struct crush_bucket {__s32 id;/* this'll be negative */ -__u16 type;/* non-zero; type=0 is reserved for devices */ +__u16 type;/* non-zero; CRUSH_ITEM_TYPE_DEVICE is reserved */ __u8 alg;/* one of CRUSH_BUCKET_* */ __u8 hash;/* which hash function to use, CRUSH_HASH_* */ __u32 weight;/* 16-bit fixed point */
net/ceph/osdmap.c +2 -0
@@ -518,6 +518,8 @@ static struct crush_map *crush_decode(void *pbyval, void *end)ceph_decode_need (p, end,4 *sizeof (u32), bad); b->id =ceph_decode_32 (p); b->type =ceph_decode_16 (p); +if (b->type ==CRUSH_ITEM_TYPE_DEVICE ) + goto bad; b->alg =ceph_decode_8 (p);if (b->alg != alg) { b->alg =0 ;
Signature · 1 line
2.47.3
PATCH v4 2/2 libceph: crush: use CRUSH_ITEM_TYPE_DEVICE for leaf device checks
From: Douya Le <[email protected]>
The CRUSH mapper uses type 0 to identify leaf devices in several paths.
Replace those hardcoded checks with CRUSH_ITEM_TYPE_DEVICE so the code uses the named device-type constant consistently.
--- net/ceph/crush/mapper.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
net/ceph/crush/mapper.c +4 -4
@@ -530,7 +530,7 @@ static int crush_choose_firstn(const struct crush_map *map,if (item <0 ) itemtype = map->buckets[-1 -item]->type;else - itemtype =0 ; + itemtype =CRUSH_ITEM_TYPE_DEVICE ;dprintk (" item %d type %d\n" , item, itemtype);/* keep going? */
@@ -588,7 +588,7 @@ static int crush_choose_firstn(const struct crush_map *map,if (!reject && !collide) {/* out? */ -if (itemtype ==0 ) +if (itemtype ==CRUSH_ITEM_TYPE_DEVICE ) reject =is_out (map, weight, weight_max, item, x);
@@ -743,7 +743,7 @@ static void crush_choose_indep(const struct crush_map *map,if (item <0 ) itemtype = map->buckets[-1 -item]->type;else - itemtype =0 ; + itemtype =CRUSH_ITEM_TYPE_DEVICE ;dprintk (" item %d type %d\n" , item, itemtype);/* keep going? */
@@ -796,7 +796,7 @@ static void crush_choose_indep(const struct crush_map *map, }/* out? */ -if (itemtype ==0 && +if (itemtype ==CRUSH_ITEM_TYPE_DEVICE &&is_out (map, weight, weight_max, item, x))break ;
Signature · 1 line
2.47.3
Viacheslav Dubeyko
maintainerQuoted 59 lines · Ren Wei
From: Douya Le <[email protected]>
CRUSH bucket type 0 is reserved for devices. The mapper relies on that invariant and uses type 0 to identify leaf devices.
If
crush_decode()accepts a bucket with type 0, a malformed CRUSH map can make the mapper treat a negative bucket ID as a device and pass it
tois_out(), causing an out-of-bounds access when indexing the OSD weight array with a negative value.Reject zero bucket types while decoding the CRUSH map so the invalid state never reaches the mapper.
Define the reserved device type as a standalone named constant instead
of mixing it into the bucket algorithm enum.Fixes f24e9980eb86 ("ceph: OSD client")Reported-by Yuan Tan <[email protected]>Reported-by Zhengchuan Liang <[email protected]>Reported-by Xin Liu <[email protected]>Assisted-by Codex:GPT-5.4Signed-off-by Douya Le <[email protected]>Signed-off-by Ren Wei <[email protected]>---
Changes in v4:
- defineCRUSH_ITEM_TYPE_DEVICEas a standalone constant instead of putting it in the bucket algorithm enum
- move the remaining type-0-to-constant cleanups into a separate patch
- v3 Link:
https://lore.kernel.org/all/[email protected]/Changes in v3:
- useCRUSH_BUCKET_DEVICEinstead ofCRUSH_ITEM_TYPE_DEVICE
- v2 Link:
https://lore.kernel.org/all/[email protected]/Changes in v2:
- useCRUSH_ITEM_TYPE_DEVICEinstead of hardcoded 0
- v1 Link:
https://lore.kernel.org/all/4ad15b49fbbae00d86dfe12348bf94d45aa60ac2.1779949116.git.ldy3087146292@gmail.com/---
include/linux/crush/crush.h | 4 +++-
net/ceph/osdmap.c | 2 ++2 files changed, 5 insertions(+), 1 deletion(-)include/linux/crush/crush.h +0
-0index
30dba392b7302de3e14861f8b769242cd8cbac9e..76bd1fa036538df86d548b1c668163035c30ade6100644
include/linux/crush/crush.h struct crush_rule { · line 113
Quoted 4 lines
include/linux/crush/crush.h +1
-0@@ -110,6 +110,8 @@ struct crush_rule {* straw O(n) better better * straw2 O(n) optimal optimal */ +#define CRUSH_ITEM_TYPE_DEVICE 0
If I understood correctly, the b->type field encodes the CRUSH topology type (host, rack, row, datacenter, etc.). So, I think to have enum will be much better here.
Thanks,
Slava.
Quoted 33 lines
include/linux/crush/crush.h +1
-0+ enum { CRUSH_BUCKET_UNIFORM = 1, CRUSH_BUCKET_LIST = 2,@@ -130,7 +132,7 @@ extern const char *crush_bucket_alg_name(int alg);
structcrush_bucket{
__s32id; /* this'll be negative */include/linux/crush/crush.h +0
-1- __u16 type; /* non-zero; type=0 is reserved for devices*/
include/linux/crush/crush.h +1
-0+ __u16 type; /* non-zero; CRUSH_ITEM_TYPE_DEVICE isreserved */
__u8alg; /* one of CRUSH_BUCKET_* */
__u8hash; /* which hash function to use, CRUSH_HASH_* */
__u32weight; /* 16-bit fixed point */net/ceph/osdmap.c +0
-0index
8b5b0587a0cfa2c437b76c8c87251199efe20291..21cdd33f2945e0c1f45277ac05fda373bf217586100644net/ceph/osdmap.c +2
-0@@ -518,6 +518,8 @@ static struct crush_map *crush_decode(void *pbyval, void *end) ceph_decode_need(p, end, 4*sizeof(u32), bad); b->id = ceph_decode_32(p); b->type = ceph_decode_16(p); +if (b->type ==CRUSH_ITEM_TYPE_DEVICE ) + goto bad; b->alg = ceph_decode_8(p); if (b->alg != alg) {
b->alg= 0;
Re: [PATCH v4 2/2] libceph: crush: use CRUSH_ITEM_TYPE_DEVICE for leaf device checks
In reply to Ren WeiQuoted 8 lines · Ren Wei
From: Douya Le <[email protected]>
The CRUSH mapper uses type 0 to identify leaf devices in several paths.
Replace those hardcoded checks with
CRUSH_ITEM_TYPE_DEVICEso the code
uses the named device-type constant consistently.
Usually, patchset requires a cover letter. The patch itself looks good.
Thanks,
Slava.
Quoted 66 lines
Signed-off-by Douya Le <[email protected]>Signed-off-by Ren Wei <[email protected]>---
net/ceph/crush/mapper.c | 8 ++++----1 file changed, 4 insertions(+), 4 deletions(-)net/ceph/crush/mapper.c +0
-0index
17b041779fb9da2deb8cdbf58439392d7fa2e8ef..00e139efd54eae36f75ed7cebd79ac9b0dc27262100644net/ceph/crush/mapper.c +0
-0@@ -530,7 +530,7 @@ static int crush_choose_firstn(const struct crush_map *map, if (item < 0)itemtype = map->buckets[-1- item]->type;
elsenet/ceph/crush/mapper.c +1
-1- itemtype =0 ; + itemtype =
CRUSH_ITEM_TYPE_DEVICE;
dprintk(" item %d type %d\n", item, itemtype);
/* keep going? */net/ceph/crush/mapper.c +0
-0@@ -588,7 +588,7 @@ static int crush_choose_firstn(const struct crush_map *map,
if (!reject && !collide) { /* out? */net/ceph/crush/mapper.c +1
-1-if (itemtype ==0 ) + if (itemtype ==
CRUSH_ITEM_TYPE_DEVICE)
reject = is_out(map, weight,
weight_max,
item , x);net/ceph/crush/mapper.c +0
-0@@ -743,7 +743,7 @@ static void crush_choose_indep(const struct crush_map *map, if (item < 0)itemtype = map->buckets[-1- item]->type;
elsenet/ceph/crush/mapper.c +1
-1- itemtype =0 ; + itemtype =
CRUSH_ITEM_TYPE_DEVICE;
dprintk(" item %d type %d\n", item, itemtype);
/* keep going? */net/ceph/crush/mapper.c +0
-0@@ -796,7 +796,7 @@ static void crush_choose_indep(const struct crush_map *map, }
/* out? */net/ceph/crush/mapper.c +1
-1- if (itemtype == 0 && + if (itemtype ==
CRUSH_ITEM_TYPE_DEVICE&&
is_out(map, weight, weight_max, item, x))
break;