From 79542158f47ac589b2248ad40102a3c8e545d413 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Tue, 17 Aug 2021 13:10:18 +0100 Subject: [PATCH] Fix `max_connections` option not being correctly counted. --- ChangeLog.txt | 1 + lib/net_mosq.c | 1 + test/broker/01-connect-max-connections.py | 79 +++++++++++++++++++++++ test/broker/Makefile | 1 + test/broker/test.py | 1 + 5 files changed, 83 insertions(+) create mode 100755 test/broker/01-connect-max-connections.py diff --git a/ChangeLog.txt b/ChangeLog.txt index f81dd4c7..0d733af2 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -5,6 +5,7 @@ Broker: - Fix possible out of bounds memory reads when reading a corrupt/crafted configuration file. Unless your configuration file is writable by untrusted users this is not a risk. Closes #567213. +- Fix `max_connections` option not being correctly counted. Clients: - mosquitto_sub and mosquitto_rr now open stdout in binary mode on Windows diff --git a/lib/net_mosq.c b/lib/net_mosq.c index 550592a7..738e881a 100644 --- a/lib/net_mosq.c +++ b/lib/net_mosq.c @@ -250,6 +250,7 @@ int net__socket_close(struct mosquitto *mosq) #ifdef WITH_BROKER if(mosq->listener){ mosq->listener->client_count--; + mosq->listener = NULL; } #endif diff --git a/test/broker/01-connect-max-connections.py b/test/broker/01-connect-max-connections.py new file mode 100755 index 00000000..a41fea13 --- /dev/null +++ b/test/broker/01-connect-max-connections.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 + +# Test whether max_connections works with repeated connections + +from mosq_test_helper import * + +def write_config(filename, port): + with open(filename, 'w') as f: + f.write("listener %d\n" % (port)) + f.write("allow_anonymous true\n") + f.write("max_connections 10\n") + +def do_test(): + rc = 1 + + connect_packets_ok = [] + connack_packets_ok = [] + for i in range(0, 10): + connect_packets_ok.append(mosq_test.gen_connect("max-conn-%d"%i, proto_ver=5)) + connack_packets_ok.append(mosq_test.gen_connack(rc=0, proto_ver=5)) + + connect_packet_bad = mosq_test.gen_connect("max-conn-bad", proto_ver=5) + connack_packet_bad = b"" + + port = mosq_test.get_port() + conf_file = os.path.basename(__file__).replace('.py', '.conf') + write_config(conf_file, port) + broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port) + + socks = [] + try: + # Open all allowed connections, a limit of 10 + for i in range(0, 10): + socks.append(mosq_test.do_client_connect(connect_packets_ok[i], connack_packets_ok[i], port=port)) + + # Try to open an 11th connection + try: + sock_bad = mosq_test.do_client_connect(connect_packet_bad, connack_packet_bad, port=port) + except ConnectionResetError: + # Expected behaviour + pass + + # Close all allowed connections + for i in range(0, 10): + socks[i].close() + + ## Now repeat - check it works as before + + # Open all allowed connections, a limit of 10 + for i in range(0, 10): + socks.append(mosq_test.do_client_connect(connect_packets_ok[i], connack_packets_ok[i], port=port)) + + # Try to open an 11th connection + try: + sock_bad = mosq_test.do_client_connect(connect_packet_bad, connack_packet_bad, port=port) + except ConnectionResetError: + # Expected behaviour + pass + + # Close all allowed connections + for i in range(0, 10): + socks[i].close() + + rc = 0 + except mosq_test.TestError: + pass + except Exception as err: + print(err) + finally: + os.remove(conf_file) + broker.terminate() + broker.wait() + (stdo, stde) = broker.communicate() + if rc: + print(stde.decode('utf-8')) + exit(rc) + +do_test() +exit(0) diff --git a/test/broker/Makefile b/test/broker/Makefile index 384e17a4..0bffe9a4 100644 --- a/test/broker/Makefile +++ b/test/broker/Makefile @@ -30,6 +30,7 @@ test : test-compile 01 02 03 04 05 06 07 08 09 10 11 12 13 14 ./01-connect-invalid-id-utf8.py ./01-connect-invalid-protonum.py ./01-connect-invalid-reserved.py + ./01-connect-max-connections.py ./01-connect-success.py ./01-connect-uname-invalid-utf8.py ./01-connect-uname-no-flag.py diff --git a/test/broker/test.py b/test/broker/test.py index bfeaef24..537f6b68 100755 --- a/test/broker/test.py +++ b/test/broker/test.py @@ -15,6 +15,7 @@ tests = [ (1, './01-connect-invalid-id-utf8.py'), (1, './01-connect-invalid-protonum.py'), (1, './01-connect-invalid-reserved.py'), + (1, './01-connect-max-connections.py'), (1, './01-connect-success.py'), (1, './01-connect-uname-invalid-utf8.py'), (1, './01-connect-uname-no-flag.py'),