Explorar o código

totemsrp: Fix memb_commit_token handler overflow

Previously, message_handler_memb_commit_token was not checking the
maximum length of the message, only the minimum message length
(in check_memb_commit_token_sanity). When a received message
is longer than the commit_token_storage (by default 40000 bytes),
it will be memcpy'd, overwriting memory.

The solution is to also check the maximum message length.

With this patch, the alloca call is also moved after the sanity check,
so if the message is excessively long, it will not cause
a stack overflow.

Reported-by: Adam Bedard <adambedard@gmail.com>
Signed-off-by: Jan Friesse <jfriesse@redhat.com>
Reviewed-by: Adam Bedard <adambedard@gmail.com>
Also-proposed-by: nicholasyang <nicholas.yang@suse.com>
Jan Friesse hai 1 semana
pai
achega
22f2b8abd4
Modificáronse 1 ficheiros con 14 adicións e 3 borrados
  1. 14 3
      exec/totemsrp.c

+ 14 - 3
exec/totemsrp.c

@@ -3811,7 +3811,8 @@ static int check_memb_commit_token_sanity(
 	struct totemsrp_instance *instance,
 	const void *msg,
 	size_t msg_len,
-	int endian_conversion_needed)
+	int endian_conversion_needed,
+	size_t max_msg_len)
 {
 	const struct memb_commit_token *mct_msg = (const struct memb_commit_token *)msg;
 	unsigned int addr_entries;
@@ -3824,6 +3825,13 @@ static int check_memb_commit_token_sanity(
 		return (-1);
 	}
 
+	if (msg_len > max_msg_len) {
+		log_printf (instance->totemsrp_log_level_security,
+		    "Received memb_commit_token message exceeds the maximum allowed size...  ignoring.");
+
+		return (-1);
+	}
+
 	addr_entries = mct_msg->addr_entries;
 	if (endian_conversion_needed) {
 		addr_entries = swab32(addr_entries);
@@ -4880,7 +4888,7 @@ static int message_handler_memb_commit_token (
 	size_t msg_len,
 	int endian_conversion_needed)
 {
-	struct memb_commit_token *memb_commit_token_convert = alloca (msg_len);
+	struct memb_commit_token *memb_commit_token_convert;
 	struct memb_commit_token *memb_commit_token;
 	struct srp_addr sub[PROCESSOR_COUNT_MAX];
 	int sub_entries;
@@ -4890,10 +4898,13 @@ static int message_handler_memb_commit_token (
 	log_printf (instance->totemsrp_log_level_debug,
 		"got commit token");
 
-	if (check_memb_commit_token_sanity(instance, msg, msg_len, endian_conversion_needed) == -1) {
+	if (check_memb_commit_token_sanity(instance, msg, msg_len, endian_conversion_needed,
+	    sizeof(instance->commit_token_storage)) == -1) {
 		return (0);
 	}
 
+	memb_commit_token_convert = alloca (msg_len);
+
 	if (endian_conversion_needed) {
 		memb_commit_token_endian_convert (msg, memb_commit_token_convert);
 	} else {