Skip to contentSkip to conversation

time: tip/timers/urgent: Validate potential mult overflows

PATCH Superseded John Stultz 2 participants 4 messages Jan 2, 2015 – Jan 3, 2015

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#1 lore raw

PATCH 1/2 time: settimeofday: Validate the values of tv from user

In reply to John Stultz

From: Sasha Levin <[email protected]>

An unvalidated user input is multiplied by a constant, which can result in an undefined behaviour for large values. While this is validated later, we should avoid triggering undefined behaviour.

---
 include/linux/time.h | 13 +++++++++++++
 kernel/time/time.c   |  4 ++++
 2 files changed, 17 insertions(+)
include/linux/time.h +13 -0
index 8c42cf8..7a10ec1 100644
@@ -99,6 +99,19 @@ static inline bool timespec_valid_strict(const struct timespec *ts)
 	return true;
 }
 
+static inline bool timeval_valid(const struct timeval *tv)
+{
+	/* Dates before 1970 are bogus */
+	if (tv->tv_sec < 0)
+		return false;
+
+	/* Can't have more miliseconds then a second */
+	if (tv->tv_usec < 0 || tv->tv_usec >= USEC_PER_SEC)
+		return false;
+
+	return true;
+}
+
 extern struct timespec timespec_trunc(struct timespec t, unsigned gran);
 
 #define CURRENT_TIME		(current_kernel_time())
kernel/time/time.c +4 -0
index a9ae20f..22d5d3b 100644
@@ -196,6 +196,10 @@ SYSCALL_DEFINE2(settimeofday, struct timeval __user *, tv,
 	if (tv) {
 		if (copy_from_user(&user_tv, tv, sizeof(*tv)))
 			return -EFAULT;
+
+		if (!timeval_valid(&user_tv))
+			return -EINVAL;
+
 		new_ts.tv_sec = user_tv.tv_sec;
 		new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC;
 	}
Signature · 1 line
1.9.1
#3 lore raw

PATCH 2/2 time: adjtimex: Validate the ADJ_FREQUENCY values

In reply to John Stultz

From: Sasha Levin <[email protected]>

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

Unverified values can cause overflows later on.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc stable <[email protected]>
[jstultz: Fix up bug for negative values and drop redunent cap check]
---
 kernel/time/ntp.c | 7 +++++++
 1 file changed, 7 insertions(+)
kernel/time/ntp.c +7 -0
index 87a346f..28bf91c 100644
@@ -633,6 +633,13 @@ int ntp_validate_timex(struct timex *txc)
 	if ((txc->modes & ADJ_SETOFFSET) && (!capable(CAP_SYS_TIME)))
 		return -EPERM;
 
+	if (txc->modes & ADJ_FREQUENCY) {
+		if (LONG_MIN / PPM_SCALE > txc->freq)
+			return -EINVAL;
+		if (LONG_MAX / PPM_SCALE < txc->freq)
+			return -EINVAL;
+	}
+
 	return 0;
 }
 
Signature · 1 line
1.9.1
#0 lore raw

cover time: tip/timers/urgent: Validate potential mult overflows

Hey Ingo, Thomas,

Here are two mult overflow validation fixes from Sasha for
tip/timers/urgent that I didn't manage to send out before the holidays.

Let me know if you have any objections.

thanks
-john

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Sasha Levin <[email protected]>

If you want they can be pulled via:

The following changes since commit b2776bf7149bddd1f4161f14f79520f17fc1d71d:

Linux 3.18 (2014-12-07 14:21:05 -0800)

are available in the git repository at:

https://git.linaro.org/people/john.stultz/linux.git fortglx/3.19-stable/time

for you to fetch changes up to ac1e640cd8d3f5baf48103fdbf794788034bf50c:

time: adjtimex: Validate the ADJ_FREQUENCY values (2014-12-19 12:07:10 -0800)

----------------------------------------------------------------
Sasha Levin (2):
time: settimeofday: Validate the values of tv from user
time: adjtimex: Validate the ADJ_FREQUENCY values

 include/linux/time.h | 13 +++++++++++++
 kernel/time/ntp.c    |  7 +++++++
 kernel/time/time.c   |  4 ++++
 3 files changed, 24 insertions(+)
Signature · 1 line
1.9.1
#2 lore raw

Re: [PATCH 1/2] time: settimeofday: Validate the values of tv from user

In reply to John Stultz
Quoted 21 lines · John Stultz

From: Sasha Levin <[email protected]>

An unvalidated user input is multiplied by a constant, which can result in an undefined behaviour for large values. While this is validated later, we should avoid triggering undefined behaviour.

---
 include/linux/time.h | 13 +++++++++++++
 kernel/time/time.c   |  4 ++++
 2 files changed, 17 insertions(+)
include/linux/time.h +0 -0
index 8c42cf8..7a10ec1 100644

include/linux/time.h static inline bool timespec_valid_strict(const struct timespec *ts) · line 108

Quoted 10 lines
include/linux/time.h +7 -0
@@ -99,6 +99,19 @@ static inline bool timespec_valid_strict(const struct timespec *ts)
 	return true;
 }
 
+static inline bool timeval_valid(const struct timeval *tv)
+{
+	/* Dates before 1970 are bogus */
+	if (tv->tv_sec < 0)
+		return false;
+
+	/* Can't have more miliseconds then a second */

Trivial nit: that should be "microseconds".

--Andy

Quoted 25 lines
include/linux/time.h +6 -0
+	if (tv->tv_usec < 0 || tv->tv_usec >= USEC_PER_SEC)
+		return false;
+
+	return true;
+}
+
 extern struct timespec timespec_trunc(struct timespec t, unsigned gran);
 
 #define CURRENT_TIME		(current_kernel_time())
kernel/time/time.c +4 -0
index a9ae20f..22d5d3b 100644
@@ -196,6 +196,10 @@ SYSCALL_DEFINE2(settimeofday, struct timeval __user *, tv,
 	if (tv) {
 		if (copy_from_user(&user_tv, tv, sizeof(*tv)))
 			return -EFAULT;
+
+		if (!timeval_valid(&user_tv))
+			return -EINVAL;
+
 		new_ts.tv_sec = user_tv.tv_sec;
 		new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC;
 	}