Received: by 10.223.185.116 with SMTP id b49csp2383438wrg; Thu, 15 Feb 2018 10:46:19 -0800 (PST) X-Google-Smtp-Source: AH8x224rRy5ugCOv5B/ecAGPvf2AlhipI7miUb2NFg1ExZC7aWLdhFTHAgk70iPRP9dMOA5Z7VOf X-Received: by 2002:a17:902:e85:: with SMTP id 5-v6mr3398704plx.208.1518720379649; Thu, 15 Feb 2018 10:46:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518720379; cv=none; d=google.com; s=arc-20160816; b=QG1D3XqqdFwzpd07UIe7WDkwLJZpE8XmKF785Po/h9C7+2y8pmzxhKrKV3EOe6x8Qe EHu0Uh+QNjie84YDz2xmU0dEVaZTNtWwtwwv8Sx6fLQWbcpXk84Js5lFHO9zPXAJOyU/ oJF3O+IfUpSx7N10hSss1B7dW432C16JBPr+gE4kLQbVzDNOV4LoMeW4jkhjeLbLcL4t w2eV6uUVApmd9MQEsRI3R9oVooj9uk4u+0oN4jEEl+gF5DNN8ZoOt1cL5wHTN2GHsck2 U66m3w2yoIJPh2MlxyPhuHoFhKgmg3wt1Pl3FBoWEOeyulkPy5hQ7Rx180YGgSJWfpUr vUxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=nbvXrHZLfDnT1HlWxz1U1Qq6rpEGbd0bG/9ZpkjQb84=; b=uDcWhipyLXWaNaBs1L8ZE/Vt0b91qUseoJg5Ohu06gpMJvtx/crK4efV7YOVjT5iUQ z6+pN1ppi8zdSZfnkBPvwwmai5rtkKUYQ5M2vmfzWwkcIZ7P8i+Vg0qv3sQVWhJmysza Tu/3yjlBWV5oqEDOHzwTUrHCM8MrKYeXZzmdaKTwzYiW9r3gon7qstcu+8Dye3/Hmmu6 +y61CwshPcVSHDNtLfGzMeADSvtWJYwrkMD3nFTYeBtLpsglIsNqnzV761PdZd2U2Apt eSOPvXq8KiicBtzllsLxQwk19Q+HQpD2Ic015a+MCLnv4kWxMOfdhqOhW8hRQMVc5MMe 6abA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 1-v6si1927533pla.496.2018.02.15.10.46.04; Thu, 15 Feb 2018 10:46:19 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1162569AbeBOPXH (ORCPT + 99 others); Thu, 15 Feb 2018 10:23:07 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:50586 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1161910AbeBOPXE (ORCPT ); Thu, 15 Feb 2018 10:23:04 -0500 Received: from localhost (LFbn-1-12258-90.w90-92.abo.wanadoo.fr [90.92.71.90]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 0AA18E8C; Thu, 15 Feb 2018 15:22:33 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Biggers , Dmitry Vyukov , Alexey Dobriyan , Thomas Gleixner , John Stultz Subject: [PATCH 4.4 047/108] posix-timer: Properly check sigevent->sigev_notify Date: Thu, 15 Feb 2018 16:16:44 +0100 Message-Id: <20180215151228.997834157@linuxfoundation.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180215151222.267507937@linuxfoundation.org> References: <20180215151222.267507937@linuxfoundation.org> User-Agent: quilt/0.65 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: Thomas Gleixner commit cef31d9af908243421258f1df35a4a644604efbe upstream. 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 Signed-off-by: Greg Kroah-Hartman --- kernel/time/posix-timers.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -507,17 +507,22 @@ static struct pid *good_sigevent(sigeven { struct task_struct *rtn = current->group_leader; - if ((event->sigev_notify & SIGEV_THREAD_ID ) && - (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) || - !same_thread_group(rtn, current) || - (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL)) + switch (event->sigev_notify) { + case SIGEV_SIGNAL | SIGEV_THREAD_ID: + rtn = 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 <= 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) != SIGEV_NONE) && - ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX))) - return NULL; - - return task_pid(rtn); + } } void posix_timers_register_clock(const clockid_t clock_id, @@ -745,8 +750,7 @@ common_timer_get(struct k_itimer *timr, /* interval timer ? */ if (iv.tv64) cur_setting->it_interval = ktime_to_timespec(iv); - else if (!hrtimer_active(timer) && - (timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) + else if (!hrtimer_active(timer) && timr->it_sigev_notify != SIGEV_NONE) return; now = timer->base->get_time(); @@ -757,7 +761,7 @@ common_timer_get(struct k_itimer *timr, * expiry is > now. */ if (iv.tv64 && (timr->it_requeue_pending & REQUEUE_PENDING || - (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE)) + timr->it_sigev_notify == SIGEV_NONE)) timr->it_overrun += (unsigned int) hrtimer_forward(timer, now, iv); remaining = __hrtimer_expires_remaining_adjusted(timer, now); @@ -767,7 +771,7 @@ common_timer_get(struct k_itimer *timr, * A single shot SIGEV_NONE timer must return 0, when * it is expired ! */ - if ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) + if (timr->it_sigev_notify != SIGEV_NONE) cur_setting->it_value.tv_nsec = 1; } else cur_setting->it_value = ktime_to_timespec(remaining); @@ -865,7 +869,7 @@ common_timer_set(struct k_itimer *timr, timr->it.real.interval = timespec_to_ktime(new_setting->it_interval); /* SIGEV_NONE timers are not queued ! See common_timer_get */ - if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE)) { + if (timr->it_sigev_notify == SIGEV_NONE) { /* Setup correct expiry time for relative timers */ if (mode == HRTIMER_MODE_REL) { hrtimer_add_expires(timer, timer->base->get_time());