Skip to content

pem: _libssh2_pem_parse() extend error handling, limit size to 1 MiB - #1880

Closed
vszakats wants to merge 2 commits into
libssh2:masterfrom
vszakats:pemloadcheck
Closed

vszakats wants to merge 2 commits into
libssh2:masterfrom
vszakats:pemloadcheck

Conversation

@vszakats

Copy link
Copy Markdown
Member

Limit size of loaded keys to 1 MiB.

Ref: #1878 (comment)
Suggested-by: Will Cosgrove

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/pem.c Resolved
Comment thread src/pem.c
Comment on lines +132 to +135
if(file_size > (1024 * 1024)) {
_libssh2_error(session, LIBSSH2_ERROR_FILE,
"Input too large in PEM parsing");
goto out;
@vszakats

vszakats commented Apr 24, 2026 •

Copy link
Copy Markdown
Member Author

Pre-existing issue: ftell() returns an undefined value for files opened in text mode:
https://en.cppreference.com/c/io/ftell

→ #1883

vszakats added a commit that referenced this pull request Apr 24, 2026
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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 for headerend, and it may also memcmp() against an uninitialized line buffer. Consider making the memory parser (or readline_memory()) return an error on EOF and explicitly error out when headerbegin/headerend aren’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.

Comment thread src/pem.c Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ret to -1 and simplifies failure paths via goto 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.

Comment thread src/pem.c Resolved
Comment thread src/pem.c Resolved
@vszakats vszakats changed the title pem: _libssh2_pem_parse() extend error handling and limit size pem: _libssh2_pem_parse() extend error handling, limit size to 1 MiB Apr 24, 2026
@vszakats
vszakats requested a review from Copilot April 24, 2026 08:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/pem.c
Comment on lines +121 to +128
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,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Patch pending, also a pre-existing issue.

@vszakats vszakats closed this in a173bd9 Apr 24, 2026
@vszakats
vszakats deleted the pemloadcheck branch April 24, 2026 09:17
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.

2 participants