This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 2e55fcfb5276032672835bfe430338092073c491 Author: dongjiuzhu1 <[email protected]> AuthorDate: Thu Feb 26 14:31:43 2026 +0800 fs/epoll: push the TLS cleanup before file_get() epoll_wait() pushed epoll_cleanup() only around the semaphore wait. A thread killed after epoll_head_from_fd() took the file reference, but outside that window, never ran the cleanup and leaked the reference. Push the cleanup in epoll_head_from_fd() before file_get() and pop it where the reference is dropped. Signed-off-by: dongjiuzhu1 <[email protected]> Signed-off-by: Bowen Wang <[email protected]> --- fs/vfs/fs_epoll.c | 57 ++++++++++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/fs/vfs/fs_epoll.c b/fs/vfs/fs_epoll.c index 023bd3c2369..06768f5933f 100644 --- a/fs/vfs/fs_epoll.c +++ b/fs/vfs/fs_epoll.c @@ -117,6 +117,7 @@ static int epoll_do_poll(FAR struct file *filep, static int epoll_setup(FAR epoll_head_t *eph); static int epoll_teardown(FAR epoll_head_t *eph, FAR struct epoll_event *evs, int maxevents); +static void epoll_cleanup(FAR void *arg); /**************************************************************************** * Private Data @@ -155,11 +156,18 @@ static FAR epoll_head_t *epoll_head_from_fd(int fd, FAR struct file **filep) { int ret; + /* Push a cancellation point onto the stack. This will be called if + * the thread is canceled. + */ + + tls_cleanup_push(tls_get_info(), epoll_cleanup, filep); + /* Get file pointer by file descriptor */ ret = file_get(fd, filep); if (ret < 0) { + tls_cleanup_pop(tls_get_info(), 0); set_errno(-ret); return NULL; } @@ -170,6 +178,7 @@ static FAR epoll_head_t *epoll_head_from_fd(int fd, FAR struct file **filep) (*filep)->f_inode->u.i_ops != &g_epoll_ops) { file_put(*filep); + tls_cleanup_pop(tls_get_info(), 0); set_errno(EBADF); return NULL; } @@ -177,6 +186,12 @@ static FAR epoll_head_t *epoll_head_from_fd(int fd, FAR struct file **filep) return (*filep)->f_priv; } +static void epoll_filep_close(FAR struct file *filep) +{ + file_put(filep); + tls_cleanup_pop(tls_get_info(), 0); +} + static int epoll_do_open(FAR struct file *filep) { FAR epoll_head_t *eph = filep->f_priv; @@ -542,7 +557,7 @@ static int epoll_teardown(FAR epoll_head_t *eph, FAR struct epoll_event *evs, static void epoll_cleanup(FAR void *arg) { - file_put(arg); + file_put(*(FAR struct file **)arg); } /**************************************************************************** @@ -657,7 +672,7 @@ void epoll_close(int epfd) int epoll_ctl(int epfd, int op, int fd, FAR struct epoll_event *ev) { FAR struct list_node *extend; - FAR struct file *filep; + FAR struct file *filep = NULL; FAR epoll_head_t *eph; FAR epoll_node_t *epn; int ret; @@ -899,12 +914,12 @@ int epoll_ctl(int epfd, int op, int fd, FAR struct epoll_event *ev) out: nxmutex_unlock(&eph->lock); - file_put(filep); + epoll_filep_close(filep); return OK; err: nxmutex_unlock(&eph->lock); err_without_lock: - file_put(filep); + epoll_filep_close(filep); set_errno(-ret); return ERROR; } @@ -916,7 +931,7 @@ err_without_lock: int epoll_pwait(int epfd, FAR struct epoll_event *evs, int maxevents, int timeout, FAR const sigset_t *sigmask) { - FAR struct file *filep; + FAR struct file *filep = NULL; FAR epoll_head_t *eph; #ifndef CONFIG_DISABLE_ALL_SIGNALS sigset_t oldsigmask; @@ -954,12 +969,6 @@ retry: nxsig_procmask(SIG_SETMASK, sigmask, &oldsigmask); #endif - /* Push a cancellation point onto the stack. This will be called if - * the thread is canceled. - */ - - tls_cleanup_push(tls_get_info(), epoll_cleanup, filep); - if (timeout == 0) { ret = -ETIMEDOUT; @@ -973,10 +982,6 @@ retry: ret = nxsem_wait(&eph->sem); } - /* Pop the cancellation point */ - - tls_cleanup_pop(tls_get_info(), 0); - #ifndef CONFIG_DISABLE_ALL_SIGNALS nxsig_procmask(SIG_SETMASK, &oldsigmask, NULL); #endif @@ -996,11 +1001,12 @@ retry: ret = num; } - file_put(filep); + epoll_filep_close(filep); + return ret; err: - file_put(filep); + epoll_filep_close(filep); set_errno(-ret); out: ferr("epoll wait failed:%d, timeout:%d\n", errno, timeout); @@ -1021,7 +1027,7 @@ out: int epoll_wait(int epfd, FAR struct epoll_event *evs, int maxevents, int timeout) { - FAR struct file *filep; + FAR struct file *filep = NULL; FAR epoll_head_t *eph; int ret; @@ -1050,12 +1056,6 @@ retry: goto err; } - /* Push a cancellation point onto the stack. This will be called if - * the thread is canceled. - */ - - tls_cleanup_push(tls_get_info(), epoll_cleanup, filep); - /* Wait the poll ready */ if (timeout == 0) @@ -1071,10 +1071,6 @@ retry: ret = nxsem_wait(&eph->sem); } - /* Pop the cancellation point */ - - tls_cleanup_pop(tls_get_info(), 0); - if (ret < 0 && ret != -ETIMEDOUT) { goto err; @@ -1091,11 +1087,12 @@ retry: ret = num; } - file_put(filep); + epoll_filep_close(filep); + return ret; err: - file_put(filep); + epoll_filep_close(filep); set_errno(-ret); out: ferr("epoll wait failed:%d, timeout:%d\n", errno, timeout);
