Skip to contentSkip to conversation

ntp: verify offset doesn't overflow in ntp_update_offset

PATCH In review Sasha Levin 2 participants 4 messages Dec 3, 2015 – Dec 8, 2015 Timekeeping, clocksource core, NTP, alarmtimer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ntp: verify offset doesn't overflow in ntp_update_offset

We need to make sure that the offset is valid before manipulating it, otherwise it might overflow on the multiplication.

---
 kernel/time/ntp.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/time/ntp.c +4 -2
index 149cc80..36616c3 100644
@@ -297,6 +297,9 @@ static void ntp_update_offset(long offset)
 	if (!(time_status & STA_PLL))
 		return;
 
+	/* Make sure the multiplication below won't overflow */
+	offset = clamp(offset, -MAXPHASE, MAXPHASE);
+
 	if (!(time_status & STA_NANO))
 		offset *= NSEC_PER_USEC;
 
@@ -304,8 +307,7 @@ static void ntp_update_offset(long offset)
 	 * Scale the phase adjustment and
 	 * clamp to the operating range.
 	 */
-	offset = min(offset, MAXPHASE);
-	offset = max(offset, -MAXPHASE);
+	offset = clamp(offset, -MAXPHASE, MAXPHASE);
 
 	/*
 	 * Select how the frequency is to be controlled
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 4 lines · Sasha Levin

We need to make sure that the offset is valid before manipulating it, otherwise it might overflow on the multiplication.

Thanks for sending this in. I've queued it for 4.5

thanks
-john

#2 lore raw
In reply to Sasha Levin
Quoted 13 lines · Sasha Levin

We need to make sure that the offset is valid before manipulating it, otherwise it might overflow on the multiplication.

---
 kernel/time/ntp.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/time/ntp.c +0 -0
index 149cc80..36616c3 100644

kernel/time/ntp.c static void ntp_update_offset(long offset) · line 304

Quoted 8 lines
kernel/time/ntp.c +3 -0
@@ -297,6 +297,9 @@ static void ntp_update_offset(long offset)
        if (!(time_status & STA_PLL))
                return;
 
+       /* Make sure the multiplication below won't overflow */
+       offset = clamp(offset, -MAXPHASE, MAXPHASE);
+
        if (!(time_status & STA_NANO))
                offset *= NSEC_PER_USEC;

So looking at this a bit closer, this bit looks sort of crazy since we clam the offset, do the multiply and then do the exact same clamp.

I'd much rather do a more logical clamp(offset, -USEC_PER_SEC, USEC_PER_SEC), but only in the case where we do the multiply.

Any objection to that?

thanks
-john

#3 lore raw
Quoted 25 lines · John Stultz
Quoted 16 lines · Sasha Levin
Quoted 16 lines

We need to make sure that the offset is valid before manipulating it, otherwise it might overflow on the multiplication.

---
 kernel/time/ntp.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/time/ntp.c +0 -0
index 149cc80..36616c3 100644
@@ -297,6 +297,9 @@ static void ntp_update_offset(long offset)
        if (!(time_status & STA_PLL))
                return;
 
Quoted 5 lines

kernel/time/ntp.c static void ntp_update_offset(long offset) · line 304

Quoted 5 lines
kernel/time/ntp.c +3 -0
+       /* Make sure the multiplication below won't overflow */
+       offset = clamp(offset, -MAXPHASE, MAXPHASE);
+
        if (!(time_status & STA_NANO))
                offset *= NSEC_PER_USEC;

So looking at this a bit closer, this bit looks sort of crazy since we clam the offset, do the multiply and then do the exact same clamp.

Quoted 4 lines

I'd much rather do a more logical clamp(offset, -USEC_PER_SEC, USEC_PER_SEC), but only in the case where we do the multiply.

Any objection to that?

Nope. Sounds right.

Thanks,
Sasha