Skip to content

Agent forwarding implementation - #752

Merged
vszakats merged 20 commits into
libssh2:masterfrom
MichaelBuckley:agent-forwarding-implementation
Apr 22, 2023
Merged

vszakats merged 20 commits into
libssh2:masterfrom
MichaelBuckley:agent-forwarding-implementation

Conversation

@MichaelBuckley

Copy link
Copy Markdown
Contributor

This PR contains a series of patches that date back many years and I believe were discussed on the mailing list, but never merged. We have been using these in our local copy of libssh2 without issue since 2015, if not earlier. I believe this is the full set of changes, as we tried to use comments to mark where our copy of libssh2 differs from the canonical version.

This also contains changes I made earlier this year, but which were not discussed on the mailing list, to support certificates and FIDO2 keys with agent forwarding.

Note that this is not a complete implementation of agent forwarding, as that is outside the scope of libssh2. Clients still need to provide their own implementation that parses ssh-agent methods after calling libssh2_channel_read() and calls the appropriate callback messages in libssh2. See the man page changes in this PR for more details.

Comment thread src/userauth.c Resolved
Comment thread docs/libssh2_agent_sign.3 Resolved
Comment thread docs/libssh2_agent_sign.3 Resolved
Comment thread docs/libssh2_agent_sign.3 Resolved
Comment thread docs/libssh2_session_callback_set.3 Resolved
Comment thread docs/libssh2_session_callback_set.3 Resolved
Comment thread src/agent.c Resolved
Comment thread include/libssh2.h Resolved
Comment thread include/libssh2.h Resolved
Comment thread src/agent.c Resolved
Comment thread src/libssh2_priv.h Resolved
Comment thread src/libssh2_priv.h Resolved
Comment thread src/packet.c Resolved
Comment thread src/packet.c Resolved
Comment thread src/packet.c Resolved
Comment thread src/agent.c Resolved
Comment thread src/userauth.c Resolved
Comment thread src/packet.c Resolved
Comment thread docs/Makefile.am Resolved
src\packet.c(1215,59): warning C4267: 'function': conversion from 'size_t' to 'unsigned long', possible loss of data
@vszakats
vszakats force-pushed the agent-forwarding-implementation branch from 9849954 to 1f6706b Compare April 21, 2023 16:44
@vszakats
vszakats force-pushed the agent-forwarding-implementation branch from 1f6706b to 8eb4da5 Compare April 21, 2023 16:46
@vszakats

vszakats commented Apr 21, 2023 •

Copy link
Copy Markdown
Member

@MichaelBuckley Thank you for your update!

