History

misc: fastrpc: fix use-after-free race in fastrpc_map_create

fastrpc_map_lookup returns a raw pointer after releasing fl->lock. The caller fastrpc_map_create then calls fastrpc_map_get (kref_get_unless_zero) on this unprotected pointer. A concurrent MEM_UNMAP can free the map between the lock release and the kref operation, resulting in a use-after-free on the freed slab object. Restore the take_ref parameter to fastrpc_map_lookup so the reference is acquired atomically under fl->lock before the pointer is exposed to the caller.

Zhenghang Xiao committed 5 months ago

Commit 07ebe871 parent 5401fb41 file · +11 −14Browse files

Unified · Split

drivers/misc/fastrpc.c+11−14
@@ -388,7 +388,7 @@ static int fastrpc_map_get(struct fastrpc_map *map) · 387 unmodified lines
388388
389389
390390 static int fastrpc_map_lookup(struct fastrpc_user *fl, int fd,
391− struct fastrpc_map **ppmap)
391+ struct fastrpc_map **ppmap, bool take_ref)
392392 {
393393 struct fastrpc_map *map = NULL;
394394 struct dma_buf *buf;
@@ -403,6 +403,12 @@ static int fastrpc_map_lookup(struct fastrpc_user *fl, int fd, · 8 unmodified lines
403403 if (map->fd != fd || map->buf != buf)
404404 continue;
405405
406+ if (take_ref) {
407+ ret = fastrpc_map_get(map);
408+ if (ret)
409+ break;
410+ }
411+
406412 *ppmap = map;
407413 ret = 0;
408414 break;
@@ -920,19 +926,10 @@ get_err: · 511 unmodified lines
920926 static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
921927 u64 len, u32 attr, struct fastrpc_map **ppmap)
922928 {
923− struct fastrpc_session_ctx *sess = fl->sctx;
924− int err = 0;
925−
926− if (!fastrpc_map_lookup(fl, fd, ppmap)) {
927− if (!fastrpc_map_get(*ppmap))
928− return 0;
929− dev_dbg(sess->dev, "%s: Failed to get map fd=%d\n",
930− __func__, fd);
931− }
932−
933− err = fastrpc_map_attach(fl, fd, len, attr, ppmap);
929+ if (!fastrpc_map_lookup(fl, fd, ppmap, true))
930+ return 0;
934931
935− return err;
932+ return fastrpc_map_attach(fl, fd, len, attr, ppmap);
936933 }
937934
938935 /*
@@ -1202,7 +1199,7 @@ cleanup_fdlist: · 263 unmodified lines
12021199 for (i = 0; i < FASTRPC_MAX_FDLIST; i++) {
12031200 if (!fdlist[i])
12041201 break;
1205− if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap))
1202+ if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap, false))
12061203 fastrpc_map_put(mmap);
12071204 }
12081205

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t