dfs/posix: validate bounds and safely detach listener in signalfd_close (#11935)

* 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>
This commit is contained in:
Adil Alperen ÇiftciandAdil Alperen Ciftci authored and GitHub committed 2026-11-05 16:37:23 +08:00
1 parent 40e07d970e
commit d279c43627
5 files changed
+358 -60

No files matched your search

File diff suppressed because it is too large. Load diff
+9
View File
@@ -83,6 +83,15 @@ struct rt_lwp_notify
* @param signo Signal number
*/
void (*notify)(rt_wqueue_t *signalfd_queue, int signo);
/**
* @brief Callback invoked when the registered process exits
* @param signalfd_queue Wait queue for signal file descriptors
* @param lwp The exiting process
* @note Called under LWP_LOCK after the node has been unlinked from
* lwp->signalfd_notify_head; the node is freed by the caller
* once this returns.
*/
void (*detach)(rt_wqueue_t *signalfd_queue, struct rt_lwp *lwp);
rt_wqueue_t *signalfd_queue; /**< Wait queue for signal file descriptors */
rt_slist_t list_node; /**< List node for notification */
};
+1
View File
@@ -2188,6 +2188,7 @@ static void _resr_cleanup(struct rt_lwp *lwp)
LWP_LOCK(lwp);
lwp_signal_detach(&lwp->signal);
lwp_signal_notify_detach(lwp);
/**
* @brief Detach children from lwp
+54
View File
@@ -449,6 +449,60 @@ static void lwp_signal_notify(rt_slist_t *list_head, lwp_siginfo_t siginfo)
}
}
void lwp_signal_notify_add(struct rt_lwp *lwp, struct rt_lwp_notify *notify)
{
LWP_ASSERT_LOCKED(lwp);
rt_slist_append(&lwp->signalfd_notify_head, &notify->list_node);
}
void lwp_signal_notify_remove_queue(struct rt_lwp *lwp, rt_wqueue_t *signalfd_queue)
{
rt_slist_t *prev;
rt_slist_t *curr;
LWP_LOCK(lwp);
prev = &lwp->signalfd_notify_head;
curr = prev->next;
while (curr != RT_NULL)
{
struct rt_lwp_notify *n = rt_slist_entry(curr, struct rt_lwp_notify, list_node);
if (n->signalfd_queue == signalfd_queue)
{
prev->next = curr->next;
rt_free(n);
curr = prev->next;
}
else
{
prev = curr;
curr = curr->next;
}
}
LWP_UNLOCK(lwp);
}
void lwp_signal_notify_detach(struct rt_lwp *lwp)
{
rt_slist_t *node;
LWP_ASSERT_LOCKED(lwp);
while ((node = rt_slist_first(&lwp->signalfd_notify_head)) != RT_NULL)
{
struct rt_lwp_notify *n = rt_slist_entry(node, struct rt_lwp_notify, list_node);
lwp->signalfd_notify_head.next = node->next;
node->next = RT_NULL;
if (n->detach)
{
n->detach(n->signalfd_queue, lwp);
}
rt_free(n);
}
}
rt_err_t lwp_signal_init(struct lwp_signal *sig)
{
rt_err_t rc = RT_EOK;
+34
View File
@@ -96,6 +96,40 @@ rt_err_t lwp_signal_init(struct lwp_signal *sig);
rt_err_t lwp_signal_detach(struct lwp_signal *signal);
struct rt_lwp_notify;
/**
* @brief Register a signal notification listener on a process
*
* @param lwp target process
* @param notify listener node; owned by the process list until it is removed
*
* @note Caller holds LWP_LOCK, the lock lwp_signal_notify() walks the list
* under, so the caller can publish its own registration state in the
* same critical section.
*/
void lwp_signal_notify_add(struct rt_lwp *lwp, struct rt_lwp_notify *notify);
/**
* @brief Unlink and free every listener of a wait queue on a process
*
* @param lwp target process
* @param signalfd_queue wait queue the listeners belong to
*
* @note Takes LWP_LOCK; no notification can be inside a node once it returns.
*/
void lwp_signal_notify_remove_queue(struct rt_lwp *lwp, rt_wqueue_t *signalfd_queue);
/**
* @brief Release every listener of an exiting process
*
* @param lwp the exiting process
*
* @note Caller holds LWP_LOCK. Each listener's detach callback runs after
* its node is unlinked; the node is freed afterwards.
*/
void lwp_signal_notify_detach(struct rt_lwp *lwp);
rt_inline void lwp_thread_signal_detach(struct lwp_thread_signal *tsig)
{
lwp_sigqueue_clear(&tsig->sig_queue);