Skip to content

Commit 5e0edd8

Browse files
committed
session BUGFIX freeing invalid multi-channel SSH sessions
Fixes CESNET/netopeer2#518
1 parent 8d1ad2e commit 5e0edd8

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

src/session.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,14 +796,20 @@ nc_session_free(struct nc_session *session, void (*data_free)(void *))
796796
}
797797
/* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
798798
if (session->flags & NC_SESSION_SSH_MSG_CB) {
799-
for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
799+
siter = session->ti.libssh.next;
800+
while (siter && siter->status != NC_STATUS_RUNNING) {
800801
if (siter->ti.libssh.next == session) {
801802
ERRINT;
802803
break;
803804
}
805+
siter = siter->ti.libssh.next;
804806
}
807+
/* siter may be NULL in case all the sessions terminated at the same time (socket was disconnected),
808+
* we set session to NULL because we do not expect any new message to arrive */
805809
ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
806-
siter->flags |= NC_SESSION_SSH_MSG_CB;
810+
if (siter) {
811+
siter->flags |= NC_SESSION_SSH_MSG_CB;
812+
}
807813
}
808814
}
809815

src/session_server_ssh.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,11 +1130,11 @@ nc_sshcb_msg(ssh_session UNUSED(sshsession), ssh_message msg, void *data)
11301130
}
11311131

11321132
VRB("Received an SSH message \"%s\" of subtype \"%s\".", str_type, str_subtype);
1133-
if ((session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) {
1133+
if (!session || (session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) {
11341134
/* "valid" situation if, for example, receiving some auth or channel request timeouted,
11351135
* but we got it now, during session free */
11361136
VRB("SSH message arrived on a %s session, the request will be denied.",
1137-
(session->status == NC_STATUS_CLOSING ? "closing" : "invalid"));
1137+
(session && session->status == NC_STATUS_CLOSING ? "closing" : "invalid"));
11381138
ssh_message_reply_default(msg);
11391139
return 0;
11401140
}

0 commit comments

Comments
 (0)