qemu-ga patch queue for hard-freeze
The following changes since commit 73995d15557a3cf2328cc6b7982264897c65cf65:
Merge remote-tracking branch 'remotes/stsquad/tags/pull-more-fixes-150420-1' into staging (2020-04-15 12:02:59 +0100)
are available in the Git repository at:
git://github.com/mdroth/qemu.git tags/qga-pull-2020-04-15-tag
for you to fetch changes up to 1329651fb4d4c5068ad12fd86aff7e52f9e18c34:
qga: Restrict guest-file-read count to 48 MB to avoid crashes (2020-04-15 09:18:48 -0500)
----------------------------------------------------------------
qemu-ga patch queue for hard-freeze
* enforce 48MB limit for guest-file-read to avoid memory allocation failures
----------------------------------------------------------------
Philippe Mathieu-Daudé (4):
Revert "prevent crash when executing guest-file-read with large count"
qga: Extract guest_file_handle_find() to commands-common.h
qga: Extract qmp_guest_file_read() to common commands.c
qga: Restrict guest-file-read count to 48 MB to avoid crashes
qga/commands-common.h | 21 +++++++++++++++++++++ qga/commands-posix.c | 29 +++++++---------------------- qga/commands-win32.c | 35 ++++++++--------------------------- qga/commands.c | 33 +++++++++++++++++++++++++++++++++ qga/qapi-schema.json | 6 ++++-- 5 files changed, 73 insertions(+), 51 deletions(-) create mode 100644 qga/commands-common.h
[PULL for-5.0 1/4] Revert "prevent crash when executing guest-file-read with large count"
From: Philippe Mathieu-Daudé <[email protected]>
As noted by Daniel Berrangé in [*], the fix from commit 807e2b6fce which replaced malloc() by try_malloc() is not enough, the process can still run out of memory a few line later:
346 buf = g_try_malloc0(count + 1);
347 if (!buf) {
348 error_setg(errp,
349 "failed to allocate sufficient memory "
350 "to complete the requested service");
351 return NULL;
352 }
353 is_ok = ReadFile(fh, buf, count, &read_count, NULL);
354 if (!is_ok) {
355 error_setg_win32(errp, GetLastError(), "failed to read file");
356 slog("guest-file-read failed, handle %" PRId64, handle);
357 } else {
358 buf[read_count] = 0;
359 read_data = g_new0(GuestFileRead, 1);
^^^^^^Instead we are going to put a low hard limit on 'count' in the next commits. This reverts commit 807e2b6fce022707418bc8f61c069d91c613b3d2.
[*] https://lists.gnu.org/archive/html/qemu-devel/2018-06/msg03471.html
--- qga/commands-win32.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)
qga/commands-win32.c +1 -7
@@ -343,13 +343,7 @@ GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count, } fh = gfh->fh; - buf =g_try_malloc0 (count +1 ); -if (!buf) { -error_setg (errp, -"failed to allocate sufficient memory " -"to complete the requested service" ); -return NULL ; - } + buf =g_malloc0 (count +1 ); is_ok =ReadFile (fh, buf, count, &read_count,NULL );if (!is_ok) {error_setg_win32 (errp,GetLastError (),"failed to read file" );
Signature · 1 line
2.17.1
[PULL for-5.0 3/4] qga: Extract qmp_guest_file_read() to common commands.c
In reply to Michael RothFrom: Philippe Mathieu-Daudé <[email protected]>
Extract the common code shared by both POSIX/Win32 implementations.
--- qga/commands-common.h | 3 +++ qga/commands-posix.c | 22 +++------------------- qga/commands-win32.c | 20 +++----------------- qga/commands.c | 26 ++++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 36 deletions(-)
qga/commands-common.h +3 -0
@@ -15,4 +15,7 @@ typedef struct GuestFileHandle GuestFileHandle;GuestFileHandle *guest_file_handle_find (int64_t id,Error **errp); +GuestFileRead *guest_file_read_unsafe (GuestFileHandle *gfh, +int64_t count,Error **errp); +#endif
qga/commands-posix.c +3 -19
@@ -461,29 +461,14 @@ void qmp_guest_file_close(int64_t handle, Error **errp)g_free (gfh); } -struct GuestFileRead *qmp_guest_file_read (int64_t handle,bool has_count, -int64_t count,Error **errp) +GuestFileRead *guest_file_read_unsafe (GuestFileHandle *gfh, +int64_t count,Error **errp) { -GuestFileHandle *gfh =guest_file_handle_find (handle, errp);GuestFileRead *read_data =NULL ;guchar *buf; -FILE *fh; +FILE *fh = gfh->fh;size_t read_count; -if (!gfh) { -return NULL ; - } - -if (!has_count) { - count =QGA_READ_COUNT_DEFAULT ; - }else if (count <0 || count >=UINT32_MAX ) { -error_setg (errp,"value '%" PRId64"' is invalid for argument count" , - count); -return NULL ; - } - - fh = gfh->fh; -/* explicitly flush when switching from writing to reading */ if (gfh->state ==RW_STATE_WRITING ) {int ret =fflush (fh);
@@ -498,7 +483,6 @@ struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count, read_count =fread (buf,1 , count, fh);if (ferror (fh)) {error_setg_errno (errp, errno,"failed to read file" ); -slog ("guest-file-read failed, handle: %" PRId64, handle); }else { buf[read_count] =0 ; read_data =g_new0 (GuestFileRead,1 );
qga/commands-win32.c +3 -17
@@ -322,33 +322,19 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp) } } -GuestFileRead *qmp_guest_file_read (int64_t handle,bool has_count, -int64_t count,Error **errp) +GuestFileRead *guest_file_read_unsafe (GuestFileHandle *gfh, +int64_t count,Error **errp) {GuestFileRead *read_data =NULL ;guchar *buf; -HANDLE fh; +HANDLE fh = gfh->fh;bool is_ok;DWORD read_count; -GuestFileHandle *gfh =guest_file_handle_find (handle, errp); - -if (!gfh) { -return NULL ; - } -if (!has_count) { - count =QGA_READ_COUNT_DEFAULT ; - }else if (count <0 || count >=UINT32_MAX ) { -error_setg (errp,"value '%" PRId64 -"' is invalid for argument count" , count); -return NULL ; - } - fh = gfh->fh; buf =g_malloc0 (count +1 ); is_ok =ReadFile (fh, buf, count, &read_count,NULL );if (!is_ok) {error_setg_win32 (errp,GetLastError (),"failed to read file" ); -slog ("guest-file-read failed, handle %" PRId64, handle); }else { buf[read_count] =0 ; read_data =g_new0 (GuestFileRead,1 );
qga/commands.c +26 -0
@@ -18,6 +18,7 @@#include "qemu/base64.h" #include "qemu/cutils.h" #include "qemu/atomic.h" +#include "commands-common.h" /* Maximum captured guest-exec out_data/err_data - 16MB */ #define GUEST_EXEC_MAX_OUTPUT (16*1024*1024)
@@ -547,3 +548,28 @@ error:g_free (info);return NULL ; } + +GuestFileRead *qmp_guest_file_read (int64_t handle,bool has_count, +int64_t count,Error **errp) +{ +GuestFileHandle *gfh =guest_file_handle_find (handle, errp); +GuestFileRead *read_data; + +if (!gfh) { +return NULL ; + } +if (!has_count) { + count =QGA_READ_COUNT_DEFAULT ; + }else if (count <0 || count >=UINT32_MAX ) { +error_setg (errp,"value '%" PRId64"' is invalid for argument count" , + count); +return NULL ; + } + + read_data =guest_file_read_unsafe (gfh, count, errp); +if (!read_data) { +slog ("guest-file-write failed, handle: %" PRId64, handle); + } + +return read_data; +}
Signature · 1 line
2.17.1
[PULL for-5.0 2/4] qga: Extract guest_file_handle_find() to commands-common.h
In reply to Michael RothFrom: Philippe Mathieu-Daudé <[email protected]>
As we are going to reuse this method, declare it in common header.
--- qga/commands-common.h | 18 ++++++++++++++++++ qga/commands-posix.c | 7 ++++--- qga/commands-win32.c | 7 ++++--- 3 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 qga/commands-common.h
qga/commands-common.h new file +18 -0
@@ -0,0 +1,18 @@ +/* +* QEMU Guest Agent common/cross-platform common commands +* +* Copyright (c) 2020 Red Hat, Inc. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ +#ifndef QGA_COMMANDS_COMMON_H +#define QGA_COMMANDS_COMMON_H + +#include "qga-qapi-types.h" + +typedef struct GuestFileHandle GuestFileHandle ; + +GuestFileHandle *guest_file_handle_find (int64_t id,Error **errp); + +#endif
qga/commands-posix.c +4 -3
@@ -26,6 +26,7 @@#include "qemu/sockets.h" #include "qemu/base64.h" #include "qemu/cutils.h" +#include "commands-common.h" #ifdef HAVE_UTMPX #include <utmpx.h>
@@ -237,12 +238,12 @@ typedef enum {RW_STATE_WRITING , } RwState; -typedef struct GuestFileHandle { +struct GuestFileHandle {uint64_t id;FILE *fh;RwState state;QTAILQ_ENTRY (GuestFileHandle) next; -}GuestFileHandle ; +}; static struct { QTAILQ_HEAD(, GuestFileHandle) filehandles;
@@ -268,7 +269,7 @@ static int64_t guest_file_handle_add(FILE *fh, Error **errp)return handle; } -static GuestFileHandle *guest_file_handle_find (int64_t id,Error **errp) +GuestFileHandle *guest_file_handle_find (int64_t id,Error **errp) {GuestFileHandle *gfh;
qga/commands-win32.c +4 -3
@@ -37,6 +37,7 @@#include "qemu/queue.h" #include "qemu/host-utils.h" #include "qemu/base64.h" +#include "commands-common.h" #ifndef SHTDN_REASON_FLAG_PLANNED #define SHTDN_REASON_FLAG_PLANNED 0x80000000
@@ -50,11 +51,11 @@#define INVALID_SET_FILE_POINTER ((DWORD)-1) -typedef struct GuestFileHandle { +struct GuestFileHandle {int64_t id;HANDLE fh;QTAILQ_ENTRY (GuestFileHandle) next; -}GuestFileHandle ; +}; static struct { QTAILQ_HEAD(, GuestFileHandle) filehandles;
@@ -126,7 +127,7 @@ static int64_t guest_file_handle_add(HANDLE fh, Error **errp)return handle; } -static GuestFileHandle *guest_file_handle_find (int64_t id,Error **errp) +GuestFileHandle *guest_file_handle_find (int64_t id,Error **errp) {GuestFileHandle *gfh; QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next) {
Signature · 1 line
2.17.1
[PULL for-5.0 4/4] qga: Restrict guest-file-read count to 48 MB to avoid crashes
In reply to Michael RothFrom: Philippe Mathieu-Daudé <[email protected]>
On [*] Daniel Berrangé commented:
The QEMU guest agent protocol is not sensible way to access huge files inside the guest. It requires the inefficient process of reading the entire data into memory than duplicating it again in base64 format, and then copying it again in the JSON serializer / monitor code.
For arbitrary general purpose file access, especially for large files, use a real file transfer program or use a network block device, not the QEMU guest agent.
To avoid bug reports as BZ#1594054 (CVE-2018-12617), follow his suggestion to put a low, hard limit on "count" in the guest agent QAPI schema, and don't allow count to be larger than 48 MB.
[*] https://www.mail-archive.com/[email protected]/msg693176.html
*update schema documentation to indicate 48MB limit instead of 10MB
--- qga/commands.c | 9 ++++++++- qga/qapi-schema.json | 6 ++++-- 2 files changed, 12 insertions(+), 3 deletions(-)
qga/commands.c +8 -1
@@ -11,6 +11,7 @@*/ #include "qemu/osdep.h" +#include "qemu/units.h" #include "guest-agent-core.h" #include "qga-qapi-commands.h" #include "qapi/error.h"
@@ -24,6 +25,12 @@#define GUEST_EXEC_MAX_OUTPUT (16*1024*1024)/* Allocation and I/O buffer for reading guest-exec out_data/err_data - 4KB */ #define GUEST_EXEC_IO_SIZE (4*1024) +/* +* Maximum file size to read - 48MB +* +* (48MB + Base64 3:4 overhead = JSON parser 64 MB limit) +*/ +#define GUEST_FILE_READ_COUNT_MAX (48 * MiB)/* Note: in some situations, like with the fsfreeze, logging may be * temporarilly disabled. if it is necessary that a command be able
@@ -560,7 +567,7 @@ GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count, }if (!has_count) { count =QGA_READ_COUNT_DEFAULT ; - }else if (count <0 || count >=UINT32_MAX ) { + }else if (count <0 || count >GUEST_FILE_READ_COUNT_MAX ) {error_setg (errp,"value '%" PRId64"' is invalid for argument count" , count);return NULL ;
qga/qapi-schema.json +4 -2
@@ -266,11 +266,13 @@ ## # @guest-file-read: # -# Read from an open file in the guest. Data will be base64-encoded +# Read from an open file in the guest. Data will be base64-encoded. +# As this command is just for limited, ad-hoc debugging, such as log +# file access, the number of bytes to read is limited to 48 MB. # # @handle: filehandle returned by guest-file-open # -# @count: maximum number of bytes to read (default is 4KB) +# @count: maximum number of bytes to read (default is 4KB, maximum is 48MB) # # Returns: @GuestFileRead on success. #
Signature · 1 line
2.17.1
Re: [PULL for-5.0 4/4] qga: Restrict guest-file-read count to 48 MB to avoid crashes
Quoted 20 lines · Michael Roth
From: Philippe Mathieu-Daudé <[email protected]>
On [*] Daniel Berrangé commented:
The QEMU guest agent protocol is not sensible way to access huge files inside the guest. It requires the inefficient process of reading the entire data into memory than duplicating it again in base64 format, and then copying it again in the JSON serializer / monitor code.
For arbitrary general purpose file access, especially for large files, use a real file transfer program or use a network block device, not the QEMU guest agent.
To avoid bug reports as BZ#1594054 (CVE-2018-12617), follow his suggestion to put a low, hard limit on "count" in the guest agent QAPI schema, and don't allow count to be larger than 48 MB.
[*] https://www.mail-archive.com/[email protected]/msg693176.html
Quoted 7 lines
Fixes CVE-2018-12617Reported-by Fakhri Zulkifli <[email protected]>Suggested-by Daniel P. Berrangé <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>Reviewed-by Daniel P. Berrangé <[email protected]>*update schema documentation to indicate 48MB limit instead of 10MB
Thanks!
Quoted 5 lines
Signed-off-by Michael Roth <[email protected]>--- qga/commands.c | 9 ++++++++- qga/qapi-schema.json | 6 ++++-- 2 files changed, 12 insertions(+), 3 deletions(-)
Peter Maydell
AppliedQuoted 19 lines · Michael Roth
The following changes since commit
73995d15557a3cf2328cc6b7982264897c65cf65:Merge remote-tracking branch 'remotes/stsquad/tags/pull-more-fixes-150420-1' into staging (2020-04-15 12:02:59 +0100)
are available in the Git repository at:
git://github.com/mdroth/qemu.gittags/qga-pull-2020-04-15-tagfor you to fetch changes up to
1329651fb4d4c5068ad12fd86aff7e52f9e18c34:qga: Restrict guest-file-read count to 48 MB to avoid crashes (2020-04-15 09:18:48 -0500)
----------------------------------------------------------------
qemu-ga patch queue for hard-freeze* enforce 48MB limit for guest-file-read to avoid memory allocation failures
Quoted 7 lines
----------------------------------------------------------------
Philippe Mathieu-Daudé (4):
Revert "prevent crash when executing guest-file-read with large count"
qga: Extractguest_file_handle_find()tocommands-common.h
qga: Extractqmp_guest_file_read()to commoncommands.c
qga: Restrict guest-file-read count to 48 MB to avoid crashes
Applied, thanks.
Please update the changelog at https://wiki.qemu.org/ChangeLog/5.0
for any user-visible changes.
-- PMM