Skip to contentSkip to conversation

time: adjtimex: validate the ADJ_FREQUENCY case

PATCH In review Sasha Levin 2 participants 5 messages Dec 4, 2014 – Dec 18, 2014 Timekeeping, clocksource core, NTP, alarmtimer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 time: adjtimex: validate the ADJ_FREQUENCY case

Verify that the frequency value from userspace is valid and makes sense.

Unverified values can cause overflows later on.

---
 kernel/time/ntp.c |    9 +++++++++
 1 file changed, 9 insertions(+)
kernel/time/ntp.c +9 -0
index 87a346f..54828cf 100644
@@ -633,6 +633,15 @@ int ntp_validate_timex(struct timex *txc)
 	if ((txc->modes & ADJ_SETOFFSET) && (!capable(CAP_SYS_TIME)))
 		return -EPERM;
 
+	if (txc->modes & ADJ_FREQUENCY) {
+		if (!capable(CAP_SYS_TIME))
+			return -EPERM;
+		if (txc->freq < 0)
+			return -EINVAL;
+		if (LONG_MAX / PPM_SCALE < txc->freq)
+			return -EINVAL;
+	}
+
 	return 0;
 }
 
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 14 lines · Sasha Levin

Verify that the frequency value from userspace is valid and makes sense.

Unverified values can cause overflows later on.

---
 kernel/time/ntp.c |    9 +++++++++
 1 file changed, 9 insertions(+)
kernel/time/ntp.c +0 -0
index 87a346f..54828cf 100644

kernel/time/ntp.c int ntp_validate_timex(struct timex *txc) · line 638

Quoted 6 lines
kernel/time/ntp.c +3 -0
@@ -633,6 +633,15 @@ int ntp_validate_timex(struct timex *txc)
        if ((txc->modes & ADJ_SETOFFSET) && (!capable(CAP_SYS_TIME)))
                return -EPERM;
 
+       if (txc->modes & ADJ_FREQUENCY) {
+               if (!capable(CAP_SYS_TIME))
+                       return -EPERM;

So does this actually change behavior? We check CAP_SYS_TIME if modes is set to anything a few lines above (with the exception of ADJ_ADJTIME which only allows for ADJ_OFFSET_SINGLESHOT or ADJ_OFFSET_READONLY).

Granted, that logic isn't intuitive to read (and probably needs a cleanup) but seems ok.

kernel/time/ntp.c int ntp_validate_timex(struct timex *txc) · line 640

Quoted 2 lines
kernel/time/ntp.c +2 -0
+               if (txc->freq < 0)
+                       return -EINVAL;

? Freq adjustments can be negative.... Am I just missing something here?

kernel/time/ntp.c int ntp_validate_timex(struct timex *txc) · line 643

Quoted 3 lines
kernel/time/ntp.c +3 -0
+               if (LONG_MAX / PPM_SCALE < txc->freq)
+                       return -EINVAL;
+       }

This part seems reasonable though. We bound the output, but overflows could result in negative result when it was specified positive.

I'm curious: I know many of your patches come from trinity issues, but this one isn't super clear in the commit message how it was found. Did an actually issue crop up here, or was this just something you came up with while looking at the 3.18rc hang problem?

thanks
-john

#2 lore raw
Quoted 27 lines · John Stultz
Quoted 14 lines · Sasha Levin

Verify that the frequency value from userspace is valid and makes sense.

Unverified values can cause overflows later on.

---
 kernel/time/ntp.c |    9 +++++++++
 1 file changed, 9 insertions(+)
kernel/time/ntp.c +0 -0
index 87a346f..54828cf 100644

kernel/time/ntp.c int ntp_validate_timex(struct timex *txc) · line 638

Quoted 6 lines
kernel/time/ntp.c +3 -0
@@ -633,6 +633,15 @@ int ntp_validate_timex(struct timex *txc)
        if ((txc->modes & ADJ_SETOFFSET) && (!capable(CAP_SYS_TIME)))
                return -EPERM;
 
+       if (txc->modes & ADJ_FREQUENCY) {
+               if (!capable(CAP_SYS_TIME))
+                       return -EPERM;

So does this actually change behavior? We check CAP_SYS_TIME if modes is set to anything a few lines above (with the exception of ADJ_ADJTIME which only allows for ADJ_OFFSET_SINGLESHOT or ADJ_OFFSET_READONLY).

Quoted 2 lines

Granted, that logic isn't intuitive to read (and probably needs a cleanup) but seems ok.

No, it doesn't change behaviour. The logic, as you said, is a mess - so I tried to keep this change (I actually have a few more which look very similar) as readable and safe as possible

Quoted 4 lines

kernel/time/ntp.c int ntp_validate_timex(struct timex *txc) · line 640

Quoted 2 lines
kernel/time/ntp.c +2 -0
+               if (txc->freq < 0)
+                       return -EINVAL;

? Freq adjustments can be negative.... Am I just missing something here?

No, My bad, this should actually be:

	if (LONG_MIN / PPM_SCALE > txc->freq)
		return -EINVAL;
Quoted 6 lines

kernel/time/ntp.c int ntp_validate_timex(struct timex *txc) · line 643

Quoted 3 lines
kernel/time/ntp.c +3 -0
+               if (LONG_MAX / PPM_SCALE < txc->freq)
+                       return -EINVAL;
+       }

This part seems reasonable though. We bound the output, but overflows could result in negative result when it was specified positive.

The overflows could actually result in being anything, as this is considered undefined behaviour.

Quoted 4 lines

I'm curious: I know many of your patches come from trinity issues, but this one isn't super clear in the commit message how it was found. Did an actually issue crop up here, or was this just something you came up with while looking at the 3.18rc hang problem?

