1
0
mirror of https://github.com/libssh2/libssh2.git synced 2026-10-24 03:48:58 +03:00

agent: add NULL guards to all public libssh2_agent_*() API functions

All public API functions in `src/agent.c` that take a `LIBSSH2_AGENT *`
were missing a NULL-pointer check at entry.

A caller that passes NULL (for example, after `libssh2_agent_init()`
returns NULL due to OOM) immediately dereferences the null pointer and
crashes.  Every other source file in this tree already has the pattern:
`channel.c`, `sftp.c`, `session.c`, `userauth.c`, and `knownhost.c` all
guard their primary handle before touching any field. `agent.c` was the
only outlier.

Add `if(!agent)` guards consistent with the style used in the rest of
the codebase.

Closes #2635
This commit is contained in:
filtede98
2026-10-01 20:34:44 +02:00
committed by Viktor Szakats
parent 4f2692fd81
commit 1f977e77f5

View File

@@ -1129,6 +1129,10 @@ LIBSSH2_AGENT *libssh2_agent_init(LIBSSH2_SESSION *session)
int libssh2_agent_connect(LIBSSH2_AGENT *agent)
{
int i, rc = LIBSSH2_ERROR_METHOD_NOT_SUPPORTED;
if(!agent)
return LIBSSH2_ERROR_BAD_USE;
for(i = 0; agent_supported_backends[i].name; i++) {
agent->ops = agent_supported_backends[i].ops;
rc = agent->ops->connect(agent);
@@ -1145,6 +1149,9 @@ int libssh2_agent_connect(LIBSSH2_AGENT *agent)
*/
int libssh2_agent_list_identities(LIBSSH2_AGENT *agent)
{
if(!agent)
return LIBSSH2_ERROR_BAD_USE;
memset(&agent->transctx, 0, sizeof(agent->transctx));
/* Abandon the last fetched identities */
agent_free_identities(agent);
@@ -1166,6 +1173,10 @@ int libssh2_agent_get_identity(LIBSSH2_AGENT *agent,
struct libssh2_agent_publickey *prev)
{
struct agent_publickey *node;
if(!agent)
return LIBSSH2_ERROR_BAD_USE;
if(prev && prev->node) {
/* we have a starting point */
struct agent_publickey *prev_node = prev->node;
@@ -1197,6 +1208,9 @@ int libssh2_agent_userauth(LIBSSH2_AGENT *agent,
void *abstract = agent;
int rc;
if(!agent)
return LIBSSH2_ERROR_BAD_USE;
if(agent->session->userauth_pblc_state == ssh2_NB_state_idle) {
memset(&agent->transctx, 0, sizeof(agent->transctx));
agent->identity = identity->node;
@@ -1230,6 +1244,9 @@ int libssh2_agent_sign(LIBSSH2_AGENT *agent,
int rc;
uint32_t key_kind_len;
if(!agent)
return LIBSSH2_ERROR_BAD_USE;
if(agent->session->userauth_pblc_state == ssh2_NB_state_idle) {
memset(&agent->transctx, 0, sizeof(agent->transctx));
agent->identity = identity->node;
@@ -1273,6 +1290,9 @@ cleanup:
*/
int libssh2_agent_disconnect(LIBSSH2_AGENT *agent)
{
if(!agent)
return LIBSSH2_ERROR_BAD_USE;
if(agent->ops && agent->fd != LIBSSH2_INVALID_SOCKET)
return agent->ops->disconnect(agent);
return LIBSSH2_ERROR_NONE;
@@ -1284,6 +1304,9 @@ int libssh2_agent_disconnect(LIBSSH2_AGENT *agent)
*/
void libssh2_agent_free(LIBSSH2_AGENT *agent)
{
if(!agent)
return;
/* Allow connection freeing when the socket has lost its connection */
if(agent->fd != LIBSSH2_INVALID_SOCKET)
libssh2_agent_disconnect(agent);
@@ -1300,6 +1323,9 @@ void libssh2_agent_free(LIBSSH2_AGENT *agent)
*/
void libssh2_agent_set_identity_path(LIBSSH2_AGENT *agent, const char *path)
{
if(!agent)
return;
if(agent->identity_agent_path)
SSH2_SAFEFREE(agent->session, agent->identity_agent_path);
@@ -1321,5 +1347,8 @@ void libssh2_agent_set_identity_path(LIBSSH2_AGENT *agent, const char *path)
*/
const char *libssh2_agent_get_identity_path(LIBSSH2_AGENT *agent)
{
if(!agent)
return NULL;
return agent->identity_agent_path;
}