psi: fix integer overflow on unsigned int multiply on 32 bit systems
Subsystems (2)
A message that is not in the archive (sent off-list or not yet received): CGME20210913085226epcas5p2b516f0f591926c927faa9c60d211bf44@epcas5p2.samsung.com
PATCH 1/1 psi: fix integer overflow on unsigned int multiply on 32 bit systems
psi accepts window sizes upto WINDOW_MAX_US(10000000). In the case where window_us is larger than 4294967, the result of an multiplication overflows an unsigned int/long(4 bytes on 32 bit system).
For example, this can happen when the window_us is 5000000 so 5000000
* 1000 (NSEC_PER_USEC) will result in 5000000000 which is greater than UINT_MAX(4294967295). Due to this overflow, 705032704 is stored in t->win.size instead of 5000000000. Now psi will be monitoring the window size of 705 msecs instead of 5 secs as expected by user.
Fix this by type casting the first term of the mutiply to a u64.
Issue doesnot occur on 64 bit systems because NSEC_PER_USEC is of type long which is 8 bytes on 64 bit systems.
--- kernel/sched/psi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/psi.c +1 -1
@@ -1145,7 +1145,7 @@ struct psi_trigger *psi_trigger_create(struct psi_group *group, t->group = group; t->state = state; t->threshold = threshold_us *NSEC_PER_USEC ; - t->win.size = window_us *NSEC_PER_USEC ; + t->win.size = (u64 )window_us *NSEC_PER_USEC ;window_reset (&t->win,0 ,0 ,0 ); t->event =0 ;
Signature · 1 line
2.17.1
CC Suren
Sorry, this fell through the cracks.
Quoted 11 lines · Ravi Singh
psi accepts window sizes upto
WINDOW_MAX_US(10000000). In the case wherewindow_usis larger than 4294967, the result of an multiplication overflows an unsigned int/long(4 bytes on 32 bit system).For example, this can happen when the
window_usis 5000000 so 5000000
* 1000 (NSEC_PER_USEC) will result in 5000000000 which is greater thanUINT_MAX(4294967295). Due to this overflow, 705032704 is stored int->win.sizeinstead of 5000000000. Now psi will be monitoring the window size of 705 msecs instead of 5 secs as expected by user.
Quoted 6 lines
Fix this by type casting the first term of the mutiply to a u64.
Issue doesnot occur on 64 bit systems because
NSEC_PER_USECis of type long which is 8 bytes on 64 bit systems.Signed-off-by Ravi Singh <[email protected]>
Peter would you mind taking this through -tip?
Quoted 19 lines
--- kernel/sched/psi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/sched/psi.c +1
-1@@ -1145,7 +1145,7 @@ struct psi_trigger *psi_trigger_create(struct psi_group *group, t->group = group; t->state = state; t->threshold = threshold_us *NSEC_PER_USEC ; - t->win.size = window_us *NSEC_PER_USEC ; + t->win.size = (u64 )window_us *NSEC_PER_USEC ;window_reset (&t->win,0 ,0 ,0 ); t->event =0 ;Signature · 1 line
2.17.1
Quoted 2 lines · Johannes Weiner
CC Suren
Thanks!
Quoted 16 lines
Sorry, this fell through the cracks.
Quoted 12 lines · Ravi Singh
psi accepts window sizes upto
WINDOW_MAX_US(10000000). In the case wherewindow_usis larger than 4294967, the result of an multiplication overflows an unsigned int/long(4 bytes on 32 bit system).For example, this can happen when the
window_usis 5000000 so 5000000
* 1000 (NSEC_PER_USEC) will result in 5000000000 which is greater thanUINT_MAX(4294967295). Due to this overflow, 705032704 is stored int->win.sizeinstead of 5000000000. Now psi will be monitoring the window size of 705 msecs instead of 5 secs as expected by user.Fix this by type casting the first term of the mutiply to a u64.
Looks reasonable to me.
Quoted 8 lines
Quoted 5 lines
Issue doesnot occur on 64 bit systems because
NSEC_PER_USECis of type long which is 8 bytes on 64 bit systems.Signed-off-by Ravi Singh <[email protected]>Fixes 0e94682b73bf psi: introduce psi monitorAcked-by Johannes Weiner <[email protected]>
Quoted 22 lines
Peter would you mind taking this through -tip?
Quoted 19 lines
--- kernel/sched/psi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/sched/psi.c +1
-2@@ -1145,7 +1145,7 @@ struct psi_trigger *psi_trigger_create(struct psi_group *group, t->group = group; t->state = state; t->threshold = threshold_us *NSEC_PER_USEC ; - t->win.size = window_us *NSEC_PER_USEC ; + t->win.size = (u64 )window_us *NSEC_PER_USEC ;window_reset (&t->win,0 ,0 ,0 ); t->event =0 ; --2.17.1