CV-Bowen opened a new pull request, #22115:
URL: https://github.com/apache/nuttx/pull/22115
## Summary
`backtrace_stack()` stores the return address it is given before checking
that the buffer has room for it. `up_backtrace()` rejects `size <= 0` on
entry, but in interrupt context it walks the interrupt stack first and then
calls `backtrace_stack()` again with the interrupted A0 and whatever room
the first walk left:
```c
ret += backtrace_stack(rtcb->stack_base_ptr,
rtcb->stack_base_ptr + rtcb->adj_stack_size,
running_regs()[REG_A1],
running_regs()[REG_A0],
&buffer[ret], size - ret, &skip);
```
When the first walk fills the buffer, `size - ret` is 0 and the A0 entry
is written one slot past the end of the caller's buffer. The room is now
checked together with `ra` and `skip`.
## Impact
- `up_backtrace()` on xtensa no longer writes past the caller's buffer
when it is called from an interrupt with a deep interrupt stack.
- No change when the buffer has room: the A0 entry is still recorded
first, and `skip` is still consumed in the same order.
## Testing
Host: Ubuntu 22.04.5 LTS x86_64; gcc 13.1.0 with `-fsanitize=address`.
No xtensa toolchain or target was available, so the check runs the
verbatim `backtrace_stack()` from `xtensa_backtrace.c`, master and this
PR, on the host: a frame chain deeper than a 4-entry buffer, walked the
way `up_backtrace()` does in interrupt context (first with `ra = NULL`,
then with A0 and the room that is left).
| tree | result |
|---|---|
| master | `AddressSanitizer: heap-buffer-overflow`, `WRITE of size 8 ... 0
bytes after 32-byte region`, in `backtrace_stack` at the `buffer[i++] =
MAKE_PC_FROM_RA(ra)` line |
| this PR | `first walk: 4 of 4`, `total: 4`, no ASan report |
The xtensa configurations themselves are left to CI to build.
No regression in rpmsg: sim:rpserver/rpproxy and qemu-armv8a:rpserver/rpproxy
boot and pass the fixed rpmsg cases (dump, ping, rpmsgfs).
<details><summary>Host harness (not part of this PR)</summary>
`bt_master.inc` / `bt_pr.inc` hold `backtrace_stack()` cut out of
`arch/xtensa/src/common/xtensa_backtrace.c` unchanged.
```c
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define CONFIG_ARCH_INTERRUPTSTACK 0
#define nosanitize_address
#define MAKE_PC_FROM_RA(ra) ((void *)(ra))
#define xtensa_ptr_exec(p) ((p) != NULL)
#define xtensa_sp_sane(sp) ((sp) != 0)
#include BT_FUNC
#define NFRAMES 8
#define SIZE 4
int main(void)
{
uintptr_t stack[8 * (NFRAMES + 1)];
uintptr_t *base = stack;
uintptr_t *limit = stack + 8 * (NFRAMES + 1);
void **buffer = malloc(SIZE * sizeof(void *));
int skip = 0;
int ret;
int k;
/* sp[-4] is the return address and sp[-3] the caller's sp */
for (k = 0; k < NFRAMES; k++)
{
stack[8 * (k + 1) - 4] = 0x1000 + k;
stack[8 * (k + 1) - 3] = (uintptr_t)(stack + 8 * (k + 2));
}
ret = backtrace_stack(base, limit, stack + 8, NULL, buffer, SIZE, &skip);
printf("first walk: %d of %d\n", ret, SIZE);
ret += backtrace_stack(base, limit, stack + 8, (uintptr_t *)0x2000,
&buffer[ret], SIZE - ret, &skip);
printf("total: %d\n", ret);
free(buffer);
return 0;
}
```
```
gcc -g -O0 -fsanitize=address -DBT_FUNC='"bt_master.inc"' harness.c -o
h_master
gcc -g -O0 -fsanitize=address -DBT_FUNC='"bt_pr.inc"' harness.c -o h_pr
```
</details>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]