From patchwork Thu Apr 4 10:02:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Carpenter X-Patchwork-Id: 10885339 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8F56717EE for ; Thu, 4 Apr 2019 10:04:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 74C7128691 for ; Thu, 4 Apr 2019 10:04:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 68B4E286E0; Thu, 4 Apr 2019 10:04:47 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F124728691 for ; Thu, 4 Apr 2019 10:04:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728955AbfDDKEq (ORCPT ); Thu, 4 Apr 2019 06:04:46 -0400 Received: from aserp2130.oracle.com ([141.146.126.79]:51944 "EHLO aserp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728930AbfDDKEp (ORCPT ); Thu, 4 Apr 2019 06:04:45 -0400 Received: from pps.filterd (aserp2130.oracle.com [127.0.0.1]) by aserp2130.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x34A3mXF158589; Thu, 4 Apr 2019 10:04:33 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=date : from : to : cc : subject : message-id : mime-version : content-type; s=corp-2018-07-02; bh=7PcHI9HmkoOGUNLPVEDRBmxeIfCM+qtgtsTpdsPRKN0=; b=qp7OlwdvJ+C95CWG6NmlsUyaQB6BBneaEYdW1uJbxB6q2E00aJoPt7KZO1Hq90vSBDA2 6dJ4ecCquYZGM4boqonr1R/QD5KxoDudr8zKjq+KPzo/bCIN308T57J3DpadsCv1yUBG WwvgHF7I7ppJphXQGJX01ur/fnr2zH4yYd0umk0u4+1caMB7O+rnsZzG9gBgkq2ytgxx eU9jTgpCdz3ulpnPkBKebgpfSuapK3LZZo2QRpQHHIY/k6LLwoc8xIxQeixT1qDVRQD6 XTOMk3ymZwVvNz738lxiBlVsMblpxFbDX/1jpFXy1icfdzy4u+OcsUaDmKj+jQ64FYBf fg== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by aserp2130.oracle.com with ESMTP id 2rhwydea30-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 04 Apr 2019 10:04:33 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x34A2SWX074919; Thu, 4 Apr 2019 10:02:33 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by aserp3030.oracle.com with ESMTP id 2rm8f5tn9f-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 04 Apr 2019 10:02:32 +0000 Received: from abhmp0001.oracle.com (abhmp0001.oracle.com [141.146.116.7]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id x34A2UOl000999; Thu, 4 Apr 2019 10:02:31 GMT Received: from kadam (/41.202.241.37) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 04 Apr 2019 03:02:29 -0700 Date: Thu, 4 Apr 2019 13:02:19 +0300 From: Dan Carpenter To: "David S. Miller" , Alexander Viro , Jens Axboe Cc: Amritha Nambiar , Willem de Bruijn , kernel-janitors@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Peter Zijlstra Subject: [PATCH 1/2] cpumask: Introduce possible_cpu_safe() Message-ID: <20190404100218.GA26946@kadam> MIME-Version: 1.0 Content-Disposition: inline X-Mailer: git-send-email haha only kidding User-Agent: Mutt/1.9.4 (2018-02-28) X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9216 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=981 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1904040071 X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9216 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1011 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1904040071 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP There have been two cases recently where we pass user a controlled "cpu" to possible_cpus(). That's not allowed. If it's invalid, it will trigger a WARN_ONCE() and an out of bounds read which could result in an Oops. This patch introduces possible_cpu_safe() which first checks to see if the cpu is valid, turns off speculation and then checks if the cpu is possible. Signed-off-by: Dan Carpenter --- include/linux/cpumask.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index 147bdec42215..515179760c54 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -11,6 +11,7 @@ #include #include #include +#include /* Don't assign or return these: may not be this big! */ typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t; @@ -102,6 +103,7 @@ extern struct cpumask __cpu_active_mask; #define num_active_cpus() cpumask_weight(cpu_active_mask) #define cpu_online(cpu) cpumask_test_cpu((cpu), cpu_online_mask) #define cpu_possible(cpu) cpumask_test_cpu((cpu), cpu_possible_mask) +#define cpu_possible_safe(cpu) cpumask_test_cpu_safe((cpu), cpu_possible_mask) #define cpu_present(cpu) cpumask_test_cpu((cpu), cpu_present_mask) #define cpu_active(cpu) cpumask_test_cpu((cpu), cpu_active_mask) #else @@ -111,6 +113,7 @@ extern struct cpumask __cpu_active_mask; #define num_active_cpus() 1U #define cpu_online(cpu) ((cpu) == 0) #define cpu_possible(cpu) ((cpu) == 0) +#define cpu_possible_safe(cpu) ((cpu) == 0) #define cpu_present(cpu) ((cpu) == 0) #define cpu_active(cpu) ((cpu) == 0) #endif @@ -344,6 +347,21 @@ static inline int cpumask_test_cpu(int cpu, const struct cpumask *cpumask) return test_bit(cpumask_check(cpu), cpumask_bits((cpumask))); } +/** + * cpumask_test_cpu_safe - test for a cpu in a cpumask + * @cpu: cpu number + * @cpumask: the cpumask pointer + * + * Returns 1 if @cpu is valid and set in @cpumask, else returns 0 + */ +static inline int cpumask_test_cpu_safe(int cpu, const struct cpumask *cpumask) +{ + if ((unsigned int)cpu >= nr_cpu_ids) + return 0; + cpu = array_index_nospec(cpu, NR_CPUS); + return test_bit(cpu, cpumask_bits(cpumask)); +} + /** * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask * @cpu: cpu number (< nr_cpu_ids) From patchwork Thu Apr 4 10:04:05 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Carpenter X-Patchwork-Id: 10885345 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1B49817EE for ; Thu, 4 Apr 2019 10:05:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 026BB28691 for ; Thu, 4 Apr 2019 10:05:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EA6D9289EC; Thu, 4 Apr 2019 10:05:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0598428691 for ; Thu, 4 Apr 2019 10:05:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728721AbfDDKEn (ORCPT ); Thu, 4 Apr 2019 06:04:43 -0400 Received: from userp2120.oracle.com ([156.151.31.85]:56636 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727053AbfDDKEn (ORCPT ); Thu, 4 Apr 2019 06:04:43 -0400 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x34A3g0T163450; Thu, 4 Apr 2019 10:04:19 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=date : from : to : cc : subject : message-id : mime-version : content-type : in-reply-to; s=corp-2018-07-02; bh=HSJ9MRGkEWjkSpig+MiLw4kI99Yke2oEBvlJf/AcmLI=; b=oxKjwQfKirZPqLt1ep3xl/95PfZFiziJdrVeryB1uswQpzco53m8xL35yA3XBXWovcWc ldwGnY7QbGJdZAKgIaAzxzI38CftnU1jrMv2mQic2KK7XfwXuis+1D+iz9WXebb1M9Qc 0+SX9hROe+lYNCM3B29552TRf3AKAikv/AeeD/XF2fOfBpjsckDmiheygoHbSLEXg80H LTexR9Kf9L43xTNNYoyj8dRP+XHx0x2g2/mOHHQZepdxUlqQ4BX7/UTn7jxh9+bMTjCT CIxQvD5IzD5ckzSdhyPfvEoG1otdElg+XiNcVyLcAIW/AoTd8TxAqIswAu9C8GdHCWj7 9A== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by userp2120.oracle.com with ESMTP id 2rj13qe22w-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 04 Apr 2019 10:04:18 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x34A3Sln117333; Thu, 4 Apr 2019 10:04:18 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by aserp3020.oracle.com with ESMTP id 2rm9mjh72c-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 04 Apr 2019 10:04:17 +0000 Received: from abhmp0020.oracle.com (abhmp0020.oracle.com [141.146.116.26]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id x34A4HKc003367; Thu, 4 Apr 2019 10:04:17 GMT Received: from kadam (/41.202.241.37) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 04 Apr 2019 03:04:17 -0700 Date: Thu, 4 Apr 2019 13:04:05 +0300 From: Dan Carpenter To: "David S. Miller" , Alexander Viro , Jens Axboe Cc: Amritha Nambiar , Willem de Bruijn , kernel-janitors@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Peter Zijlstra Subject: [PATCH 2/2] io_uring: Potential Oops in io_sq_offload_start() Message-ID: <20190404100405.GA27094@kadam> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20190404100218.GA26946@kadam> X-Mailer: git-send-email haha only kidding User-Agent: Mutt/1.9.4 (2018-02-28) X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9216 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1904040071 X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9216 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1011 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1904040071 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The "p->sq_thread_cpu" variable is an integer which comes from the user. We can't pass values greater than NR_CPUS to cpu_possible() or it could lead to an out of bound read and possibly an Oops. The recently added cpu_possible_safe() function can handle unfiltered input so lets use that instead. This patch also removes the call to array_index_nospec() since that is handled in cpu_possible_safe(). Fixes: 6c271ce2f1d5 ("io_uring: add submission polling") Signed-off-by: Dan Carpenter --- fs/io_uring.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index c0fecb9d889f..9789887b0d36 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -2240,16 +2240,13 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx, ctx->sq_thread_idle = HZ; ret = -EINVAL; - if (!cpu_possible(p->sq_thread_cpu)) + if (!cpu_possible_safe(p->sq_thread_cpu)) goto err; if (ctx->flags & IORING_SETUP_SQPOLL) { if (p->flags & IORING_SETUP_SQ_AFF) { - int cpu; - - cpu = array_index_nospec(p->sq_thread_cpu, NR_CPUS); ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, - ctx, cpu, + ctx, p->sq_thread_cpu, "io_uring-sq"); } else { ctx->sqo_thread = kthread_create(io_sq_thread, ctx,