Skip to contentSkip to conversation

Fix for ADJ_SETOFFSET w/ ADJ_NANO

PATCH In review John Stultz 5 participants 9 messages Jan 21, 2016 – Jan 26, 2016

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover Fix for ADJ_SETOFFSET w/ ADJ_NANO

David Herrmann mailed me pointing out that one of the changes that landed in 4.5-rc broke users of ADJ_SETOFFSET when used with ADJ_NANO.

I've implemented a fix to this issue and also introduced more unit tests to validate these going forward.

Thomas: Can you queue the first patch for tip/timers/urgent?

Shuah: The kselftests patch can wait to the next merge window if you'd prefer.

Let me know if you have any thoughts or objections!

thanks
-john

Cc Sasha Levin <[email protected]>
Cc Richard Cochran <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Harald Hoyer <[email protected]>
Cc Kay Sievers <[email protected]>
Cc David Herrmann <[email protected]>
Cc Shuah Khan <[email protected]>

John Stultz (2):
ntp: Fix ADJ_SETOFFSET being used w/ ADJ_NANO
kselftests: timers: Add adjtimex SETOFFSET validity tests

 kernel/time/ntp.c                               |  14 ++-
 tools/testing/selftests/timers/valid-adjtimex.c | 139 +++++++++++++++++++++++-
 2 files changed, 150 insertions(+), 3 deletions(-)
Signature · 1 line
1.9.1
#1 lore raw

PATCH 1/2 ntp: Fix ADJ_SETOFFSET being used w/ ADJ_NANO

Recently, in commit 37cf4dc3370f
("time: Verify time values in adjtimex ADJ_SETOFFSET to avoid overflow") I forgot to check if the timeval being passed was actually a timespec (as is signaled with ADJ_NANO).

This resulted in that patch breaking ADJ_SETOFFSET users who set ADJ_NANO, by rejecting valid timespecs that were compared with valid timeval ranges.

This patch addresses this by checking for the ADJ_NANO flag and using the timepsec check instead in that case.

Cc Sasha Levin <[email protected]>
Cc Richard Cochran <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Harald Hoyer <[email protected]>
Cc Kay Sievers <[email protected]>
Cc David Herrmann <[email protected]>
---
 kernel/time/ntp.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
kernel/time/ntp.c +12 -2
index 36f2ca0..6df8927 100644
@@ -685,8 +685,18 @@ int ntp_validate_timex(struct timex *txc)
 		if (!capable(CAP_SYS_TIME))
 			return -EPERM;
 
-		if (!timeval_inject_offset_valid(&txc->time))
-			return -EINVAL;
+		if (txc->modes & ADJ_NANO) {
+			struct timespec ts;
+
+			ts.tv_sec = txc->time.tv_sec;
+			ts.tv_nsec = txc->time.tv_usec;
+			if (!timespec_inject_offset_valid(&ts))
+				return -EINVAL;
+
+		} else {
+			if (!timeval_inject_offset_valid(&txc->time))
+				return -EINVAL;
+		}
 	}
 
 	/*
Signature · 1 line
1.9.1
#4 lore raw

PATCH 2/2 kselftests: timers: Add adjtimex SETOFFSET validity tests

In reply to John Stultz

Add some simple tests to check both valid and invalid offsets when using adjtimex's ADJ_SETOFFSET method.

Cc Sasha Levin <[email protected]>
Cc Richard Cochran <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Harald Hoyer <[email protected]>
Cc Kay Sievers <[email protected]>
Cc David Herrmann <[email protected]>
Cc Shuah Khan <[email protected]>
---
 tools/testing/selftests/timers/valid-adjtimex.c | 139 +++++++++++++++++++++++-
 1 file changed, 138 insertions(+), 1 deletion(-)
tools/testing/selftests/timers/valid-adjtimex.c +138 -1
index e86d937..60fe3c5 100644
@@ -45,7 +45,17 @@ static inline int ksft_exit_fail(void)
 }
 #endif
 
-#define NSEC_PER_SEC 1000000000L
+#define NSEC_PER_SEC 1000000000LL
+#define USEC_PER_SEC 1000000LL
+
+#define ADJ_SETOFFSET 0x0100
+
+#include <sys/syscall.h>
+static int clock_adjtime(clockid_t id, struct timex *tx)
+{
+	return syscall(__NR_clock_adjtime, id, tx);
+}
+
 
 /* clear NTP time_status & time_state */
 int clear_time_state(void)
