Skip to contentSkip to conversation

libceph: reject zero bucket types in crush_decode

PATCH Superseded Ren Wei 3 participants 3 messages Jun 2 Ceph common code (libceph)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Ren Wei

author
#0 lore raw

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.

Fixes f24e9980eb86 ("ceph: OSD client")
Assisted-by Codex:GPT-5.4

---
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
index 30dba392b7302de3e14861f8b769242cd8cbac9e..ea0b1de2902585ba9a63566cfbb2b0569846a00b 100644
@@ -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
index 8b5b0587a0cfa2c437b76c8c87251199efe20291..f6cc93939907bc61943069c462cb752d13c8b3ed 100644
@@ -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
#1 lore raw
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 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.

Fixes f24e9980eb86 ("ceph: OSD client")
Assisted-by Codex:GPT-5.4

---
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 +0 -0
index 30dba392b7302de3e14861f8b769242cd8cbac9e..ea0b1de2902585ba9a63566cfbb2b0569846a00b 100644

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
index 8b5b0587a0cfa2c437b76c8c87251199efe20291..f6cc93939907bc61943069c462cb752d13c8b3ed 100644
@@ -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

#2 lore raw
Quoted 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
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.

Fixes f24e9980eb86 ("ceph: OSD client")
Assisted-by Codex:GPT-5.4

---
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 +0 -0

index 30dba392b7302de3e14861f8b769242cd8cbac9e..ea0b1de2902585ba9a63566cf bb2b0569846a00b 100644

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.

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.