Skip to content

sched/misc: fix potential out-of-bounds access in coredump stack emission#18450

Open
anchao wants to merge 1 commit intoapache:masterfrom
anchao:26022605
Open

sched/misc: fix potential out-of-bounds access in coredump stack emission#18450
anchao wants to merge 1 commit intoapache:masterfrom
anchao:26022605

Conversation

@anchao
Copy link
Contributor

@anchao anchao commented Feb 26, 2026

Summary

sched/misc: fix potential out-of-bounds access in coredump stack emission

The elf_emit_tcb_stack() function in coredump.c was calculating the stackbuffer length
and emitting the stack data without validating whether thecalculated buffer range
(buf + len) exceeds the actual bounds of the TCB'sstack memory region (stack_base_ptr + adj_stack_size).

This could lead to out-of-bounds memory access when the calculated stacklength is larger
than the available stack space, potentially causing memorycorruption, crashes, or incorrect
core dump generation.

This fix adds a bounds check:

  1. Compares the end of the intended stack buffer (buf + len) against the
    upper limit of the TCB's stack (stack_base_ptr + adj_stack_size).
  2. If the buffer would exceed the stack bounds, truncates the length to fit
    within the valid stack memory range.

The change ensures safe memory access during core dump generation,preventing out-of-bounds
reads and improving the robustness of the coredumpfeature.

Signed-off-by: chao an anchao.archer@bytedance.com

Impact

N/A

Testing

sim/nsh, ostest
sabre-6quad/coredump

…sion

The elf_emit_tcb_stack() function in coredump.c was calculating the stackbuffer length
and emitting the stack data without validating whether thecalculated buffer range
(buf + len) exceeds the actual bounds of the TCB'sstack memory region (stack_base_ptr + adj_stack_size).

This could lead to out-of-bounds memory access when the calculated stacklength is larger
than the available stack space, potentially causing memorycorruption, crashes, or incorrect
core dump generation.

This fix adds a bounds check:
1. Compares the end of the intended stack buffer (buf + len) against the
   upper limit of the TCB's stack (stack_base_ptr + adj_stack_size).
2. If the buffer would exceed the stack bounds, truncates the length to fit
   within the valid stack memory range.

The change ensures safe memory access during core dump generation,preventing out-of-bounds
reads and improving the robustness of the coredumpfeature.

Signed-off-by: chao an <anchao.archer@bytedance.com>
@github-actions github-actions bot added Area: OS Components OS Components issues Size: XS The size of the change in this PR is very small labels Feb 26, 2026
@@ -429,6 +429,13 @@ static void elf_emit_tcb_stack(FAR struct elf_dumpinfo_s *cinfo,
len = ALIGN_UP(len + (buf - sp), PROGRAM_ALIGNMENT);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we align sp up and len down directly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aligning sp up might lose some important information; checking the length using stack size might be more accurate.

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

Labels

Area: OS Components OS Components issues Size: XS The size of the change in this PR is very small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants