Ver código fonte

[xfer] Avoid using stack-allocated memory in xfer_printf()

xfer_printf() occasionally has to deal with strings that are
potentially long, such as HTTP URIs with multiple query parameters.
Allocating these on the stack can lead to stack overruns and memory
corruption.

Fix by using vasprintf() instead of a stack allocation.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 anos atrás
pai
commit
de2616165b
1 arquivos alterados com 19 adições e 7 exclusões
  1. 19
    7
      src/core/xfer.c

+ 19
- 7
src/core/xfer.c Ver arquivo

@@ -19,6 +19,7 @@
19 19
 FILE_LICENCE ( GPL2_OR_LATER );
20 20
 
21 21
 #include <string.h>
22
+#include <stdlib.h>
22 23
 #include <stdio.h>
23 24
 #include <errno.h>
24 25
 #include <ipxe/iobuf.h>
@@ -297,17 +298,28 @@ int xfer_deliver_raw ( struct interface *intf, const void *data, size_t len ) {
297 298
  */
298 299
 int xfer_vprintf ( struct interface *intf, const char *format,
299 300
 		   va_list args ) {
300
-	size_t len;
301 301
 	va_list args_tmp;
302
+	char *buf;
303
+	int len;
304
+	int rc;
302 305
 
306
+	/* Create temporary string */
303 307
 	va_copy ( args_tmp, args );
304
-	len = vsnprintf ( NULL, 0, format, args );
305
-	{
306
-		char buf[len + 1];
307
-		vsnprintf ( buf, sizeof ( buf ), format, args_tmp );
308
-		va_end ( args_tmp );
309
-		return xfer_deliver_raw ( intf, buf, len );
308
+	len = vasprintf ( &buf, format, args );
309
+	if ( len < 0 ) {
310
+		rc = len;
311
+		goto err_asprintf;
310 312
 	}
313
+	va_end ( args_tmp );
314
+
315
+	/* Transmit string */
316
+	if ( ( rc = xfer_deliver_raw ( intf, buf, len ) ) != 0 )
317
+		goto err_deliver;
318
+
319
+ err_deliver:
320
+	free ( buf );
321
+ err_asprintf:
322
+	return rc;
311 323
 }
312 324
 
313 325
 /**

Carregando…
Cancelar
Salvar