Skip to content

lib: parse_canframe: strlen OOB read when input is not null-terminated #632

Closed

Description

@natie17

Summary

parse_canframe() in lib.c calls strlen(cs) as its first operation
without verifying that the input buffer is null-terminated. Passing a
buffer without a null terminator causes an out-of-bounds read in strlen,
detected by AddressSanitizer as a heap-buffer-overflow.

Affected code

lib.c, line 168:

int parse_canframe(char *cs, union cfu *cu)
{
    int len;
    len = strlen(cs);   // ← OOB read if cs has no null terminator
    ...
}

Call sites at risk

parse_canframe is called with file-read buffers (not just CLI argv),
which may not be null-terminated if the input is malformed or crafted:

  • canplayer.c:509 — parse_canframe(afrbuf, &cu)
  • log2asc.c:449 — parse_canframe(afrbuf, &cu)
  • log2long.c:84 — parse_canframe(afrbuf, &cu)

Reproduction (minimal)

#include "lib.h"
#include <string.h>

int main(void) {
    union cfu cu;
    char input[1] = {0x0a};   // 1 byte, no null terminator
    parse_canframe(input, &cu);
    return 0;
}

Compile and run:

clang -g -fsanitize=address \
  -DCANXL_VCID_MASK=0x00FF0000 -DCANXL_VCID_OFFSET=16 \
  -o repro repro.c lib.c
./repro

ASAN output

​ ==ERROR: AddressSanitizer: stack-buffer-overflow READ of size 2 at ... thread T0 #0 in strlen #1 in parse_canframe lib.c:168 #2 in main repro.c:7 SUMMARY: AddressSanitizer: stack-buffer-overflow in strlen ​

Suggested fix

Add a size_t len parameter so callers can pass the known buffer length,
and replace strlen with strnlen or an explicit length check:

// Option A — add length parameter
int parse_canframe_n(char *cs, size_t cs_len, union cfu *cu);

// Option B — guard strlen with a maximum length
size_t len = strnlen(cs, MAX_CANFRAME_STR_LEN);
if (len == MAX_CANFRAME_STR_LEN) return 0;  // reject unterminated input

Discovery

Found using an LLM-assisted fuzzing pipeline (LogosFuzz) with
libFuzzer + AddressSanitizer. Triggered with a 1-byte input (0x0a)
in under 30 seconds from an empty corpus.

Activity

  1. hartkopp commented on Aug 28, 2026

    @hartkopp
    Member

    Hi @natie17 !

    Thanks for the review and report!

    I would suggest to go for your "Option A" so that e.g. the code in canplayer ends up with

    parse_canframe(afrbuf, sizeof(afrbuf), &cu)

    This is a compile time calculation and does not need a defined constant that might get out of sync.

    Would you like to create a patch on your own to fix the reported issue?

    Best regards,
    Oliver

  2. seldatrgt35 commented on Sep 1, 2026

    @seldatrgt35

    Hi, I’d be interested in working on this
    I can first reproduce the issue under ASan and audit all parse_canframe() call sites. My proposed scope would include a bounded parsing API, updates to the file-buffer call sites, and a regression test for non-null-terminated input while preserving the existing behavior for valid CAN CC/FD/XL strings. Before implementing it, would the maintainers prefer adding a length-aware parse_canframe_n() API while retaining parse_canframe() for compatibility, or changing the existing API and updating all callers?

  3. hartkopp commented on Sep 1, 2026

    @hartkopp
    Member

    Hi @seldatrgt35 !

    Many thanks for your offer! Your project plan sounds impressive. I really did not realize that one might think this is such a big thing.
    I would simply change the parse_canframe() function and the four(!) callers as I've suggested above. I could also do this on my own but wanted to credit @natie17 if he would provide a patch.

    IMO the changes are that simple that we do not need to do some testing beyond the PoC reproducer above.

    I hope my estimation does not disappoint you. You are still invited to send a patch or open a PR. ;-)

    Best regards,
    Oliver

  4. seldatrgt35 commented on Sep 2, 2026

    @seldatrgt35

    Hi,
    Thanks for the clarification. I’ve implemented the focused change you suggested and opened PR #633:
    #633

    Best regards,
    Selda

  5. hartkopp commented on Sep 3, 2026

    @hartkopp
    Member

    Hi Selda and @natie17 !

    I reviewed the code again and IMO there are no changes needed as the can-utils tools will never see an unterminated string in parse_canframe().

    $ git grep parse_canframe
    canplayer.c:                                    txmtu = parse_canframe(afrbuf, &cu); /* dual-use frame */
    cansend.c:      required_mtu = parse_canframe(argv[2], &cu);
    lib.c:int parse_canframe(char *cs, union cfu *cu)
    lib.h:int parse_canframe(char *cs, union cfu *cu);
    log2asc.c:                      mtu = parse_canframe(afrbuf, &cu);
    log2long.c:             mtu = parse_canframe(afrbuf, &cu);
    
    • cansend provides argv[2] which is always null terminated.
    • canplayer/log2asc/log2long use a fixed zero-initialized buffer and this fgets(buf, BUFSZ-1, stdin) call, which always adds a terminating zero to the read content.

    So there IS no problem within the can-utils.

    The "Reproduction" code above is not really a valid attack surface. Maybe the LLM does not know about fgets() and libc ;-)

    Sorry for the effort!

    Best regards,
    Oliver

  6. olerem commented on Sep 3, 2026

    @olerem
    Contributor

    We are in 2026.09! LLM agents can test actual reproduces instead questing them. I would suggest updating the LLM model or better instructing the agent :)

  7. hartkopp commented on Sep 3, 2026

    @hartkopp
    Member

    Yes. Interesting times. From home computers in the early 80s to LLMs. I'm very grateful that I was able to cover all these technology jumps in my lifetime.

  8. router0mail commented on Sep 20, 2026

    @router0mail

    Following up here since #633 was closed as "nothing to fix" for this general area -- want to flag a distinct variant in case it changes the calculus, while understanding you may view this the same way.

    parse_canframe() (lib.c) probes fixed offsets cs[5] (line 185, CAN-XL VCID/PRIO id-width detection) and cs[idx+14] (line 242, CAN-XL frame) with no length check against the actual string length before each probe. Unlike #632 (an unterminated buffer), this reproduces on a correctly NUL-terminated, valid-looking string that's simply short:

    $ ./repro "1234"        # 4 chars, 5-byte heap allocation
    ERROR: AddressSanitizer: heap-buffer-overflow ... READ of size 1
        #0 parse_canframe lib.c:185      <- cs[5]
    
    $ ./repro "12345678#"   # 9 chars, 10-byte heap allocation
    ERROR: AddressSanitizer: heap-buffer-overflow ... READ of size 1
        #0 parse_canframe lib.c:242      <- cs[idx+14]
    

    Built against current HEAD (95aae6b) with ASan, both confirmed. These offsets were introduced by 34a1cfa ("lib: make parse_canframe CAN XL aware") -- the pre-CAN-XL code only read cs[3]/cs[8], both already covered by the existing len < 4 gate; the CAN-XL id-width detection added the two unguarded probes above it.

    Understood if the position here is the same as #632 (any caller passing untrusted/short strings to this API is out of scope) -- just wanted the specific repro on record since it's a different trigger shape (valid short string vs. missing terminator) in case that's relevant to how you'd want to handle it.

  9. hartkopp commented on Sep 20, 2026

    @hartkopp
    Member

    Thanks @router0mail for pointing at the missing length checks when reading delimiter characters!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions