Skip to contentSkip to conversation

util/async.c: Forbid negative min/max in aio_context_set_thread_pool_params()

PATCH Applied Peter Maydell 3 participants 5 messages Jul 23, 2024 – Jul 25, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 util/async.c: Forbid negative min/max in aio_context_set_thread_pool_params()

aio_context_set_thread_pool_params() takes two int64_t arguments to set the minimum and maximum number of threads in the pool. We do some bounds checking on these, but we don't catch the case where the inputs are negative. This means that later in the function when we assign these inputs to the AioContext::thread_pool_min and ::thread_pool_max fields, which are of type int, the values might overflow the smaller type.

A negative number of threads is meaningless, so make aio_context_set_thread_pool_params() return an error if either min or max are negative.

Resolves: Coverity CID 1547605

---
 util/async.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/async.c +1 -1
index 0467890052a..3e3e4fc7126 100644
@@ -746,7 +746,7 @@ void aio_context_set_thread_pool_params(AioContext *ctx, int64_t min,
                                         int64_t max, Error **errp)
 {
 
-    if (min > max || !max || min > INT_MAX || max > INT_MAX) {
+    if (min > max || max <= 0 || min < 0 || min > INT_MAX || max > INT_MAX) {
         error_setg(errp, "bad thread-pool-min/thread-pool-max values");
         return;
     }
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 23 lines · Peter Maydell

aio_context_set_thread_pool_params() takes two int64_t arguments to set the minimum and maximum number of threads in the pool. We do some bounds checking on these, but we don't catch the case where the inputs are negative. This means that later in the function when we assign these inputs to the AioContext::thread_pool_min and ::thread_pool_max fields, which are of type int, the values might overflow the smaller type.

A negative number of threads is meaningless, so make aio_context_set_thread_pool_params() return an error if either min or max are negative.

Resolves: Coverity CID 1547605

---
  util/async.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
util/async.c +0 -0
index 0467890052a..3e3e4fc7126 100644

util/async.c void aio_context_set_thread_pool_params(AioContext *ctx, int64_t min, · line 752

Quoted 8 lines
util/async.c +1 -1
@@ -746,7 +746,7 @@ void aio_context_set_thread_pool_params(AioContext *ctx, int64_t min,
                                          int64_t max, Error **errp)
  {
 
-    if (min > max || !max || min > INT_MAX || max > INT_MAX) {
+    if (min > max || max <= 0 || min < 0 || min > INT_MAX || max > INT_MAX) {
          error_setg(errp, "bad thread-pool-min/thread-pool-max values");
          return;
      }

I don't get the point of using signed min/max here...

#2 lore raw
Quoted 36 lines · Philippe Mathieu-Daudé
Quoted 23 lines · Peter Maydell

aio_context_set_thread_pool_params() takes two int64_t arguments to set the minimum and maximum number of threads in the pool. We do some bounds checking on these, but we don't catch the case where the inputs are negative. This means that later in the function when we assign these inputs to the AioContext::thread_pool_min and ::thread_pool_max fields, which are of type int, the values might overflow the smaller type.

A negative number of threads is meaningless, so make aio_context_set_thread_pool_params() return an error if either min or max are negative.

Resolves: Coverity CID 1547605

---
  util/async.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
util/async.c +0 -0
index 0467890052a..3e3e4fc7126 100644

util/async.c void aio_context_set_thread_pool_params(AioContext *ctx, int64_t min, · line 752

Quoted 8 lines
util/async.c +1 -1
@@ -746,7 +746,7 @@ void aio_context_set_thread_pool_params(AioContext *ctx, int64_t min,
                                          int64_t max, Error **errp)
  {
 
-    if (min > max || !max || min > INT_MAX || max > INT_MAX) {
+    if (min > max || max <= 0 || min < 0 || min > INT_MAX || max > INT_MAX) {
          error_setg(errp, "bad thread-pool-min/thread-pool-max values");
          return;
      }
Quoted 1 line

I don't get the point of using signed min/max here...

I think this is because those values may originate in a QAPI command structure (EventLoopBaseProperties), where they are defined as "int" rather than a specifically unsigned type. So we carry them around as int64_t until they get to here, where we do the validation of whether they're sensible or not.

thanks
-- PMM

#3 lore raw
Quoted 37 lines · Peter Maydell
Quoted 36 lines · Philippe Mathieu-Daudé
Quoted 23 lines · Peter Maydell

aio_context_set_thread_pool_params() takes two int64_t arguments to set the minimum and maximum number of threads in the pool. We do some bounds checking on these, but we don't catch the case where the inputs are negative. This means that later in the function when we assign these inputs to the AioContext::thread_pool_min and ::thread_pool_max fields, which are of type int, the values might overflow the smaller type.

A negative number of threads is meaningless, so make aio_context_set_thread_pool_params() return an error if either min or max are negative.

Resolves: Coverity CID 1547605

---
   util/async.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
util/async.c +0 -0
index 0467890052a..3e3e4fc7126 100644

util/async.c void aio_context_set_thread_pool_params(AioContext *ctx, int64_t min, · line 752

Quoted 8 lines
util/async.c +1 -1
@@ -746,7 +746,7 @@ void aio_context_set_thread_pool_params(AioContext *ctx, int64_t min,
                                           int64_t max, Error **errp)
   {
 
-    if (min > max || !max || min > INT_MAX || max > INT_MAX) {
+    if (min > max || max <= 0 || min < 0 || min > INT_MAX || max > INT_MAX) {
           error_setg(errp, "bad thread-pool-min/thread-pool-max values");
           return;
       }
Quoted 8 lines
Quoted 1 line

I don't get the point of using signed min/max here...

I think this is because those values may originate in a QAPI command structure (EventLoopBaseProperties), where they are defined as "int" rather than a specifically unsigned type. So we carry them around as int64_t until they get to here, where we do the validation of whether they're sensible or not.

Ah indeed. Probably very old code hard to change now (QAPI does support unsigned types).

#4 lore raw
In reply to Peter Maydell
Quoted 12 lines · Peter Maydell

aio_context_set_thread_pool_params() takes two int64_t arguments to set the minimum and maximum number of threads in the pool. We do some bounds checking on these, but we don't catch the case where the inputs are negative. This means that later in the function when we assign these inputs to the AioContext::thread_pool_min and ::thread_pool_max fields, which are of type int, the values might overflow the smaller type.

A negative number of threads is meaningless, so make aio_context_set_thread_pool_params() return an error if either min or max are negative.

Quoted 5 lines

Resolves: Coverity CID 1547605

---
 util/async.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Thanks, applied to my block tree:
https://gitlab.com/stefanha/qemu/commits/block

Stefan