Skip to content

wireless/bluetooth: Validate lengths when parsing advertising reports. - #20184

Open
acassis wants to merge 1 commit into
apache:masterfrom
acassis:ble_nuttx
Open

acassis wants to merge 1 commit into
apache:masterfrom
acassis:ble_nuttx

Conversation

@acassis

@acassis acassis commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

Summary

le_adv_report() took the report count and each report's data length from the event and used them without checking either against the data that was actually received:

  • the declared data length indexes the RSSI octet, so a length larger than the event reads past the end of the buffer;
  • the loop was bounded only by the report count, so a count larger than the payload walks off the end of it;
  • bt_buf_consume() only checks its bound with DEBUGASSERT(), so on a build without assertions the buffer length underflows rather than reporting the problem.

Check that the event is long enough for the count, then check each report against the remaining length before reading its data or its RSSI, and stop parsing when a report does not fit.

While here, include the RSSI octet when advancing to the next report. sizeof() of the report structure does not account for it, because the data member is a zero-length array, so every report after the first started one octet early.

Ref: Core v6.0, Vol 4, Part E, 7.7.65.2 (LE Advertising Report event) Testing: builds for sim:bluetooth with Make; every commit in this series verified to build individually. Not yet exercised at runtime - the scriptable controller that can inject a malformed report is added separately.

Impact

Improvement

Testing

Before this change:

  le_adv_report: Adv number of reports 2
  le_adv_report: AA:AA:AA:AA:AA:01 (public) event 0, len 3, rssi -40 dBm
  le_adv_report: BB:BB:BB:BB:02:00 (public) event 216, len 187, rssi 0 dBm
  btnet_scan_callback: ERROR: Scan result is too big:  187

The second report is read one octet early because the advance did not
count the RSSI octet: its address is shifted, its event type is 216,
which is the first report's RSSI, and its length is 187, which is an
octet of its own address. That length then indexes the RSSI 187 octets
past the report. "bt bnep0 scan get" lists only the first advertiser.

After:

  le_adv_report: AA:AA:AA:AA:AA:01 (public) event 0, len 3, rssi -40 dBm
  le_adv_report: BB:BB:BB:BB:BB:02 (public) event 0, len 3, rssi -50 dBm

and both advertisers are listed.

le_adv_report() took the report count and each report's data length from
the event and used them without checking either against the data that was
actually received:

  - the declared data length indexes the RSSI octet, so a length larger
    than the event reads past the end of the buffer;
  - the loop was bounded only by the report count, so a count larger than
    the payload walks off the end of it;
  - bt_buf_consume() only checks its bound with DEBUGASSERT(), so on a
    build without assertions the buffer length underflows rather than
    reporting the problem.

Check that the event is long enough for the count, then check each report
against the remaining length before reading its data or its RSSI, and
stop parsing when a report does not fit.

While here, include the RSSI octet when advancing to the next report.
sizeof() of the report structure does not account for it, because the
data member is a zero-length array, so every report after the first
started one octet early.

Ref: Core v6.0, Vol 4, Part E, 7.7.65.2 (LE Advertising Report event)
Testing: builds for sim:bluetooth with Make; every commit in this series
verified to build individually.  Not yet exercised at runtime - the
scriptable controller that can inject a malformed report is added
separately.

Signed-off-by: Alan C. Assis <acassis@gmail.com>
Assisted-by: Claude Code Opus 5
@github-actions github-actions Bot added Area: Bluetooth Size: S The size of the change in this PR is small labels Sep 18, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

@acassis

acassis commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

@xiaoxiang781216 there is some error with the SO_TIMESTAMP:

candump.c:82: error: "SOF_TIMESTAMPING_SOFTWARE" redefined [-Werror]
   82 | #define SOF_TIMESTAMPING_SOFTWARE (1<<4)
      | 
