misc: fastrpc: Don't remove map on creater_process and device_release

Do not remove the map from the list on error path in
fastrpc_init_create_process, instead call fastrpc_map_put, to avoid
use-after-free. Do not remove it on fastrpc_device_release either,
call fastrpc_map_put instead.

The fastrpc_free_map is the only proper place to remove the map.
This is called only after the reference count is 0.

Fixes: b49f6d83e290 ("misc: fastrpc: Fix a possible double free")
Cc: stable <stable@kernel.org>
Co-developed-by: Ola Jeppsson <ola@snap.com>
Signed-off-by: Ola Jeppsson <ola@snap.com>
Signed-off-by: Abel Vesa <abel.vesa@linaro.org>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
Link: https://lore.kernel.org/r/20221124174941.418450-3-srinivas.kandagatla@linaro.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Abel Vesa and committed by
Greg Kroah-Hartman
5bb96c8f 9446fa16

+9 -9
+9 -9
drivers/misc/fastrpc.c
··· 334 334 dma_buf_put(map->buf); 335 335 } 336 336 337 + if (map->fl) { 338 + spin_lock(&map->fl->lock); 339 + list_del(&map->node); 340 + spin_unlock(&map->fl->lock); 341 + map->fl = NULL; 342 + } 343 + 337 344 kfree(map); 338 345 } 339 346 ··· 1441 1434 fl->init_mem = NULL; 1442 1435 fastrpc_buf_free(imem); 1443 1436 err_alloc: 1444 - if (map) { 1445 - spin_lock(&fl->lock); 1446 - list_del(&map->node); 1447 - spin_unlock(&fl->lock); 1448 - fastrpc_map_put(map); 1449 - } 1437 + fastrpc_map_put(map); 1450 1438 err: 1451 1439 kfree(args); 1452 1440 ··· 1517 1515 fastrpc_context_put(ctx); 1518 1516 } 1519 1517 1520 - list_for_each_entry_safe(map, m, &fl->maps, node) { 1521 - list_del(&map->node); 1518 + list_for_each_entry_safe(map, m, &fl->maps, node) 1522 1519 fastrpc_map_put(map); 1523 - } 1524 1520 1525 1521 list_for_each_entry_safe(buf, b, &fl->mmaps, node) { 1526 1522 list_del(&buf->node);