sd: Fix out-of-bounds assertions
v2 cover sd: Fix out-of-bounds assertions
The v2 changes include the fix for the incorrect assertion for poll_fds in util/main-loop.c according to the review comments from the previous sd fixes.
Lidong Chen (2):
sd: Fix out-of-bounds assertions
util/main-loop: Fix incorrect assertion
hw/sd/sd.c | 4 ++-- util/main-loop.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
Signature · 1 line
1.8.3.1
PATCH v2 1/2 sd: Fix out-of-bounds assertions
Due to an off-by-one error, the assert statements allow an out-of-bound array access.
--- hw/sd/sd.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
hw/sd/sd.c +2 -2
@@ -144,7 +144,7 @@ static const char *sd_state_name(enum SDCardStates state)if (state == sd_inactive_state) {return "inactive" ; } -assert (state <=ARRAY_SIZE (state_name)); +assert (state <ARRAY_SIZE (state_name));return state_name[state]; }
@@ -165,7 +165,7 @@ static const char *sd_response_name(sd_rsp_type_t rsp)if (rsp == sd_r1b) { rsp = sd_r1; } -assert (rsp <=ARRAY_SIZE (response_name)); +assert (rsp <ARRAY_SIZE (response_name));return response_name[rsp]; }
Signature · 1 line
1.8.3.1
Quoted 2 lines · Lidong Chen
Due to an off-by-one error, the assert statements allow an out-of-bound array access.
Not sure via which tree this patch is going (trivial?). To the maintainer, please consider adding when applying:
"This access can not happen. Fix to silent static analyzer warnings."
As confirmed by Lidong in v1 here:
https://lists.gnu.org/archive/html/qemu-devel/2019-04/msg01337.html
Thanks,
Phil.
Quoted 33 lines
Signed-off-by Lidong Chen <[email protected]>Reviewed-by Liam Merwick <[email protected]>Reviewed-by Marc-André Lureau <[email protected]>Reviewed-by Philippe Mathieu-Daudé <[email protected]>Reviewed-by Li Qiang <[email protected]>Reviewed-by Darren Kenny <[email protected]>--- hw/sd/sd.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)hw/sd/sd.c +2
-2@@ -144,7 +144,7 @@ static const char *sd_state_name(enum SDCardStates state)if (state == sd_inactive_state) {return "inactive" ; } -assert (state <=ARRAY_SIZE (state_name)); +assert (state <ARRAY_SIZE (state_name));return state_name[state]; }@@ -165,7 +165,7 @@ static const char *sd_response_name(sd_rsp_type_t rsp)if (rsp == sd_r1b) { rsp = sd_r1; } -assert (rsp <=ARRAY_SIZE (response_name)); +assert (rsp <ARRAY_SIZE (response_name));return response_name[rsp]; }
PATCH v2 2/2 util/main-loop: Fix incorrect assertion
In reply to Lidong ChenThe check for poll_fds in g_assert() was incorrect. The correct assertion should check "n_poll_fds + w->num <= ARRAY_SIZE(poll_fds)" because the subsequent for-loop is doing access to poll_fds[n_poll_fds + i] where i is in [0, w->num).
--- util/main-loop.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
util/main-loop.c +1 -1
@@ -422,7 +422,7 @@ static int os_host_main_loop_wait(int64_t timeout)g_main_context_prepare (context, &max_priority); n_poll_fds =g_main_context_query (context, max_priority, &poll_timeout, poll_fds,ARRAY_SIZE (poll_fds)); -g_assert (n_poll_fds <=ARRAY_SIZE (poll_fds)); +g_assert (n_poll_fds + w->num <=ARRAY_SIZE (poll_fds));for (i =0 ; i < w->num; i++) { poll_fds[n_poll_fds + i].fd = (DWORD_PTR )w->events[i];
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [Qemu-devel PATCH v2 2/2] util/main-loop: Fix incorrect assertion
Quoted 9 lines · Lidong Chen <[email protected]> 于2019年6月5日周三 下午2:23写道:
The check for
poll_fdsing_assert()was incorrect. The correct assertion should check "n_poll_fds+w->num<=ARRAY_SIZE(poll_fds)" because the subsequent for-loop is doing access to poll_fds[n_poll_fds + i] where i is in [0,w->num).Signed-off-by Lidong Chen <[email protected]>Reviewed-by Liran Alon <[email protected]>Reviewed-by Darren Kenny <[email protected]>
Thanks,
Li Qiang
Quoted 22 lines
--- util/main-loop.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)util/main-loop.c +1
-1@@ -422,7 +422,7 @@ static int os_host_main_loop_wait(int64_t timeout)g_main_context_prepare (context, &max_priority); n_poll_fds =g_main_context_query (context, max_priority, &poll_timeout, poll_fds,ARRAY_SIZE (poll_fds)); -g_assert (n_poll_fds <=ARRAY_SIZE (poll_fds)); +g_assert (n_poll_fds + w->num <=ARRAY_SIZE (poll_fds));for (i =0 ; i < w->num; i++) {poll_fds[n_poll_fds + i].fd = (DWORD_PTR)w->events[i];--
1.8.3.1
Re: [Qemu-devel] [Qemu-devel PATCH v2 2/2] util/main-loop: Fix incorrect assertion
In reply to Lidong ChenThis patch doesn't seem related to the series cover.
Quoted 5 lines · Lidong Chen
The check for
poll_fdsing_assert()was incorrect. The correct assertion should check "n_poll_fds+w->num<=ARRAY_SIZE(poll_fds)" because the subsequent for-loop is doing access to poll_fds[n_poll_fds + i] where i is in [0,w->num).
Quoted 3 lines
Signed-off-by Lidong Chen <[email protected]>Reviewed-by Liran Alon <[email protected]>Reviewed-by Darren Kenny <[email protected]>
Quoted 18 lines
--- util/main-loop.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)util/main-loop.c +1
-1@@ -422,7 +422,7 @@ static int os_host_main_loop_wait(int64_t timeout)g_main_context_prepare (context, &max_priority); n_poll_fds =g_main_context_query (context, max_priority, &poll_timeout, poll_fds,ARRAY_SIZE (poll_fds)); -g_assert (n_poll_fds <=ARRAY_SIZE (poll_fds)); +g_assert (n_poll_fds + w->num <=ARRAY_SIZE (poll_fds));for (i =0 ; i < w->num; i++) { poll_fds[n_poll_fds + i].fd = (DWORD_PTR )w->events[i];
Re: [Qemu-devel] [Qemu-devel PATCH v2 2/2] util/main-loop: Fix incorrect assertion
Quoted 1 line · Philippe Mathieu-Daudé
This patch doesn't seem related to the series cover.
I will resent the patch to include more details to the cover.
Quoted 8 lines · Lidong Chen
Quoted 5 lines
The check for
poll_fdsing_assert()was incorrect. The correct assertion should check "n_poll_fds+w->num<=ARRAY_SIZE(poll_fds)" because the subsequent for-loop is doing access to poll_fds[n_poll_fds + i] where i is in [0,w->num).Suggested-by Peter Maydell <[email protected]>
Liam Merwick also suggested this fix. So, added him as well.
Thanks,
Lidong
Quoted 24 lines
Quoted 4 lines
Signed-off-by Lidong Chen <[email protected]>Reviewed-by Liran Alon <[email protected]>Reviewed-by Darren Kenny <[email protected]>Reviewed-by Philippe Mathieu-Daudé <[email protected]>Quoted 18 lines
--- util/main-loop.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)util/main-loop.c +1
-1@@ -422,7 +422,7 @@ static int os_host_main_loop_wait(int64_t timeout)g_main_context_prepare (context, &max_priority); n_poll_fds =g_main_context_query (context, max_priority, &poll_timeout, poll_fds,ARRAY_SIZE (poll_fds)); -g_assert (n_poll_fds <=ARRAY_SIZE (poll_fds)); +g_assert (n_poll_fds + w->num <=ARRAY_SIZE (poll_fds));for (i =0 ; i < w->num; i++) { poll_fds[n_poll_fds + i].fd = (DWORD_PTR )w->events[i];