Skip to contentSkip to conversation

virtio_balloon: prevent uninitialized variable use

PATCH In review Arnd Bergmann 5 participants 9 messages Mar 23, 2017 – Mar 28, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio_balloon: prevent uninitialized variable use

The latest gcc-7.0.1 snapshot reports a new warning:

virtio/virtio_balloon.c: In function 'update_balloon_stats':
virtio/virtio_balloon.c:258:26: error: 'events[2]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:260:26: error: 'events[3]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:261:56: error: 'events[18]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:262:56: error: 'events[17]' is used uninitialized in this function [-Werror=uninitialized]

This seems absolutely right, so we should add an extra check to prevent copying uninitialized stack data into the statistics. From all I can tell, this has been broken since the statistics code was originally added in 2.6.34.

Fixes 9564e138b1f6 ("virtio: Add memory statistics reporting to the balloon driver (V4)")
---
 drivers/virtio/virtio_balloon.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/virtio/virtio_balloon.c +2 -0
index 4e1191508228..cd5c54e2003d 100644
@@ -254,12 +254,14 @@ static void update_balloon_stats(struct virtio_balloon *vb)
 
 	available = si_mem_available();
 
+#ifdef CONFIG_VM_EVENT_COUNTERS
 	update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN,
 				pages_to_bytes(events[PSWPIN]));
 	update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT,
 				pages_to_bytes(events[PSWPOUT]));
 	update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
 	update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
+#endif
 	update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE,
 				pages_to_bytes(i.freeram));
 	update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT,
Signature · 1 line
2.9.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Arnd Bergmann

The latest gcc-7.0.1 snapshot reports a new warning:

virtio/virtio_balloon.c: In function 'update_balloon_stats':
virtio/virtio_balloon.c:258:26: error: 'events[2]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:260:26: error: 'events[3]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:261:56: error: 'events[18]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:262:56: error: 'events[17]' is used uninitialized in this function [-Werror=uninitialized]

Quoted 7 lines

This seems absolutely right, so we should add an extra check to prevent copying uninitialized stack data into the statistics. From all I can tell, this has been broken since the statistics code was originally added in 2.6.34.

Fixes 9564e138b1f6 ("virtio: Add memory statistics reporting to the balloon driver (V4)")
Quoted 23 lines
---
 drivers/virtio/virtio_balloon.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/virtio/virtio_balloon.c +2 -0
index 4e1191508228..cd5c54e2003d 100644
@@ -254,12 +254,14 @@ static void update_balloon_stats(struct virtio_balloon *vb)
 
 	available = si_mem_available();
 
+#ifdef CONFIG_VM_EVENT_COUNTERS
 	update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN,
 				pages_to_bytes(events[PSWPIN]));
 	update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT,
 				pages_to_bytes(events[PSWPOUT]));
 	update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
 	update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
+#endif
 	update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE,
 				pages_to_bytes(i.freeram));
 	update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT,
#2 lore raw
In reply to Arnd Bergmann
Quoted 29 lines · Arnd Bergmann

The latest gcc-7.0.1 snapshot reports a new warning:

virtio/virtio_balloon.c: In function 'update_balloon_stats':
virtio/virtio_balloon.c:258:26: error: 'events[2]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:260:26: error: 'events[3]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:261:56: error: 'events[18]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:262:56: error: 'events[17]' is used uninitialized in this function [-Werror=uninitialized]

This seems absolutely right, so we should add an extra check to prevent copying uninitialized stack data into the statistics. From all I can tell, this has been broken since the statistics code was originally added in 2.6.34.

Fixes 9564e138b1f6 ("virtio: Add memory statistics reporting to the balloon driver (V4)")
---
 drivers/virtio/virtio_balloon.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/virtio/virtio_balloon.c +1 -0
index 4e1191508228..cd5c54e2003d 100644
@@ -254,12 +254,14 @@ static void update_balloon_stats(struct virtio_balloon *vb)
 
 	available = si_mem_available();
 
+#ifdef CONFIG_VM_EVENT_COUNTERS

