Skip to contentSkip to conversation

qga-posix: Avoid crashing process when failing to allocate memory

PATCH-for-5.0 Philippe Mathieu-Daudé 5 participants 11 messages Mar 24, 2020 – Mar 31, 2020

lore
Share & tools
lore
#0 lore raw

Similarly to commit 807e2b6fce0 for Windows, kindly return a QMP error message instead of crashing the whole process.

---
 qga/commands-posix.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
qga/commands-posix.c +7 -1
index 93474ff770..8f127788e6 100644
@@ -493,7 +493,13 @@ struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
         gfh->state = RW_STATE_NEW;
     }
 
-    buf = g_malloc0(count+1);
+    buf = g_try_malloc0(count + 1);
+    if (!buf) {
+        error_setg(errp,
+                   "failed to allocate sufficient memory "
+                   "to complete the requested service");
+        return NULL;
+    }
     read_count = fread(buf, 1, count, fh);
     if (ferror(fh)) {
         error_setg_errno(errp, errno, "failed to read file");
Signature · 1 line
2.21.1
#1 lore raw

but error_setg() also calls malloc, so this does not help at all?

Quoted 35 lines · Philippe Mathieu-Daudé

Similarly to commit 807e2b6fce0 for Windows, kindly return a QMP error message instead of crashing the whole process.

---
 qga/commands-posix.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
qga/commands-posix.c +7 -1
index 93474ff770..8f127788e6 100644
@@ -493,7 +493,13 @@ struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
         gfh->state = RW_STATE_NEW;
     }
 
-    buf = g_malloc0(count+1);
+    buf = g_try_malloc0(count + 1);
+    if (!buf) {
+        error_setg(errp,
+                   "failed to allocate sufficient memory "
+                   "to complete the requested service");
+        return NULL;
+    }
     read_count = fread(buf, 1, count, fh);
     if (ferror(fh)) {
         error_setg_errno(errp, errno, "failed to read file");
Signature · 1 line
2.21.1
#2 lore raw
Quoted 1 line · Dietmar Maurer

but error_setg() also calls malloc, so this does not help at all?

IIUC the problem, you can send a QMP command to ask to read let's say 3GB of a file, and QEMU crashes. But this doesn't mean there the .heap is empty, there is probably few bytes still available, enough to respond with an error message.

Quoted 37 lines
Quoted 36 lines · Philippe Mathieu-Daudé

Similarly to commit 807e2b6fce0 for Windows, kindly return a QMP error message instead of crashing the whole process.

---
  qga/commands-posix.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
qga/commands-posix.c +7 -1
index 93474ff770..8f127788e6 100644
@@ -493,7 +493,13 @@ struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
          gfh->state = RW_STATE_NEW;
      }
 
-    buf = g_malloc0(count+1);
+    buf = g_try_malloc0(count + 1);
+    if (!buf) {
+        error_setg(errp,
+                   "failed to allocate sufficient memory "
+                   "to complete the requested service");
+        return NULL;
+    }
      read_count = fread(buf, 1, count, fh);
      if (ferror(fh)) {
          error_setg_errno(errp, errno, "failed to read file");
Signature · 1 line
2.21.1
#3 lore raw
Quoted 7 lines · Philippe Mathieu-Daudé
Quoted 1 line · Dietmar Maurer

but error_setg() also calls malloc, so this does not help at all?

IIUC the problem, you can send a QMP command to ask to read let's say 3GB of a file, and QEMU crashes. But this doesn't mean there the .heap is empty, there is probably few bytes still available, enough to respond with an error message.

We've discussed how to handle out-of-memory conditions many times. Here's one instance:

Subject: When it's okay to treat OOM as fatal?
Message-ID: <[email protected]>
https://lists.nongnu.org/archive/html/qemu-devel/2018-10/msg03212.html

No improvement since then; there's no guidance on when to check for OOM. Actual code tends to check only "large" allocations (for subjective values of "large").

I reiterate my opinion that whatever OOM handling we have is too unreliable to be worth much, since it can only help when (1) allocations actually fail (they generally don't[*]), and (2) the allocation that fails is actually handled (they generally aren't), and (3) the handling actually works (we don't test OOM, so it generally doesn't).

[*] Linux overcommits memory, which means malloc() pretty much always succeeds, but when you try to use "too much" of the memory you supposedly allocated, a lethal signal is coming your way. Reasd the thread I quoted for examples.

#4 lore raw

Cc'ing the ppl who responded the thread you quoted.

Quoted 52 lines · Markus Armbruster

Philippe Mathieu-Daudé <[email protected]> writes:

---
  qga/commands-posix.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
qga/commands-posix.c +7 -1
index 93474ff770..8f127788e6 100644
@@ -493,7 +493,13 @@ struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
          gfh->state = RW_STATE_NEW;
      }
 
