Skip to content

base64: allow zero length argument to base64_encode - #18617

Closed
bagder wants to merge 1 commit into
masterfrom
bagder/base64-zero-length
Closed

bagder wants to merge 1 commit into
masterfrom
bagder/base64-zero-length

Conversation

@bagder

@bagder bagder commented Sep 19, 2025

Copy link
Copy Markdown
Member

We used to treat 0 as "call strlen() to get the length" for curlx_base64_encode, but it turns out this is rather fragile as we easily do the mistake of passing in zero when the data is actually not there and then calling strlen() is wrong.

Force the caller to pass in the correct size. Encoding a zero length string now returns a zero length output and a NULL pointer.

@bagder bagder changed the title base64: forbid zero length argument to base64_encode base64: allow zero length argument to base64_encode Sep 19, 2025
bagder added a commit that referenced this pull request Sep 19, 2025
We used to treat 0 as "call strlen() to get the length" for
curlx_base64_encode, but it turns out this is rather fragile as we
easily do the mistake of passing in zero when the data is actually not
there and then calling strlen() is wrong.

Force the caller to pass in the correct size. A zero length input string
now returns a zero length output and a NULL pointer.

Closes #18617
@bagder
bagder force-pushed the bagder/base64-zero-length branch from cfc7f62 to 680fecd Compare September 19, 2025 12:07
@github-actions github-actions Bot added the tests label Sep 19, 2025
We used to treat 0 as "call strlen() to get the length" for
curlx_base64_encode, but it turns out this is rather fragile as we
easily do the mistake of passing in zero when the data is actually not
there and then calling strlen() is wrong.

Force the caller to pass in the correct size. A zero length input string
now returns a zero length output and a NULL pointer.

Closes #18617
@bagder
bagder force-pushed the bagder/base64-zero-length branch from 26cc180 to e5e65ab Compare September 19, 2025 12:32
@bagder
bagder marked this pull request as ready for review September 19, 2025 12:34
@bagder bagder closed this in 5e2d4d7 Sep 19, 2025
@bagder
bagder deleted the bagder/base64-zero-length branch September 19, 2025 20:57
vszakats added a commit to vszakats/libssh2 that referenced this pull request Jul 20, 2026
…ownhost_add*()`

To apply the same fix made earlier to `ssh2_base64_encode()`, but this
one level up in the call chain.

This is stricly speaking an incompatible change, but because the
automatic-strlen behavior was never documented for these functions, it
required a valid `keylen` value be passed, and because the fallback
feature is fragile, potentially unsafe, I propose this.

Replace by returning `LIBSSH2_ERROR_BAD_USE` if zero `keylen` is passed.

Refs:
https://github.com/libssh2/libssh2/pull/2363/files#diff-5182b571dc174e7b59106f0ea398a54784a0eb6c32eb95b9952cfa1aca893a71R866
curl/curl@5e2d4d7
curl/curl#18617

Follow-up to 102e444 libssh2#2363
Follow-up to d1c0e14 libssh2#1641
vszakats added a commit to libssh2/libssh2 that referenced this pull request Jul 20, 2026
…ownhost_add*()`

To complement/replace similar fixes made earlier to
`ssh2_base64_encode()` and `knownhost_line()`, respectively.

Until libssh2 1.11.1, `libssh2_knownhost_add*()` public APIs had a
special behavior when they received `keylen == 0` argument. They
automatically determined the length of the `key` buffer via `strlen()`.
When passing zero length either on purpose or by accident, it could
result in an OOB read if the buffer's actual length was zero. (Such case
could also happen within libssh2's own code.)

This behavior was not documented on the man page, but the public
`libssh2.h` header mentioned it in comments.

Earlier patch #1641 dropped this behavior for one codepath, which was a
breaking change, arguably for the better. This patch fixes the remaining
codepath and removes this behavior from these functions completely. They
now return `LIBSSH2_ERROR_BAD_USE` when called with a zero `keylen`
argument, with an explanatory error string.

Recommended fix is passing `key` buffer length, or `strlen()` it at the
call site if the buffer is null-terminated, and making sure the buffer
is non-empty.

Refs:
https://github.com/libssh2/libssh2/pull/2363/files#diff-5182b571dc174e7b59106f0ea398a54784a0eb6c32eb95b9952cfa1aca893a71R866
curl/curl@5e2d4d7
curl/curl#18617

Follow-up to 102e444 #2363
Follow-up to d1c0e14 #1641
Follow-up to 4b991b2

Closes #2364
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

1 participant