Skip to content

[Bug] ptyfs: ptyfs_unregister_pts() never frees the pts file — unprivileged open/close loop exhausts kernel memory and panics the kernel #12023

Closed

Description

@XueDugu
  • RT-Thread Version: master (v5.4.0-dev, commit 5d1fb66, 2026-10-15); affected v5.1.0 ~ master
    (introduced by 83e95bd, 2024-03-28, PR sync smart & dfs #8672; verified present/unfixed in v5.1.0, v5.2.0, v5.3.0 and master)
  • Affected area: Components / filesystems → components/dfs/dfs_v2/filesystems/ptyfs/ptyfs.c
  • Architecture: ARM64 (any RT-Smart target; verified on aarch64)
  • Board / hardware: QEMU qemu-virt64-aarch64, official default config (terminal + ptyfs compiled in
    with RT_USING_SMART)
  • Develop Toolchain: RT-Thread official CI musl toolchain gcc 7.3.0 + scons; PoC is an unprivileged
    RT-Smart user program using only official syscalls (mkdir/mount/open/close)

Describe the bug

Every open("/dev/pts/ptmx") allocates a struct ptyfs_file for the new pts slave, but the release path
never frees it:

/* ptyfs.c:243 ptyfs_register_pts() — on every ptmx open */
pts_file = rt_calloc(1, sizeof(struct ptyfs_file));      /* 312 bytes */

/* ptyfs.c:282 ptyfs_unregister_pts() — on close (ptsdrv_free) */
pts_file = ptyfile_lookup(sb, path_buf);
if (pts_file)
{
    ptyfile_remove_from_root(sb, pts_file);   /* list-remove only */
    rid_bitmap_put(ptsno_pool, ptsno);        /* number recycled  */
}                                             /* <-- no rt_free(pts_file), the whole
                                                 file contains zero rt_free calls */

Because the pts number is recycled, the LWP_PTY_MAX_PARIS_LIMIT bound on live pairs never bounds
repeated open/close cycles: an unprivileged user program looping open()+close() leaks 312 bytes plus the
allocator header on every cycle, forever.

Additionally, when the kernel heap is finally exhausted, the next open() crashes the kernel instead of
failing cleanly: pts_alloc()'s allocation-failure cleanup path tears down a half-initialized tty and
dereferences NULL in rt_list_remove() (write to NULL+8) — a permanent panic requiring a reboot.

To reproduce (full chain, unprivileged user, official syscalls only)

Reference: rtthread-ws/audit/RT_ptyfs/exp/ (PoC source + logs).

  1. Boot stock qemu-virt64-aarch64 (official default config, RT-Smart).
  2. Run an unprivileged user program that only uses official syscalls:
    mkdir("/dev/pts"), mount("none", "/dev/pts", "ptyfs") (sys_mount has no privilege check),
    then loop fd = open("/dev/pts/ptmx", O_RDWR); close(fd);.
  3. Attribution run — 20,000 cycles (exp/leak7.log):
    free available 66,901,384 → 59,522,504 = 7,378,880 bytes lost = 368.94 bytes/cycle,
    exactly sizeof(struct ptyfs_file) (312) + memheap header; all 20,000 open/close calls succeed —
    this is a pure, deterministic leak with no other side effects.
  4. Kill run — keep looping (exp/leak6.log): after ~150,000 cycles the heap is exhausted and the kernel
    panics on the next open():
    epc = rt_list_remove (rtservice.h:91), Data abort, fault addr = 0x8, write; backtrace:
    sys_open ← dfs_file_open (dfs_file.c:739) ← dfs_devfs_open (devfs.c:62) ← ptm_fops_open (tty_ptmx.c:37) ← pts_alloc (tty_pts.c:795) ← lwp_tty_delete (tty_device.c:445) ← rt_device_unregister (device.c:109) ← rt_list_remove. The exception loops forever; the system must be rebooted.
  5. Control: without the loop the system is healthy; with only 20k cycles it stays healthy (step 3, zero
    failures) — the crash is purely the accumulated leak hitting the allocation-failure path.

Expected behaviour

ptyfs_unregister_pts() must rt_free(pts_file) after removing it (the rid can only be recycled together
with the file object), and pts_alloc()'s error path must not tear down a half-initialized device
(rt_device_unregister on an object whose registration never completed).

Actual behaviour

Each posix_openpt/close pair permanently leaks ~369 bytes of kernel heap; an unprivileged user program
exhausts kernel memory within a minute and the following open() panics the kernel (NULL write in the OOM
cleanup path) — permanent denial of service requiring reboot.

Other additional context

  • Attack prerequisite (explicit): local unprivileged code execution on an RT-Smart target (any
    application). ptyfs is compiled in with RT-Smart by default but not mounted; sys_mount performs no
    privilege check, so the attacker mounts it first (mkdir /dev/pts + mount none /dev/pts ptyfs),
    both from the unprivileged program itself. No privileges, no user interaction.
  • Severity: CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:N/I:N/A:H = 6.2 (unprivileged local user,
    permanent kernel panic / full availability loss; no confidentiality/integrity impact).
  • CWE: CWE-401 (missing release of memory before effective removal of last reference — primary);
    CWE-476 (NULL pointer dereference in the allocation-failure path — amplifier that turns exhaustion
    into an immediate panic).
  • Versions: introduced by 83e95bd (2024-03-28, PR sync smart & dfs #8672); first affected release v5.1.0;
    v5.1.0/v5.2.0/v5.3.0/master all verified affected (unregister path contains no rt_free in any of them).
  • Relation to existing reports: new. [Bug] [Smart] terminal tty: ioctl passes raw user pointer into kernel dereference — arbitrary kernel read/write from unprivileged user process #11937 (terminal ioctl raw-pointer passthrough) is a different
    root cause in an adjacent file; this issue is the ptyfs filesystem-side lifecycle bug and also documents
    the sys_mount no-privilege-check enabler first noted in that audit.

Activity

  1. github-actions commented on Nov 10, 2026

    @github-actions

    如需认领并处理此 Issue,请评论 /claim。

    To claim and work on this issue, comment /claim.

  2. added
    bug
    on Nov 10, 2026
  3. BernardXiong commented on Nov 10, 2026

    @BernardXiong
    Member

    已确认:ptyfs_unregister_pts() 只从根链表移除并回收 pts 编号,没有释放 struct ptyfs_file。

    修复 PR:#12029。释放该对象,同时把 pts_unit 初始化为 -1、修正 lwp_tty_create() 失败路径的释放顺序,并在 lwp_tty_delete() 中跳过未注册设备的 rt_device_unregister()。

    验证:qemu-virt64-aarch64 RT-Smart + ptyfs,20,000 次打开/关闭 /dev/pts/ptmx 后,available 从 245156 KB 变为 245116 KB(修复前为 245164 KB → 237932 KB,约 369 字节/次)。未强制 OOM 路径,也未用非特权进程单独测试。


    Confirmed: ptyfs_unregister_pts() only unlinks the pts file from the root list and recycles its unit number; the struct ptyfs_file is never freed.

    Fix in PR #12029: free the object, initialize pts_unit to -1, fix the release order on the lwp_tty_create() failure path, and skip rt_device_unregister() for a tty that was never registered.

    Validation: qemu-virt64-aarch64 RT-Smart + ptyfs. After 20,000 open/close cycles of /dev/pts/ptmx, available went from 245156 KB to 245116 KB (before the fix: 245164 KB -> 237932 KB, about 369 bytes/cycle). The OOM path was not forced and the loop was not run from an unprivileged process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions