From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtpng1.m.smailru.net (smtpng1.m.smailru.net [94.100.181.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 557F145C30A for ; Sun, 13 Dec 2020 20:15:41 +0300 (MSK) From: Vladislav Shpilevoy Date: Sun, 13 Dec 2020 18:15:27 +0100 Message-Id: <8178e04ab747795298ad57bc33d0b28392b291fe.1607879643.git.v.shpilevoy@tarantool.org> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH 5/8] raft: fix crash when received 0 term message List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: tarantool-patches@dev.tarantool.org, sergepetrenko@tarantool.org Term in raft can never be 0. It starts from 1 and can only grow. It was assumed it can't be received from other nodes because they do the same. There was an assertion for that. But in raft_msg, used as a transport unit between raft nodes, it was still possible to send 0 term. It could happen as a result of a bug, or if someone would try to mimic the protocol but made a mistake. That led to a crash in the assert in debug build. Part of #5303 --- src/lib/raft/raft.c | 1 - test/unit/raft.c | 8 +++++++- test/unit/raft.result | 3 ++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/lib/raft/raft.c b/src/lib/raft/raft.c index 4d07d37e5..df34d81dc 100644 --- a/src/lib/raft/raft.c +++ b/src/lib/raft/raft.c @@ -241,7 +241,6 @@ raft_sm_become_candidate(struct raft *raft); static const char * raft_msg_to_string(const struct raft_msg *req) { - assert(req->term != 0); char buf[1024]; int size = sizeof(buf); char *pos = buf; diff --git a/test/unit/raft.c b/test/unit/raft.c index dfb5f8e43..1ed8b7af7 100644 --- a/test/unit/raft.c +++ b/test/unit/raft.c @@ -247,7 +247,7 @@ raft_test_recovery(void) static void raft_test_bad_msg(void) { - raft_start_test(6); + raft_start_test(7); struct raft_msg msg; struct raft_node node; struct vclock vclock; @@ -280,6 +280,12 @@ raft_test_bad_msg(void) "candidate without vclock"); is(node.raft.term, 1, "term from the bad message wasn't used"); + msg = (struct raft_msg){ + .state = RAFT_STATE_FOLLOWER, + .term = 0, + }; + is(raft_node_process_msg(&node, &msg, 2), -1, "term can't be 0"); + raft_node_destroy(&node); raft_finish_test(); } diff --git a/test/unit/raft.result b/test/unit/raft.result index a63515ed3..2398bd71c 100644 --- a/test/unit/raft.result +++ b/test/unit/raft.result @@ -45,13 +45,14 @@ ok 1 - subtests ok 2 - subtests *** raft_test_recovery: done *** *** raft_test_bad_msg *** - 1..6 + 1..7 ok 1 - state can't be 0 ok 2 - term from the bad message wasn't used ok 3 - node can't be a candidate but vote for another node ok 4 - term from the bad message wasn't used ok 5 - node can't be a candidate without vclock ok 6 - term from the bad message wasn't used + ok 7 - term can't be 0 ok 3 - subtests *** raft_test_bad_msg: done *** *** raft_test_vote *** -- 2.24.3 (Apple Git-128)