drivers/virtio/virtio_balloon.c static void update_balloon_stats(struct virtio_balloon *vb) · line 267

Quoted 10 lines
drivers/virtio/virtio_balloon.c +1 -0
 	update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN,
 				pages_to_bytes(events[PSWPIN]));
 	update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT,
 				pages_to_bytes(events[PSWPOUT]));
 	update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
 	update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
+#endif
 	update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE,
 				pages_to_bytes(i.freeram));
 	update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT,

CC'ing Ladi

Signature · 3 lines
Thanks,

David
#3 lore raw
Quoted 28 lines · David Hildenbrand
Quoted 27 lines · Arnd Bergmann

The latest gcc-7.0.1 snapshot reports a new warning:

virtio/virtio_balloon.c: In function 'update_balloon_stats':
virtio/virtio_balloon.c:258:26: error: 'events[2]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:260:26: error: 'events[3]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:261:56: error: 'events[18]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:262:56: error: 'events[17]' is used uninitialized in this function [-Werror=uninitialized]

This seems absolutely right, so we should add an extra check to prevent copying uninitialized stack data into the statistics. From all I can tell, this has been broken since the statistics code was originally added in 2.6.34.

Fixes 9564e138b1f6 ("virtio: Add memory statistics reporting to the balloon driver (V4)")
---
 drivers/virtio/virtio_balloon.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/virtio/virtio_balloon.c +0 -0
index 4e1191508228..cd5c54e2003d 100644
@@ -254,12 +254,14 @@ static void update_balloon_stats(struct virtio_balloon *vb)
 
      available = si_mem_available();
 
Quoted 11 lines

drivers/virtio/virtio_balloon.c static void update_balloon_stats(struct virtio_balloon *vb) · line 267

Quoted 11 lines
drivers/virtio/virtio_balloon.c +2 -0
+#ifdef CONFIG_VM_EVENT_COUNTERS
      update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN,
                              pages_to_bytes(events[PSWPIN]));
      update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT,
                              pages_to_bytes(events[PSWPOUT]));
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
+#endif
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE,
                              pages_to_bytes(i.freeram));
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT,

This will leave four uninitialized slots in vb->stats if CONFIG_VM_EVENT_COUNTERS is not defined. update_balloon_stats should have

  BUG_ON(idx < VIRTIO_BALLOON_S_NR);

at the end.

You need to make sure that vb->stats is smaller, either by using something else than VIRTIO_BALLOON_S_NR for its size or something else than sizeof(vb->stats) as the last argument to sg_init_one in this file.

Quoted 1 line

CC'ing Ladi

Thanks!

Quoted 5 lines

--

Thanks,

David

#4 lore raw
Quoted 45 lines · Ladi Prosek
Quoted 28 lines · David Hildenbrand
Quoted 27 lines · Arnd Bergmann

The latest gcc-7.0.1 snapshot reports a new warning:

virtio/virtio_balloon.c: In function 'update_balloon_stats':
virtio/virtio_balloon.c:258:26: error: 'events[2]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:260:26: error: 'events[3]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:261:56: error: 'events[18]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:262:56: error: 'events[17]' is used uninitialized in this function [-Werror=uninitialized]

This seems absolutely right, so we should add an extra check to prevent copying uninitialized stack data into the statistics. From all I can tell, this has been broken since the statistics code was originally added in 2.6.34.

Fixes 9564e138b1f6 ("virtio: Add memory statistics reporting to the balloon driver (V4)")
---
 drivers/virtio/virtio_balloon.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/virtio/virtio_balloon.c +0 -0
index 4e1191508228..cd5c54e2003d 100644
@@ -254,12 +254,14 @@ static void update_balloon_stats(struct virtio_balloon *vb)
 
      available = si_mem_available();
 
Quoted 11 lines

drivers/virtio/virtio_balloon.c static void update_balloon_stats(struct virtio_balloon *vb) · line 267

Quoted 11 lines
drivers/virtio/virtio_balloon.c +2 -0
+#ifdef CONFIG_VM_EVENT_COUNTERS
      update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN,
                              pages_to_bytes(events[PSWPIN]));
      update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT,
                              pages_to_bytes(events[PSWPOUT]));
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
+#endif
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE,
                              pages_to_bytes(i.freeram));
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT,

This will leave four uninitialized slots in vb->stats if CONFIG_VM_EVENT_COUNTERS is not defined. update_balloon_stats should have

Quoted 8 lines
  BUG_ON(idx < VIRTIO_BALLOON_S_NR);

at the end.

You need to make sure that vb->stats is smaller, either by using something else than VIRTIO_BALLOON_S_NR for its size or something else than sizeof(vb->stats) as the last argument to sg_init_one in this file.

Ah, got it. Would one of you create a fixed patch for this, or should I?

An easy way to solve it would be to preinitialize the events array and return zeroes to the host, but I don't know if that's the right solution.

Another option would be to return 'idx' from update_balloon_stats, and use that for the size:

drivers/virtio/virtio_balloon.c +0 -0
index cd5c54e2003d..bea3cfb88e53 100644
@@ -242,7 +242,7 @@ static inline void update_stat(struct

virtio_balloon *vb, int idx,

#define pages_to_bytes(x) ((u64)(x) << PAGE_SHIFT)

-static void update_balloon_stats(struct virtio_balloon *vb) +static unsigned int update_balloon_stats(struct virtio_balloon *vb)
{
unsigned long events[NR_VM_EVENT_ITEMS];
struct sysinfo i;
@@ -268,6 +268,8 @@ static void update_balloon_stats(struct virtio_balloon *vb)
pages_to_bytes(i.totalram));
update_stat(vb, idx++, VIRTIO_BALLOON_S_AVAIL, pages_to_bytes(available));
+
+ return idx;
}

