vl.c: Check -smp option ranges before setting int globals
PATCH 1/1 vl.c: Check -smp option ranges before setting int globals
When parsing the -smp option, check if the values (that can be anywhere in the [0, UINT64_MAX] range) are in the int range before setting the int globals smp_cpus, max_cpus, smp_cores, smp_threads.
Without this, it was posbible to make smp_cpus and max_cpus negative.
--- vl.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-)
vl.c +26 -5
@@ -1393,10 +1393,11 @@ static void smp_parse(QemuOpts *opts) {if (opts) { -unsigned cpus =qemu_opt_get_number (opts,"cpus" ,0 ); -unsigned sockets =qemu_opt_get_number (opts,"sockets" ,0 ); -unsigned cores =qemu_opt_get_number (opts,"cores" ,0 ); -unsigned threads =qemu_opt_get_number (opts,"threads" ,0 ); +uint64_t cpus =qemu_opt_get_number (opts,"cpus" ,0 ); +uint64_t sockets =qemu_opt_get_number (opts,"sockets" ,0 ); +uint64_t cores =qemu_opt_get_number (opts,"cores" ,0 ); +uint64_t threads =qemu_opt_get_number (opts,"threads" ,0 ); +uint64_t maxcpus =qemu_opt_get_number (opts,"maxcpus" ,0 );/* compute missing values, prefer sockets over cores over threads */ if (cpus ==0 || sockets ==0 ) {
@@ -1415,8 +1416,28 @@ static void smp_parse(QemuOpts *opts) } } - max_cpus =qemu_opt_get_number (opts,"maxcpus" ,0 ); +if (cpus >INT_MAX ) { +fprintf (stderr,"smp: Invalid value for 'cpus': %" PRIu64"\n" , + cpus); +exit (1 ); + } +if (cores >INT_MAX ) { +fprintf (stderr,"smp: Invalid value for 'cores': %" PRIu64"\n" , + cores); +exit (1 ); + } +if (threads >INT_MAX ) { +fprintf (stderr,"smp: Invalid value for 'threads': %" PRIu64"\n" , + threads); +exit (1 ); + } +if (maxcpus >INT_MAX ) { +fprintf (stderr,"smp: Invalid value for 'maxcpus': %" PRIu64"\n" , + maxcpus); +exit (1 ); + } + max_cpus = maxcpus; smp_cpus = cpus; smp_cores = cores >0 ? cores :1 ; smp_threads = threads >0 ? threads :1 ;
Signature · 1 line
1.9.0
Quoted 7 lines · Eduardo Habkost
When parsing the -smp option, check if the values (that can be anywhere in the [0,
UINT64_MAX] range) are in the int range before setting the int globalssmp_cpus,max_cpus,smp_cores,smp_threads.Without this, it was posbible to make
smp_cpusandmax_cpusnegative.Signed-off-by Eduardo Habkost <[email protected]>
The INT_MAX limit might be too generous, but your code sure beats the silent truncation we have now.
Quoted 6 lines · Markus Armbruster
Quoted 4 lines · Eduardo Habkost
When parsing the -smp option, check if the values (that can be anywhere in the [0,
UINT64_MAX] range) are in the int range before setting the int globalssmp_cpus,max_cpus,smp_cores,smp_threads.
Quoted 7 lines
Quoted 3 lines
Without this, it was posbible to make
smp_cpusandmax_cpusnegative.Signed-off-by Eduardo Habkost <[email protected]>The
INT_MAXlimit might be too generous, but your code sure beats the silent truncation we have now.
We will enforce other less generous limits (i.e. machine->max_cpus, MAX_CPUMASK_BITS) later, outside smp_parse(), on the machine initialization code.
These new checks are just to make sure smp_parse() won't silently truncate the values when setting the globals (which is impossible to check outside smp_parse()).
Quoted 1 line
Reviewed-by Markus Armbruster <[email protected]>
Thanks!
Signature · 1 line
Eduardo