* dfs/posix: enforce bounds check and clean unregister in signalfd
Validate array bounds on signalfd notification registration and remove
the unsafe global index counter.
Tie the registered process lifetime to the registration: each slot in
sfd->lwp[] holds an LWP reference, and the listener node carries a detach
hook that the process exit path runs under LWP_LOCK to unlink the node,
clear the slot and drop the reference. Final close therefore never walks
a freed process, and a child that registered through an inherited
descriptor is unregistered when it exits.
Move every change of lwp->signalfd_notify_head behind lwp_signal_notify_*
helpers in the lwp core, which take LWP_LOCK, the lock lwp_signal_notify()
traverses the list under. The lock order is LWP_LOCK -> sfd->lock, the
same order the signal path already uses; sfd->lock is never held while
taking LWP_LOCK, and a node is only unlinked under LWP_LOCK, so no
in-flight notification can be inside it when it is freed.
Tear down the vnode, context and mutex before fd_release() when the
initial registration fails, and return instead of falling through to
freed objects. Propagate registration failures: read returns the error
and poll reports POLLERR before anything is added to the poll queue.
Fixes: #11933
Signed-off-by: Adil Alperen Ciftci <adilalperenciftci@users.noreply.github.com>
* dfs/posix: state the signalfd context lifetime and tighten close
State the lifetime assumption of the signalfd context explicitly: the
descriptor the caller operates on pins the context for the whole read,
poll and registration, and a concurrent close of that same descriptor by
another thread is not supported, which is what the existing read and poll
paths already rely on. The post-registration slot re-check therefore does
not add a new requirement.
Clear all registration slots in one pass under sfd->lock in close and run
the per-process unlink and reference drop outside the lock afterwards, so
the "clear first, unlink later" invariant is one block instead of a lock
round-trip per slot.
Rename SIGNALFD_SHART_MAX to SIGNALFD_SHARE_MAX while the macro block is
being touched.
Signed-off-by: Adil Alperen Ciftci <adilalperenciftci@users.noreply.github.com>
* dfs/posix: keep the LWP lock out of signalfd close and make registration atomic
dfs_file_close() calls the signalfd close callback under the global DFS
file lock, and the process exit path takes that same lock while it already
holds LWP_LOCK (__exit_files() under _resr_cleanup()). Unlinking the
listeners from close() through lwp_signal_notify_remove_queue() therefore
took the two locks in the opposite order to process exit, and a parent
closing a shared signalfd while a registered child exits could block each
side on the other's lock.
Close no longer takes any LWP lock. It marks the context closing, detaches
it from the vnode and submits a release work item to the system workqueue.
The work takes the slots over under sfd->lock, unlinks the listeners under
each process's own LWP_LOCK with no other lock held, drops the references
the slots owned, and frees the context. A process that exits before the
work runs has already cleared its slot and dropped its reference through
the exit hook, so the work only sees slots that are still live.
Registration now does the lookup, the slot assignment and the listener
insertion in one critical section under LWP_LOCK(self) -> sfd->lock, the
same order the exit hook already uses. A slot is never visible without its
listener, so a second thread cannot observe a registration whose listener
is still missing, and the lookup cannot go stale between the check and the
assignment. lwp_signal_notify_add() accordingly expects the caller to hold
LWP_LOCK; the single-node unlink helper that the post-registration
re-check used is gone with that re-check. A registration that races with
close finds the closing flag and fails with EBADF instead of taking a slot
the release work will not see.
Signed-off-by: Adil Alperen Ciftci <adilalperenciftci@users.noreply.github.com>
* dfs/posix: do not allocate for an existing signalfd registration
signalfd_add_notify() allocated the listener node before looking the
calling process up, so read and poll on an already registered descriptor
failed with ENOMEM or POLLERR under memory pressure even though no node
was needed and a signal may already have been queued.
Look the process up first, under LWP_LOCK(self) -> sfd->lock, and return
from there when it is registered or the context is closing. Only when a
new registration is needed drop the locks, allocate the node, take the
locks again and repeat the lookup unconditionally: the closing flag, an
existing registration and the free slot are all re-evaluated before the
slot is assigned and the listener is linked in the same critical section.
A node left over because another thread registered the process meanwhile
is freed after the locks are released.
Signed-off-by: Adil Alperen Ciftci <adilalperenciftci@users.noreply.github.com>
* dfs/posix: align signalfd context member comments for clang-format
Signed-off-by: Adil Alperen Ciftci <adilalperenciftci@users.noreply.github.com>
---------
Signed-off-by: Adil Alperen Ciftci <adilalperenciftci@users.noreply.github.com>
Co-authored-by: Adil Alperen Ciftci <adilalperenciftci@users.noreply.github.com>
* [smart] lwp: enforce user buffer validation and copyin/copyout in terminal ioctl
In the RT-Smart terminal subsystem, userspace ioctl commands (such as TIOCGWINSZ, TIOCSWINSZ, TIOCGPGRP, TIOCGSID, FIONREAD, and TIOCGPTN) were previously passed directly to underlying FreeBSD ioctl handlers as raw user pointers without kernel staging or copy validation.
This patch addresses the user pointer dereference sinks:
1. In lwp_tty_ioctl_adapter(), stage arguments in kernel stack buffers and use _copy_from_user() / _copy_to_user() (lwp_get_from_user / lwp_put_to_user) for TIOCGWINSZ, TIOCSWINSZ, TIOCGPGRP, TIOCSPGRP, TIOCGSID, TIOCSTI, TIOCOUTQ, FIONREAD, and modem commands.
2. In ptm_fops_ioctl(), safely buffer arguments for TIOCGPTN, TIOCPKT, TIOCSIG, TIOCGPGRP, TIOCGSID, FIONREAD, TIOCOUTQ, TIOCGWINSZ, TIOCSWINSZ, and termios settings, returning -ENOTTY on unrecognized commands.
3. In tty_generic_ioctl(), add defense-in-depth boundary checks using lwp_in_user_space() to safeguard kernel memory from arbitrary pointer dereferences.
Signed-off-by: Adil Alperen Çiftci <adilalperenciftci@users.noreply.github.com>
* [smart] lwp: make the tty ioctl adapters the single uaccess boundary
lwp_tty_ioctl_adapter() and ptm_fops_ioctl() (through
lwp_ptmx_ioctl_forward()) already stage every user argument into a kernel
buffer before entering the BSD tty layer, so tty_generic_ioctl() only ever
sees kernel pointers on the syscall path. Drop the lwp_in_user_space()
branches that were added there, and the older one on TIOCSPGRP, so the
trust boundary is in exactly one place per entry point and the switch no
longer mixes two models.
Zero the staging union in lwp_ptmx_ioctl_forward() before the backend
runs: an *_OUT command copies it back to the caller, and a backend that
returns 0 without filling it would otherwise hand out stack contents.
Add the BSD termios commands TIOCGETA, TIOCSETA, TIOCSETAW and TIOCSETAF
to the /dev/ptmx argument table so they keep reaching the tty layer as
they did before the table existed. The termio-based TCGETA family is not
handled by the BSD layer on this path and stays ENOTTY, as before.
Rename lwp_ptmx_ioctl_arg_type() to lwp_ptmx_ioctl_arg_type_get() so the
function no longer shares its name with the enum tag, rename the test
case to testcases.lwp.tty_ptmx.regression since it covers more than the
buffer bounds, and add the terminal directory to the utest CPPPATH
instead of a relative include.
Signed-off-by: Adil Alperen Ciftci <adilalperenciftci@users.noreply.github.com>
* [smart] lwp: drop duplicate termios cases from the ptmx ioctl table
In this port TIOCGETA and TIOCSETA/TIOCSETAW/TIOCSETAF are defined to the
same values as TCGETS and TCSETS/TCSETSW/TCSETSF, so listing both sets
makes the switch fail to compile with duplicate case values. The TCGETS
family entries already cover the BSD names; the test asserts for the BSD
spellings stay valid through the aliases.
Signed-off-by: Adil Alperen Ciftci <adilalperenciftci@users.noreply.github.com>
---------
Signed-off-by: Adil Alperen Çiftci <adilalperenciftci@users.noreply.github.com>
Signed-off-by: Adil Alperen Ciftci <adilalperenciftci@users.noreply.github.com>
Co-authored-by: Adil Alperen Çiftci <adilalperenciftci@users.noreply.github.com>
Co-authored-by: Rbb666 <zhangbingru@rt-thread.com>
Add components.drivers.watchdog.smoke covering device find/open and
timeout set/get. Optional start/keepalive/stop stays off by default
to avoid board reset when STOP is unsupported.
Co-Authored-By: Grok <seteiro@mail.grokbot.com>
* [DM/PCI] Validate MSI-X table BIR and offset against BAR
BIR and the table offset in the MSI-X capability are controlled by the
device. msix_table_remap() used BIR directly as an index into
pdev->resource[] and never checked the table against the BAR size, so a
malicious device could make the kernel map and write to an arbitrary
physical address during probe.
Reject out-of-range BIR values, unassigned or zero-sized BARs, and
tables that do not fit completely inside the assigned BAR. Also verify
that the requested vector count does not exceed the table size read
from the MSI-X control register.
Fixes#11985
* [DM/PCI] Fix cppcheck warnings in MSI-X code
- Restore the default IRQ only when an MSI descriptor exists, avoiding
a potential NULL pointer dereference in rt_pci_msi_shutdown
- Initialize the error code in msix_setup_msi_descs so that an empty
vector list returns a defined value
is_pte_valid() accepted a GPT entry as long as both LBAs were inside the
disk, so a crafted image could set ending_lba < starting_lba. The size
computed in efi_partition() then underflowed to a huge value and was
registered as the logical partition size.
Reject entries whose end LBA is below the start LBA, and compute the
partition start/size only after the entry passed validation. This avoids
evaluating the underflow at all and reuses the validated start value for
the size calculation.
FixesRT-Thread/rt-thread#11271.
Co-authored-by: DeepSeek 4.1 Flash <noreply@deepseek.com>
Closes#10097.
The coding style only said that comments go above or to the right of the code,
without saying which applies to a macro. The repository already follows a
consistent rule, it just was not written down:
- 135 macro comments in include/ use a trailing /**<
- a further set of function-like macros use a /** block above the definition with
@def
The rule is about how much explanation the macro needs, not about line length.
A single line goes trailing; a paragraph goes above. That distinction matters
because the column-aligned groups of constant macros routinely exceed 80 columns
once the comment is appended - 87 of those 135 lines (64%) are longer than 80
columns, and shortening them would break the alignment that makes them readable.
Both language versions get the same section, with the real examples from
include/klibc/kerrno.h and include/rtdef.h, plus a note that with
MACRO_EXPANSION enabled a trailing /**< follows the expansion site while a block
comment stays with the definition - which is why frequently expanded macros
should use the block form.
sys_getaddrinfo() validated the top-level res buffer, then wrote the
resolved socket address through res->ai_addr, a user-controlled nested
pointer that was never checked. On Smart/MMU builds a user program could
therefore turn getaddrinfo() into an arbitrary kernel write. The hints
fields were also read directly from user memory after the access check,
leaving a TOCTOU window.
Copy res and hints into kernel temporaries with lwp_get_from_user() so
all nested pointers come from one validated snapshot. Write the nested
sockaddr and the outer addrinfo back with lwp_put_to_user(), which
validates the destination range and never dereferences an unchecked user
pointer in kernel context.
Also fix the servname copy check (a size_t result can never be < 0),
allocate the kernel addrinfo with sizeof(*k_hints), and release k_res on
the new error paths.
Co-Authored-By: Claude Code <noreply@anthropic.com>
- register SCMI reset and power-domain drivers during subsystem initialization
- resolve SCMI reset state through the reset-controller provider
- use the parsed DT reset domain ID when querying reset attributes
- retain the SCMI device reference in the reset provider instance
Signed-off-by: GuEe-GUI <2991707448@qq.com>
* lwp: fix pid leak on exec failure before task startup
When msh tries to execute a non-ELF path, lwp_execve() may allocate a PID
before lwp_load() fails. The old error path only dropped the LWP reference,
leaving the PID tree entry pointing to a freed LWP.
In an init-less boot flow, this can poison pid 1 after a failed command from
msh. A later LWP launch may then treat the stale pid 1 entry as a valid parent
LWP, resulting in invalid pgrp/session state and a job-control assertion during
process exit.
Add lwp_pid_rollback() for exec/spawn failures before the process becomes
runnable. Unlike lwp_pid_put(), it always releases the PID lock and does not
enter the "no more pid allocation" state when the PID tree becomes empty.
Use the rollback helper in lwp_execve() failure paths after PID allocation.
Signed-off-by: zhangyang <gaoshanliukou@163.com>
* [drivers][sdio] avoid SDHCI device name stack overflow
rt_sdhci_init_host() allocated a fixed 32-byte buffer for device-derived object names, but sdio_host_set_name() may copy up to RT_NAME_MAX bytes into the output buffer. When RT_NAME_MAX is larger than 32, this can overwrite the stack and corrupt nearby locals.
Size the temporary name buffer with RT_NAME_MAX, force it to be NUL-terminated, and derive the append offset from the actual string length. Also replace the suffix rt_sprintf() calls with bounded rt_snprintf() so timer and workqueue suffixes cannot overflow the buffer when the base name is already close to RT_NAME_MAX.
Signed-off-by: zhangyangysu <zhangyangysu0928@gmail.com>
---------
Signed-off-by: zhangyang <gaoshanliukou@163.com>
Signed-off-by: zhangyangysu <zhangyangysu0928@gmail.com>
Co-authored-by: yang.zhang <zhangyang01@kylinos.cn>
- move syscall implementations from lwp_syscall.c into syscall/
- group memory, file, socket, process, IPC and time syscalls
- add a shared syscall dispatch table and internal header
- register the new directory from the LWP build script
- no functional change
- place the TIOCLINUX case brace on its own line
- use sizeof(tty_ctrl_charset) directly as the memcpy size
- fix the cppcheck sizeofDivisionMemfunc warning
- return zero keyboard modifiers for TIOCLINUX instead of failing
- keep terminal applications that probe console state working
- initialize the TTY window size to 24x80 when none is reported