This is just me playing with the undefined behaviour/gcc5 patch and trinity, it doesn't have anything to do with the hang problem.

Thanks,
Sasha

#3 lore raw
Quoted 28 lines · Sasha Levin
Quoted 27 lines · John Stultz
Quoted 14 lines · Sasha Levin

Verify that the frequency value from userspace is valid and makes sense.

Unverified values can cause overflows later on.

---
 kernel/time/ntp.c |    9 +++++++++
 1 file changed, 9 insertions(+)
kernel/time/ntp.c +0 -0
index 87a346f..54828cf 100644

kernel/time/ntp.c int ntp_validate_timex(struct timex *txc) · line 638

Quoted 6 lines
kernel/time/ntp.c +3 -0
@@ -633,6 +633,15 @@ int ntp_validate_timex(struct timex *txc)
        if ((txc->modes & ADJ_SETOFFSET) && (!capable(CAP_SYS_TIME)))
                return -EPERM;
 
+       if (txc->modes & ADJ_FREQUENCY) {
+               if (!capable(CAP_SYS_TIME))
+                       return -EPERM;

So does this actually change behavior? We check CAP_SYS_TIME if modes is set to anything a few lines above (with the exception of ADJ_ADJTIME which only allows for ADJ_OFFSET_SINGLESHOT or ADJ_OFFSET_READONLY).

Quoted 6 lines
Quoted 2 lines

Granted, that logic isn't intuitive to read (and probably needs a cleanup) but seems ok.

No, it doesn't change behaviour. The logic, as you said, is a mess - so I tried to keep this change (I actually have a few more which look very similar) as readable and safe as possible

Ok, could you maybe just add the (fixed) overflow check in one patch (which we'll need to backport to -stable) and we'll try to do a cleanup of the logic in a separate patch?

Quoted 20 lines
Quoted 4 lines

kernel/time/ntp.c int ntp_validate_timex(struct timex *txc) · line 640

Quoted 2 lines
kernel/time/ntp.c +2 -0
+               if (txc->freq < 0)
+                       return -EINVAL;

? Freq adjustments can be negative.... Am I just missing something here?

No, My bad, this should actually be:

        if (LONG_MIN / PPM_SCALE > txc->freq)
                return -EINVAL;
Quoted 6 lines

kernel/time/ntp.c int ntp_validate_timex(struct timex *txc) · line 643

Quoted 3 lines
kernel/time/ntp.c +3 -0
+               if (LONG_MAX / PPM_SCALE < txc->freq)
+                       return -EINVAL;
+       }

This part seems reasonable though. We bound the output, but overflows could result in negative result when it was specified positive.

The overflows could actually result in being anything, as this is considered undefined behaviour.

Quoted 7 lines
Quoted 4 lines

I'm curious: I know many of your patches come from trinity issues, but this one isn't super clear in the commit message how it was found. Did an actually issue crop up here, or was this just something you came up with while looking at the 3.18rc hang problem?

This is just me playing with the undefined behaviour/gcc5 patch and trinity, it doesn't have anything to do with the hang problem.

Ok, just curious. Thanks!
-john

#4 lore raw
Quoted 32 lines · John Stultz
Quoted 31 lines · Sasha Levin
Quoted 29 lines · John Stultz
Quoted 14 lines · Sasha Levin

Verify that the frequency value from userspace is valid and makes sense.

Unverified values can cause overflows later on.

---
 kernel/time/ntp.c |    9 +++++++++
 1 file changed, 9 insertions(+)
kernel/time/ntp.c +0 -0
index 87a346f..54828cf 100644

kernel/time/ntp.c int ntp_validate_timex(struct timex *txc) · line 638

Quoted 6 lines
kernel/time/ntp.c +3 -0
@@ -633,6 +633,15 @@ int ntp_validate_timex(struct timex *txc)
        if ((txc->modes & ADJ_SETOFFSET) && (!capable(CAP_SYS_TIME)))
                return -EPERM;
 
+       if (txc->modes & ADJ_FREQUENCY) {
+               if (!capable(CAP_SYS_TIME))
+                       return -EPERM;

So does this actually change behavior? We check CAP_SYS_TIME if modes is set to anything a few lines above (with the exception of ADJ_ADJTIME which only allows for ADJ_OFFSET_SINGLESHOT or ADJ_OFFSET_READONLY).

Granted, that logic isn't intuitive to read (and probably needs a cleanup) but seems ok.

Quoted 29 lines
Quoted 3 lines

No, it doesn't change behaviour. The logic, as you said, is a mess - so I tried to keep this change (I actually have a few more which look very similar) as readable and safe as possible

Ok, could you maybe just add the (fixed) overflow check in one patch (which we'll need to backport to -stable) and we'll try to do a cleanup of the logic in a separate patch?

Quoted 20 lines
Quoted 4 lines

kernel/time/ntp.c int ntp_validate_timex(struct timex *txc) · line 640

Quoted 2 lines
kernel/time/ntp.c +2 -0
+               if (txc->freq < 0)
+                       return -EINVAL;

? Freq adjustments can be negative.... Am I just missing something here?

No, My bad, this should actually be:

        if (LONG_MIN / PPM_SCALE > txc->freq)
                return -EINVAL;
Quoted 6 lines

kernel/time/ntp.c int ntp_validate_timex(struct timex *txc) · line 643

Quoted 3 lines
kernel/time/ntp.c +3 -0
+               if (LONG_MAX / PPM_SCALE < txc->freq)
+                       return -EINVAL;
+       }

This part seems reasonable though. We bound the output, but overflows could result in negative result when it was specified positive.

The overflows could actually result in being anything, as this is considered undefined behaviour.

Just FYI, I've queued a fixed up version of your patch for testing. Will submit it w/ stable cc'ed once that's done.

thanks
-john