-    buf = g_malloc0(count+1);
+    buf = g_try_malloc0(count + 1);
+    if (!buf) {
+        error_setg(errp,
+                   "failed to allocate sufficient memory "
+                   "to complete the requested service");
+        return NULL;
+    }
      read_count = fread(buf, 1, count, fh);
      if (ferror(fh)) {
          error_setg_errno(errp, errno, "failed to read file");
Quoted 9 lines · Dietmar Maurer
Quoted 1 line

but error_setg() also calls malloc, so this does not help at all?

IIUC the problem, you can send a QMP command to ask to read let's say 3GB of a file, and QEMU crashes. But this doesn't mean there the .heap is empty, there is probably few bytes still available, enough to respond with an error message.

We've discussed how to handle out-of-memory conditions many times. Here's one instance:

Subject: When it's okay to treat OOM as fatal?
Message-ID: <[email protected]>
https://lists.nongnu.org/archive/html/qemu-devel/2018-10/msg03212.html

No improvement since then; there's no guidance on when to check for OOM. Actual code tends to check only "large" allocations (for subjective values of "large").

I reiterate my opinion that whatever OOM handling we have is too unreliable to be worth much, since it can only help when (1) allocations actually fail (they generally don't[*]), and (2) the allocation that fails is actually handled (they generally aren't), and (3) the handling actually works (we don't test OOM, so it generally doesn't).

Quoted 4 lines

[*] Linux overcommits memory, which means malloc() pretty much always succeeds, but when you try to use "too much" of the memory you supposedly allocated, a lethal signal is coming your way. Reasd the thread I quoted for examples.

So this patch takes Stefan reasoning:
https://lists.nongnu.org/archive/html/qemu-devel/2018-10/msg03525.html

My thinking has been to use g_new() for small QEMU-internal structures and g_try_new() for large amounts of memory allocated in response to untrusted inputs. (Untrusted inputs must never be used for unbounded allocation sizes but those bounded sizes can still be large.)

In any cases (malloc/malloc_try) we have a denial of service (https://www.openwall.com/lists/oss-security/2018/10/17/4) and the service is restarted.

Daniel suggests such behavior should be catched by external firewall guard (either on the process or on the network). This seems out of scope of QEMU and hard to fix.

So, can we improve something? Or should we let this code as it?

#5 lore raw
Quoted 70 lines · Philippe Mathieu-Daudé

Cc'ing the ppl who responded the thread you quoted.

qga/commands-posix.c struct GuestFileRead

Quoted 10 lines · Markus Armbruster

Philippe Mathieu-Daudé <[email protected]> writes:
---
   qga/commands-posix.c | 8 +++++++-

   1 file changed, 7 insertions(+), 1 deletion(-)
qga/commands-posix.c +0 -0
index 93474ff770..8f127788e6 100644
@@ -493,7 +493,13 @@ struct GuestFileRead 

*qmp_guest_file_read(int64_t handle, bool has_count,

Quoted 43 lines
qga/commands-posix.c +7 -1
            gfh->state = RW_STATE_NEW;
        }
 
-    buf = g_malloc0(count+1);
+    buf = g_try_malloc0(count + 1);
+    if (!buf) {
+        error_setg(errp,
+                   "failed to allocate sufficient memory "
+                   "to complete the requested service");
+        return NULL;
+    }
        read_count = fread(buf, 1, count, fh);
        if (ferror(fh)) {
            error_setg_errno(errp, errno, "failed to read file");
Quoted 9 lines · Dietmar Maurer
Quoted 1 line

but error_setg() also calls malloc, so this does not help at all?

IIUC the problem, you can send a QMP command to ask to read let's say 3GB of a file, and QEMU crashes. But this doesn't mean there the .heap is empty, there is probably few bytes still available, enough to respond with an error message.

We've discussed how to handle out-of-memory conditions many times. Here's one instance:

     Subject: When it's okay to treat OOM as fatal?
     Message-ID: <[email protected]>

https://lists.nongnu.org/archive/html/qemu-devel/2018-10/msg03212.html

No improvement since then; there's no guidance on when to check for OOM. Actual code tends to check only "large" allocations (for subjective values of "large").

I reiterate my opinion that whatever OOM handling we have is too unreliable to be worth much, since it can only help when (1) allocations actually fail (they generally don't[*]), and (2) the allocation that fails is actually handled (they generally aren't), and (3) the handling actually works (we don't test OOM, so it generally doesn't).

Quoted 4 lines

[*] Linux overcommits memory, which means malloc() pretty much always succeeds, but when you try to use "too much" of the memory you supposedly allocated, a lethal signal is coming your way.  Reasd the thread I quoted for examples.

So this patch takes Stefan reasoning:
https://lists.nongnu.org/archive/html/qemu-devel/2018-10/msg03525.html

  My thinking has been to use g_new() for small QEMU-internal structures and g_try_new() for large amounts of memory allocated in response to untrusted inputs.  (Untrusted inputs must never be used for unbounded allocation sizes but those bounded sizes can still be large.)

Quoted 5 lines

In any cases (malloc/malloc_try) we have a denial of service (https://www.openwall.com/lists/oss-security/2018/10/17/4) and the service is restarted.

Daniel suggests such behavior should be catched by external firewall guard (either on the process or on the network). This seems out of scope of QEMU and hard to fix.

So, can we improve something? Or should we let this code as it?

But if we don't want to modify this code, we should document somewhere that maintstream QEMU doesn't care about DoS and it is the responsibility to protect it with something else...

#10 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 5 lines · Philippe Mathieu-Daudé

Similarly to commit 807e2b6fce0 for Windows, kindly return a QMP error message instead of crashing the whole process.

I find that bug report pretty dubious

"The QEMU Guest Agent in QEMU is vulnerable to an integer
overflow in the qmp_guest_file_read(). An attacker could exploit this by sending a crafted QMP command (including guest-file-read with a large count value) to the agent via the listening socket to trigger a g_malloc() call with a large memory chunk resulting in a segmentation fault."

"the attacker" in this case has to have access to the QEMU chardev associated with the guest agent. IOW, in any sensible configuration of the chardev, "the attacker" is the same person who launched QEMU in the first place. There's no elevation of privilege here and any denial of service is inflicted against themselves. Finally it doesn't cause a segmentation fault, it causes an abort.

This is *NOT* a security bug.

In fact I'd call this NOTABUG entirely. It is just user error to set the "count" to such a huge value that it hits OOM.

Quoted 12 lines
---
 qga/commands-posix.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
qga/commands-posix.c +0 -0
index 93474ff770..8f127788e6 100644
@@ -493,7 +493,13 @@ struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
         gfh->state = RW_STATE_NEW;

qga/commands-posix.c struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count, · line 502

Quoted 10 lines
qga/commands-posix.c +7 -1
     }
 
-    buf = g_malloc0(count+1);
+    buf = g_try_malloc0(count + 1);
+    if (!buf) {
+        error_setg(errp,
+                   "failed to allocate sufficient memory "
+                   "to complete the requested service");
+        return NULL;
+    }

So you've prevented an OOM when we call fread() on the file.

The contents of 'buf' now need to be turned into JSON which means the buffer need to be base64 encoded. This will consume even more memory than the original read. So checking malloc here has achieved nothing AFAICT.

qga/commands-posix.c struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count, · line 505

Quoted 3 lines
qga/commands-posix.c +0 -0
     read_count = fread(buf, 1, count, fh);
     if (ferror(fh)) {
         error_setg_errno(errp, errno, "failed to read file");

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#6 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 73 lines · * Philippe Mathieu-Daudé ([email protected]) wrote:

Cc'ing the ppl who responded the thread you quoted.

qga/commands-posix.c struct GuestFileRead *qmp_guest_file_read(int64_t

Quoted 10 lines · Markus Armbruster

Philippe Mathieu-Daudé <[email protected]> writes:

---
  qga/commands-posix.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
qga/commands-posix.c +0 -0
index 93474ff770..8f127788e6 100644
@@ -493,7 +493,13 @@ struct GuestFileRead *qmp_guest_file_read(int64_t

handle, bool has_count,

Quoted 42 lines
qga/commands-posix.c +7 -1
          gfh->state = RW_STATE_NEW;
      }
 
-    buf = g_malloc0(count+1);
+    buf = g_try_malloc0(count + 1);
+    if (!buf) {
+        error_setg(errp,
+                   "failed to allocate sufficient memory "
+                   "to complete the requested service");
+        return NULL;
+    }
      read_count = fread(buf, 1, count, fh);
      if (ferror(fh)) {
          error_setg_errno(errp, errno, "failed to read file");
Quoted 9 lines · Dietmar Maurer
Quoted 1 line

but error_setg() also calls malloc, so this does not help at all?

IIUC the problem, you can send a QMP command to ask to read let's say 3GB of a file, and QEMU crashes. But this doesn't mean there the .heap is empty, there is probably few bytes still available, enough to respond with an error message.

We've discussed how to handle out-of-memory conditions many times. Here's one instance:

Subject: When it's okay to treat OOM as fatal?
Message-ID: <[email protected]>
https://lists.nongnu.org/archive/html/qemu-devel/2018-10/msg03212.html

No improvement since then; there's no guidance on when to check for OOM. Actual code tends to check only "large" allocations (for subjective values of "large").

I reiterate my opinion that whatever OOM handling we have is too unreliable to be worth much, since it can only help when (1) allocations actually fail (they generally don't[*]), and (2) the allocation that fails is actually handled (they generally aren't), and (3) the handling actually works (we don't test OOM, so it generally doesn't).

Quoted 4 lines

[*] Linux overcommits memory, which means malloc() pretty much always succeeds, but when you try to use "too much" of the memory you supposedly allocated, a lethal signal is coming your way. Reasd the thread I quoted for examples.

So this patch takes Stefan reasoning:
https://lists.nongnu.org/archive/html/qemu-devel/2018-10/msg03525.html

My thinking has been to use g_new() for small QEMU-internal structures and g_try_new() for large amounts of memory allocated in response to untrusted inputs. (Untrusted inputs must never be used for unbounded allocation sizes but those bounded sizes can still be large.)

In any cases (malloc/malloc_try) we have a denial of service
(https://www.openwall.com/lists/oss-security/2018/10/17/4) and the service is restarted.

Quoted 5 lines

Daniel suggests such behavior should be catched by external firewall guard (either on the process or on the network). This seems out of scope of QEMU and hard to fix.

So, can we improve something? Or should we let this code as it?

I'll agree with Stefan's description; we should use 'try' for anything 'large' (badly defined) or user controlled.

So I think this should switch to having the try.

Dave

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#7 lore raw
Quoted 78 lines · Dr. David Alan Gilbert
Quoted 77 lines · * Philippe Mathieu-Daudé ([email protected]) wrote:

Cc'ing the ppl who responded the thread you quoted.

qga/commands-posix.c struct GuestFileRead *qmp_guest_file_read(int64_t

Quoted 10 lines · Markus Armbruster

Philippe Mathieu-Daudé <[email protected]> writes:

---
  qga/commands-posix.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
qga/commands-posix.c +0 -0
index 93474ff770..8f127788e6 100644
@@ -493,7 +493,13 @@ struct GuestFileRead *qmp_guest_file_read(int64_t

handle, bool has_count,

Quoted 42 lines
qga/commands-posix.c +7 -1
          gfh->state = RW_STATE_NEW;
      }
 
-    buf = g_malloc0(count+1);
+    buf = g_try_malloc0(count + 1);
+    if (!buf) {
+        error_setg(errp,
+                   "failed to allocate sufficient memory "
+                   "to complete the requested service");
+        return NULL;
+    }
      read_count = fread(buf, 1, count, fh);
      if (ferror(fh)) {
          error_setg_errno(errp, errno, "failed to read file");
Quoted 9 lines · Dietmar Maurer
Quoted 1 line

but error_setg() also calls malloc, so this does not help at all?

IIUC the problem, you can send a QMP command to ask to read let's say 3GB of a file, and QEMU crashes. But this doesn't mean there the .heap is empty, there is probably few bytes still available, enough to respond with an error message.

We've discussed how to handle out-of-memory conditions many times. Here's one instance:

Subject: When it's okay to treat OOM as fatal?
Message-ID: <[email protected]>
https://lists.nongnu.org/archive/html/qemu-devel/2018-10/msg03212.html

No improvement since then; there's no guidance on when to check for OOM. Actual code tends to check only "large" allocations (for subjective values of "large").

I reiterate my opinion that whatever OOM handling we have is too unreliable to be worth much, since it can only help when (1) allocations actually fail (they generally don't[*]), and (2) the allocation that fails is actually handled (they generally aren't), and (3) the handling actually works (we don't test OOM, so it generally doesn't).

Quoted 4 lines

[*] Linux overcommits memory, which means malloc() pretty much always succeeds, but when you try to use "too much" of the memory you supposedly allocated, a lethal signal is coming your way. Reasd the thread I quoted for examples.

So this patch takes Stefan reasoning:
https://lists.nongnu.org/archive/html/qemu-devel/2018-10/msg03525.html

My thinking has been to use g_new() for small QEMU-internal structures and g_try_new() for large amounts of memory allocated in response to untrusted inputs. (Untrusted inputs must never be used for unbounded allocation sizes but those bounded sizes can still be large.)

In any cases (malloc/malloc_try) we have a denial of service
(https://www.openwall.com/lists/oss-security/2018/10/17/4) and the service is restarted.

Daniel suggests such behavior should be catched by external firewall guard (either on the process or on the network). This seems out of scope of QEMU and hard to fix.

Quoted 6 lines
Quoted 1 line

So, can we improve something? Or should we let this code as it?

I'll agree with Stefan's description; we should use 'try' for anything 'large' (badly defined) or user controlled.

So I think this should switch to having the try.

IMHO it is pointless to use try here as its not really solving the problem. This is just the first of several mallocs in the process of running this command. Consider the size is large, but still small enough for the first g_try_malloc() to succeed. Soon after GLib is going to g_malloc an even larger bock of (count / 3 + 1) * 4 + 1) bytes in order to base64 encode this data, and that can push us over into OOM again.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#8 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 69 lines · Philippe Mathieu-Daudé

Cc'ing the ppl who responded the thread you quoted.

qga/commands-posix.c struct GuestFileRead *qmp_guest_file_read(int64_t

Quoted 10 lines · Markus Armbruster

Philippe Mathieu-Daudé <[email protected]> writes:

---
  qga/commands-posix.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
qga/commands-posix.c +0 -0
index 93474ff770..8f127788e6 100644
@@ -493,7 +493,13 @@ struct GuestFileRead *qmp_guest_file_read(int64_t

handle, bool has_count,

Quoted 42 lines
qga/commands-posix.c +7 -1
          gfh->state = RW_STATE_NEW;
      }
 
-    buf = g_malloc0(count+1);
+    buf = g_try_malloc0(count + 1);
+    if (!buf) {
+        error_setg(errp,
+                   "failed to allocate sufficient memory "
+                   "to complete the requested service");
+        return NULL;
+    }
      read_count = fread(buf, 1, count, fh);
      if (ferror(fh)) {
          error_setg_errno(errp, errno, "failed to read file");
Quoted 9 lines · Dietmar Maurer
Quoted 1 line

but error_setg() also calls malloc, so this does not help at all?

IIUC the problem, you can send a QMP command to ask to read let's say 3GB of a file, and QEMU crashes. But this doesn't mean there the .heap is empty, there is probably few bytes still available, enough to respond with an error message.

We've discussed how to handle out-of-memory conditions many times. Here's one instance:

Subject: When it's okay to treat OOM as fatal?
Message-ID: <[email protected]>
https://lists.nongnu.org/archive/html/qemu-devel/2018-10/msg03212.html

No improvement since then; there's no guidance on when to check for OOM. Actual code tends to check only "large" allocations (for subjective values of "large").

I reiterate my opinion that whatever OOM handling we have is too unreliable to be worth much, since it can only help when (1) allocations actually fail (they generally don't[*]), and (2) the allocation that fails is actually handled (they generally aren't), and (3) the handling actually works (we don't test OOM, so it generally doesn't).

Quoted 4 lines

[*] Linux overcommits memory, which means malloc() pretty much always succeeds, but when you try to use "too much" of the memory you supposedly allocated, a lethal signal is coming your way. Reasd the thread I quoted for examples.

So this patch takes Stefan reasoning:
https://lists.nongnu.org/archive/html/qemu-devel/2018-10/msg03525.html

My thinking has been to use g_new() for small QEMU-internal structures and g_try_new() for large amounts of memory allocated in response to untrusted inputs. (Untrusted inputs must never be used for unbounded allocation sizes but those bounded sizes can still be large.)

Quoted 7 lines

In any cases (malloc/malloc_try) we have a denial of service
(https://www.openwall.com/lists/oss-security/2018/10/17/4) and the service is restarted.

Daniel suggests such behavior should be catched by external firewall guard (either on the process or on the network). This seems out of scope of QEMU and hard to fix.

Did I ?

IMHO if the guest agent is connected to a chardev that allows access to a user that isn't the one running QEMU, that is serious mis-configuration. IOW, it is user error, not a QEMU guest agent bug. Firewalls are not required - QEMU should be reconfigured to not make it accessible over TCP. This is the same situation documented in 4f24430821c568936aeda417bbb00e989a9e1984 when it was claimed that QEMU had a security flaw because the QMP monitor could be exposed over TCP.

The guest agent is a privileged interface to interact with a guest OS, and as such it should only be made available to users who are trustworthy on the host.

Quoted 1 line

So, can we improve something? Or should we let this code as it?

Personally I would have just put a low, hard limit on "count" in the guest agent spec & impl. eg don't allow count to be larger than 10 MB and document that this command is just for limited, ad-hoc debugging, such as log file access. In extremis we could make the hard limit be a configuration parameter for the guest agent.

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.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#9 lore raw
Quoted 92 lines · Daniel P. Berrangé
Quoted 69 lines · Philippe Mathieu-Daudé

Cc'ing the ppl who responded the thread you quoted.

qga/commands-posix.c struct GuestFileRead *qmp_guest_file_read(int64_t

Quoted 10 lines · Markus Armbruster

Philippe Mathieu-Daudé <[email protected]> writes:

---
   qga/commands-posix.c | 8 +++++++-
   1 file changed, 7 insertions(+), 1 deletion(-)
qga/commands-posix.c +0 -0
index 93474ff770..8f127788e6 100644
@@ -493,7 +493,13 @@ struct GuestFileRead *qmp_guest_file_read(int64_t

handle, bool has_count,

Quoted 42 lines
qga/commands-posix.c +7 -1
           gfh->state = RW_STATE_NEW;
       }
 
-    buf = g_malloc0(count+1);
+    buf = g_try_malloc0(count + 1);
+    if (!buf) {
+        error_setg(errp,
+                   "failed to allocate sufficient memory "
+                   "to complete the requested service");
+        return NULL;
+    }
       read_count = fread(buf, 1, count, fh);
       if (ferror(fh)) {
           error_setg_errno(errp, errno, "failed to read file");
Quoted 9 lines · Dietmar Maurer
Quoted 1 line

but error_setg() also calls malloc, so this does not help at all?

IIUC the problem, you can send a QMP command to ask to read let's say 3GB of a file, and QEMU crashes. But this doesn't mean there the .heap is empty, there is probably few bytes still available, enough to respond with an error message.

We've discussed how to handle out-of-memory conditions many times. Here's one instance:

Subject: When it's okay to treat OOM as fatal?
Message-ID: <[email protected]>
https://lists.nongnu.org/archive/html/qemu-devel/2018-10/msg03212.html

No improvement since then; there's no guidance on when to check for OOM. Actual code tends to check only "large" allocations (for subjective values of "large").

I reiterate my opinion that whatever OOM handling we have is too unreliable to be worth much, since it can only help when (1) allocations actually fail (they generally don't[*]), and (2) the allocation that fails is actually handled (they generally aren't), and (3) the handling actually works (we don't test OOM, so it generally doesn't).

Quoted 4 lines

[*] Linux overcommits memory, which means malloc() pretty much always succeeds, but when you try to use "too much" of the memory you supposedly allocated, a lethal signal is coming your way. Reasd the thread I quoted for examples.

So this patch takes Stefan reasoning:
https://lists.nongnu.org/archive/html/qemu-devel/2018-10/msg03525.html

My thinking has been to use g_new() for small QEMU-internal structures and g_try_new() for large amounts of memory allocated in response to untrusted inputs. (Untrusted inputs must never be used for unbounded allocation sizes but those bounded sizes can still be large.)

Quoted 7 lines

In any cases (malloc/malloc_try) we have a denial of service
(https://www.openwall.com/lists/oss-security/2018/10/17/4) and the service is restarted.

Daniel suggests such behavior should be catched by external firewall guard (either on the process or on the network). This seems out of scope of QEMU and hard to fix.

Did I ?

IMHO if the guest agent is connected to a chardev that allows access to a user that isn't the one running QEMU, that is serious mis-configuration. IOW, it is user error, not a QEMU guest agent bug. Firewalls are not required - QEMU should be reconfigured to not make it accessible over TCP. This is the same situation documented in 4f24430821c568936aeda417bbb00e989a9e1984 when it was claimed that QEMU had a security flaw because the QMP monitor could be exposed over TCP.

The guest agent is a privileged interface to interact with a guest OS, and as such it should only be made available to users who are trustworthy on the host.

Quoted 7 lines
Quoted 1 line

So, can we improve something? Or should we let this code as it?

Personally I would have just put a low, hard limit on "count" in the guest agent spec & impl. eg don't allow count to be larger than 10 MB and document that this command is just for limited, ad-hoc debugging, such as log file access. In extremis we could make the hard limit be a configuration parameter for the guest agent.

This is clearly a smarter/cleaner fix, thanks!

Quoted 13 lines

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.

Regards,
Daniel