@@ -193,10 +203,137 @@ out:
 }
 
 
+int set_offset(long long offset, int use_nano)
+{
+	struct timex tmx = {};
+	int ret;
+
+	tmx.modes = ADJ_SETOFFSET;
+	if (use_nano) {
+		tmx.modes |= ADJ_NANO;
+
+		tmx.time.tv_sec = offset / NSEC_PER_SEC;
+		tmx.time.tv_usec = offset % NSEC_PER_SEC;
+
+		if (offset < 0 && tmx.time.tv_usec) {
+			tmx.time.tv_sec -= 1;
+			tmx.time.tv_usec += NSEC_PER_SEC;
+		}
+	} else {
+		tmx.time.tv_sec = offset / USEC_PER_SEC;
+		tmx.time.tv_usec = offset % USEC_PER_SEC;
+
+		if (offset < 0 && tmx.time.tv_usec) {
+			tmx.time.tv_sec -= 1;
+			tmx.time.tv_usec += USEC_PER_SEC;
+		}
+	}
+
+	ret = clock_adjtime(CLOCK_REALTIME, &tmx);
+	if (ret < 0) {
+		printf("(sec: %ld  usec: %ld) ", tmx.time.tv_sec, tmx.time.tv_usec);
+		printf("[FAIL]\n");
+		return -1;
+	}
+	return 0;
+}
+
+int set_bad_offset(long sec, long usec, int use_nano)
+{
+	struct timex tmx = {};
+	int ret;
+
+	tmx.modes = ADJ_SETOFFSET;
+	if (use_nano)
+		tmx.modes |= ADJ_NANO;
+
+	tmx.time.tv_sec = sec;
+	tmx.time.tv_usec = usec;
+	ret = clock_adjtime(CLOCK_REALTIME, &tmx);
+	if (ret >= 0) {
+		printf("Invalid (sec: %ld  usec: %ld) did not fail! ", tmx.time.tv_sec, tmx.time.tv_usec);
+		printf("[FAIL]\n");
+		return -1;
+	}
+	return 0;
+}
+
+int validate_set_offset(void)
+{
+	printf("Testing ADJ_SETOFFSET... ");
+
+	/* Test valid values */
+	if (set_offset(NSEC_PER_SEC - 1, 1))
+		return -1;
+
+	if (set_offset(-NSEC_PER_SEC + 1, 1))
+		return -1;
+
+	if (set_offset(-NSEC_PER_SEC - 1, 1))
+		return -1;
+
+	if (set_offset(5 * NSEC_PER_SEC, 1))
+		return -1;
+
+	if (set_offset(-5 * NSEC_PER_SEC, 1))
+		return -1;
+
+	if (set_offset(5 * NSEC_PER_SEC + NSEC_PER_SEC / 2, 1))
+		return -1;
+
+	if (set_offset(-5 * NSEC_PER_SEC - NSEC_PER_SEC / 2, 1))
+		return -1;
+
+	if (set_offset(USEC_PER_SEC - 1, 0))
+		return -1;
+
+	if (set_offset(-USEC_PER_SEC + 1, 0))
+		return -1;
+
+	if (set_offset(-USEC_PER_SEC - 1, 0))
+		return -1;
+
+	if (set_offset(5 * USEC_PER_SEC, 0))
+		return -1;
+
+	if (set_offset(-5 * USEC_PER_SEC, 0))
+		return -1;
+
+	if (set_offset(5 * USEC_PER_SEC + USEC_PER_SEC / 2, 0))
+		return -1;
+
+	if (set_offset(-5 * USEC_PER_SEC - USEC_PER_SEC / 2, 0))
+		return -1;
+
+	/* Test invalid values */
+	if (set_bad_offset(0, -1, 1))
+		return -1;
+	if (set_bad_offset(0, -1, 0))
+		return -1;
+	if (set_bad_offset(0, 2 * NSEC_PER_SEC, 1))
+		return -1;
+	if (set_bad_offset(0, 2 * USEC_PER_SEC, 0))
+		return -1;
+	if (set_bad_offset(0, NSEC_PER_SEC, 1))
+		return -1;
+	if (set_bad_offset(0, USEC_PER_SEC, 0))
+		return -1;
+	if (set_bad_offset(0, -NSEC_PER_SEC, 1))
+		return -1;
+	if (set_bad_offset(0, -USEC_PER_SEC, 0))
+		return -1;
+
+	printf("[OK]\n");
+	return 0;
+}
+
 int main(int argc, char **argv)
 {
 	if (validate_freq())
 		return ksft_exit_fail();
 
+	if (validate_set_offset())
+		return ksft_exit_fail();
+
 	return ksft_exit_pass();
 }
Signature · 1 line
1.9.1
#6 lore raw
In reply to John Stultz
Quoted 11 lines · John Stultz

David Herrmann mailed me pointing out that one of the changes that landed in 4.5-rc broke users of ADJ_SETOFFSET when used with ADJ_NANO.

I've implemented a fix to this issue and also introduced more unit tests to validate these going forward.

Thomas: Can you queue the first patch for tip/timers/urgent?

Shuah: The kselftests patch can wait to the next merge window if you'd prefer.

Yeah. Probably it has to wait until the next merge window as this is a new test. I can pull this into linux-kselftest next after merge window closes.

thanks,
-- Shuah

Signature · 5 lines
Shuah Khan
Sr. Linux Kernel Developer
Open Source Innovation Group
Samsung Research America (Silicon Valley)
[email protected] | (970) 217-8978
#7 lore raw
Quoted 8 lines · Shuah Khan
Quoted 7 lines · John Stultz

David Herrmann mailed me pointing out that one of the changes that landed in 4.5-rc broke users of ADJ_SETOFFSET when used with ADJ_NANO.

I've implemented a fix to this issue and also introduced more unit tests to validate these going forward.

Quoted 8 lines
Quoted 4 lines

Thomas: Can you queue the first patch for tip/timers/urgent?

Shuah: The kselftests patch can wait to the next merge window if you'd prefer.

Yeah. Probably it has to wait until the next merge window as this is a new test. I can pull this into linux-kselftest next after merge window closes.

We really should not delay selftests, especially if they have been written along with a fix for a recently detected problem.

Thanks,

tglx

Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] ntp: Fix ADJ_SETOFFSET being used w/ ADJ_NANO

In reply to John Stultz

Hi

Quoted 12 lines · John Stultz

Recently, in commit 37cf4dc3370f
("time: Verify time values in adjtimex ADJ_SETOFFSET to avoid overflow") I forgot to check if the timeval being passed was actually a timespec (as is signaled with ADJ_NANO).

This resulted in that patch breaking ADJ_SETOFFSET users who set ADJ_NANO, by rejecting valid timespecs that were compared with valid timeval ranges.

This patch addresses this by checking for the ADJ_NANO flag and using the timepsec check instead in that case.

Quoted 10 lines
Cc Sasha Levin <[email protected]>
Cc Richard Cochran <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Harald Hoyer <[email protected]>
Cc Kay Sievers <[email protected]>
Cc David Herrmann <[email protected]>

Thanks for picking this up. Looks good to me:

Thanks
David

Quoted 32 lines
---
 kernel/time/ntp.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
kernel/time/ntp.c +12 -3
index 36f2ca0..6df8927 100644
@@ -685,8 +685,18 @@ int ntp_validate_timex(struct timex *txc)
                if (!capable(CAP_SYS_TIME))
                        return -EPERM;
 
