Skip to contentSkip to conversation

net: ceph: make free_choose_arg_map() resilient to partial allocation

PATCH Applied Tuo Li 3 participants 3 messages Dec 20, 2025 – Jan 5 Ceph common code (libceph)

lore
Share & tools
lore

Tuo Li

author
#0 lore raw

PATCH v2 1/1 net: ceph: make free_choose_arg_map() resilient to partial allocation

free_choose_arg_map() may dereference a NULL pointer if its caller fails after a partial allocation.

For example, in decode_choose_args(), if allocation of arg_map->args fails, execution jumps to the fail label and free_choose_arg_map() is called. Since arg_map->size is updated to a non-zero value before memory allocation, free_choose_arg_map() will iterate over arg_map->args and dereference a NULL pointer.

To prevent this potential NULL pointer dereference and make free_choose_arg_map() more resilient, add checks for pointers before iterating.

---
v2:
* Add pointer checks before iterating in free_choose_arg_map(), instead of moving the arg_map->size assignment in decode_choose_args(). Thanks to Viacheslav Dubeyko for pointing out the issue with the previous patch, and to Ilya Dryomov for the helpful advice.

---
 net/ceph/osdmap.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
net/ceph/osdmap.c +12 -8
index 34b3ab59602f..08157945af43 100644
@@ -241,22 +241,26 @@ static struct crush_choose_arg_map *alloc_choose_arg_map(void)
 
 static void free_choose_arg_map(struct crush_choose_arg_map *arg_map)
 {
-	if (arg_map) {
-		int i, j;
+	int i, j;
+
+	if (!arg_map)
+		return;
 
-		WARN_ON(!RB_EMPTY_NODE(&arg_map->node));
+	WARN_ON(!RB_EMPTY_NODE(&arg_map->node));
 
+	if (arg_map->args) {
 		for (i = 0; i < arg_map->size; i++) {
 			struct crush_choose_arg *arg = &arg_map->args[i];
-
-			for (j = 0; j < arg->weight_set_size; j++)
-				kfree(arg->weight_set[j].weights);
-			kfree(arg->weight_set);
+			if (arg->weight_set) {
+				for (j = 0; j < arg->weight_set_size; j++)
+					kfree(arg->weight_set[j].weights);
+				kfree(arg->weight_set);
+			}
 			kfree(arg->ids);
 		}
 		kfree(arg_map->args);
-		kfree(arg_map);
 	}
+	kfree(arg_map);
 }
 
 DEFINE_RB_FUNCS(choose_arg_map, struct crush_choose_arg_map, choose_args_index,
Signature · 1 line
2.48.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 53 lines · Tuo Li

free_choose_arg_map() may dereference a NULL pointer if its caller fails after a partial allocation.

For example, in decode_choose_args(), if allocation of arg_map->args fails, execution jumps to the fail label and free_choose_arg_map() is called. Since arg_map->size is updated to a non-zero value before memory allocation, free_choose_arg_map() will iterate over arg_map->args and dereference a NULL pointer.

To prevent this potential NULL pointer dereference and make free_choose_arg_map() more resilient, add checks for pointers before iterating.

---
v2:
* Add pointer checks before iterating in free_choose_arg_map(), instead of moving the arg_map->size assignment in decode_choose_args(). Thanks to Viacheslav Dubeyko for pointing out the issue with the previous patch, and to Ilya Dryomov for the helpful advice.

---
 net/ceph/osdmap.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
net/ceph/osdmap.c +10 -7
index 34b3ab59602f..08157945af43 100644
@@ -241,22 +241,26 @@ static struct crush_choose_arg_map *alloc_choose_arg_map(void)
 
 static void free_choose_arg_map(struct crush_choose_arg_map *arg_map)
 {
-	if (arg_map) {
-		int i, j;
+	int i, j;
+
+	if (!arg_map)
+		return;
 
-		WARN_ON(!RB_EMPTY_NODE(&arg_map->node));
+	WARN_ON(!RB_EMPTY_NODE(&arg_map->node));
 
+	if (arg_map->args) {
 		for (i = 0; i < arg_map->size; i++) {
 			struct crush_choose_arg *arg = &arg_map->args[i];
-
-			for (j = 0; j < arg->weight_set_size; j++)
-				kfree(arg->weight_set[j].weights);
-			kfree(arg->weight_set);
+			if (arg->weight_set) {
+				for (j = 0; j < arg->weight_set_size; j++)
+					kfree(arg->weight_set[j].weights);
+				kfree(arg->weight_set);

net/ceph/osdmap.c static struct crush_choose_arg_map *alloc_choose_arg_map(void) · line 266

Quoted 10 lines
net/ceph/osdmap.c +2 -1
+			}
 			kfree(arg->ids);
 		}
 		kfree(arg_map->args);
-		kfree(arg_map);
 	}
+	kfree(arg_map);
 }
 
 DEFINE_RB_FUNCS(choose_arg_map, struct crush_choose_arg_map, choose_args_index,

Looks good. Thanks a lot for the fix.

Thanks,
Slava.

maintainer Applied
#2 lore raw
Quoted 64 lines · Viacheslav Dubeyko
Quoted 62 lines · Tuo Li

free_choose_arg_map() may dereference a NULL pointer if its caller fails after a partial allocation.

For example, in decode_choose_args(), if allocation of arg_map->args fails, execution jumps to the fail label and free_choose_arg_map() is called. Since arg_map->size is updated to a non-zero value before memory allocation, free_choose_arg_map() will iterate over arg_map->args and dereference a NULL pointer.

To prevent this potential NULL pointer dereference and make free_choose_arg_map() more resilient, add checks for pointers before iterating.

---
v2:
* Add pointer checks before iterating in free_choose_arg_map(), instead of moving the arg_map->size assignment in decode_choose_args(). Thanks to Viacheslav Dubeyko for pointing out the issue with the previous patch, and to Ilya Dryomov for the helpful advice.

---
 net/ceph/osdmap.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
net/ceph/osdmap.c +12 -8
index 34b3ab59602f..08157945af43 100644
@@ -241,22 +241,26 @@ static struct crush_choose_arg_map *alloc_choose_arg_map(void)
 
 static void free_choose_arg_map(struct crush_choose_arg_map *arg_map)
 {
-     if (arg_map) {
-             int i, j;
+     int i, j;
+
+     if (!arg_map)
+             return;
 
-             WARN_ON(!RB_EMPTY_NODE(&arg_map->node));
+     WARN_ON(!RB_EMPTY_NODE(&arg_map->node));
 
+     if (arg_map->args) {
              for (i = 0; i < arg_map->size; i++) {
                      struct crush_choose_arg *arg = &arg_map->args[i];
-
-                     for (j = 0; j < arg->weight_set_size; j++)
-                             kfree(arg->weight_set[j].weights);
-                     kfree(arg->weight_set);
+                     if (arg->weight_set) {
+                             for (j = 0; j < arg->weight_set_size; j++)
+                                     kfree(arg->weight_set[j].weights);
+                             kfree(arg->weight_set);
+                     }
                      kfree(arg->ids);
              }
              kfree(arg_map->args);
-             kfree(arg_map);
      }
+     kfree(arg_map);
 }
 
Quoted 8 lines

net/ceph/osdmap.c static struct crush_choose_arg_map *alloc_choose_arg_map(void) · line 266

Quoted 1 line
net/ceph/osdmap.c +0 -0
 DEFINE_RB_FUNCS(choose_arg_map, struct crush_choose_arg_map, choose_args_index,

Looks good. Thanks a lot for the fix.

Thanks,
Slava.

Applied.

Thanks,

Ilya