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.
Unified · Split
| @@ -388,7 +388,7 @@ static int fastrpc_map_get(struct fastrpc_map *map) · 387 unmodified lines | |||
| 388 | 388 | ||
| 389 | 389 | ||
| 390 | static int fastrpc_map_lookup(struct fastrpc_user *fl, int fd, | 390 | static int fastrpc_map_lookup(struct fastrpc_user *fl, int fd, |
| 391 | struct fastrpc_map **ppmap) | 391 | struct fastrpc_map **ppmap, bool take_ref) |
| 392 | { | 392 | { |
| 393 | struct fastrpc_map *map = NULL; | 393 | struct fastrpc_map *map = NULL; |
| 394 | struct dma_buf *buf; | 394 | struct dma_buf *buf; |
| @@ -403,6 +403,12 @@ static int fastrpc_map_lookup(struct fastrpc_user *fl, int fd, · 8 unmodified lines | |||
| 403 | if (map->fd != fd || map->buf != buf) | 403 | if (map->fd != fd || map->buf != buf) |
| 404 | continue; | 404 | continue; |
| 405 | 405 | ||
| 406 | if (take_ref) { | ||
| 407 | ret = fastrpc_map_get(map); | ||
| 408 | if (ret) | ||
| 409 | break; | ||
| 410 | } | ||
| 411 | |||
| 406 | *ppmap = map; | 412 | *ppmap = map; |
| 407 | ret = 0; | 413 | ret = 0; |
| 408 | break; | 414 | break; |
| @@ -920,19 +926,10 @@ get_err: · 511 unmodified lines | |||
| 920 | static int fastrpc_map_create(struct fastrpc_user *fl, int fd, | 926 | static int fastrpc_map_create(struct fastrpc_user *fl, int fd, |
| 921 | u64 len, u32 attr, struct fastrpc_map **ppmap) | 927 | u64 len, u32 attr, struct fastrpc_map **ppmap) |
| 922 | { | 928 | { |
| 923 | struct fastrpc_session_ctx *sess = fl->sctx; | 929 | if (!fastrpc_map_lookup(fl, fd, ppmap, true)) |
| 924 | int err = 0; | 930 | return 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); | ||
| 934 | 931 | ||
| 935 | return err; | 932 | return fastrpc_map_attach(fl, fd, len, attr, ppmap); |
| 936 | } | 933 | } |
| 937 | 934 | ||
| 938 | /* | 935 | /* |
| @@ -1202,7 +1199,7 @@ cleanup_fdlist: · 263 unmodified lines | |||
| 1202 | for (i = 0; i < FASTRPC_MAX_FDLIST; i++) { | 1199 | for (i = 0; i < FASTRPC_MAX_FDLIST; i++) { |
| 1203 | if (!fdlist[i]) | 1200 | if (!fdlist[i]) |
| 1204 | break; | 1201 | break; |
| 1205 | if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap)) | 1202 | if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap, false)) |
| 1206 | fastrpc_map_put(mmap); | 1203 | fastrpc_map_put(mmap); |
| 1207 | } | 1204 | } |
| 1208 | 1205 | ||