libceph: reject zero bucket types in crush_decode
PATCH v3 1/1 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(), which then indexes 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.
---
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 | 1 + net/ceph/osdmap.c | 2 ++ 2 files changed, 3 insertions(+)
include/linux/crush/crush.h +1 -0
@@ -111,6 +111,7 @@ struct crush_rule {* straw2 O(n) optimal optimal */ enum { +CRUSH_BUCKET_DEVICE =0 ,CRUSH_BUCKET_UNIFORM =1 ,CRUSH_BUCKET_LIST =2 ,CRUSH_BUCKET_TREE =3 ,
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_BUCKET_DEVICE ) + goto bad; b->alg =ceph_decode_8 (p);if (b->alg != alg) { b->alg =0 ;
Signature · 1 line
2.47.3
Quoted 41 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(), which then indexes 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.
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 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 | 1 + net/ceph/osdmap.c | 2 ++ 2 files changed, 3 insertions(+)include/linux/crush/crush.h +0
-0
include/linux/crush/crush.h struct crush_rule { · line 117
Quoted 7 lines
include/linux/crush/crush.h +1
-0@@ -111,6 +111,7 @@ struct crush_rule {* straw2 O(n) optimal optimal */ enum { +CRUSH_BUCKET_DEVICE =0 ,CRUSH_BUCKET_UNIFORM =1 ,CRUSH_BUCKET_LIST =2 ,CRUSH_BUCKET_TREE =3 ,
Hi,
this enum contains all existing bucket algorithms. Type and algorithm are different things and should not be mixed up. Therefore, this is the wrong place for this definition. If necessary, I would instead suggest defining it as a single new named constant. The previous name from v2 of the patch would also be more appropriate for this as CRUSH_BUCKET_* currently refers exclusively to bucket algorithms.
Also, when creating this constant, it would be good to use it consistently rather than only for one occurrence of type 0. However, I wouldn't say that this has to be done in this specific bugfix patch.
Best regards,
Raphael
Quoted 17 lines
net/ceph/osdmap.c +2
-1@@ -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_BUCKET_DEVICE ) + goto bad; b->alg =ceph_decode_8 (p);if (b->alg != alg) { b->alg =0 ; --2.47.3
Viacheslav Dubeyko
maintainerQuoted 71 lines · Raphael Zimmer
Quoted 50 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(), which then indexes 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.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 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 | 1 +
net/ceph/osdmap.c | 2 ++2 files changed, 3 insertions(+)include/linux/crush/crush.h +0
-0index
30dba392b7302de3e14861f8b769242cd8cbac9e..ea0b1de2902585ba9a63566cfbb2b0569846a00b100644include/linux/crush/crush.h struct crush_rule { · line 117
Quoted 7 lines
include/linux/crush/crush.h +1
-0@@ -111,6 +111,7 @@ struct crush_rule {* straw2 O(n) optimal optimal */ enum { + CRUSH_BUCKET_DEVICE = 0, CRUSH_BUCKET_UNIFORM = 1, CRUSH_BUCKET_LIST = 2, CRUSH_BUCKET_TREE = 3,Hi,
this enum contains all existing bucket algorithms. Type and algorithm are different things and should not be mixed up. Therefore, this is the
wrong place for this definition. If necessary, I would instead suggest
defining it as a single new named constant. The previous name from v2 of
the patch would also be more appropriate for this as CRUSH_BUCKET_* currently refers exclusively to bucket algorithms.
Quoted 6 lines
Also, when creating this constant, it would be good to use it consistently rather than only for one occurrence of type 0. However, I
wouldn't say that this has to be done in this specific bugfix patch.
Yeah, you are right here. I didn't double check the constant declarations. My bad. :)
Thanks,
Slava.