Skip to content

obdscanner: actually reject out-of-range CAN identifiers - #5313

Open
qianeric-backup wants to merge 1 commit into
secdev:masterfrom
qianeric-backup:fix/obdscanner-can-id-range
Open

qianeric-backup wants to merge 1 commit into
secdev:masterfrom
qianeric-backup:fix/obdscanner-can-id-range

Conversation

@qianeric-backup

Copy link
Copy Markdown

What

obdscanner documents --source / --destination as 11-bit CAN identifiers (0x000–0x7ff) and already contains the check for it, but that check can never fire: it is written as the chained comparison 0 > source >= 0x800, which expands to 0 > source and source >= 0x800 and is always false. Only the third operand (source == destination) of the same if statement works.

Why

Out-of-range identifiers are accepted silently and the scan "succeeds":

$ python3 -m scapy.tools.automotive.obdscanner --channel vcan0 \
      --source 0x800 --destination 0x7df --timeout 0.01 -1
...
=== No data to display ===
$ echo $?
0

A capture on vcan0 shows the frames are really transmitted as 29-bit extended identifiers (scapy/contrib/automotive/isotp_native_socket.py sets CAN_EFF_FLAG for tx_id > 0x7ff), so no OBD ECU will ever answer and the user gets a zero-exit "no data" scan instead of a diagnostic:

--source frame on the wire
0x800 (invalid) id=0x800 flags=extended len=16
0x7e0 (valid) id=0x7e0 flags= len=16

Without a CAN interface the invalid id is reported as a missing device (Socket couldn't be created. Check your arguments. / [Errno 19] No such device), which points at the wrong problem.

Clean sibling

In the same function, the sibling checks already behave correctly and print a single line with a non-zero exit code, which is what this if was meant to do:

    if 0 > source >= 0x800 or 0 > destination >= 0x800\
            or source == destination:      # only this operand works
        print("The ids must be >= 0 and < 0x800 and not equal.", file=sys.stderr)
        sys.exit(1)

    if 0 > timeout:                        # works
        print("The timeout must be a positive value")
        sys.exit(1)
invocation output rc
--source 0x7e0 --destination 0x7e0 (third operand) The ids must be >= 0 and < 0x800 and not equal. 1
--timeout -5 (adjacent range check) The timeout must be a positive value 1
--source 0x800 --destination 0x7df (nothing — full scan) 0

Change

Make the two range predicates real comparisons; the existing message, exit code and control flow are unchanged:

-    if 0 > source >= 0x800 or 0 > destination >= 0x800\
+    if not (0 <= source < 0x800) or not (0 <= destination < 0x800) \
             or source == destination:
         print("The ids must be >= 0 and < 0x800 and not equal.",
               file=sys.stderr)

No behaviour change for valid identifiers, and no new user-visible strings.

Test

Two subprocess tests added to test/tools/obdscanner.uts (+ Usage tests, next to the existing usage tests), covering an out-of-range source and an out-of-range destination id:

$ unshare -r python3 -m scapy.tools.UTscapy -t test/tools/obdscanner.uts -n 0,1,2,3,4 -f text
passed 05A9F891 000.26s Imports
passed 7E35973F 000.15s Test wrong usage
passed 8B313EE8 000.14s Test show help
passed 99A3FCD9 000.14s Test out of range source id
passed 7263A3CC 000.14s Test out of range destination id
 └ Passed=5
 └ Failed=0

On master the same two tests fail (assert returncode != 0 → the scan exits 0). tox -e flake8 is clean on both trees (0 findings).

Before / after

command (--channel vcan0, -t 0.01 -1 unless noted) master this PR
--source 0x800 --destination 0x7df rc=0, full scan, No data to display rc=1, The ids must be >= 0 and < 0x800 and not equal.
--source 0x7e0 --destination 0x800 rc=0, full scan rc=1, same message
--source 0x7e0 --destination 0x7e0 rc=1, same message rc=1, same message
--source 0x7e0 --destination 0x7df rc=0 rc=0
--timeout -5 rc=1, The timeout must be a positive value rc=1, unchanged
--channel can0 --source 0x800 … (no device) rc=1, [Errno 19] No such device rc=1, id message (checked before any socket is opened)
Full-suite comparison

220 .uts files run per tree. The only differences between the two runs were port-contention flakes in doip.uts, tlsclientserver.uts and p0f.uts that reproduce on the patched tree when run alone (0 failures), i.e. no test passes on master and fails with this patch.

AI-Assisted: yes (DeepSeek agent)

The range check used a chained comparison (`0 > source >= 0x800`), which is
always false, so out-of-range ids were accepted and silently sent as 29-bit
extended identifiers. The scan then finished with exit status 0 and no data.

AI-Assisted: yes (DeepSeek agent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant