You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@zookeeper.apache.org by GitBox <gi...@apache.org> on 2020/03/28 19:50:09 UTC

[GitHub] [zookeeper] symat opened a new pull request #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages

symat opened a new pull request #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages
URL: https://github.com/apache/zookeeper/pull/1300
 
 
   Using ZooKeeper with JDK 12.0.2 on CentOS 7 when the current leader is killed, we saw a few times that some partial Leader Election notification (vote) messages were delivered to the other ZooKeeper servers. The malformed / partial messages are causing different exceptions in the WorkerReceiver thread of FastLeaderElection which were not handled before. This was leading to the death of the WorkerReceiver thread, which caused that the given ZooKeeper Server was unable to receive any leader election messages and never was able to re-join to any quorum until it got restarted.
   
   In the proposed fix I created unit tests to simulate certain error cases with regards to partial leader election messages, and fixed the error handling in FastLeaderElection.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [zookeeper] symat commented on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages

Posted by GitBox <gi...@apache.org>.
symat commented on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages
URL: https://github.com/apache/zookeeper/pull/1300#issuecomment-605851598
 
 
   FYI: as the [last comment](https://issues.apache.org/jira/browse/ZOOKEEPER-3769?focusedCommentId=17070311&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-17070311) on the Jira ticket says, it wasn't really tested with Netty, so we are not sure if the partial message hits the leader election with Netty. 
   
   Anyway, handling these exceptions / doing some extra checks in the Fast Leader Election class makes sense I think, independently from Netty / NIO.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [zookeeper] eolivelli closed pull request #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages

Posted by GitBox <gi...@apache.org>.
eolivelli closed pull request #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages
URL: https://github.com/apache/zookeeper/pull/1300
 
 
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [zookeeper] symat commented on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages

Posted by GitBox <gi...@apache.org>.
symat commented on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages
URL: https://github.com/apache/zookeeper/pull/1300#issuecomment-607794733
 
 
   well, actually I was not able to reproduce the issue even with JDK 12. I am not sure if it is specific to JDK 12 or not. I know that the issue was reproducible for Lasaro on his environment (with JDK 12 on CentOS 7) for a while, but now even he can not reproduce it. (Maybe some OS upgrade or network change fixed it for him?) So honestly I don't know what triggered this issue or why we haven't seen this before.
   
   But I think it is good to handle these exceptions in the code, even if I can not trigger them with real ZooKeeper, only by unit tests. And having more tests for malformed / partial messages never hurts. 
   
   Actually one can consider this as a security fix as well, since in theory you were able to knock out any Follower from the quorum just by sending a partial election vote message.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [zookeeper] eolivelli commented on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages

Posted by GitBox <gi...@apache.org>.
eolivelli commented on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages
URL: https://github.com/apache/zookeeper/pull/1300#issuecomment-605515279
 
 
   Do you have some stacktrace for the error?
   We should prevent falling into BUException and not catch it

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [zookeeper] symat edited a comment on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages

Posted by GitBox <gi...@apache.org>.
symat edited a comment on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages
URL: https://github.com/apache/zookeeper/pull/1300#issuecomment-607794733
 
 
   well, actually I was not able to reproduce the issue even with JDK 12. I am not sure if it is specific to JDK 12 or not. I know that the issue was reproducible for Lasaro on his environment (with JDK 12 on CentOS 7) for a while, but now even he can not reproduce it. (Maybe some OS upgrade or network change fixed it for him?) So honestly I don't know what triggered this issue or why we haven't seen this before.
   
   But I think it is good to handle these exceptions in the code, even if I can not trigger them with real ZooKeeper, only by unit tests. And having more tests for malformed / partial messages never hurts. 
   
   Actually one can consider this as a security fix as well, since in theory you were able to knock out any Follower from the quorum just by sending a partial election vote message. (at least you were able to kill the receiver thread in fast leader election... so during the next leader election the server will be unable to connect to the new leader, until restart)

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [zookeeper] nkalmar commented on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages

Posted by GitBox <gi...@apache.org>.
nkalmar commented on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages
URL: https://github.com/apache/zookeeper/pull/1300#issuecomment-605878984
 
 
   After I sent my comment on FastLeaderElection.buildMsg() in the tests I realised you can't re-write all to use the function... sorry, made since to just write the buffer yourself than. (I still think it's fine to use buildMsg() if you can, so don't revert, I'm just saying it makes more sense now that you built the message yourself :) ) 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [zookeeper] symat commented on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages

Posted by GitBox <gi...@apache.org>.
symat commented on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages
URL: https://github.com/apache/zookeeper/pull/1300#issuecomment-605597388
 
 
   I saw two kinds of exceptions, related to this problem. (if I remember correctly, the stack traces were gathered on 3.5.7)
   
   ```
   03/24/20 11:16:16,297 [WorkerReceiver[myid=1]] ERROR [org.apache.zookeeper.server.NIOServerCnxnFactory] (NIOServerCnxnFactory.java:92) - Thread Thread[WorkerReceiver[myid=1],5,main] died
   
   java.nio.BufferUnderflowException: null
        at java.nio.HeapByteBuffer.get(HeapByteBuffer.java:178) ~[?:?]
        at java.nio.ByteBuffer.get(ByteBuffer.java:762) ~[?:?]
        at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerReceiver.run(FastLeaderElection.java:278) ~[zookeeper-3.5.7.jar:3.5.7]        
   ```
   And an other case (this can happen when wrong protocol format is expected due to the wrong message size):
   ```
   03/23/20 10:14:46,127 [WorkerReceiver[myid=1]] ERROR [org.apache.zookeeper.server.NIOServerCnxnFactory] (NIOServerCnxnFactory.java:92) - Thread Thread[WorkerReceiver[myid=1],5,main] died
   java.lang.NegativeArraySizeException: -1082130432
        at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerReceiver.run(FastLeaderElection.java:276) ~[zookeeper-3.5.7.jar:3.5.7]
        at java.lang.Thread.run(Thread.java:835) ~[?:?]
   ```
   
   I was able to reproduce these errors with the unit tests I created. But unfortunately I am not able to reproduce the problem with real ZooKeeper.
   
   Lasaro Camargos (who reported the issue) said that the error happened both with NIO and Netty.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [zookeeper] symat edited a comment on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages

Posted by GitBox <gi...@apache.org>.
symat edited a comment on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages
URL: https://github.com/apache/zookeeper/pull/1300#issuecomment-605510389
 
 
   This error may happen in both 3.4, 3.5 and 3.6. I created this patch for the master branch, but I already have an other one for the branch 3.5 (it is not a clean cherry-pick) which I will submit after this PR gets reviewed.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [zookeeper] symat commented on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages

Posted by GitBox <gi...@apache.org>.
symat commented on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages
URL: https://github.com/apache/zookeeper/pull/1300#issuecomment-605872079
 
 
   well, the parsing logic is not trivial. I was also reading it for some time. There are multiple message versions kept for backward-compatibility / rolling-upgrade support reasons. The older versions differ only in length, but later a proper 'version number' was introduced during the dynamic-reconfig modifications.
   
   I actually was convinced for some time that the logic is wrong (around the 36 vs 40 bits long message parsing), but then I think I understood it finally. Not a very nice code... I am not sure if we want to change the quorum-internal protocols in ZooKeeper 4.0, but I think it would make sense to write a new Leader Election class that is using extendable message protocol that handles versions properly. 
   
   Anyway, I haven't touched the logic itself, only added extra exception handling and a check before allocating memory for the buffer where the configuration string gets copied.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [zookeeper] symat commented on a change in pull request #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages

Posted by GitBox <gi...@apache.org>.
symat commented on a change in pull request #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages
URL: https://github.com/apache/zookeeper/pull/1300#discussion_r400018467
 
 

 ##########
 File path: zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/FLEMalformedNotificationMessageTest.java
 ##########
 @@ -0,0 +1,262 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.server.quorum;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+import org.apache.zookeeper.PortAssignment;
+import org.apache.zookeeper.ZKTestCase;
+import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer;
+import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState;
+import org.apache.zookeeper.test.ClientBase;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class FLEMalformedNotificationMessageTest extends ZKTestCase {
+    private static final Logger LOG = LoggerFactory.getLogger(FLEMalformedNotificationMessageTest.class);
+    private static final byte[] CONFIG_BYTES = "my very invalid config string".getBytes();
+    private static final int CONFIG_BYTES_LENGTH = CONFIG_BYTES.length;
+
+    int count;
+    HashMap<Long, QuorumServer> peers;
+    File tmpdir[];
+    int port[];
+
+    QuorumCnxManager mockCnxManager;
+    FLETestUtils.LEThread leaderElectionThread;
+    QuorumPeer peerRunningLeaderElection;
+
+
+    @Before
+    public void setUp() throws Exception {
+        count = 3;
+
+        peers = new HashMap<>(count);
+        tmpdir = new File[count];
+        port = new int[count];
+
+        LOG.info("FLEMalformedNotificationMessageTest: {}, {}", getTestName(), count);
+        for (int i = 0; i < count; i++) {
+            int clientport = PortAssignment.unique();
+            peers.put((long) i,
+                      new QuorumServer(i,
+                                       new InetSocketAddress(clientport),
+                                       new InetSocketAddress(PortAssignment.unique())));
+            tmpdir[i] = ClientBase.createTmpDir();
+            port[i] = clientport;
+        }
+
+        /*
+         * Start server 0
+         */
+        peerRunningLeaderElection = new QuorumPeer(peers, tmpdir[0], tmpdir[0], port[0], 3, 0, 1000, 2, 2, 2);
+        peerRunningLeaderElection.startLeaderElection();
+        leaderElectionThread = new FLETestUtils.LEThread(peerRunningLeaderElection, 0);
+        leaderElectionThread.start();
+    }
+
+
+    @After
+    public void tearDown() throws Exception {
+        peerRunningLeaderElection.shutdown();
+        mockCnxManager.halt();
+    }
+
+
+    @Test
+    public void testTooShortPartialNotificationMessage() throws Exception {
+
+        /*
+         * Start mock server 1, send a message too short to be compatible with any protocol version
+         * This simulates the case when only some parts of the whole message
+         */
+        startMockServer(1);
+        byte requestBytes[] = new byte[12];
+        ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes);
+        requestBuffer.clear();
+        requestBuffer.putInt(ServerState.LOOKING.ordinal());   // state
+        requestBuffer.putLong(0);                              // leader
+        mockCnxManager.toSend(0L, requestBuffer);
+
+        /*
+         * Assert that the message receiver thread in leader election is still healthy:
+         * we are sending valid votes and waiting for the leader election to be finished.
+         */
+        sendValidNotifications(1, 0);
+        leaderElectionThread.join(5000);
+        if (leaderElectionThread.isAlive()) {
+            Assert.fail("Leader election thread didn't join, something went wrong.");
+        }
+    }
+
+
+    @Test
+    public void testNotificationMessageWithNegativeConfigLength() throws Exception {
+
+        /*
+         * Start mock server 1, send a message with negative configLength field
+         * This simulates the case when only some parts of the whole message
+         */
+        startMockServer(1);
+        byte requestBytes[] = new byte[48];
+        ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes);
+        requestBuffer.clear();
+        requestBuffer.putInt(ServerState.LOOKING.ordinal());   // state
+        requestBuffer.putLong(0);                              // leader
+        requestBuffer.putLong(0);                              // zxid
+        requestBuffer.putLong(0);                              // electionEpoch
+        requestBuffer.putLong(0);                              // epoch
+        requestBuffer.putInt(FastLeaderElection.Notification.CURRENTVERSION);   // version
+        requestBuffer.putInt(-123);                            // configData.length
+        mockCnxManager.toSend(0L, requestBuffer);
+
+        /*
+         * Assert that the message receiver thread in leader election is still healthy:
+         * we are sending valid votes and waiting for the leader election to be finished.
+         */
+        sendValidNotifications(1, 0);
+        leaderElectionThread.join(5000);
+        if (leaderElectionThread.isAlive()) {
+            Assert.fail("Leader election thread didn't join, something went wrong.");
+        }
+    }
+
+
+    @Test
+    public void testNotificationMessageWithInvalidConfigLength() throws Exception {
+
+        /*
+         * Start mock server 1, send a message with an invalid configLength field
+         * (instead of sending CONFIG_BYTES_LENGTH, we send 10000)
+         * This simulates the case when only some parts of the whole message
+         */
+        startMockServer(1);
+        byte requestBytes[] = new byte[48 + CONFIG_BYTES_LENGTH];
+        ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes);
+        requestBuffer.clear();
+        requestBuffer.putInt(ServerState.LOOKING.ordinal());   // state
+        requestBuffer.putLong(0);                              // leader
+        requestBuffer.putLong(0);                              // zxid
+        requestBuffer.putLong(0);                              // electionEpoch
+        requestBuffer.putLong(0);                              // epoch
+        requestBuffer.putInt(FastLeaderElection.Notification.CURRENTVERSION);   // version
+        requestBuffer.putInt(10000);                           // configData.length
+        requestBuffer.put(CONFIG_BYTES);                       // configData
+        mockCnxManager.toSend(0L, requestBuffer);
+
+        /*
+         * Assert that the message receiver thread in leader election is still healthy:
+         * we are sending valid votes and waiting for the leader election to be finished.
+         */
+        sendValidNotifications(1, 0);
+        leaderElectionThread.join(5000);
+        if (leaderElectionThread.isAlive()) {
+            Assert.fail("Leader election thread didn't join, something went wrong.");
+        }
+    }
+
+
+    @Test
+    public void testNotificationMessageWithInvalidConfig() throws Exception {
+
+        /*
+         * Start mock server 1, send a message with an invalid config field
+         * (the receiver should not be able to parse the content)
+         * This simulates the case when only some parts of the whole message
+         */
+        startMockServer(1);
+        byte requestBytes[] = new byte[48 + CONFIG_BYTES_LENGTH];
+        ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes);
 
 Review comment:
   true, that is easier / nicer I think.
   I will fix it quickly.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [zookeeper] symat commented on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages

Posted by GitBox <gi...@apache.org>.
symat commented on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages
URL: https://github.com/apache/zookeeper/pull/1300#issuecomment-605510389
 
 
   This error may happen in both 3.4, 3.5 and 3.6. I created this patch for the master branch, but I already have an other one for the branch 3.5 which I will submit when this PR gets reviewed.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [zookeeper] nkalmar commented on a change in pull request #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages

Posted by GitBox <gi...@apache.org>.
nkalmar commented on a change in pull request #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages
URL: https://github.com/apache/zookeeper/pull/1300#discussion_r400014214
 
 

 ##########
 File path: zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/FLEMalformedNotificationMessageTest.java
 ##########
 @@ -0,0 +1,262 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.server.quorum;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+import org.apache.zookeeper.PortAssignment;
+import org.apache.zookeeper.ZKTestCase;
+import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer;
+import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState;
+import org.apache.zookeeper.test.ClientBase;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class FLEMalformedNotificationMessageTest extends ZKTestCase {
+    private static final Logger LOG = LoggerFactory.getLogger(FLEMalformedNotificationMessageTest.class);
+    private static final byte[] CONFIG_BYTES = "my very invalid config string".getBytes();
+    private static final int CONFIG_BYTES_LENGTH = CONFIG_BYTES.length;
+
+    int count;
+    HashMap<Long, QuorumServer> peers;
+    File tmpdir[];
+    int port[];
+
+    QuorumCnxManager mockCnxManager;
+    FLETestUtils.LEThread leaderElectionThread;
+    QuorumPeer peerRunningLeaderElection;
+
+
+    @Before
+    public void setUp() throws Exception {
+        count = 3;
+
+        peers = new HashMap<>(count);
+        tmpdir = new File[count];
+        port = new int[count];
+
+        LOG.info("FLEMalformedNotificationMessageTest: {}, {}", getTestName(), count);
+        for (int i = 0; i < count; i++) {
+            int clientport = PortAssignment.unique();
+            peers.put((long) i,
+                      new QuorumServer(i,
+                                       new InetSocketAddress(clientport),
+                                       new InetSocketAddress(PortAssignment.unique())));
+            tmpdir[i] = ClientBase.createTmpDir();
+            port[i] = clientport;
+        }
+
+        /*
+         * Start server 0
+         */
+        peerRunningLeaderElection = new QuorumPeer(peers, tmpdir[0], tmpdir[0], port[0], 3, 0, 1000, 2, 2, 2);
+        peerRunningLeaderElection.startLeaderElection();
+        leaderElectionThread = new FLETestUtils.LEThread(peerRunningLeaderElection, 0);
+        leaderElectionThread.start();
+    }
+
+
+    @After
+    public void tearDown() throws Exception {
+        peerRunningLeaderElection.shutdown();
+        mockCnxManager.halt();
+    }
+
+
+    @Test
+    public void testTooShortPartialNotificationMessage() throws Exception {
+
+        /*
+         * Start mock server 1, send a message too short to be compatible with any protocol version
+         * This simulates the case when only some parts of the whole message
+         */
+        startMockServer(1);
+        byte requestBytes[] = new byte[12];
+        ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes);
+        requestBuffer.clear();
+        requestBuffer.putInt(ServerState.LOOKING.ordinal());   // state
+        requestBuffer.putLong(0);                              // leader
+        mockCnxManager.toSend(0L, requestBuffer);
+
+        /*
+         * Assert that the message receiver thread in leader election is still healthy:
+         * we are sending valid votes and waiting for the leader election to be finished.
+         */
+        sendValidNotifications(1, 0);
+        leaderElectionThread.join(5000);
+        if (leaderElectionThread.isAlive()) {
+            Assert.fail("Leader election thread didn't join, something went wrong.");
+        }
+    }
+
+
+    @Test
+    public void testNotificationMessageWithNegativeConfigLength() throws Exception {
+
+        /*
+         * Start mock server 1, send a message with negative configLength field
+         * This simulates the case when only some parts of the whole message
+         */
+        startMockServer(1);
+        byte requestBytes[] = new byte[48];
+        ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes);
+        requestBuffer.clear();
+        requestBuffer.putInt(ServerState.LOOKING.ordinal());   // state
+        requestBuffer.putLong(0);                              // leader
+        requestBuffer.putLong(0);                              // zxid
+        requestBuffer.putLong(0);                              // electionEpoch
+        requestBuffer.putLong(0);                              // epoch
+        requestBuffer.putInt(FastLeaderElection.Notification.CURRENTVERSION);   // version
+        requestBuffer.putInt(-123);                            // configData.length
+        mockCnxManager.toSend(0L, requestBuffer);
+
+        /*
+         * Assert that the message receiver thread in leader election is still healthy:
+         * we are sending valid votes and waiting for the leader election to be finished.
+         */
+        sendValidNotifications(1, 0);
+        leaderElectionThread.join(5000);
+        if (leaderElectionThread.isAlive()) {
+            Assert.fail("Leader election thread didn't join, something went wrong.");
+        }
+    }
+
+
+    @Test
+    public void testNotificationMessageWithInvalidConfigLength() throws Exception {
+
+        /*
+         * Start mock server 1, send a message with an invalid configLength field
+         * (instead of sending CONFIG_BYTES_LENGTH, we send 10000)
+         * This simulates the case when only some parts of the whole message
+         */
+        startMockServer(1);
+        byte requestBytes[] = new byte[48 + CONFIG_BYTES_LENGTH];
+        ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes);
+        requestBuffer.clear();
+        requestBuffer.putInt(ServerState.LOOKING.ordinal());   // state
+        requestBuffer.putLong(0);                              // leader
+        requestBuffer.putLong(0);                              // zxid
+        requestBuffer.putLong(0);                              // electionEpoch
+        requestBuffer.putLong(0);                              // epoch
+        requestBuffer.putInt(FastLeaderElection.Notification.CURRENTVERSION);   // version
+        requestBuffer.putInt(10000);                           // configData.length
+        requestBuffer.put(CONFIG_BYTES);                       // configData
+        mockCnxManager.toSend(0L, requestBuffer);
+
+        /*
+         * Assert that the message receiver thread in leader election is still healthy:
+         * we are sending valid votes and waiting for the leader election to be finished.
+         */
+        sendValidNotifications(1, 0);
+        leaderElectionThread.join(5000);
+        if (leaderElectionThread.isAlive()) {
+            Assert.fail("Leader election thread didn't join, something went wrong.");
+        }
+    }
+
+
+    @Test
+    public void testNotificationMessageWithInvalidConfig() throws Exception {
+
+        /*
+         * Start mock server 1, send a message with an invalid config field
+         * (the receiver should not be able to parse the content)
+         * This simulates the case when only some parts of the whole message
+         */
+        startMockServer(1);
+        byte requestBytes[] = new byte[48 + CONFIG_BYTES_LENGTH];
+        ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes);
 
 Review comment:
   wouldn't 
   *FastLeaderElection.buildMsg(ServerState.LOOKING.ordinal(), 1, 0, 0, 0, CONFIG_BYTES)* work here? Seems it does the same.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [zookeeper] symat edited a comment on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages

Posted by GitBox <gi...@apache.org>.
symat edited a comment on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages
URL: https://github.com/apache/zookeeper/pull/1300#issuecomment-605510389
 
 
   This error may happen in both 3.4, 3.5 and 3.6. I created this patch for the master branch, but I already have an other one for the branch 3.5 (it is not a clean cherry-pick) which I will submit after this PR gets reviewed. I am not sure about 3.4... (?)

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [zookeeper] symat commented on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages

Posted by GitBox <gi...@apache.org>.
symat commented on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages
URL: https://github.com/apache/zookeeper/pull/1300#issuecomment-607797088
 
 
   thanks for the comments and the quick review for both of you!
   I will prepare the patch for branch 3.5 too. I think 3.4 will not be needed...

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [zookeeper] symat edited a comment on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages

Posted by GitBox <gi...@apache.org>.
symat edited a comment on issue #1300: ZOOKEEPER-3769: handling malformed Leader Election notification messages
URL: https://github.com/apache/zookeeper/pull/1300#issuecomment-605510389
 
 
   This error may happen in both 3.4, 3.5 and 3.6. I created this patch for the master branch, but I already have an other one for the branch 3.5 which I will submit after this PR gets reviewed.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services