From eec9f0cfa99435bb9b990d0823ef150b1bd9cdc4 Mon Sep 17 00:00:00 2001 From: prabha Date: Thu, 25 Nov 2021 13:09:50 +0530 Subject: [PATCH] msm: adsprpc: Handle UAF in process shell memory Added flag to indicate memory used in process initialization. And, this memory would not removed in internal unmap to avoid UAF or double free. Change-Id: Ie470fe58ac334421d186feb41fa67bd24bb5efea Signed-off-by: prabha --- drivers/char/adsprpc.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/drivers/char/adsprpc.c b/drivers/char/adsprpc.c index 8c3103eb30a2..05cfe33e6987 100644 --- a/drivers/char/adsprpc.c +++ b/drivers/char/adsprpc.c @@ -573,6 +573,7 @@ struct fastrpc_mmap { bool in_use; /* Indicates if persistent map is in use*/ struct timespec64 map_start_time; struct timespec64 map_end_time; + bool is_filemap; /*flag to indicate map used in process init*/ }; enum fastrpc_perfkeys { @@ -1199,9 +1200,10 @@ static int fastrpc_mmap_remove(struct fastrpc_file *fl, int fd, uintptr_t va, spin_lock(&me->hlock); hlist_for_each_entry_safe(map, n, &me->maps, hn) { - if ((fd < 0 || map->fd == fd) && map->raddr == va && + if (map->refs == 1 && map->raddr == va && map->raddr + map->len == va + len && - map->refs == 1 && !map->is_persistent) { + /*Remove map if not used in process initialization*/ + !map->is_filemap) { match = map; hlist_del_init(&map->hn); break; @@ -1213,9 +1215,10 @@ static int fastrpc_mmap_remove(struct fastrpc_file *fl, int fd, uintptr_t va, return 0; } hlist_for_each_entry_safe(map, n, &fl->maps, hn) { - if ((fd < 0 || map->fd == fd) && map->raddr == va && + if (map->refs == 1 && map->raddr == va && map->raddr + map->len == va + len && - map->refs == 1) { + /*Remove map if not used in process initialization*/ + !map->is_filemap) { match = map; hlist_del_init(&map->hn); break; @@ -1399,6 +1402,7 @@ static int fastrpc_mmap_create(struct fastrpc_file *fl, int fd, map->fd = fd; map->attr = attr; map->frpc_md_index = -1; + map->is_filemap = false; ktime_get_real_ts64(&map->map_start_time); if (mflags == ADSP_MMAP_HEAP_ADDR || mflags == ADSP_MMAP_REMOTE_HEAP_ADDR) { @@ -3683,6 +3687,8 @@ static int fastrpc_init_create_dynamic_process(struct fastrpc_file *fl, mutex_lock(&fl->map_mutex); err = fastrpc_mmap_create(fl, init->filefd, 0, init->file, init->filelen, mflags, &file); + if (file) + file->is_filemap = true; mutex_unlock(&fl->map_mutex); if (err) goto bail; @@ -3904,6 +3910,8 @@ static int fastrpc_init_create_static_process(struct fastrpc_file *fl, mutex_lock(&fl->map_mutex); err = fastrpc_mmap_create(fl, -1, 0, init->mem, init->memlen, ADSP_MMAP_REMOTE_HEAP_ADDR, &mem); + if (mem) + mem->is_filemap = true; mutex_unlock(&fl->map_mutex); if (err) goto bail;