-               if (!timeval_inject_offset_valid(&txc->time))
-                       return -EINVAL;
+               if (txc->modes & ADJ_NANO) {
+                       struct timespec ts;
+
+                       ts.tv_sec = txc->time.tv_sec;
+                       ts.tv_nsec = txc->time.tv_usec;
+                       if (!timespec_inject_offset_valid(&ts))
+                               return -EINVAL;
+
+               } else {
+                       if (!timeval_inject_offset_valid(&txc->time))
+                               return -EINVAL;
+               }
        }
 
        /*
--

1.9.1

#3 lore raw

[tip:timers/urgent] ntp: Fix ADJ_SETOFFSET being used w/ ADJ_NANO

In reply to John Stultz
ntp: Fix ADJ_SETOFFSET being used w/ ADJ_NANO
Commit-ID:  dd4e17ab704269bce71402285f5e8b9ac24b1eff
Gitweb:     http://git.kernel.org/tip/dd4e17ab704269bce71402285f5e8b9ac24b1eff
Author:     John Stultz <[email protected]>
AuthorDate: Thu, 21 Jan 2016 15:03:34 -0800
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Fri, 22 Jan 2016 12:01:42 +0100

ntp: Fix ADJ_SETOFFSET being used w/ ADJ_NANO

Recently, in commit 37cf4dc3370f I forgot to check if the timeval being passed was actually a timespec (as is signaled with ADJ_NANO).

This resulted in that patch breaking ADJ_SETOFFSET users who set ADJ_NANO, by rejecting valid timespecs that were compared with valid timeval ranges.

This patch addresses this by checking for the ADJ_NANO flag and using the timepsec check instead in that case.

Fixes 37cf4dc3370f "time: Verify time values in adjtimex ADJ_SETOFFSET to avoid overflow"
Cc Sasha Levin <[email protected]>
Cc Richard Cochran <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc David Herrmann <[email protected]>
---
 kernel/time/ntp.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
kernel/time/ntp.c +12 -2
index 36f2ca0..6df8927 100644
@@ -685,8 +685,18 @@ int ntp_validate_timex(struct timex *txc)
 		if (!capable(CAP_SYS_TIME))
 			return -EPERM;
 
-		if (!timeval_inject_offset_valid(&txc->time))
-			return -EINVAL;
+		if (txc->modes & ADJ_NANO) {
+			struct timespec ts;
+
+			ts.tv_sec = txc->time.tv_sec;
+			ts.tv_nsec = txc->time.tv_usec;
+			if (!timespec_inject_offset_valid(&ts))
+				return -EINVAL;
+
+		} else {
+			if (!timeval_inject_offset_valid(&txc->time))
+				return -EINVAL;
+		}
 	}
 
 	/*
Acked-by
#8 lore raw
In reply to Thomas Gleixner
Quoted 14 lines · Thomas Gleixner
Quoted 13 lines · Shuah Khan
Quoted 11 lines · John Stultz

David Herrmann mailed me pointing out that one of the changes that landed in 4.5-rc broke users of ADJ_SETOFFSET when used with ADJ_NANO.

I've implemented a fix to this issue and also introduced more unit tests to validate these going forward.

Thomas: Can you queue the first patch for tip/timers/urgent?

Shuah: The kselftests patch can wait to the next merge window if you'd prefer.

Quoted 7 lines
Quoted 3 lines

Yeah. Probably it has to wait until the next merge window as this is a new test. I can pull this into linux-kselftest next after merge window closes.

We really should not delay selftests, especially if they have been written along with a fix for a recently detected problem.

Thomas,

Yes. That is why I have "probably" in my response. Could you please fold this test in with your urgent fix, so they can go in together.

thanks,
-- Shuah

Signature · 5 lines
Shuah Khan
Sr. Linux Kernel Developer
Open Source Innovation Group
Samsung Research America (Silicon Valley)
[email protected] | (970) 217-8978
#5 lore raw

[tip:timers/urgent] kselftests: timers: Add adjtimex SETOFFSET validity tests

In reply to John Stultz
kselftests: timers: Add adjtimex SETOFFSET validity tests
Commit-ID:  e03a58c320e1103ebe97bda8ebdfcc5c9829c53f
Gitweb:     http://git.kernel.org/tip/e03a58c320e1103ebe97bda8ebdfcc5c9829c53f
Author:     John Stultz <[email protected]>
AuthorDate: Thu, 21 Jan 2016 15:03:35 -0800
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Tue, 26 Jan 2016 16:26:06 +0100

kselftests: timers: Add adjtimex SETOFFSET validity tests

Add some simple tests to check both valid and invalid offsets when using adjtimex's ADJ_SETOFFSET method.

---
 tools/testing/selftests/timers/valid-adjtimex.c | 139 +++++++++++++++++++++++-
 1 file changed, 138 insertions(+), 1 deletion(-)
tools/testing/selftests/timers/valid-adjtimex.c +138 -1
index e86d937..60fe3c5 100644
@@ -45,7 +45,17 @@ static inline int ksft_exit_fail(void)
 }
 #endif
 
-#define NSEC_PER_SEC 1000000000L
+#define NSEC_PER_SEC 1000000000LL
+#define USEC_PER_SEC 1000000LL
+
+#define ADJ_SETOFFSET 0x0100
+
+#include <sys/syscall.h>
+static int clock_adjtime(clockid_t id, struct timex *tx)
+{
+	return syscall(__NR_clock_adjtime, id, tx);
+}
+
 
 /* clear NTP time_status & time_state */
 int clear_time_state(void)
@@ -193,10 +203,137 @@ out:
 }
 
 
