You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
lib: parse_canframe: strlen OOB read when input is not null-terminated #632
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:
intparse_canframe(char*cs, unioncfu*cu)
{
intlen;
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:
==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 parameterintparse_canframe_n(char*cs, size_tcs_len, unioncfu*cu);
// Option B — guard strlen with a maximum lengthsize_tlen=strnlen(cs, MAX_CANFRAME_STR_LEN);
if (len==MAX_CANFRAME_STR_LEN) return0; // 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.
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?
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. ;-)
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 ;-)
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 :)
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.
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:
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.
Activity
hartkopp commentedon Aug 28, 2026
Hi @natie17 !
Thanks for the review and report!
I would suggest to go for your "Option A" so that e.g. the code in
canplayerends up withparse_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
seldatrgt35 commentedon Sep 1, 2026
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?
hartkopp commentedon Sep 1, 2026
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
seldatrgt35 commentedon Sep 2, 2026
Hi,
Thanks for the clarification. I’ve implemented the focused change you suggested and opened PR #633:
#633
Best regards,
Selda
hartkopp commentedon Sep 3, 2026
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().
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
olerem commentedon Sep 3, 2026
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 :)
hartkopp commentedon Sep 3, 2026
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.
router0mail commentedon Sep 20, 2026
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 offsetscs[5](line 185, CAN-XL VCID/PRIO id-width detection) andcs[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:Built against current HEAD (
95aae6b) with ASan, both confirmed. These offsets were introduced by34a1cfa("lib: make parse_canframe CAN XL aware") -- the pre-CAN-XL code only readcs[3]/cs[8], both already covered by the existinglen < 4gate; 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.
lib: parse_canframe: guard delimiter reads with length checks
hartkopp commentedon Sep 20, 2026
Thanks @router0mail for pointing at the missing length checks when reading delimiter characters!