Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3653194pxf; Mon, 5 Apr 2021 18:46:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzlK4OzhjO2X2BHf65id7sVuoRk0NQrZZL9Q1ZYWtD0VeAyFHhyqMgVJfW5GmuNYUEGMQtR X-Received: by 2002:a05:6e02:1a6f:: with SMTP id w15mr22292664ilv.26.1617673619680; Mon, 05 Apr 2021 18:46:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617673619; cv=none; d=google.com; s=arc-20160816; b=O8WKUhVwZnOA8SC5vLbEFaVdrFzaVv2roNoqTBHY8HLGck9tDK8MrWRVmBO3zJLikL 45BSkGs0S9ydDFt4hyXXXocBQPc0Q3/5nR0kAbXWFbPqBYCjFG7ZJoXHSiJF/d76PocY gCDaTHpPVXQ/17v+VLBbw3tljjG2HpOrbejakakGJt7W0LUoNNzFR5hVNr4lh4EDOjqA qcENzFSTI+VvZcz+QF61QVsr9mWPVSiiOp4p3W7TcK0XvzbD/8FcSb6Vy34Cgndxys4I TH8AkUuFjEbi0Lh56bTi6/hliwUbVpoG6iU3pkaMIyGRAIRQJhKe1FoTO1FwV34qedef nwMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=XdA1YfJSMOJpqyDuOz5w86zSQQjVtdHjeMOx+PtEa8I=; b=L8e7L+8B+sBn/96If7e57YDszRuCNq1DknCVn1nJg+J8a+7WnHy0q4zjDUS6zQtW9h A/DFyhkS09vTGrOF9YcX6+h76bF8XbmgK5GRpK8ZkbHFqu3rvKLiu6GH47G+Otd3G6ar CeQERcbg142+Ez60DLLvoegz5eVi1EWTmlHpDgBzq95xxq46JnJRqtAFTdPmcTKn14cl 7FkK/laJq6HYHfTirPO6+fWZpaAEZmOPvsb4PfXMJ3azMRdaFfKRhlzh23RAo1wFVLpo qgulhLdwuB8J12nUAmDWmwDSgJCmxNKUBScDPYpj+wFpblGDAgEGqigWDjsR+5pWKMZr 7cfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="KPERcj/Z"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x8si17096239jao.12.2021.04.05.18.46.47; Mon, 05 Apr 2021 18:46:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="KPERcj/Z"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242228AbhDEQFL (ORCPT + 99 others); Mon, 5 Apr 2021 12:05:11 -0400 Received: from mail.kernel.org ([198.145.29.99]:56144 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241953AbhDEQEf (ORCPT ); Mon, 5 Apr 2021 12:04:35 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1D867613CB; Mon, 5 Apr 2021 16:04:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1617638668; bh=FmeEp9DlJn1O6l/gDwyedOEMenAYz7+FkaYusJwSGo0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KPERcj/ZTr60rXzkvluvhEXJVzd8pYT7+OxCeUS4DoD0FqWN9Q7vFqwhpODdECW+p vxjZhp8bQj1nuMw+k+fEXeR9lFJ81rKW83ajbYhP+Nw+K0JrFdTEw0hRvcHuN+N2Ov MPPmo7iIrGLOAGnqxy3eHHHcdNfYnT0ezN981mhbHwxpgo7qdWul8OLGR5y7WwbyIx Ahh2b/f4w0OfyXF2o7nJwGbvWwCJZrXXPZ/H8g69k0r31mhdDMFo6iZne06jjIJOA9 SeqWb7SL/rpINaRRHPbjbJ7Mgk7ZMfhNbaENmay3WX46UT8rN1cO3gnZzO5ihChRPc BxUVlG+x1y4Qg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Ben Dooks , syzbot+e74b94fe601ab9552d69@syzkaller.appspotmail.com, Arnd Bergman , Palmer Dabbelt , Sasha Levin , linux-riscv@lists.infradead.org Subject: [PATCH AUTOSEL 5.11 20/22] riscv: evaluate put_user() arg before enabling user access Date: Mon, 5 Apr 2021 12:04:03 -0400 Message-Id: <20210405160406.268132-20-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210405160406.268132-1-sashal@kernel.org> References: <20210405160406.268132-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ben Dooks [ Upstream commit 285a76bb2cf51b0c74c634f2aaccdb93e1f2a359 ] The header has a problem with put_user(a, ptr) if the 'a' is not a simple variable, such as a function. This can lead to the compiler producing code as so: 1: enable_user_access() 2: evaluate 'a' into register 'r' 3: put 'r' to 'ptr' 4: disable_user_acess() The issue is that 'a' is now being evaluated with the user memory protections disabled. So we try and force the evaulation by assigning 'x' to __val at the start, and hoping the compiler barriers in enable_user_access() do the job of ordering step 2 before step 1. This has shown up in a bug where 'a' sleeps and thus schedules out and loses the SR_SUM flag. This isn't sufficient to fully fix, but should reduce the window of opportunity. The first instance of this we found is in scheudle_tail() where the code does: $ less -N kernel/sched/core.c 4263 if (current->set_child_tid) 4264 put_user(task_pid_vnr(current), current->set_child_tid); Here, the task_pid_vnr(current) is called within the block that has enabled the user memory access. This can be made worse with KASAN which makes task_pid_vnr() a rather large call with plenty of opportunity to sleep. Signed-off-by: Ben Dooks Reported-by: syzbot+e74b94fe601ab9552d69@syzkaller.appspotmail.com Suggested-by: Arnd Bergman -- Changes since v1: - fixed formatting and updated the patch description with more info Changes since v2: - fixed commenting on __put_user() (schwab@linux-m68k.org) Change since v3: - fixed RFC in patch title. Should be ready to merge. Signed-off-by: Palmer Dabbelt Signed-off-by: Sasha Levin --- arch/riscv/include/asm/uaccess.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/uaccess.h b/arch/riscv/include/asm/uaccess.h index 824b2c9da75b..f944062c9d99 100644 --- a/arch/riscv/include/asm/uaccess.h +++ b/arch/riscv/include/asm/uaccess.h @@ -306,7 +306,9 @@ do { \ * data types like structures or arrays. * * @ptr must have pointer-to-simple-variable type, and @x must be assignable - * to the result of dereferencing @ptr. + * to the result of dereferencing @ptr. The value of @x is copied to avoid + * re-ordering where @x is evaluated inside the block that enables user-space + * access (thus bypassing user space protection if @x is a function). * * Caller must check the pointer with access_ok() before calling this * function. @@ -316,12 +318,13 @@ do { \ #define __put_user(x, ptr) \ ({ \ __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \ + __typeof__(*__gu_ptr) __val = (x); \ long __pu_err = 0; \ \ __chk_user_ptr(__gu_ptr); \ \ __enable_user_access(); \ - __put_user_nocheck(x, __gu_ptr, __pu_err); \ + __put_user_nocheck(__val, __gu_ptr, __pu_err); \ __disable_user_access(); \ \ __pu_err; \ -- 2.30.2