+int set_offset(long long offset, int use_nano)
+{
+	struct timex tmx = {};
+	int ret;
+
+	tmx.modes = ADJ_SETOFFSET;
+	if (use_nano) {
+		tmx.modes |= ADJ_NANO;
+
+		tmx.time.tv_sec = offset / NSEC_PER_SEC;
+		tmx.time.tv_usec = offset % NSEC_PER_SEC;
+
+		if (offset < 0 && tmx.time.tv_usec) {
+			tmx.time.tv_sec -= 1;
+			tmx.time.tv_usec += NSEC_PER_SEC;
+		}
+	} else {
+		tmx.time.tv_sec = offset / USEC_PER_SEC;
+		tmx.time.tv_usec = offset % USEC_PER_SEC;
+
+		if (offset < 0 && tmx.time.tv_usec) {
+			tmx.time.tv_sec -= 1;
+			tmx.time.tv_usec += USEC_PER_SEC;
+		}
+	}
+
+	ret = clock_adjtime(CLOCK_REALTIME, &tmx);
+	if (ret < 0) {
+		printf("(sec: %ld  usec: %ld) ", tmx.time.tv_sec, tmx.time.tv_usec);
+		printf("[FAIL]\n");
+		return -1;
+	}
+	return 0;
+}
+
+int set_bad_offset(long sec, long usec, int use_nano)
+{
+	struct timex tmx = {};
+	int ret;
+
+	tmx.modes = ADJ_SETOFFSET;
+	if (use_nano)
+		tmx.modes |= ADJ_NANO;
+
+	tmx.time.tv_sec = sec;
+	tmx.time.tv_usec = usec;
+	ret = clock_adjtime(CLOCK_REALTIME, &tmx);
+	if (ret >= 0) {
+		printf("Invalid (sec: %ld  usec: %ld) did not fail! ", tmx.time.tv_sec, tmx.time.tv_usec);
+		printf("[FAIL]\n");
+		return -1;
+	}
+	return 0;
+}
+
+int validate_set_offset(void)
+{
+	printf("Testing ADJ_SETOFFSET... ");
+
+	/* Test valid values */
+	if (set_offset(NSEC_PER_SEC - 1, 1))
+		return -1;
+
+	if (set_offset(-NSEC_PER_SEC + 1, 1))
+		return -1;
+
+	if (set_offset(-NSEC_PER_SEC - 1, 1))
+		return -1;
+
+	if (set_offset(5 * NSEC_PER_SEC, 1))
+		return -1;
+
+	if (set_offset(-5 * NSEC_PER_SEC, 1))
+		return -1;
+
+	if (set_offset(5 * NSEC_PER_SEC + NSEC_PER_SEC / 2, 1))
+		return -1;
+
+	if (set_offset(-5 * NSEC_PER_SEC - NSEC_PER_SEC / 2, 1))
+		return -1;
+
+	if (set_offset(USEC_PER_SEC - 1, 0))
+		return -1;
+
+	if (set_offset(-USEC_PER_SEC + 1, 0))
+		return -1;
+
+	if (set_offset(-USEC_PER_SEC - 1, 0))
+		return -1;
+
+	if (set_offset(5 * USEC_PER_SEC, 0))
+		return -1;
+
+	if (set_offset(-5 * USEC_PER_SEC, 0))
+		return -1;
+
+	if (set_offset(5 * USEC_PER_SEC + USEC_PER_SEC / 2, 0))
+		return -1;
+
+	if (set_offset(-5 * USEC_PER_SEC - USEC_PER_SEC / 2, 0))
+		return -1;
+
+	/* Test invalid values */
+	if (set_bad_offset(0, -1, 1))
+		return -1;
+	if (set_bad_offset(0, -1, 0))
+		return -1;
+	if (set_bad_offset(0, 2 * NSEC_PER_SEC, 1))
+		return -1;
+	if (set_bad_offset(0, 2 * USEC_PER_SEC, 0))
+		return -1;
+	if (set_bad_offset(0, NSEC_PER_SEC, 1))
+		return -1;
+	if (set_bad_offset(0, USEC_PER_SEC, 0))
+		return -1;
+	if (set_bad_offset(0, -NSEC_PER_SEC, 1))
+		return -1;
+	if (set_bad_offset(0, -USEC_PER_SEC, 0))
+		return -1;
+
+	printf("[OK]\n");
+	return 0;
+}
+
 int main(int argc, char **argv)
 {
 	if (validate_freq())
 		return ksft_exit_fail();
 
+	if (validate_set_offset())
+		return ksft_exit_fail();
+
 	return ksft_exit_pass();
 }