Skip to content

Commit

Permalink
libssh2: Fix infinite loop when enabling callback without handlers
Browse files Browse the repository at this point in the history
When libssh2_session_callback_set() is used to activate a callback
for which no handler was registered in libssh2_session_init_ex(),
this can result in an infinite loop because the user event from
lvssh2 is always registered automatically. When the callback is
activated, it always returns default values because the VI refnum
is undefined.

This changes the implementation so that the LabVIEW user event is
only created when a valid VI refnum is provided. The extension
library returns an error if the user event does not exist, which
effectively breaks any infinite loops.
  • Loading branch information
logmanoriginal committed Oct 4, 2024
1 parent 89c0972 commit 898305a
Show file tree
Hide file tree
Showing 22 changed files with 44 additions and 6 deletions.
23 changes: 18 additions & 5 deletions extensions/lvssh2_extensions.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ void lvssh2_trace_handler_function(LIBSSH2_SESSION* session, void* context, cons
LIBSSH2_SEND_FUNC(lvssh2_session_callback_send_function) {
ASSERT_LABVIEW_MAXLEN(length);

lvssh2_abstract* lv_abstract = *(lvssh2_abstract**)abstract;
if (!lv_abstract->send)
{
return LIBSSH2_ERROR_BAD_USE;
}

lvssh2_session_callback_send_function_input_args payload = { 0 };
payload.socket = socket;
payload.buffer = NULL;
Expand All @@ -30,7 +36,6 @@ LIBSSH2_SEND_FUNC(lvssh2_session_callback_send_function) {

data_buffer_to_LStrHandle(buffer, (int32)length, &payload.buffer);

lvssh2_abstract* lv_abstract = *(lvssh2_abstract**)abstract;
PostLVUserEvent(lv_abstract->send, &payload);

DSDisposeHandle(payload.buffer);
Expand All @@ -39,6 +44,12 @@ LIBSSH2_SEND_FUNC(lvssh2_session_callback_send_function) {
}

LIBSSH2_RECV_FUNC(lvssh2_session_callback_recv_function) {
lvssh2_abstract* lv_abstract = *(lvssh2_abstract**)abstract;
if (!lv_abstract->recv)
{
return LIBSSH2_ERROR_BAD_USE;
}

lvssh2_session_callback_recv_function_input_args payload = { 0 };
payload.socket = socket;
payload.buffer = buffer;
Expand All @@ -48,8 +59,6 @@ LIBSSH2_RECV_FUNC(lvssh2_session_callback_recv_function) {
ssize_t bytes_received = 0;
payload.bytes_received = &bytes_received;

lvssh2_abstract* lv_abstract = *(lvssh2_abstract**)abstract;

PostLVUserEvent(lv_abstract->recv, &payload);

return bytes_received;
Expand All @@ -63,6 +72,12 @@ LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC(lvssh2_userauth_keyboard_interactive_respo
return;
}

lvssh2_abstract* lv_abstract = *(lvssh2_abstract**)abstract;
if (!lv_abstract->kbdint_response)
{
return;
}

LStrHandle* lv_responses = (LStrHandle*)malloc(num_prompts * sizeof(LStrHandle));
if (!lv_responses) {
return;
Expand All @@ -81,8 +96,6 @@ LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC(lvssh2_userauth_keyboard_interactive_respo
payload.prompts = prompts;
payload.responses = lv_responses;

lvssh2_abstract* lv_abstract = *(lvssh2_abstract**)abstract;

PostLVUserEvent(lv_abstract->kbdint_response, &payload);

for (int i = 0; i < num_prompts; i++) {
Expand Down
Binary file modified libssh2/Create Session Refnum.vi
Binary file not shown.
Binary file modified libssh2/Delete Session Refnum.vi
Binary file not shown.
Binary file modified libssh2/Read Session Refnum.vi
Binary file not shown.
Binary file added libssh2/Session Refnum Data.ctl
Binary file not shown.
25 changes: 24 additions & 1 deletion libssh2/libssh2.lvlib
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@
<Item Name="Read Session Refnum.vi" Type="VI" URL="../Read Session Refnum.vi">
<Property Name="NI.LibItem.Scope" Type="Int">2</Property>
</Item>
<Item Name="Session Refnum Data.ctl" Type="VI" URL="../Session Refnum Data.ctl">
<Property Name="NI.LibItem.Scope" Type="Int">2</Property>
</Item>
<Item Name="Session Refnum.ctl" Type="VI" URL="../Session Refnum.ctl"/>
</Item>
<Item Name="Session Callback" Type="Folder">
Expand All @@ -133,7 +136,9 @@
<Item Name="lvssh2_session_callback_events_init.vi" Type="VI" URL="../lvssh2_session_callback_events_init.vi">
<Property Name="NI.LibItem.Scope" Type="Int">2</Property>
</Item>
<Item Name="lvssh2_session_callback_get_function.vi" Type="VI" URL="../lvssh2_session_callback_get_function.vi"/>
<Item Name="lvssh2_session_callback_get_function.vi" Type="VI" URL="../lvssh2_session_callback_get_function.vi">
<Property Name="NI.LibItem.Scope" Type="Int">2</Property>
</Item>
<Item Name="lvssh2_session_callback_receive.vit" Type="VI" URL="../lvssh2_session_callback_receive.vit"/>
<Item Name="lvssh2_session_callback_receive_function_input_args_32.ctl" Type="VI" URL="../lvssh2_session_callback_receive_function_input_args_32.ctl">
<Property Name="NI.LibItem.Scope" Type="Int">2</Property>
Expand All @@ -147,6 +152,24 @@
<Item Name="lvssh2_session_callback_receive_handler_64.vi" Type="VI" URL="../lvssh2_session_callback_receive_handler_64.vi">
<Property Name="NI.LibItem.Scope" Type="Int">2</Property>
</Item>
<Item Name="lvssh2_session_callback_register_kbdint_response_32.vi" Type="VI" URL="../lvssh2_session_callback_register_kbdint_response_32.vi">
<Property Name="NI.LibItem.Scope" Type="Int">2</Property>
</Item>
<Item Name="lvssh2_session_callback_register_kbdint_response_64.vi" Type="VI" URL="../lvssh2_session_callback_register_kbdint_response_64.vi">
<Property Name="NI.LibItem.Scope" Type="Int">2</Property>
</Item>
<Item Name="lvssh2_session_callback_register_receive_32.vi" Type="VI" URL="../lvssh2_session_callback_register_receive_32.vi">
<Property Name="NI.LibItem.Scope" Type="Int">2</Property>
</Item>
<Item Name="lvssh2_session_callback_register_receive_64.vi" Type="VI" URL="../lvssh2_session_callback_register_receive_64.vi">
<Property Name="NI.LibItem.Scope" Type="Int">2</Property>
</Item>
<Item Name="lvssh2_session_callback_register_send_32.vi" Type="VI" URL="../lvssh2_session_callback_register_send_32.vi">
<Property Name="NI.LibItem.Scope" Type="Int">2</Property>
</Item>
<Item Name="lvssh2_session_callback_register_send_64.vi" Type="VI" URL="../lvssh2_session_callback_register_send_64.vi">
<Property Name="NI.LibItem.Scope" Type="Int">2</Property>
</Item>
<Item Name="lvssh2_session_callback_send.vit" Type="VI" URL="../lvssh2_session_callback_send.vit"/>
<Item Name="lvssh2_session_callback_send_function_input_args_32.ctl" Type="VI" URL="../lvssh2_session_callback_send_function_input_args_32.ctl">
<Property Name="NI.LibItem.Scope" Type="Int">2</Property>
Expand Down
Binary file modified libssh2/libssh2_session_abstract.vi
Binary file not shown.
Binary file modified libssh2/libssh2_session_init_ex.vi
Binary file not shown.
Binary file modified libssh2/lvssh2_extensions.dll
Binary file not shown.
Binary file modified libssh2/lvssh2_extensions_64.dll
Binary file not shown.
Binary file modified libssh2/lvssh2_session_callback_events_init.vi
Binary file not shown.
Binary file modified libssh2/lvssh2_session_callback_receive_handler_64.vi
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified libssh2/lvssh2_session_callback_send_handler_64.vi
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/integration_tests/integration_tests.lvlib
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<Item Name="test_libssh2_session_banner_get.vi" Type="VI" URL="../test_libssh2_session_banner_get.vi"/>
<Item Name="test_libssh2_session_banner_set.vi" Type="VI" URL="../test_libssh2_session_banner_set.vi"/>
<Item Name="test_libssh2_session_block_directions.vi" Type="VI" URL="../test_libssh2_session_block_directions.vi"/>
<Item Name="test_libssh2_session_callback_set - no handler.vi" Type="VI" URL="../test_libssh2_session_callback_set - no handler.vi"/>
<Item Name="test_libssh2_session_callback_set.vi" Type="VI" URL="../test_libssh2_session_callback_set.vi"/>
<Item Name="test_libssh2_session_disconnect.vi" Type="VI" URL="../test_libssh2_session_disconnect.vi"/>
<Item Name="test_libssh2_session_handshake.vi" Type="VI" URL="../test_libssh2_session_handshake.vi"/>
Expand Down Expand Up @@ -72,6 +73,7 @@
<Item Name="test_libssh2_sftp_write.vi" Type="VI" URL="../test_libssh2_sftp_write.vi"/>
<Item Name="test_libssh2_userauth_authenticated.vi" Type="VI" URL="../test_libssh2_userauth_authenticated.vi"/>
<Item Name="test_libssh2_userauth_banner.vi" Type="VI" URL="../test_libssh2_userauth_banner.vi"/>
<Item Name="test_libssh2_userauth_keyboard_interactive_ex - no handler.vi" Type="VI" URL="../test_libssh2_userauth_keyboard_interactive_ex - no handler.vi"/>
<Item Name="test_libssh2_userauth_keyboard_interactive_ex.vi" Type="VI" URL="../test_libssh2_userauth_keyboard_interactive_ex.vi"/>
<Item Name="test_libssh2_userauth_list.vi" Type="VI" URL="../test_libssh2_userauth_list.vi"/>
<Item Name="test_libssh2_userauth_password.vi" Type="VI" URL="../test_libssh2_userauth_password.vi"/>
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit 898305a

Please sign in to comment.