Skip to contentSkip to conversation

block: Fix crash after setting latency historygram with single bin

PATCH In review Kevin Wolf 3 participants 3 messages Mar 31 Block layer

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

PATCH 1/1 block: Fix crash after setting latency historygram with single bin

Passing an empty list of boundaries to block-latency-histogram-set sets up a state that leads to a NULL pointer dereference when the next request should be accounted for. This is not a useful configuration, so just error out if the user tries to set it.

The crash can easily be reproduced with the following script:

    qmp() {
    cat <<EOF
    {'execute':'qmp_capabilities'}
    {'execute':'block-latency-histogram-set',
     'arguments': {'id':'ide0','boundaries':[]}}
    {'execute':'cont'}
    EOF
    }

    qmp | ./qemu-system-x86_64 -S -qmp stdio \
        -drive if=none,format=raw,file=null-co:// \
        -device ide-hd,drive=none0,id=ide0
---
 block/accounting.c | 9 +++++++++
 1 file changed, 9 insertions(+)
block/accounting.c +9 -0
index 5cf51f029b1..f00fe997403 100644
@@ -185,6 +185,15 @@ int block_latency_histogram_set(BlockAcctStats *stats, enum BlockAcctType type,
         prev = entry->value;
     }
 
+    /*
+     * block_latency_histogram_account() assumes that it can always access
+     * hist->boundaries[0], so require at least one boundary. A histogram with
+     * a single bin is useless anyway.
+     */
+    if (new_nbins <= 1) {
+        return -EINVAL;
+    }
+
     hist->nbins = new_nbins;
     g_free(hist->boundaries);
     hist->boundaries = g_new(uint64_t, hist->nbins - 1);
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 20 lines · Kevin Wolf

Passing an empty list of boundaries to block-latency-histogram-set sets up a state that leads to a NULL pointer dereference when the next request should be accounted for. This is not a useful configuration, so just error out if the user tries to set it.

The crash can easily be reproduced with the following script:

     qmp() {
     cat <<EOF
     {'execute':'qmp_capabilities'}
     {'execute':'block-latency-histogram-set',
      'arguments': {'id':'ide0','boundaries':[]}}
     {'execute':'cont'}
     EOF
     }

     qmp | ./qemu-system-x86_64 -S -qmp stdio \
         -drive if=none,format=raw,file=null-co:// \
         -device ide-hd,drive=none0,id=ide0
Quoted 4 lines
---
  block/accounting.c | 9 +++++++++
  1 file changed, 9 insertions(+)

(Though the subject should probably say "histogram" instead of "historygram")

Reviewed-by on v1 1/1
#2 lore raw
In reply to Kevin Wolf
Quoted 20 lines · Kevin Wolf

Passing an empty list of boundaries to block-latency-histogram-set sets up a state that leads to a NULL pointer dereference when the next request should be accounted for. This is not a useful configuration, so just error out if the user tries to set it.

The crash can easily be reproduced with the following script:

     qmp() {
     cat <<EOF
     {'execute':'qmp_capabilities'}
     {'execute':'block-latency-histogram-set',
      'arguments': {'id':'ide0','boundaries':[]}}
     {'execute':'cont'}
     EOF
     }

     qmp | ./qemu-system-x86_64 -S -qmp stdio \
         -drive if=none,format=raw,file=null-co:// \
         -device ide-hd,drive=none0,id=ide0
Quoted 4 lines
---
  block/accounting.c | 9 +++++++++
  1 file changed, 9 insertions(+)