File 0988-ssh-Send-ssh_msg_channel_failure-according-to-RFC-42.patch of Package erlang
From ac1be24b1fe60a4f2831e65b39dd76731868bc6b Mon Sep 17 00:00:00 2001
From: Hans Nilsson <hans@erlang.org>
Date: Fri, 25 Sep 2020 10:18:07 +0200
Subject: [PATCH] ssh: Send ssh_msg_channel_failure according to RFC 4254
---
lib/ssh/src/ssh_connection.erl | 29 ++++++++++++++++-------------
lib/ssh/src/ssh_message.erl | 26 +++++++++++++++++++-------
2 files changed, 35 insertions(+), 20 deletions(-)
diff --git a/lib/ssh/src/ssh_connection.erl b/lib/ssh/src/ssh_connection.erl
index 9b9d66aaa8..06dbba0dd9 100644
--- a/lib/ssh/src/ssh_connection.erl
+++ b/lib/ssh/src/ssh_connection.erl
@@ -727,20 +727,23 @@ handle_msg(#ssh_msg_channel_request{request_type = "env"},
%% The client SHOULD ignore env requests.
{[], Connection};
-handle_msg(#ssh_msg_channel_request{recipient_channel = ChannelId,
- request_type = _Other,
- want_reply = WantReply},
+handle_msg(#ssh_msg_channel_request{recipient_channel = ChannelId},
#connection{channel_cache = Cache} = Connection, _) ->
- if WantReply == true ->
- case ssh_client_channel:cache_lookup(Cache, ChannelId) of
- #channel{remote_id = RemoteId} ->
- FailMsg = channel_failure_msg(RemoteId),
- {[{connection_reply, FailMsg}], Connection};
- undefined -> %% Chanel has been closed
- {[], Connection}
- end;
- true ->
- {[], Connection}
+ %% Not a valid request_type. All valid types are handling the
+ %% parameter checking in their own clauses above.
+ %%
+ %% The special ReqType faulty_msg signals that something went
+ %% wrong found during decoding.
+ %%
+ %% RFC4254 says:
+ %% "If the request is not recognized or is not
+ %% supported for the channel, SSH_MSG_CHANNEL_FAILURE is returned."
+ case ssh_client_channel:cache_lookup(Cache, ChannelId) of
+ #channel{remote_id = RemoteId} ->
+ FailMsg = channel_failure_msg(RemoteId),
+ {[{connection_reply, FailMsg}], Connection};
+ undefined -> %% Chanel has been closed
+ {[], Connection}
end;
handle_msg(#ssh_msg_global_request{name = _Type,
diff --git a/lib/ssh/src/ssh_message.erl b/lib/ssh/src/ssh_message.erl
index e5ab449b8b..fab9c50867 100644
--- a/lib/ssh/src/ssh_message.erl
+++ b/lib/ssh/src/ssh_message.erl
@@ -366,13 +366,25 @@ decode(<<?BYTE(?SSH_MSG_CHANNEL_CLOSE), ?UINT32(Recipient)>>) ->
recipient_channel = Recipient
};
decode(<<?BYTE(?SSH_MSG_CHANNEL_REQUEST), ?UINT32(Recipient),
- ?DEC_BIN(RequestType,__0), ?BYTE(Bool), Data/binary>>) ->
- #ssh_msg_channel_request{
- recipient_channel = Recipient,
- request_type = ?unicode_list(RequestType),
- want_reply = erl_boolean(Bool),
- data = Data
- };
+ ?DEC_BIN(RequestType,__0), ?BYTE(Bool), Data/binary>>=Bytes) ->
+ try
+ #ssh_msg_channel_request{
+ recipient_channel = Recipient,
+ request_type = ?unicode_list(RequestType),
+ want_reply = erl_boolean(Bool),
+ data = Data
+ }
+ catch _:_ ->
+ %% Faulty, RFC4254 says:
+ %% "If the request is not recognized or is not
+ %% supported for the channel, SSH_MSG_CHANNEL_FAILURE is returned."
+ %% So we provoke such a message to be sent
+ #ssh_msg_channel_request{
+ recipient_channel = Recipient,
+ request_type = faulty_msg,
+ data = Bytes
+ }
+ end;
decode(<<?BYTE(?SSH_MSG_CHANNEL_SUCCESS), ?UINT32(Recipient)>>) ->
#ssh_msg_channel_success{
recipient_channel = Recipient
--
2.26.2