Skip to content

Commit

Permalink
libsmb2: fix cb_data leak when session fails during early connect
Browse files Browse the repository at this point in the history
Signed-off-by: Ronnie Sahlberg <[email protected]>
  • Loading branch information
sahlberg committed Oct 6, 2024
1 parent 1c7d607 commit 9918577
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/libsmb2-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ struct smb2_context {
smb2_error_cb error_cb;
smb2_command_cb connect_cb;
void *connect_data;
void *connect_cb_data;

int credits;

Expand Down
1 change: 1 addition & 0 deletions lib/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ void smb2_destroy_context(struct smb2_context *smb2)
free(smb2->session_key);
smb2->session_key = NULL;

free(smb2->connect_cb_data);
free(discard_const(smb2->user));
free(discard_const(smb2->server));
free(discard_const(smb2->share));
Expand Down
10 changes: 6 additions & 4 deletions lib/libsmb2.c
Original file line number Diff line number Diff line change
Expand Up @@ -538,16 +538,18 @@ free_c_data(struct smb2_context *smb2, struct connect_data *c_data)
#endif
}

if (smb2->connect_data == c_data) {
smb2->connect_data = NULL; /* to prevent double-free in smb2_destroy_context */
}
if (smb2->connect_cb_data == c_data->cb_data) {
smb2->connect_cb_data = NULL;
}
free(c_data->utf8_unc);
free(c_data->utf16_unc);
free(discard_const(c_data->server));
free(discard_const(c_data->share));
free(discard_const(c_data->user));
free(c_data);

if (smb2->connect_data == c_data) {
smb2->connect_data = NULL; /* to prevent double-free in smb2_destroy_context */
}
}

static void
Expand Down
2 changes: 2 additions & 0 deletions lib/sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ int smb2_connect_share(struct smb2_context *smb2,
smb2_set_error(smb2, "Failed to allocate sync_cb_data");
return -ENOMEM;
}
free(smb2->connect_cb_data);
smb2->connect_cb_data = cb_data;

rc = smb2_connect_share_async(smb2, server, share, user, connect_cb, cb_data);
if (rc < 0) {
Expand Down

0 comments on commit 9918577

Please sign in to comment.