In file included from candump.c:62:
/github/workspace/sources/nuttx/include/sys/socket.h:240: note: this is the location of the previous definition
  240 | #define SOF_TIMESTAMPING_SOFTWARE     SOF_TIMESTAMPING_TX_SOFTWARE
      | 
candump.c:84: error: "SOF_TIMESTAMPING_RAW_HARDWARE" redefined [-Werror]
   84 | #define SOF_TIMESTAMPING_RAW_HARDWARE (1<<6)
      | 
/github/workspace/sources/nuttx/include/sys/socket.h:241: note: this is the location of the previous definition
  241 | #define SOF_TIMESTAMPING_RAW_HARDWARE SOF_TIMESTAMPING_TX_HARDWARE
      | 
cc1: all warnings being treated as errors
make[2]: *** [/github/workspace/sources/apps/Application.mk:348: candump.c.github.workspace.sources.apps.canutils.candump.o] Error 1
make[2]: Target 'all' not remade because of errors.
make[1]: *** [Makefile:54: /github/workspace/sources/apps/canutils/candump_all] Error 2
make[1]: Target 'all' not remade because of errors.
make: *** [tools/LibTargets.mk:248: /github/workspace/sources/apps/libapps.a] Error 2
make: Target 'all' not remade because of errors.
/github/workspace/sources/nuttx/tools/testbuild.sh: line 397: /github/workspace/sources/nuttx/../nuttx/nuttx.manifest: No such file or directory
  [1/1] Normalize sim/can
/github/workspace/sources/nuttx /github/workspace/sources/nuttx
HEAD detached at pull/20184/merge
nothing to commit, working tree clean
/github/workspace/sources/nuttx
/github/workspace/sources/apps /github/workspace/sources/nuttx
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
/github/workspace/sources/nuttx
Wait 72 seconds (120 backoff)

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@xiaoxiang781216 there is some error with the SO_TIMESTAMP:

candump.c:82: error: "SOF_TIMESTAMPING_SOFTWARE" redefined [-Werror]
   82 | #define SOF_TIMESTAMPING_SOFTWARE (1<<4)
      | 
In file included from candump.c:62:
/github/workspace/sources/nuttx/include/sys/socket.h:240: note: this is the location of the previous definition
  240 | #define SOF_TIMESTAMPING_SOFTWARE     SOF_TIMESTAMPING_TX_SOFTWARE
      | 
candump.c:84: error: "SOF_TIMESTAMPING_RAW_HARDWARE" redefined [-Werror]
   84 | #define SOF_TIMESTAMPING_RAW_HARDWARE (1<<6)
      | 
/github/workspace/sources/nuttx/include/sys/socket.h:241: note: this is the location of the previous definition
  241 | #define SOF_TIMESTAMPING_RAW_HARDWARE SOF_TIMESTAMPING_TX_HARDWARE
      | 
cc1: all warnings being treated as errors
make[2]: *** [/github/workspace/sources/apps/Application.mk:348: candump.c.github.workspace.sources.apps.canutils.candump.o] Error 1
make[2]: Target 'all' not remade because of errors.
make[1]: *** [Makefile:54: /github/workspace/sources/apps/canutils/candump_all] Error 2
make[1]: Target 'all' not remade because of errors.
make: *** [tools/LibTargets.mk:248: /github/workspace/sources/apps/libapps.a] Error 2
make: Target 'all' not remade because of errors.
/github/workspace/sources/nuttx/tools/testbuild.sh: line 397: /github/workspace/sources/nuttx/../nuttx/nuttx.manifest: No such file or directory
  [1/1] Normalize sim/can
/github/workspace/sources/nuttx /github/workspace/sources/nuttx
HEAD detached at pull/20184/merge
nothing to commit, working tree clean
/github/workspace/sources/nuttx
/github/workspace/sources/apps /github/workspace/sources/nuttx
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
/github/workspace/sources/nuttx
Wait 72 seconds (120 backoff)

already fix here: apache/nuttx-apps#3788

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

Labels

Area: Bluetooth Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants