sysctl: Don't overwrite array size variable when it is set on error earlier
PATCH 1/1 sysctl: Don't overwrite array size variable when it is set on error earlier
When querying CPU topology, if caller-provided array size is smaller than number of online CPUs then, in addition to returning -ENOBUFS, sysctl is expected to provide back this number. However, this value, stored in 'i', is overwritten in the subsequent loop's control statement.
Make sure we don't do this by converting the loop to 'while'.
--- xen/common/sysctl.c | 6 +++++- 1 files changed, 5 insertions(+), 1 deletions(-)
xen/common/sysctl.c +5 -1
@@ -338,8 +338,10 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) ret = -ENOBUFS ; i = num_cpus; } +else + i =0 ; -for ( i =0 ; i < num_cpus; i++ ) +while ( i < num_cpus ) {xen_sysctl_cputopo_t cputopo;
@@ -363,6 +365,8 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) ret = -EFAULT ;break ; } + + i++; } }else
Signature · 1 line
1.7.1
Quoted 18 lines · Boris Ostrovsky
When querying CPU topology, if caller-provided array size is smaller than number of online CPUs then, in addition to returning
-ENOBUFS, sysctl is expected to provide back this number. However, this value, stored in 'i', is overwritten in the subsequent loop's control statement.Make sure we don't do this by converting the loop to 'while'.
Signed-off-by Boris Ostrovsky <[email protected]>Reported-by Andrew Cooper <[email protected]>--- xen/common/sysctl.c | 6 +++++- 1 files changed, 5 insertions(+), 1 deletions(-)xen/common/sysctl.c +0
-0
xen/common/sysctl.c long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) · line 344
Quoted 8 lines
xen/common/sysctl.c +3
-1@@ -338,8 +338,10 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) ret = -ENOBUFS ; i = num_cpus; } +else + i =0 ; -for ( i =0 ; i < num_cpus; i++ ) +while ( i < num_cpus )
This would be fine to keep as "for ( ; i < num_cpus; i++)", and helps avoid an issue if someone introduces a continue; in the future.
As for the fix itself, Reviewed-by: Andrew Cooper <[email protected]>
Quoted 12 lines
xen/common/sysctl.c +2
-0{xen_sysctl_cputopo_t cputopo;@@ -363,6 +365,8 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) ret = -EFAULT ;break ; } + + i++; } }else
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 25.03.15 at 18:12, <[email protected]> wrote:
xen/common/sysctl.c long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t)
Quoted 18 lines · Boris Ostrovsky
When querying CPU topology, if caller-provided array size is smaller than number of online CPUs then, in addition to returning
-ENOBUFS, sysctl is expected to provide back this number. However, this value, stored in 'i', is overwritten in the subsequent loop's control statement.Make sure we don't do this by converting the loop to 'while'.
Signed-off-by Boris Ostrovsky <[email protected]>Reported-by Andrew Cooper <[email protected]>--- xen/common/sysctl.c | 6 +++++- 1 files changed, 5 insertions(+), 1 deletions(-)xen/common/sysctl.c +0
-0@@ -338,8 +338,10 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t)
u_sysctl)Quoted 6 lines
xen/common/sysctl.c +2
-0ret = -ENOBUFS ; i = num_cpus; } +else + i =0 ;
Quoted 5 lines
xen/common/sysctl.c long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) · line 344
Quoted 2 lines
xen/common/sysctl.c +1
-1-for ( i =0 ; i < num_cpus; i++ ) +while ( i < num_cpus )This would be fine to keep as "for ( ; i <
num_cpus; i++)", and helps avoid an issue if someone introduces a continue; in the future.
And that's also what I've been intending to change it to before committing.
Jan