Skip to contentSkip to conversation

time/namespace: Fix overflow when converting absolute deadlines

PATCH Applied Hungyu Lin 2 participants 2 messages Oct 20 – Oct 25

Subsystems (2)
lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

PATCH 1/1 time/namespace: Fix overflow when converting absolute deadlines

do_timens_ktime_to_host() subtracts the namespace offset from an absolute deadline and clamps the result to [0, KTIME_MAX]. A negative offset can make a large deadline overflow to a negative value. Since ktime_t is signed and KTIME_MAX is its maximum value, the subsequent comparison against KTIME_MAX can never detect this overflow.

For example, with a -1 second boottime offset, an absolute CLOCK_BOOTTIME timerfd deadline of KTIME_MAX can expire immediately. An absolute clock_nanosleep() using the same clock and deadline can also return immediately instead of waiting.

The earlier tim < offset check already handles the lower bound. A negative subtraction result in the remaining branch therefore indicates overflow. Detect that result and clamp it to KTIME_MAX.

Fixes 89dd8eecfe961 ("time: Add do_timens_ktime_to_host() helper")
---
 kernel/time/namespace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/namespace.c +1 -1
index 3aff27bb0a15..635f7e544bcc 100644
@@ -51,7 +51,7 @@ ktime_t do_timens_ktime_to_host(clockid_t clockid, ktime_t tim,
 		tim = 0;
 	} else {
 		tim = ktime_sub(tim, offset);
-		if (unlikely(tim > KTIME_MAX))
+		if (unlikely(tim < 0))
 			tim = KTIME_MAX;
 	}
 
Signature · 1 line
2.43.0
merge bot Applied
#1 lore raw
The following commit has been merged into the timers/urgent branch of tip:

The following commit has been merged into the timers/urgent branch of tip:

Commit-ID:     d69cfd33b5a8d176124c564513b9ceb933595d93
Gitweb:        https://git.kernel.org/tip/d69cfd33b5a8d176124c564513b9ceb933595d93
Author:        Hungyu Lin <[email protected]>
AuthorDate:    Tue, 20 Oct 2026 23:34:33 
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Sun, 25 Oct 2026 22:43:19 +01:00

time/namespace: Fix overflow when converting absolute deadlines

do_timens_ktime_to_host() subtracts the namespace offset from an absolute deadline and clamps the result to [0, KTIME_MAX]. A negative offset can make a large deadline overflow to a negative value. Since ktime_t is signed and KTIME_MAX is its maximum value, the subsequent comparison against KTIME_MAX can never detect this overflow.

For example, with a -1 second boottime offset, an absolute CLOCK_BOOTTIME timerfd deadline of KTIME_MAX can expire immediately. An absolute clock_nanosleep() using the same clock and deadline can also return immediately instead of waiting.

The earlier tim < offset check already handles the lower bound. A negative subtraction result in the remaining branch therefore indicates overflow. Detect that result and clamp it to KTIME_MAX.

Fixes 89dd8eecfe961 ("time: Add do_timens_ktime_to_host() helper")
---
 kernel/time/namespace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/namespace.c +1 -1
index 3aff27b..635f7e5 100644
@@ -51,7 +51,7 @@ ktime_t do_timens_ktime_to_host(clockid_t clockid, ktime_t tim,
 		tim = 0;
 	} else {
 		tim = ktime_sub(tim, offset);
-		if (unlikely(tim > KTIME_MAX))
+		if (unlikely(tim < 0))
 			tim = KTIME_MAX;
 	}