Skip to content

publickey: rework bounds checks to avoid pointer comparisons - #2205

Closed
vszakats wants to merge 4 commits into
libssh2:masterfrom
vszakats:pubkey-oob-cop
Closed

vszakats wants to merge 4 commits into
libssh2:masterfrom
vszakats:pubkey-oob-cop

Conversation

@vszakats

@vszakats vszakats commented Jul 4, 2026

Copy link
Copy Markdown
Member

#2204 (comment)
#2203 (comment) (and the rest below)

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

Reworks bounds checking in src/publickey.c when parsing publickey subsystem responses, aiming to prevent out-of-bounds reads by validating packet offsets relative to the current parse cursor rather than relying on pointer-end comparisons.

Changes:

  • Replaces pointer-comparison bounds checks with data_len vs (cursor - base) style checks in publickey init response parsing.
  • Applies the same offset-based bounds-check pattern across the libssh2_publickey_list_fetch() response parsing paths (status, v1, v2, and attrs parsing).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/publickey.c
Comment on lines +442 to +443
if(session->pkeyInit_data_len <
(size_t)(s - session->pkeyInit_data) + descr_len + 4) {

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.

Fixing in a future commit by capping individual variable-length packet elements to sane values.
FTR the max length for a whole packet is 40KB, via LIBSSH2_PACKET_MAXPAYLOAD.

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.

→ PR #2207

Comment thread src/publickey.c
Comment on lines +453 to +454
if(session->pkeyInit_data_len <
(size_t)(s - session->pkeyInit_data) + lang_len) {
Comment thread src/publickey.c
Comment on lines +851 to 854
if(pkey->listFetch_data_len <
(size_t)(pkey->listFetch_s - pkey->listFetch_data) +
descr_len + 4) {
ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
Comment thread src/publickey.c
Comment on lines +863 to +864
if(pkey->listFetch_data_len <
(size_t)(pkey->listFetch_s - pkey->listFetch_data) + lang_len) {
Comment thread src/publickey.c
Comment on lines +921 to +923
if(pkey->listFetch_data_len <
(size_t)(pkey->listFetch_s - pkey->listFetch_data) +
comment_len) {
Comment thread src/publickey.c
Comment on lines +980 to +982
if(pkey->listFetch_data_len <
(size_t)(pkey->listFetch_s - pkey->listFetch_data) +
list[keys].blob_len) {
Comment thread src/publickey.c
Comment on lines +1002 to +1004
if(pkey->listFetch_data_len <
(size_t)(pkey->listFetch_s - pkey->listFetch_data) +
list[keys].name_len) {
Comment thread src/publickey.c
Comment on lines +1021 to +1023
if(pkey->listFetch_data_len <
(size_t)(pkey->listFetch_s - pkey->listFetch_data) +
list[keys].blob_len) {
Comment thread src/publickey.c
Comment on lines +1068 to +1070
if(pkey->listFetch_data_len <
(size_t)(pkey->listFetch_s - pkey->listFetch_data) +
list[keys].attrs[i].name_len) {
Comment thread src/publickey.c
Comment on lines +1089 to +1091
if(pkey->listFetch_data_len <
(size_t)(pkey->listFetch_s - pkey->listFetch_data) +
list[keys].attrs[i].value_len) {
@vszakats
vszakats marked this pull request as draft July 4, 2026 14:47
@vszakats vszakats changed the title publickey: rework bounds checks publickey: rework bounds checks to avoid pointer comparisons Jul 4, 2026
@vszakats
vszakats marked this pull request as ready for review July 4, 2026 15:01
@vszakats vszakats closed this in a0334db Jul 4, 2026
@vszakats
vszakats deleted the pubkey-oob-cop branch July 4, 2026 15:03
vszakats added a commit that referenced this pull request Jul 5, 2026
To avoid potential offset wrap in 32-bit builds when receiving overly
large values in length fields. Limit them to the maximum packet size.

Bug: #2205 (comment)

Closes #2207
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