Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libssh2_channel_exec may return -1 when using non-blocking sockets #671

Open
awaeag opened this issue Feb 9, 2022 · 3 comments
Open

libssh2_channel_exec may return -1 when using non-blocking sockets #671

awaeag opened this issue Feb 9, 2022 · 3 comments

Comments

@awaeag
Copy link

awaeag commented Feb 9, 2022

When using non-blocking sockets, libssh2_channel_exec may return -1, and the channel becomes dead.
I fixed the issue by modifying the function _libssh2_packet_requirev in packet.c in libssh2 sources:
line 1393:
old code:

        if(strchr((char *) packet_types, ret)) {
            /* Be lazy, let packet_ask pull it out of the brigade */
            int ret = _libssh2_packet_askv(session, packet_types, data,
                                        data_len, match_ofs, match_buf,
                                        match_len);
            state->start = 0;
            return ret;
        }
    }

new code:

        if(strchr((char *) packet_types, ret)) {
            /* Be lazy, let packet_ask pull it out of the brigade */
            if (_libssh2_packet_askv(session, packet_types, data,
                                        data_len, match_ofs, match_buf,
                                        match_len)) {
                return LIBSSH2_ERROR_EAGAIN;
            }
            else {
                state->start = 0;
                return 0;
            }
        }
    }

I'm no libssh2 developer, so maybe I do not have the whole picture. But this fixed my problem.

@stale
Copy link

stale bot commented Jun 13, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jun 13, 2022
@willco007 willco007 removed the stale label Jun 13, 2022
@rmsh1216
Copy link
Contributor

rmsh1216 commented Aug 3, 2023

The _libssh2_packet_askv is used to check whether there are expected messages among the messages receive from the peer.
Before that, the local has successfully receive messages from the peer by calling _libssh2_transport_read. If the local does not receive messages from the peer due to some unkown reasons, the local will try again.
When the expected message is not received, this means that the peer do not send message according to the SSH protocol (please see rfc4250 to rfc4254). In this case, an error should be reported instead of trying to receive the message from the peer again.
In your case, restarting a new ssh connection may be a better alternative.

@awaeag
Copy link
Author

awaeag commented Aug 4, 2023

Thanks for this answer. But since this is quite old, I do not really remember all the researches I did at the time I opened the issue.
About your answer: I doubt this. What I understand here is the a message has been partially read, which is perfectly possible and does not break the ssh protocol as long as the rest of the message follows.
The problem may as well come from the _libssh2_transport_read you're talking about, that should only "accept" messages when they are completed.
I was just hoping a libssh2 developer would review this code, modify it or completely changes it, but at least fix the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants