Skip to content

Commit e3fe30e

Browse files
lituo1996idryomov
andcommitted
libceph: 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. Cc: stable@vger.kernel.org Co-authored-by: Ilya Dryomov <idryomov@gmail.com> Signed-off-by: Tuo Li <islituo@gmail.com> Reviewed-by: Viacheslav Dubeyko <Slava.Dubeyko@ibm.com> Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
1 parent bc417a4 commit e3fe30e

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

net/ceph/osdmap.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,22 +241,26 @@ static struct crush_choose_arg_map *alloc_choose_arg_map(void)
241241

242242
static void free_choose_arg_map(struct crush_choose_arg_map *arg_map)
243243
{
244-
if (arg_map) {
245-
int i, j;
244+
int i, j;
245+
246+
if (!arg_map)
247+
return;
246248

247-
WARN_ON(!RB_EMPTY_NODE(&arg_map->node));
249+
WARN_ON(!RB_EMPTY_NODE(&arg_map->node));
248250

251+
if (arg_map->args) {
249252
for (i = 0; i < arg_map->size; i++) {
250253
struct crush_choose_arg *arg = &arg_map->args[i];
251-
252-
for (j = 0; j < arg->weight_set_size; j++)
253-
kfree(arg->weight_set[j].weights);
254-
kfree(arg->weight_set);
254+
if (arg->weight_set) {
255+
for (j = 0; j < arg->weight_set_size; j++)
256+
kfree(arg->weight_set[j].weights);
257+
kfree(arg->weight_set);
258+
}
255259
kfree(arg->ids);
256260
}
257261
kfree(arg_map->args);
258-
kfree(arg_map);
259262
}
263+
kfree(arg_map);
260264
}
261265

262266
DEFINE_RB_FUNCS(choose_arg_map, struct crush_choose_arg_map, choose_args_index,

0 commit comments

Comments
 (0)