diff options
| author | Bernard Xiong <[email protected]> | 2026-11-16 11:29:11 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-11-16 11:29:11 +0800 |
| commit | 7c974b59fdc258afbdb19c378f250bcd95511537 (patch) | |
| tree | 66ab0f2b15f8ce972cda4a237cc9f0323f09bc63 | |
| parent | 43791bbf2e618414fa6d59c56831b6909d05f611 (diff) | |
fix(ptyfs): release the pts file on unregister (#12029)
* fix(ptyfs): release the pts file on unregister
ptyfs_unregister_pts() removed the pts file from the root list and
recycled its unit number but never freed the allocation. Repeated
open/close of /dev/pts/ptmx therefore leaked one struct ptyfs_file per
cycle and eventually exhausted the kernel heap.
Free the file after unlinking it. Also initialize pts_unit to -1 so a
failed registration does not unregister unit 0, detach the condvars
before freeing the softc on the lwp_tty_create() failure path, and skip
rt_device_unregister() for a tty that was never registered.
* fix(dfs): keep ptyfs files alive for concurrent readers
ptyfile_lookup() returned a raw pointer after releasing sb->lock, while
ptyfs_unregister_pts() removed and freed the same node. A concurrent
getdents(), setattr() or vnode lookup could then read freed memory.
Add a reference count protected by sb->lock: lookup takes a reference and
the last ptyfile_put() frees the node. The root list reference is dropped
when the file is removed, so unregistering a pts no longer frees a file
that a reader still uses. getdents() also holds the lock while walking the
subdir list, keeping the entries alive during the copy.
| -rw-r--r-- | components/dfs/dfs_v2/filesystems/ptyfs/ptyfs.c | 55 | ||||
| -rw-r--r-- | components/lwp/terminal/freebsd/tty_pts.c | 4 | ||||
| -rw-r--r-- | components/lwp/terminal/tty_device.c | 7 |
3 files changed, 60 insertions, 6 deletions
diff --git a/components/dfs/dfs_v2/filesystems/ptyfs/ptyfs.c b/components/dfs/dfs_v2/filesystems/ptyfs/ptyfs.c index 75bffdf510..be06e02ada 100644 --- a/components/dfs/dfs_v2/filesystems/ptyfs/ptyfs.c +++ b/components/dfs/dfs_v2/filesystems/ptyfs/ptyfs.c @@ -64,6 +64,7 @@ struct ptyfs_file rt_list_t ent_node; /* entry node in subdir list */ struct ptyfs_sb *sb; /* superblock ptr */ rt_device_t device; /* device binding on this file */ + rt_uint32_t ref; /* references, protected by sb->lock */ }; struct ptyfs_sb @@ -122,6 +123,8 @@ static rt_err_t ptyfile_init(struct ptyfs_file *file, struct ptyfs_sb *sb, rt_list_init(&file->ent_node); file->sb = sb; file->device = device; + /* the root list owns the initial reference */ + file->ref = 1; return 0; } @@ -131,10 +134,9 @@ static rt_err_t ptyfile_add_to_root(struct ptyfs_sb *sb, { struct ptyfs_file *root_file = &sb->root_file; + rt_mutex_take(&sb->lock, RT_WAITING_FOREVER); /* update super block */ sb->df_size += sizeof(struct ptyfs_file); - - rt_mutex_take(&sb->lock, RT_WAITING_FOREVER); rt_list_insert_after(&(root_file->subdirs), &(new_file->ent_node)); rt_mutex_release(&sb->lock); @@ -144,16 +146,40 @@ static rt_err_t ptyfile_add_to_root(struct ptyfs_sb *sb, static rt_err_t ptyfile_remove_from_root(struct ptyfs_sb *sb, struct ptyfs_file *rm_file) { + rt_mutex_take(&sb->lock, RT_WAITING_FOREVER); /* update super block */ sb->df_size -= sizeof(struct ptyfs_file); - - rt_mutex_take(&sb->lock, RT_WAITING_FOREVER); rt_list_remove(&(rm_file->ent_node)); + /* drop the reference owned by the root list */ + RT_ASSERT(rm_file->ref > 0); + rm_file->ref--; rt_mutex_release(&sb->lock); return 0; } +/** + * Release a reference returned by ptyfile_lookup(). + * + * The last reference frees the file, so a reader that got a pointer from + * ptyfile_lookup() can keep using it while another thread unregisters the pts. + */ +static void ptyfile_put(struct ptyfs_sb *sb, struct ptyfs_file *file) +{ + if (!file || file == &sb->root_file) + { + return; + } + + rt_mutex_take(&sb->lock, RT_WAITING_FOREVER); + RT_ASSERT(file->ref > 0); + if (--file->ref == 0) + { + rt_free(file); + } + rt_mutex_release(&sb->lock); +} + static struct ptyfs_file *ptyfile_lookup(struct ptyfs_sb *superblock, const char *path) { @@ -203,6 +229,8 @@ static struct ptyfs_file *ptyfile_lookup(struct ptyfs_sb *superblock, { if (strcmp(file_iter->basename, basename) == 0) { + /* keep the file alive until the caller is done with it */ + file_iter->ref++; found_file = file_iter; break; } @@ -303,6 +331,8 @@ rt_err_t ptyfs_unregister_pts(rt_device_t ptmx, ptsno_t ptsno) ptyfile_remove_from_root(sb, pts_file); ptsno_pool = &sb->ptsno_pool; rid_bitmap_put(ptsno_pool, ptsno); + /* drop the lookup reference; a concurrent reader may still hold one */ + ptyfile_put(sb, pts_file); rc = 0; } else @@ -399,11 +429,16 @@ static int ptyfs_ops_setattr(struct dfs_dentry *dentry, struct dfs_attr *attr) pty_file = ptyfile_lookup(superblock, dentry->pathname); if (pty_file && pty_file->type == PTYFS_TYPE_FILE_SLAVE) { + rt_mutex_take(&superblock->lock, RT_WAITING_FOREVER); pty_file->mode &= ~0xFFF; pty_file->mode |= attr->st_mode & 0xFFF; + rt_mutex_release(&superblock->lock); + + ptyfile_put(superblock, pty_file); return 0; } + ptyfile_put(superblock, pty_file); return -1; } @@ -485,6 +520,8 @@ static struct dfs_vnode *ptyfs_ops_lookup(struct dfs_dentry *dentry) } } + ptyfile_put(superblock, pty_file); + return vnode; } @@ -513,6 +550,7 @@ static struct dfs_vnode *ptyfs_ops_create_vnode(struct dfs_dentry *dentry, pty_file = ptyfile_lookup(sb, vnode_path); if (!pty_file || (~pty_file->mode & mode)) { + ptyfile_put(sb, pty_file); dfs_vnode_destroy(vnode); return NULL; } @@ -539,9 +577,12 @@ static struct dfs_vnode *ptyfs_ops_create_vnode(struct dfs_dentry *dentry, else { /* unsupported types */ + ptyfile_put(sb, pty_file); dfs_vnode_destroy(vnode); return NULL; } + + ptyfile_put(sb, pty_file); } return vnode; @@ -577,6 +618,7 @@ static int devpty_deffops_getdents(struct dfs_file *file, struct dirent *dirp, count = (count / sizeof(struct dirent)); if (count == 0) { + ptyfile_put(superblock, d_file); return -EINVAL; } @@ -584,6 +626,8 @@ static int devpty_deffops_getdents(struct dfs_file *file, struct dirent *dirp, index = 0; count = 0; + /* the lock keeps the entries alive while they are being read */ + rt_mutex_take(&superblock->lock, RT_WAITING_FOREVER); rt_list_for_each(list, &d_file->subdirs) { if (index >= (rt_size_t)file->fpos) @@ -614,6 +658,9 @@ static int devpty_deffops_getdents(struct dfs_file *file, struct dirent *dirp, break; } } + rt_mutex_release(&superblock->lock); + + ptyfile_put(superblock, d_file); } return count * sizeof(struct dirent); diff --git a/components/lwp/terminal/freebsd/tty_pts.c b/components/lwp/terminal/freebsd/tty_pts.c index d25e668d86..872e191cc5 100644 --- a/components/lwp/terminal/freebsd/tty_pts.c +++ b/components/lwp/terminal/freebsd/tty_pts.c @@ -763,6 +763,8 @@ int pts_alloc(int fflags, struct rt_thread *td, struct dfs_file *ptm_file) /* Allocate TTY and softc. */ psc = rt_calloc(1, sizeof(struct pts_softc)); + /* No unit is registered yet; the free path must not unregister unit 0. */ + psc->pts_unit = -1; cv_init(&psc->pts_inwait, "ptsin"); cv_init(&psc->pts_outwait, "ptsout"); rt_wqueue_init(&psc->pts_inpoll); @@ -778,9 +780,9 @@ int pts_alloc(int fflags, struct rt_thread *td, struct dfs_file *ptm_file) tp = lwp_tty_create(&pts_class, psc); if (!tp) { - rt_free(psc); rt_condvar_detach(&psc->pts_inwait); rt_condvar_detach(&psc->pts_outwait); + rt_free(psc); return -ENOMEM; } diff --git a/components/lwp/terminal/tty_device.c b/components/lwp/terminal/tty_device.c index a2936a0d1b..59bbe4f5cb 100644 --- a/components/lwp/terminal/tty_device.c +++ b/components/lwp/terminal/tty_device.c @@ -445,7 +445,12 @@ void lwp_tty_delete(lwp_tty_t tp) if (tp->t_mtx == &tp->t_mtxobj) rt_mutex_detach(&tp->t_mtxobj); ttydevsw_free(tp); - rt_device_unregister(&tp->parent); + /* A tty that failed before lwp_tty_register() was never added to the + * device object list. Detaching such an object corrupts memory. */ + if (rt_object_get_type(&tp->parent.parent) == RT_Object_Class_Device) + { + rt_device_unregister(&tp->parent); + } rt_free(tp); } |