Comment thread include/libssh2.h Outdated
#define LIBSSH2_AUTHAGENT_SIGN_FUNC(name) \
int name(LIBSSH2_SESSION* session, \
unsigned char *blob, unsigned int blen, \
const unsigned char *data, unsigned int dlen, \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@MichaelBuckley I've added const here. Do you think this is okay?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, good catch. However, we will also need to add const to libssh2_agent_sign() to match. The agent_sign() function already marks the data parameter const.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yep, added that one already. Though we still need to sync the man page with this.

void sign(LIBSSH2_SESSION *session,
unsigned char **sig, size_t *sig_len,
const unsigned char *data, size_t data_len,
void **abstract);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The callback arguments here (LIBSSH2_CALLBACK_AUTHAGENT_IDENTITIES and LIBSSH2_CALLBACK_AUTHAGENT_SIGN) seem to differ from the ones in the code, or am I misreading?

For example from the SIGN callback, blob seems to be missing and sign comes before data here, but after in the code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right about LIBSSH2_CALLBACK_AUTHAGENT_SIGN. I think it was changed after I last edited the man page.

It should be int sign(LIBSSH2_SESSION* session, unsigned int flags, unsigned char *blob, unsigned int blen, unsigned char *data, const unsigned int dlen, unsigned char **sig, unsigned int *sig_len, const char *agent_path, void **abstract);

I'll update the man page in a bit here.

LIBSSH2_CALLBACK_AUTHAGENT_IDENTITIES looks correct to me though, unless I'm missing something.

@vszakats vszakats Apr 21, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

sign() will be fine then, thanks for looking into it.

As for identities, I'm looking at this code in include/libssh2.h:

#define LIBSSH2_ADD_IDENTITIES_FUNC(name) \
    void name(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel, \
              void **abstract)

This doesn't match LIBSSH2_CALLBACK_AUTHAGENT_IDENTITIES in
the manual or LIBSSH2_ADD_IDENTITIES() from libssh2_priv.h. They
look like the same callback, though I may be wrong.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oops, I was looking at the wrong branch. The identities in the man file was correct, but it was incorrect in the header. I was looking at a branch where it was correct in the header.

Both problems should now be fixed. The man file was updated with the correct sign(), and the header was updated to match identities() in the man file.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, no worries. We're good to merge once the CI tests finished.

@vszakats
vszakats merged commit bc4e619 into libssh2:master Apr 22, 2023
@vszakats

Copy link
Copy Markdown
Member

Merged now, thank you @MichaelBuckley!

@vszakats vszakats mentioned this pull request Apr 26, 2023
vszakats pushed a commit that referenced this pull request Apr 26, 2023
- openssl: fix potentially missing `ERR_*` constants by including
  `openssl/err.h`. This could happen with recent version of Xcode
  or when building against OpenSSL built with the `OPENSSL_NO_ENGINE`
  option.

  Regression from 097c8f0 (#789)

- channel: fix an issue that would corrupt the data stream when
  attempting to initialize the agent in non-blocking mode, as it is
  necessary to propagate the `EAGAIN` signal upstream when the transport
  returns `EAGAIN`.

  Regression from bc4e619 (#752)

- packet: the current code does not set the state machine upon reaching
  this point which means that if the code is suspended due to the
  transport returning an `EAGAIN`, this will re-initialize the structure
  every time.

  The issue is that this keeps assigning a new channel-id downstream,
  which does not match the initial channel-id that is initially
  generated, causing a lookup later to fail as there is no matching
  channel.

  Regression from bc4e619 (#752)
agreppin pushed a commit to agreppin/libssh2 that referenced this pull request Jul 14, 2024
This PR contains a series of patches that date back many years and I
believe were discussed on the mailing list, but never merged. We have
been using these in our local copy of libssh2 without issue since 2015,
if not earlier. I believe this is the full set of changes, as we tried
to use comments to mark where our copy of libssh2 differs from the
canonical version.

This also contains changes I made earlier this year, but which were not
discussed on the mailing list, to support certificates and FIDO2 keys
with agent forwarding.

Note that this is not a complete implementation of agent forwarding, as
that is outside the scope of libssh2. Clients still need to provide
their own implementation that parses ssh-agent methods after calling
libssh2_channel_read() and calls the appropriate callback messages in
libssh2. See the man page changes in this PR for more details.

Integration-patches-by: Viktor Szakats

* prefer size_t
* prefer unsigned int over u_int in public function
* add const
* docs, indent, checksrc, debug call, compiler warning fixes
agreppin pushed a commit to agreppin/libssh2 that referenced this pull request Jul 14, 2024
- openssl: fix potentially missing `ERR_*` constants by including
  `openssl/err.h`. This could happen with recent version of Xcode
  or when building against OpenSSL built with the `OPENSSL_NO_ENGINE`
  option.

  Regression from 097c8f0 (libssh2#789)

- channel: fix an issue that would corrupt the data stream when
  attempting to initialize the agent in non-blocking mode, as it is
  necessary to propagate the `EAGAIN` signal upstream when the transport
  returns `EAGAIN`.

  Regression from bc4e619 (libssh2#752)

- packet: the current code does not set the state machine upon reaching
  this point which means that if the code is suspended due to the
  transport returning an `EAGAIN`, this will re-initialize the structure
  every time.

  The issue is that this keeps assigning a new channel-id downstream,
  which does not match the initial channel-id that is initially
  generated, causing a lookup later to fail as there is no matching
  channel.

  Regression from bc4e619 (libssh2#752)
Comment thread src/userauth.c
method,
method_len)) {
const char *new_method = "[email protected]";
memcpy(method, new_method, strlen(new_method));

@vszakats vszakats Apr 25, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@MichaelBuckley clang-tidy and Copilot draw attention to the line above,
and the similar one in the if following. The new string is copied over the
input one, but without a zero-terminator. Is it intended that these two inputs
get overwritten, while the others don't? If so, wouldn't we need to copy the
NUL byte too?

(It also confused me why these two values overwrite the input, and return the
full length, while pre-existing ones return the length of the prefix without
touching the buffer.)

https://github.com/libssh2/libssh2/security/quality/ai-findings (may disappear from this page):
"The memcpy operation copies exactly strlen(new_method) bytes without null-termination. The comment suggests this is intentional, but the function returns strlen(new_method) which doesn't account for a null terminator. If the caller expects a null-terminated string (which the 'char *method' parameter suggests), this could lead to issues. Either ensure null-termination or document that the returned buffer is not null-terminated."

clang-tidy output can be seen by dropping the NOLINTLINT comments, I did
not save them unfortunately.

@vszakats vszakats Apr 30, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I double-checked and the code is fine. That's assuming all callers honor the returned length, and never consider the buffer as nul-terminated. Which was already an assumption made before this patch.

Thus, sorry about the noise and disregard the message above.

vszakats added a commit that referenced this pull request Jul 14, 2026
…key_sk()`

This code section was merged as part of PR #752, which seems to me
unrelated to the intent of the patch, and also possibly unrelated to
the description of its subcommit:
"Add support for certificate signing with ssh-agent"
55abcb0#diff-8213ab9c800f30c1a7aff37863954e756f946a3b3f6bd7be376d634fd9beb9f4R2369

"`session->userauth_pblc_method` is allocated in the ecdsa/ed25519
branches (lines 2393-2416), but then `userauth_read_blob_pubkey` is
called immediately afterward with `&session->userauth_pblc_method` as an
output parameter. Inside `userauth_read_blob_pubkey`, `*method` is
unconditionally overwritten with a newly allocated buffer (see the
`*method = pubkey` assignment), causing the allocation made just above
to be leaked. The allocation in the ecdsa/ed25519 branches should be
removed since `userauth_read_blob_pubkey` will derive and allocate the
method from the public key data itself."

Reported by GitHub Code Quality

Follow-up to bc4e619 #752
Follow-up to ed439a2 #698

Closes #2294
vszakats added a commit that referenced this pull request Jul 25, 2026
This reverts an earlier change doing the opposite. `sizeof()` determines
length at build-time, while `strlen()` does the same at runtime. In case
of literals the results are identical, but the build-time option does it
with zero runtime overhead.

Also:
- fix typo in literal used for determining length. (length was correct.)
  Follow-up to bc4e619 #752
- move literals to the right-side of comparisons, where missing.

Follow-up to 88c5fd9 #2400
Follow-up to af1b501 #2224
Follow-up to 88a960a #1179

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants