소스 검색

[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 13 년 전
부모
커밋
de2616165b
1개의 변경된 파일19개의 추가작업 그리고 7개의 파일을 삭제
  1. 19
    7
      src/core/xfer.c

+ 19
- 7
src/core/xfer.c 파일 보기

19
 FILE_LICENCE ( GPL2_OR_LATER );
19
 FILE_LICENCE ( GPL2_OR_LATER );
20
 
20
 
21
 #include <string.h>
21
 #include <string.h>
22
+#include <stdlib.h>
22
 #include <stdio.h>
23
 #include <stdio.h>
23
 #include <errno.h>
24
 #include <errno.h>
24
 #include <ipxe/iobuf.h>
25
 #include <ipxe/iobuf.h>
297
  */
298
  */
298
 int xfer_vprintf ( struct interface *intf, const char *format,
299
 int xfer_vprintf ( struct interface *intf, const char *format,
299
 		   va_list args ) {
300
 		   va_list args ) {
300
-	size_t len;
301
 	va_list args_tmp;
301
 	va_list args_tmp;
302
+	char *buf;
303
+	int len;
304
+	int rc;
302
 
305
 
306
+	/* Create temporary string */
303
 	va_copy ( args_tmp, args );
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
 /**

Loading…
취소
저장