From ea002130d720ea0d365f216167d74ac53f97b237 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Fri, 29 Jan 2016 15:32:47 +0100 Subject: [PATCH] cmdmon: reply to invalid commands If an unknown command is received (e.g. from a future client), it should get a reply and print an error code instead of timing out. --- cmdmon.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/cmdmon.c b/cmdmon.c index 218f7be..61b2e89 100644 --- a/cmdmon.c +++ b/cmdmon.c @@ -1253,14 +1253,7 @@ read_from_cmd_socket(void *anything) return; } - /* Message size sanity check */ - if (read_length >= offsetof(CMD_Request, data)) { - expected_length = PKL_CommandLength(&rx_message); - } else { - expected_length = 0; - } - - if (expected_length < offsetof(CMD_Request, data) || + if (read_length < offsetof(CMD_Request, data) || read_length < offsetof(CMD_Reply, data) || rx_message.pkt_type != PKT_TYPE_CMD_REQUEST || rx_message.res1 != 0 || @@ -1272,6 +1265,7 @@ read_from_cmd_socket(void *anything) return; } + expected_length = PKL_CommandLength(&rx_message); rx_command = ntohs(rx_message.command); tx_message.version = PROTO_VERSION_NUMBER; @@ -1299,7 +1293,8 @@ read_from_cmd_socket(void *anything) return; } - if (rx_command >= N_REQUEST_TYPES) { + if (rx_command >= N_REQUEST_TYPES || + expected_length < (int)offsetof(CMD_Request, data)) { DEBUG_LOG(LOGF_CmdMon, "Command packet has invalid command %d", rx_command); tx_message.status = htons(STT_INVALID);