apache / nuttx

Apache NuttX is a mature, real-time embedded operating system (RTOS)
https://nuttx.apache.org/
Apache License 2.0
2.87k stars 1.17k forks source link

[BUG] SMP signal dispatch seems racy #14807

Open yamt opened 2 hours ago

yamt commented 2 hours ago

Description / Steps to reproduce the issue

the signal sending logic (kill) basically does:

  1. enter_critical_section
  2. decide the target thread
  3. check signal mask of the thread
  4. make a cross call to the target cpu (nxsched_smp_call_single)

as nxsched_smp_call_single drops the critical section, there seems to be nothing to prevent the signal mask from changing in the meantime. it can end up with a signal delivered while being blocked.

similarly, the need_restore logic in sig_handler seems dangerous as it can revert unrelated changes to those fields.

On which OS does this issue occur?

[OS: Mac]

What is the version of your OS?

macOS 14.7

NuttX Version

master

Issue Architecture

[Arch: all]

Issue Area

[Area: Kernel]

Verification

xiaoxiang781216 commented 2 hours ago

@hujun260 please look at this issue.

hujun260 commented 1 hour ago

as nxsched_smp_call_single drops the critical section, there seems to be nothing to prevent the signal mask from changing in the meantime. it can end up with a signal delivered while being blocked.

I don't think this change caused https://github.com/apache/nuttx/issues/14749, because the release of the critical section is going to happen regardless of whether I call nxsched_smp_call_single or not. image

similarly, the need_restore logic in sig_handler seems dangerous as it can revert unrelated changes to those fields.

There are two fields: affinity and flag. Due to the setting of TCB_FLAG_CPU_LOCKED, the affinity will not be changed during the process. image

The tcb->flags may undergo changes, which I can further refine. During restoration, only the TCB_FLAG_CPU_LOCKED field of tcb->flags will be restored, without altering any other fields.