[PATCH v3] hrtimer: check hrtimer with a NULL function

Phil Chang posted 1 patch 1 year, 8 months ago
kernel/time/hrtimer.c | 2 ++
1 file changed, 2 insertions(+)
[PATCH v3] hrtimer: check hrtimer with a NULL function
Posted by Phil Chang 1 year, 8 months ago
To prevent improper usage of hrtimers and avoid potential kernel crashes,
this commit introduces a validation check for hrtimers with a valid function callback,
discard the hrtimers that have a NULL callback.

The `run_hrtimer` executes callbacks for every hrtimer,
and these callbacks must not be NULL. A NULL callback can lead to a kernel crash.
This update ensures that all hrtimers have properly initialized callbacks
before execution.

Signed-off-by: Phil Chang <phil.chang@mediatek.com>
---
 kernel/time/hrtimer.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index 492c14aac642..b8ee320208d4 100644
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -1285,6 +1285,8 @@ void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
 	struct hrtimer_clock_base *base;
 	unsigned long flags;
 
+	if (WARN_ON_ONCE(!timer->function))
+		return;
 	/*
 	 * Check whether the HRTIMER_MODE_SOFT bit and hrtimer.is_soft
 	 * match on CONFIG_PREEMPT_RT = n. With PREEMPT_RT check the hard
-- 
2.18.0
Re: [PATCH v3] hrtimer: check hrtimer with a NULL function
Posted by Anna-Maria Behnsen 1 year, 7 months ago
Phil Chang <phil.chang@mediatek.com> writes:

> To prevent improper usage of hrtimers and avoid potential kernel crashes,
> this commit introduces a validation check for hrtimers with a valid function callback,
> discard the hrtimers that have a NULL callback.
>
> The `run_hrtimer` executes callbacks for every hrtimer,
> and these callbacks must not be NULL. A NULL callback can lead to a kernel crash.
> This update ensures that all hrtimers have properly initialized callbacks
> before execution.

Definitely better! You could sort it, first problem description and then
solution:

The hrtimer function callback must not be NULL. It has to be specified
by the callsite but it is not validated by hrtimer code. When a hrtimer
is queued with a NULL pointer instead of a valid function, the kernel
crashes with a null pointer dereference when trying to execute the
callback in __run_hrtimer().

Introduce a validation before queueing the hrtimer in
hrtimer_start_range_ns().

>
> Signed-off-by: Phil Chang <phil.chang@mediatek.com>

Reviewed-by: Anna-Maria Behnsen <anna-maria@linutronix.de>

> ---
>  kernel/time/hrtimer.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
> index 492c14aac642..b8ee320208d4 100644
> --- a/kernel/time/hrtimer.c
> +++ b/kernel/time/hrtimer.c
> @@ -1285,6 +1285,8 @@ void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
>  	struct hrtimer_clock_base *base;
>  	unsigned long flags;
>  
> +	if (WARN_ON_ONCE(!timer->function))
> +		return;
>  	/*
>  	 * Check whether the HRTIMER_MODE_SOFT bit and hrtimer.is_soft
>  	 * match on CONFIG_PREEMPT_RT = n. With PREEMPT_RT check the hard