Uh oh!
There was an error while loading. Please reload this page.
Fix a one byte heap overflow in LoadTpmSshKey - #1164
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes a one-byte heap overflow in LoadTpmSshKey() when building an authorized_keys-style line from a key file that does not end with a trailing newline, and adds an API test to prevent regressions.
Changes:
- Increase the
WMALLOC()size inLoadTpmSshKey()fromlength + usernameLen + 2tolength + usernameLen + 3so space + username + newline + NUL always fit. - Expose
LoadTpmSshKey()(non-static) underWOLFSSH_TPMviaexamples/echoserver/echoserver.hto enable direct testing. - Add a TPM-focused API test that covers both the no-trailing-newline case and the trimmed trailing-newline case.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tests/api.c | Includes echoserver header for TPM builds and adds a regression test for LoadTpmSshKey() newline handling. |
| examples/echoserver/echoserver.c | Makes LoadTpmSshKey() externally visible and fixes the allocation off-by-one. |
| examples/echoserver/echoserver.h | Declares LoadTpmSshKey() under WOLFSSH_TPM for test access. |
| ide/Espressif/ESP-IDF/examples/wolfssh_echoserver/main/echoserver.c | Applies the same one-byte allocation fix to the ESP-IDF copy. |
Suppressed comments (1)
tests/api.c:1841
- Same as above: cleanup should assert remove(keyPath) succeeds to avoid leaving temp files behind and to match the pattern used elsewhere in this test file.
remove(keyPath);
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Uh oh!
There was an error while loading. Please reload this page.
5341aab to
b47c43fCompare
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #1164
Scan targets checked:wolfssh-bugs, wolfssh-src
Findings: 5
5 finding(s) posted as inline comments (see file-level comments below)
This review was generated automatically by Fenrir. Findings are non-blocking.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
b47c43f to
b12b8e7CompareUh oh!
There was an error while loading. Please reload this page.
Problem
LoadTpmSshKey()builds anauthorized_keysline by appending" <user>\n"and a terminating NUL to the contents of the key file, but allocates onlylength + usernameLen + 2bytes — one short:buffer[length] = ' 'lengthWMEMCPY(buffer + length + 1, username, usernameLen)length + usernameLenbuffer[length + 1 + usernameLen] = '\n'length + usernameLen + 1buffer[length + 2 + usernameLen] = '\0'length + usernameLen + 2— one past the endThe trim loop normally hides this. A key file ending in
\ndecrementslength, which pulls every write back in bounds. A well-formed key file with no trailing newline leaveslengthuntouched and the terminating NUL lands one byte past the allocation.Fix (
examples/echoserver/echoserver.c)Reserve
length + usernameLen + 3so the separator space, the username, the newline and the NUL all fit. Identical one-line change in the copy atide/Espressif/ESP-IDF/examples/wolfssh_echoserver/main/echoserver.c.Closes f-8820.
Tests (
tests/api.c)test_LoadTpmSshKey_NoTrailingNewline()stages a key file with no trailing newline, calls the loader and compares the assembled line. A second case with a\n-terminated file pins the trim path that already worked.LoadTpmSshKey()is no longerstaticand is declared inechoserver.hunderWOLFSSH_TPM, matching howexamples/client/common.hexposesClientSetTpm().api.cnow includesechoserver.hfor TPM builds as well, not only the SCP/SFTP builds that setWOLFSSH_TEST_ECHOSERVER. Without this the test silently compiles out under--enable-tpm --enable-certs, where both are off.Verification
heap-buffer-overflow WRITE of size 1atechoserver.c:2679, 0 bytes past a 71-byte region. Clean with the fix applied.api.testexits 0 under both--enable-tpm --enable-certsand--enable-all --enable-tpm.-Werror, 6 configurations (enable-all, zephyr defines, sftp-only, scp-only, default, smallstack) all clean.Not in this PR
No CI job compiles test binaries with
--enable-tpm—tpm-ssh.ymlrunsmakebut nevermake check, and--enable-alldoes not include TPM — so this test does not yet gate merges. Tracked separately.