/*
@@ -294,13 +296,14 @@ static void stats_handle_request(struct virtio_balloon *vb)
struct virtqueue *vq;
struct scatterlist sg;
unsigned int len;
+ unsigned int num_stats;

- update_balloon_stats(vb);
+ num_stats = update_balloon_stats(vb);

vq = vb->stats_vq;
if (!virtqueue_get_buf(vq, &len))
return;
- sg_init_one(&sg, vb->stats, sizeof(vb->stats));
+ sg_init_one(&sg, vb->stats, sizeof(vb->stats[0] * num_stats)); virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL); virtqueue_kick(vq);
}

Arnd

#5 lore raw
Quoted 55 lines · Arnd Bergmann
Quoted 45 lines · Ladi Prosek
Quoted 28 lines · David Hildenbrand
Quoted 27 lines · Arnd Bergmann

The latest gcc-7.0.1 snapshot reports a new warning:

virtio/virtio_balloon.c: In function 'update_balloon_stats':
virtio/virtio_balloon.c:258:26: error: 'events[2]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:260:26: error: 'events[3]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:261:56: error: 'events[18]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:262:56: error: 'events[17]' is used uninitialized in this function [-Werror=uninitialized]

This seems absolutely right, so we should add an extra check to prevent copying uninitialized stack data into the statistics. From all I can tell, this has been broken since the statistics code was originally added in 2.6.34.

Fixes 9564e138b1f6 ("virtio: Add memory statistics reporting to the balloon driver (V4)")
---
 drivers/virtio/virtio_balloon.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/virtio/virtio_balloon.c +0 -0
index 4e1191508228..cd5c54e2003d 100644
@@ -254,12 +254,14 @@ static void update_balloon_stats(struct virtio_balloon *vb)
 
      available = si_mem_available();
 
Quoted 11 lines

drivers/virtio/virtio_balloon.c static void update_balloon_stats(struct virtio_balloon *vb) · line 267

Quoted 11 lines
drivers/virtio/virtio_balloon.c +2 -0
+#ifdef CONFIG_VM_EVENT_COUNTERS
      update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN,
                              pages_to_bytes(events[PSWPIN]));
      update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT,
                              pages_to_bytes(events[PSWPOUT]));
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
+#endif
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE,
                              pages_to_bytes(i.freeram));
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT,

This will leave four uninitialized slots in vb->stats if CONFIG_VM_EVENT_COUNTERS is not defined. update_balloon_stats should have

Quoted 8 lines
  BUG_ON(idx < VIRTIO_BALLOON_S_NR);

at the end.

You need to make sure that vb->stats is smaller, either by using something else than VIRTIO_BALLOON_S_NR for its size or something else than sizeof(vb->stats) as the last argument to sg_init_one in this file.

Quoted 5 lines

Ah, got it. Would one of you create a fixed patch for this, or should I?

An easy way to solve it would be to preinitialize the events array and return zeroes to the host, but I don't know if that's the right solution.

I think that's out of spec.

Quoted 45 lines

Another option would be to return 'idx' from update_balloon_stats, and use that for the size:

drivers/virtio/virtio_balloon.c +1 -1
index cd5c54e2003d..bea3cfb88e53 100644
@@ -242,7 +242,7 @@ static inline void update_stat(struct
 virtio_balloon *vb, int idx,
 
 #define pages_to_bytes(x) ((u64)(x) << PAGE_SHIFT)
 
-static void update_balloon_stats(struct virtio_balloon *vb)
+static unsigned int update_balloon_stats(struct virtio_balloon *vb)
 {
  unsigned long events[NR_VM_EVENT_ITEMS];

struct sysinfo i;

drivers/virtio/virtio_balloon.c +5 -2
@@ -268,6 +268,8 @@ static void update_balloon_stats(struct virtio_balloon *vb)
  pages_to_bytes(i.totalram));
  update_stat(vb, idx++, VIRTIO_BALLOON_S_AVAIL,
  pages_to_bytes(available));
+
+ return idx;
 }
 
 /*
@@ -294,13 +296,14 @@ static void stats_handle_request(struct
 virtio_balloon *vb)
  struct virtqueue *vq;
  struct scatterlist sg;
  unsigned int len;
+ unsigned int num_stats;
 
- update_balloon_stats(vb);
+ num_stats = update_balloon_stats(vb);
 
  vq = vb->stats_vq;
  if (!virtqueue_get_buf(vq, &len))
  return;
- sg_init_one(&sg, vb->stats, sizeof(vb->stats));
+ sg_init_one(&sg, vb->stats, sizeof(vb->stats[0] * num_stats));
  virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
  virtqueue_kick(vq);

}

Quoted 1 line

Arnd

Sounds reasonable.

Signature · 1 line
MST
#6 lore raw
Quoted 49 lines · Michael S. Tsirkin
Quoted 48 lines · Arnd Bergmann
Quoted 47 lines · Ladi Prosek
Quoted 28 lines · David Hildenbrand
Quoted 27 lines · Arnd Bergmann

The latest gcc-7.0.1 snapshot reports a new warning:

virtio/virtio_balloon.c: In function 'update_balloon_stats':
virtio/virtio_balloon.c:258:26: error: 'events[2]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:260:26: error: 'events[3]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:261:56: error: 'events[18]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:262:56: error: 'events[17]' is used uninitialized in this function [-Werror=uninitialized]

This seems absolutely right, so we should add an extra check to prevent copying uninitialized stack data into the statistics. From all I can tell, this has been broken since the statistics code was originally added in 2.6.34.

Fixes 9564e138b1f6 ("virtio: Add memory statistics reporting to the balloon driver (V4)")
---
 drivers/virtio/virtio_balloon.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/virtio/virtio_balloon.c +0 -0
index 4e1191508228..cd5c54e2003d 100644
@@ -254,12 +254,14 @@ static void update_balloon_stats(struct virtio_balloon *vb)
 
      available = si_mem_available();
 
Quoted 11 lines

drivers/virtio/virtio_balloon.c static void update_balloon_stats(struct virtio_balloon *vb) · line 267

Quoted 11 lines
drivers/virtio/virtio_balloon.c +2 -0
+#ifdef CONFIG_VM_EVENT_COUNTERS
      update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN,
                              pages_to_bytes(events[PSWPIN]));
      update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT,
                              pages_to_bytes(events[PSWPOUT]));
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
+#endif
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE,
                              pages_to_bytes(i.freeram));
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT,

This will leave four uninitialized slots in vb->stats if CONFIG_VM_EVENT_COUNTERS is not defined. update_balloon_stats should have

  BUG_ON(idx < VIRTIO_BALLOON_S_NR);
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

at the end.

You need to make sure that vb->stats is smaller, either by using something else than VIRTIO_BALLOON_S_NR for its size or something else than sizeof(vb->stats) as the last argument to sg_init_one in this file.

Ah, got it. Would one of you create a fixed patch for this, or should I?

I will do it. If we go the route of returning 'idx' from update_balloon_stats, my patch

virtio_balloon: don't push uninitialized buffers to stats virtqueue

needs to be redone anyway. Thanks!

Quoted 58 lines
Quoted 3 lines

An easy way to solve it would be to preinitialize the events array and return zeroes to the host, but I don't know if that's the right solution.

I think that's out of spec.

Quoted 45 lines

Another option would be to return 'idx' from update_balloon_stats, and use that for the size:

drivers/virtio/virtio_balloon.c +1 -1
index cd5c54e2003d..bea3cfb88e53 100644
@@ -242,7 +242,7 @@ static inline void update_stat(struct
 virtio_balloon *vb, int idx,
 
 #define pages_to_bytes(x) ((u64)(x) << PAGE_SHIFT)
 
-static void update_balloon_stats(struct virtio_balloon *vb)
+static unsigned int update_balloon_stats(struct virtio_balloon *vb)
 {
  unsigned long events[NR_VM_EVENT_ITEMS];

struct sysinfo i;

drivers/virtio/virtio_balloon.c +5 -2
@@ -268,6 +268,8 @@ static void update_balloon_stats(struct virtio_balloon *vb)
  pages_to_bytes(i.totalram));
  update_stat(vb, idx++, VIRTIO_BALLOON_S_AVAIL,
  pages_to_bytes(available));
+
+ return idx;
 }
 
 /*
@@ -294,13 +296,14 @@ static void stats_handle_request(struct
 virtio_balloon *vb)
  struct virtqueue *vq;
  struct scatterlist sg;
  unsigned int len;
+ unsigned int num_stats;
 
- update_balloon_stats(vb);
+ num_stats = update_balloon_stats(vb);
 
  vq = vb->stats_vq;
  if (!virtqueue_get_buf(vq, &len))
  return;
- sg_init_one(&sg, vb->stats, sizeof(vb->stats));
+ sg_init_one(&sg, vb->stats, sizeof(vb->stats[0] * num_stats));
  virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
  virtqueue_kick(vq);

}

Quoted 1 line

Arnd

Sounds reasonable.

--
MST

#7 lore raw
Quoted 57 lines · Ladi Prosek
Quoted 56 lines · Michael S. Tsirkin
Quoted 55 lines · Arnd Bergmann
Quoted 53 lines · Ladi Prosek
Quoted 28 lines · David Hildenbrand
Quoted 27 lines · Arnd Bergmann

The latest gcc-7.0.1 snapshot reports a new warning:

virtio/virtio_balloon.c: In function 'update_balloon_stats':
virtio/virtio_balloon.c:258:26: error: 'events[2]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:260:26: error: 'events[3]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:261:56: error: 'events[18]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:262:56: error: 'events[17]' is used uninitialized in this function [-Werror=uninitialized]

This seems absolutely right, so we should add an extra check to prevent copying uninitialized stack data into the statistics. From all I can tell, this has been broken since the statistics code was originally added in 2.6.34.

Fixes 9564e138b1f6 ("virtio: Add memory statistics reporting to the balloon driver (V4)")
---
 drivers/virtio/virtio_balloon.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/virtio/virtio_balloon.c +0 -0
index 4e1191508228..cd5c54e2003d 100644
@@ -254,12 +254,14 @@ static void update_balloon_stats(struct virtio_balloon *vb)
 
      available = si_mem_available();
 
Quoted 11 lines

drivers/virtio/virtio_balloon.c static void update_balloon_stats(struct virtio_balloon *vb) · line 267

Quoted 11 lines
drivers/virtio/virtio_balloon.c +2 -0
+#ifdef CONFIG_VM_EVENT_COUNTERS
      update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN,
                              pages_to_bytes(events[PSWPIN]));
      update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT,
                              pages_to_bytes(events[PSWPOUT]));
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
+#endif
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE,
                              pages_to_bytes(i.freeram));
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT,

This will leave four uninitialized slots in vb->stats if CONFIG_VM_EVENT_COUNTERS is not defined. update_balloon_stats should have

  BUG_ON(idx < VIRTIO_BALLOON_S_NR);

at the end.

You need to make sure that vb->stats is smaller, either by using something else than VIRTIO_BALLOON_S_NR for its size or something else than sizeof(vb->stats) as the last argument to sg_init_one in this file.

Quoted 8 lines
Quoted 1 line
Quoted 1 line

Ah, got it. Would one of you create a fixed patch for this, or should I?

I will do it. If we go the route of returning 'idx' from update_balloon_stats, my patch

virtio_balloon: don't push uninitialized buffers to stats virtqueue

needs to be redone anyway. Thanks!

Can you do it soon pls? I'd like to do a pull request.

Quoted 58 lines
Quoted 58 lines
Quoted 3 lines

An easy way to solve it would be to preinitialize the events array and return zeroes to the host, but I don't know if that's the right solution.

I think that's out of spec.

Quoted 45 lines

Another option would be to return 'idx' from update_balloon_stats, and use that for the size:

drivers/virtio/virtio_balloon.c +1 -1
index cd5c54e2003d..bea3cfb88e53 100644
@@ -242,7 +242,7 @@ static inline void update_stat(struct
 virtio_balloon *vb, int idx,
 
 #define pages_to_bytes(x) ((u64)(x) << PAGE_SHIFT)
 
-static void update_balloon_stats(struct virtio_balloon *vb)
+static unsigned int update_balloon_stats(struct virtio_balloon *vb)
 {
  unsigned long events[NR_VM_EVENT_ITEMS];

struct sysinfo i;

drivers/virtio/virtio_balloon.c +5 -2
@@ -268,6 +268,8 @@ static void update_balloon_stats(struct virtio_balloon *vb)
  pages_to_bytes(i.totalram));
  update_stat(vb, idx++, VIRTIO_BALLOON_S_AVAIL,
  pages_to_bytes(available));
+
+ return idx;
 }
 
 /*
@@ -294,13 +296,14 @@ static void stats_handle_request(struct
 virtio_balloon *vb)
  struct virtqueue *vq;
  struct scatterlist sg;
  unsigned int len;
+ unsigned int num_stats;
 
- update_balloon_stats(vb);
+ num_stats = update_balloon_stats(vb);
 
  vq = vb->stats_vq;
  if (!virtqueue_get_buf(vq, &len))
  return;
- sg_init_one(&sg, vb->stats, sizeof(vb->stats));
+ sg_init_one(&sg, vb->stats, sizeof(vb->stats[0] * num_stats));
  virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
  virtqueue_kick(vq);

}

Quoted 1 line

Arnd

Sounds reasonable.

--
MST

#8 lore raw
Quoted 60 lines · Michael S. Tsirkin
Quoted 59 lines · Ladi Prosek
Quoted 57 lines · Michael S. Tsirkin
Quoted 56 lines · Arnd Bergmann
Quoted 45 lines · Ladi Prosek
Quoted 28 lines · David Hildenbrand
Quoted 27 lines · Arnd Bergmann

The latest gcc-7.0.1 snapshot reports a new warning:

virtio/virtio_balloon.c: In function 'update_balloon_stats':
virtio/virtio_balloon.c:258:26: error: 'events[2]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:260:26: error: 'events[3]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:261:56: error: 'events[18]' is used uninitialized in this function [-Werror=uninitialized] virtio/virtio_balloon.c:262:56: error: 'events[17]' is used uninitialized in this function [-Werror=uninitialized]

This seems absolutely right, so we should add an extra check to prevent copying uninitialized stack data into the statistics. From all I can tell, this has been broken since the statistics code was originally added in 2.6.34.

Fixes 9564e138b1f6 ("virtio: Add memory statistics reporting to the balloon driver (V4)")
---
 drivers/virtio/virtio_balloon.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/virtio/virtio_balloon.c +0 -0
index 4e1191508228..cd5c54e2003d 100644
@@ -254,12 +254,14 @@ static void update_balloon_stats(struct virtio_balloon *vb)
 
      available = si_mem_available();
 
Quoted 11 lines

drivers/virtio/virtio_balloon.c static void update_balloon_stats(struct virtio_balloon *vb) · line 267

Quoted 11 lines
drivers/virtio/virtio_balloon.c +2 -0
+#ifdef CONFIG_VM_EVENT_COUNTERS
      update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN,
                              pages_to_bytes(events[PSWPIN]));
      update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT,
                              pages_to_bytes(events[PSWPOUT]));
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
+#endif
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE,
                              pages_to_bytes(i.freeram));
      update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT,

This will leave four uninitialized slots in vb->stats if CONFIG_VM_EVENT_COUNTERS is not defined. update_balloon_stats should have

Quoted 8 lines
  BUG_ON(idx < VIRTIO_BALLOON_S_NR);

at the end.

You need to make sure that vb->stats is smaller, either by using something else than VIRTIO_BALLOON_S_NR for its size or something else than sizeof(vb->stats) as the last argument to sg_init_one in this file.

Ah, got it. Would one of you create a fixed patch for this, or should I?

Quoted 8 lines
Quoted 6 lines

I will do it. If we go the route of returning 'idx' from update_balloon_stats, my patch

virtio_balloon: don't push uninitialized buffers to stats virtqueue

needs to be redone anyway. Thanks!

Can you do it soon pls? I'd like to do a pull request.

Done.

[PATCH v3 0/3] virtio_balloon: don't push uninitialized buffers to
stats virtqueue

Thanks!

Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 3 lines

An easy way to solve it would be to preinitialize the events array and return zeroes to the host, but I don't know if that's the right solution.

I think that's out of spec.

Quoted 45 lines

Another option would be to return 'idx' from update_balloon_stats, and use that for the size:

drivers/virtio/virtio_balloon.c +1 -1
index cd5c54e2003d..bea3cfb88e53 100644
@@ -242,7 +242,7 @@ static inline void update_stat(struct
 virtio_balloon *vb, int idx,
 
 #define pages_to_bytes(x) ((u64)(x) << PAGE_SHIFT)
 
-static void update_balloon_stats(struct virtio_balloon *vb)
+static unsigned int update_balloon_stats(struct virtio_balloon *vb)
 {
  unsigned long events[NR_VM_EVENT_ITEMS];

struct sysinfo i;

drivers/virtio/virtio_balloon.c +5 -2
@@ -268,6 +268,8 @@ static void update_balloon_stats(struct virtio_balloon *vb)
  pages_to_bytes(i.totalram));
  update_stat(vb, idx++, VIRTIO_BALLOON_S_AVAIL,
  pages_to_bytes(available));
+
+ return idx;
 }
 
 /*
@@ -294,13 +296,14 @@ static void stats_handle_request(struct
 virtio_balloon *vb)
  struct virtqueue *vq;
  struct scatterlist sg;
  unsigned int len;
+ unsigned int num_stats;
 
- update_balloon_stats(vb);
+ num_stats = update_balloon_stats(vb);
 
  vq = vb->stats_vq;
  if (!virtqueue_get_buf(vq, &len))
  return;
- sg_init_one(&sg, vb->stats, sizeof(vb->stats));
+ sg_init_one(&sg, vb->stats, sizeof(vb->stats[0] * num_stats));
  virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
  virtqueue_kick(vq);

}

Quoted 1 line

Arnd

Sounds reasonable.

--
MST