From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-co1nam03on0127.outbound.protection.outlook.com ([104.47.40.127]:41117 "EHLO NAM03-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1032400AbeCAPeG (ORCPT ); Thu, 1 Mar 2018 10:34:06 -0500 From: Sasha Levin To: "stable@vger.kernel.org" , "stable-commits@vger.kernel.org" CC: Thomas Gleixner , Sasha Levin Subject: [added to the 4.1 stable tree] posix-timer: Properly check sigevent->sigev_notify Date: Thu, 1 Mar 2018 15:25:58 +0000 Message-ID: <20180301152116.1486-320-alexander.levin@microsoft.com> References: <20180301152116.1486-1-alexander.levin@microsoft.com> In-Reply-To: <20180301152116.1486-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Thomas Gleixner This patch has been added to the 4.1 stable tree. If you have any objections, please let us know. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [ Upstream commit cef31d9af908243421258f1df35a4a644604efbe ] timer_create() specifies via sigevent->sigev_notify the signal delivery for the new timer. The valid modes are SIGEV_NONE, SIGEV_SIGNAL, SIGEV_THREAD and (SIGEV_SIGNAL | SIGEV_THREAD_ID). The sanity check in good_sigevent() is only checking the valid combination for the SIGEV_THREAD_ID bit, i.e. SIGEV_SIGNAL, but if SIGEV_THREAD_ID is not set it accepts any random value. This has no real effects on the posix timer and signal delivery code, but it affects show_timer() which handles the output of /proc/$PID/timers. That function uses a string array to pretty print sigev_notify. The access to that array has no bound checks, so random sigev_notify cause access beyond the array bounds. Add proper checks for the valid notify modes and remove the SIGEV_THREAD_ID masking from various code pathes as SIGEV_NONE can never be set in combination with SIGEV_THREAD_ID. Reported-by: Eric Biggers Reported-by: Dmitry Vyukov Reported-by: Alexey Dobriyan Signed-off-by: Thomas Gleixner Cc: John Stultz Cc: stable@vger.kernel.org Signed-off-by: Sasha Levin --- kernel/time/posix-timers.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 31ea01f42e1f..2cca2e79c643 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -500,17 +500,22 @@ static struct pid *good_sigevent(sigevent_t * event) { struct task_struct *rtn =3D current->group_leader; =20 - if ((event->sigev_notify & SIGEV_THREAD_ID ) && - (!(rtn =3D find_task_by_vpid(event->sigev_notify_thread_id)) || - !same_thread_group(rtn, current) || - (event->sigev_notify & ~SIGEV_THREAD_ID) !=3D SIGEV_SIGNAL)) + switch (event->sigev_notify) { + case SIGEV_SIGNAL | SIGEV_THREAD_ID: + rtn =3D find_task_by_vpid(event->sigev_notify_thread_id); + if (!rtn || !same_thread_group(rtn, current)) + return NULL; + /* FALLTHRU */ + case SIGEV_SIGNAL: + case SIGEV_THREAD: + if (event->sigev_signo <=3D 0 || event->sigev_signo > SIGRTMAX) + return NULL; + /* FALLTHRU */ + case SIGEV_NONE: + return task_pid(rtn); + default: return NULL; - - if (((event->sigev_notify & ~SIGEV_THREAD_ID) !=3D SIGEV_NONE) && - ((event->sigev_signo <=3D 0) || (event->sigev_signo > SIGRTMAX))) - return NULL; - - return task_pid(rtn); + } } =20 void posix_timers_register_clock(const clockid_t clock_id, @@ -738,8 +743,7 @@ common_timer_get(struct k_itimer *timr, struct itimersp= ec *cur_setting) /* interval timer ? */ if (iv.tv64) cur_setting->it_interval =3D ktime_to_timespec(iv); - else if (!hrtimer_active(timer) && - (timr->it_sigev_notify & ~SIGEV_THREAD_ID) !=3D SIGEV_NONE) + else if (!hrtimer_active(timer) && timr->it_sigev_notify !=3D SIGEV_NONE) return; =20 now =3D timer->base->get_time(); @@ -750,7 +754,7 @@ common_timer_get(struct k_itimer *timr, struct itimersp= ec *cur_setting) * expiry is > now. */ if (iv.tv64 && (timr->it_requeue_pending & REQUEUE_PENDING || - (timr->it_sigev_notify & ~SIGEV_THREAD_ID) =3D=3D SIGEV_NONE)) + timr->it_sigev_notify =3D=3D SIGEV_NONE)) timr->it_overrun +=3D (unsigned int) hrtimer_forward(timer, now, iv); =20 remaining =3D ktime_sub(hrtimer_get_expires(timer), now); @@ -760,7 +764,7 @@ common_timer_get(struct k_itimer *timr, struct itimersp= ec *cur_setting) * A single shot SIGEV_NONE timer must return 0, when * it is expired ! */ - if ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) !=3D SIGEV_NONE) + if (timr->it_sigev_notify !=3D SIGEV_NONE) cur_setting->it_value.tv_nsec =3D 1; } else cur_setting->it_value =3D ktime_to_timespec(remaining); @@ -858,7 +862,7 @@ common_timer_set(struct k_itimer *timr, int flags, timr->it.real.interval =3D timespec_to_ktime(new_setting->it_interval); =20 /* SIGEV_NONE timers are not queued ! See common_timer_get */ - if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) =3D=3D SIGEV_NONE)) { + if (timr->it_sigev_notify =3D=3D SIGEV_NONE) { /* Setup correct expiry time for relative timers */ if (mode =3D=3D HRTIMER_MODE_REL) { hrtimer_add_expires(timer, timer->base->get_time()); --=20 2.14.1