This is an automated email from the ASF dual-hosted git repository. GUIDINGLI pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 737cd2d4150cbd67b6fe5895064722ce20293a48 Author: jingfei <[email protected]> AuthorDate: Tue Nov 24 13:48:10 2026 +0800 fs/shm: keep the backing store while the object is mapped shm_open(O_TRUNC) freed the backing store of an existing object even while it was mapped. mmap() only holds an inode reference, so the mapping kept pointing at freed memory, and a later ftruncate() handed out a new buffer that the old mapping did not see. Count the mappings of an object in nmaps: mmap() and a munmap() that splits a mapping in two add one, a full munmap() drops one. While the count is non-zero, shm_open(O_TRUNC) fails with -EBUSY and leaves the backing store alone. Signed-off-by: jingfei <[email protected]> --- fs/shm/shm_open.c | 11 +++++++++++ fs/shm/shmfs.c | 20 ++++++++++++++++++-- fs/shm/shmfs.h | 4 ++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/fs/shm/shm_open.c b/fs/shm/shm_open.c index b148e8f5b20..b11173a11e3 100644 --- a/fs/shm/shm_open.c +++ b/fs/shm/shm_open.c @@ -133,6 +133,17 @@ static int file_shm_open(FAR struct file *shm, FAR const char *name, { object = inode->i_private; nxmutex_lock(&object->lock); + + /* A mapping points into the backing store, so keep it */ + + if (atomic_read(&object->nmaps) > 0) + { + nxmutex_unlock(&object->lock); + ret = -EBUSY; + inode_release(inode); + goto errout_with_lock; + } + shmfs_free_buffer(object); nxmutex_unlock(&object->lock); inode->i_size = 0; diff --git a/fs/shm/shmfs.c b/fs/shm/shmfs.c index 9c0dc92f72a..9538ee77fe0 100644 --- a/fs/shm/shmfs.c +++ b/fs/shm/shmfs.c @@ -471,12 +471,20 @@ static int shmfs_mmap(FAR struct file *filep, #endif ret = shmfs_map_object(object, &entry->vaddr); + if (ret >= 0) + { + atomic_add(&object->nmaps, 1); + } out: nxmutex_unlock(&object->lock); - if (ret < 0 || - (ret = shmfs_add_map(entry, filep->f_inode)) < 0) + if (ret >= 0 && (ret = shmfs_add_map(entry, filep->f_inode)) < 0) + { + atomic_sub(&object->nmaps, 1); + } + + if (ret < 0) { inode_release(filep->f_inode); } @@ -536,6 +544,7 @@ static int shmfs_munmap_partial(FAR struct task_group_s *group, FAR void *start, size_t length) { FAR struct mm_map_s *mm = get_group_mm(group); + FAR struct shmfs_object_s *object; FAR char *entry_end = (FAR char *)entry->vaddr + entry->length; FAR char *unmap_end = (FAR char *)start + length; off_t head; @@ -600,6 +609,9 @@ static int shmfs_munmap_partial(FAR struct task_group_s *group, entry->length = head; inode_addref((FAR struct inode *)entry->priv.p); + + object = ((FAR struct inode *)entry->priv.p)->i_private; + atomic_add(&object->nmaps, 1); return OK; } @@ -632,6 +644,7 @@ static int shmfs_munmap(FAR struct task_group_s *group, FAR void *start, size_t length) { + FAR struct shmfs_object_s *object; FAR struct inode *inode; int ret; @@ -653,6 +666,9 @@ static int shmfs_munmap(FAR struct task_group_s *group, if (ret == OK) { + object = inode->i_private; + atomic_sub(&object->nmaps, 1); + ret = shmfs_release(inode); } diff --git a/fs/shm/shmfs.h b/fs/shm/shmfs.h index 88a08db1668..bc87725b815 100644 --- a/fs/shm/shmfs.h +++ b/fs/shm/shmfs.h @@ -61,6 +61,10 @@ struct shmfs_object_s */ FAR void *paddr; + + /* Number of mappings; paddr must not be freed while it is non-zero. */ + + atomic_t nmaps; }; /****************************************************************************
