Repository navigation
Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens PEM key loading in src/pem.c by improving file I/O error handling and enforcing a maximum input size (1 MiB) to prevent oversized key files from being loaded into memory.
Changes:
- Add explicit error handling for
fseek()/ftell()failures during PEM parsing. - Enforce a 1 MiB maximum size for loaded PEM inputs before allocation/read.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Pre-existing issue: → #1883 |
To fix `ftell()` returning the size in bytes in `_libssh2_pem_parse()`. Ref: #1880 (comment) Follow-up to ef6eaad #235 Closes #1883
Limit size of loaded keys to 1 MiB. Ref: libssh2#1878 (comment) Suggested-by: Will Cosgrove
There was a problem hiding this comment.
Pull request overview
This PR hardens PEM key loading by adding explicit file I/O error handling and enforcing a maximum key file size (1 MiB) before parsing, improving resilience against oversized or unreadable inputs.
Changes:
- Add error checks for
fseek()/ftell()failures when determining PEM file size. - Enforce a 1 MiB maximum input size for PEM files before reading into memory.
- Initialize the return value to a failure state to avoid undefined returns on early error paths.
Comments suppressed due to low confidence (1)
src/pem.c:158
_libssh2_pem_parse_memory()does not appear to handle the “header/footer not found” case safely (e.g., when callers probe multiple key types): it can continue past EOF and loop indefinitely waiting forheaderend, and it may alsomemcmp()against an uninitializedlinebuffer. Consider making the memory parser (orreadline_memory()) return an error on EOF and explicitly error out whenheaderbegin/headerendaren’t found, to avoid hangs when parsing non-matching/invalid PEM input.
goto out;
}
ret = _libssh2_pem_parse_memory(session, headerbegin, headerend,
passphrase, filedata, filedata_len,
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Extends _libssh2_pem_parse() error handling while adding a hard 1 MiB size limit for PEM key files, aligning the file-based PEM parser with recent key-loading hardening elsewhere in the codebase.
Changes:
- Adds
fseek()/ftell()error handling and validates file size (non-zero, ≤ 1 MiB). - Initializes
retto-1and simplifies failure paths viagoto out. - Introduces more specific file I/O error messages for PEM parsing failures.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
_libssh2_pem_parse() extend error handling and limit size_libssh2_pem_parse() extend error handling, limit size to 1 MiB
There was a problem hiding this comment.
Pull request overview
Updates PEM file parsing to be more defensive when reading key material from disk, including enforcing an upper bound on input size.
Changes:
- Add explicit error handling for
fseek()/ftell()failures in_libssh2_pem_parse(). - Reject zero-length inputs and cap PEM input size to 1 MiB before allocating/reading.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if(fseek(fp, 0L, SEEK_END) != 0) { | ||
| _libssh2_error(session, LIBSSH2_ERROR_FILE, | ||
| "Bad seek to file end in PEM parsing"); | ||
| goto out; | ||
| } | ||
| file_size = ftell(fp); | ||
| if(file_size < 0) { | ||
| _libssh2_error(session, LIBSSH2_ERROR_FILE, |
There was a problem hiding this comment.
Patch pending, also a pre-existing issue.
Limit size of loaded keys to 1 MiB.
Ref: #1878 (comment)
Suggested-by: Will Cosgrove