Skip to contentSkip to conversation

psi: fix integer overflow on unsigned int multiply on 32 bit systems

PATCH In review Ravi Singh 3 participants 3 messages Sep 13, 2021 – Nov 4, 2021 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CGME20210913085226epcas5p2b516f0f591926c927faa9c60d211bf44@epcas5p2.samsung.com

Ravi Singh

author
#1 lore raw

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
index 1652f2bb5..a2cc33dc2 100644
@@ -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
maintainer Acked-by on v1 1/1
#2 lore raw

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 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.

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_USEC is of type long which is 8 bytes on 64 bit systems.

Fixes 0e94682b73bf psi: introduce psi monitor

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
index 1652f2bb5..a2cc33dc2 100644
@@ -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
maintainer Reviewed-by
#3 lore raw
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 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.

Looks reasonable to me.

Quoted 8 lines
Quoted 5 lines

Issue doesnot occur on 64 bit systems because NSEC_PER_USEC is of type long which is 8 bytes on 64 bit systems.

Fixes 0e94682b73bf psi: introduce psi monitor
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
index 1652f2bb5..a2cc33dc2 100644
@@ -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