diff libpurple/protocols/qq/qq_base.c @ 28572:33921125348d

The output buffer passed to qq_encrypt needs to be 17 bytes bigger than the data you're encrypting, not 16 bytes bigger. Fixes #10191. It's hard to say whether this actually causes problems. My guess is that it does not. However, the way the qq protocol plugin constructs the plain text buffer to be passed to qq_encrypt is error prone, and the many calls to g_newa(guint8, MAX_PACKET_SIZE) are really bad because MAX_PACKET_SIZE is 64KB. This is a ridiculous amount of space to request on the stack. All these qq_put8 qq_put16 qq_put32 qq_putdata functions should be changed to insert data into a dynamically allocated GString instead of the stack-allocated buffers that they use now. This eliminates the potential for accidentally overwriting the end of the buffer. And the second g_newa() for the output buffer passed into qq_encrypt() should be changed to allocate space on the heap in most places because, as previously noted, 64KB is a ridiculous amount of memory to request from the stack. Heap allocation may be expensive when compared to stack allocation, but I feel it's usually worth it to eliminate the possibilty of buffer overflow.
author Mark Doliner <mark@kingant.net>
date Fri, 04 Sep 2009 22:50:26 +0000
parents f541583e31bd
children f1437342cc0e
line wrap: on
line diff
--- a/libpurple/protocols/qq/qq_base.c	Fri Sep 04 21:12:11 2009 +0000
+++ b/libpurple/protocols/qq/qq_base.c	Fri Sep 04 22:50:26 2009 +0000
@@ -245,10 +245,10 @@
 
 	g_return_if_fail(qd->ld.token != NULL && qd->ld.token_len > 0);
 
-	raw_data = g_newa(guint8, MAX_PACKET_SIZE - 16);
-	memset(raw_data, 0, MAX_PACKET_SIZE - 16);
+	raw_data = g_newa(guint8, MAX_PACKET_SIZE - 17);
+	memset(raw_data, 0, MAX_PACKET_SIZE - 17);
 
-	encrypted = g_newa(guint8, MAX_PACKET_SIZE);	/* 16 bytes more */
+	encrypted = g_newa(guint8, MAX_PACKET_SIZE);	/* 17 bytes more */
 
 	bytes = 0;
 	/* now generate the encrypted data
@@ -609,7 +609,7 @@
 	raw_data = g_newa(guint8, 128);
 	memset(raw_data, 0, 128);
 
-	encrypted = g_newa(guint8, 128 + 16);	/* 16 bytes more */
+	encrypted = g_newa(guint8, 128 + 17);	/* 17 bytes more */
 
 	bytes = 0;
 	if (qd->redirect == NULL) {
@@ -682,10 +682,10 @@
 
 	g_return_if_fail(qd->ld.token != NULL && qd->ld.token_len > 0);
 
-	raw_data = g_newa(guint8, MAX_PACKET_SIZE - 16);
-	memset(raw_data, 0, MAX_PACKET_SIZE - 16);
+	raw_data = g_newa(guint8, MAX_PACKET_SIZE - 17);
+	memset(raw_data, 0, MAX_PACKET_SIZE - 17);
 
-	encrypted = g_newa(guint8, MAX_PACKET_SIZE);	/* 16 bytes more */
+	encrypted = g_newa(guint8, MAX_PACKET_SIZE);	/* 17 bytes more */
 
 	bytes = 0;
 	bytes += qq_put8(raw_data + bytes, qd->ld.token_len);
@@ -721,10 +721,10 @@
 
 	g_return_if_fail(qd->ld.token != NULL && qd->ld.token_len > 0);
 
-	raw_data = g_newa(guint8, MAX_PACKET_SIZE - 16);
-	memset(raw_data, 0, MAX_PACKET_SIZE - 16);
+	raw_data = g_newa(guint8, MAX_PACKET_SIZE - 17);
+	memset(raw_data, 0, MAX_PACKET_SIZE - 17);
 
-	encrypted = g_newa(guint8, MAX_PACKET_SIZE);	/* 16 bytes more */
+	encrypted = g_newa(guint8, MAX_PACKET_SIZE);	/* 17 bytes more */
 
 	bytes = 0;
 	bytes += qq_put8(raw_data + bytes, qd->ld.token_len);
@@ -765,10 +765,10 @@
 	g_return_if_fail(qd->ld.token != NULL && qd->ld.token_len > 0);
 	g_return_if_fail(code != NULL && code_len > 0);
 
-	raw_data = g_newa(guint8, MAX_PACKET_SIZE - 16);
-	memset(raw_data, 0, MAX_PACKET_SIZE - 16);
+	raw_data = g_newa(guint8, MAX_PACKET_SIZE - 17);
+	memset(raw_data, 0, MAX_PACKET_SIZE - 17);
 
-	encrypted = g_newa(guint8, MAX_PACKET_SIZE);	/* 16 bytes more */
+	encrypted = g_newa(guint8, MAX_PACKET_SIZE);	/* 17 bytes more */
 
 	bytes = 0;
 	bytes += qq_put8(raw_data + bytes, qd->ld.token_len);
@@ -998,10 +998,10 @@
 
 	g_return_if_fail(qd->ld.token_ex != NULL && qd->ld.token_ex_len > 0);
 
-	raw_data = g_newa(guint8, MAX_PACKET_SIZE - 16);
-	memset(raw_data, 0, MAX_PACKET_SIZE - 16);
+	raw_data = g_newa(guint8, MAX_PACKET_SIZE - 17);
+	memset(raw_data, 0, MAX_PACKET_SIZE - 17);
 
-	encrypted = g_newa(guint8, MAX_PACKET_SIZE);	/* 16 bytes more */
+	encrypted = g_newa(guint8, MAX_PACKET_SIZE);	/* 17 bytes more */
 
 	/* Encrypted password and put in encrypted */
 	bytes = 0;
@@ -1166,10 +1166,10 @@
 
 	g_return_if_fail(qd->ld.token != NULL && qd->ld.token_len > 0);
 
-	raw_data = g_newa(guint8, MAX_PACKET_SIZE - 16);
-	memset(raw_data, 0, MAX_PACKET_SIZE - 16);
+	raw_data = g_newa(guint8, MAX_PACKET_SIZE - 17);
+	memset(raw_data, 0, MAX_PACKET_SIZE - 17);
 
-	encrypted = g_newa(guint8, MAX_PACKET_SIZE);	/* 16 bytes more */
+	encrypted = g_newa(guint8, MAX_PACKET_SIZE);	/* 17 bytes more */
 
 	/* Encrypted password and put in encrypted */
 	bytes = 0;
@@ -1342,10 +1342,10 @@
 
 	g_return_if_fail(qd->ld.token != NULL && qd->ld.token_len > 0);
 
-	raw_data = g_newa(guint8, MAX_PACKET_SIZE - 16);
-	memset(raw_data, 0, MAX_PACKET_SIZE - 16);
+	raw_data = g_newa(guint8, MAX_PACKET_SIZE - 17);
+	memset(raw_data, 0, MAX_PACKET_SIZE - 17);
 
-	encrypted = g_newa(guint8, MAX_PACKET_SIZE);	/* 16 bytes more */
+	encrypted = g_newa(guint8, MAX_PACKET_SIZE);	/* 17 bytes more */
 
 	/* Encrypted password and put in encrypted */
 	bytes = 0;