Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp1977728imm; Thu, 24 May 2018 03:56:18 -0700 (PDT) X-Google-Smtp-Source: AB8JxZptsSZB4jukYlk+utEhLY2PKO/vdTKnfCt0I5IZmFvLPpLPqUouN8DJmwNQJzFxFo2sGNgL X-Received: by 2002:a62:404f:: with SMTP id n76-v6mr6767740pfa.185.1527159378533; Thu, 24 May 2018 03:56:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527159378; cv=none; d=google.com; s=arc-20160816; b=zHz0PQMz4fAa+iivv2inLUJC5Dnp/3rziF+BVJmrf2uqP0dEplSKugqTSHOTWbzs90 tdSs8Z00GtWqWs8CUg/1TUXJkIiELL+2IEvfEdAXHSR795rCAIhVuCXB1/G5sZDwxBYm Ew9xBVMAm1lJbFrZ4DT5RofusBVrpLtQv4ifKX6Gts/PdqMSjNLFAgx1o+ZJNOEtOAVf 5tk4SRYRepAv+TebgKZOV1pouXcfRilVQz2YlQmlRYGePcCn3bDshrR5KRsZOqXKGqPI MVPr7jcCXVgju92+PTvv5iaw04Gpp6dZSC2RGPRCTX1vjDFuhsF1JZhpBxDmW4lbls40 Smiw== 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:dkim-signature :arc-authentication-results; bh=v1Wapt3mgRCQfKYM8ayl9Oxvr8kygthz8egbXPI0cF0=; b=S8CnFioWN1+u02OYQPnxtDZKdSIySJQTFfAMjjqX0CGhrVaUUx+u7qcs40PwnmVmt5 7qoBA6W82nY2I56p0hMUxh7/Iva0wTd43cnExgUwpi3nfDQpsYsalBjfXFrK50p/ffJw tKJ6YwC+sglC77CHpI1EBclXm1r2RX3eyUlkt5YY8ZYOrv89PQAa0+HRQkVOOHad+KJa ppAHf6czp1Vi+Xq2Pb6nnAtmQcpyz9vQNn1WX+6AiJWLowjHrsNJ1n3Hl0XLl+yCXKtC Bz9xW1KGKE+cfTBx/f3CkQGVUfPprUBU8L4uhjPkAJKRiR1X7fYvViNutfQNQieDZsEB McoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=IHhQOFfw; 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 a62-v6si1105262pge.492.2018.05.24.03.56.03; Thu, 24 May 2018 03:56:18 -0700 (PDT) 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; dkim=pass header.i=@kernel.org header.s=default header.b=IHhQOFfw; 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 S1032627AbeEXKzT (ORCPT + 99 others); Thu, 24 May 2018 06:55:19 -0400 Received: from mail.kernel.org ([198.145.29.99]:36496 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S967802AbeEXJ4i (ORCPT ); Thu, 24 May 2018 05:56:38 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 8B1BF208A6; Thu, 24 May 2018 09:56:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527155798; bh=LcnSW3nUrlKKT/14fdYEbMIdkL+Rto4eTIk93xMY3IQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IHhQOFfwxWXhTLjzdxoHbVOwxsy3lolBvxFXaOTBDhAAmmnhkLidHN7x526ICTrsT PYq20YYGcId6XJx728f3blM+xogaKH5jZpILOmwyRqEWev1HtRylKTF9m1358nJz+G +nOd1sWCu2VTx8YnQKFGJ0RvYZrc/ZH0M+u/dq84= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lars-Peter Clausen , Felipe Balbi , Sasha Levin Subject: [PATCH 4.14 077/165] usb: gadget: ffs: Execute copy_to_user() with USER_DS set Date: Thu, 24 May 2018 11:38:03 +0200 Message-Id: <20180524093625.143647534@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180524093621.979359379@linuxfoundation.org> References: <20180524093621.979359379@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review 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.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Lars-Peter Clausen [ Upstream commit 4058ebf33cb0be88ca516f968eda24ab7b6b93e4 ] When using a AIO read() operation on the function FS gadget driver a URB is submitted asynchronously and on URB completion the received data is copied to the userspace buffer associated with the read operation. This is done from a kernel worker thread invoking copy_to_user() (through copy_to_iter()). And while the user space process memory is made available to the kernel thread using use_mm(), some architecture require in addition to this that the operation runs with USER_DS set. Otherwise the userspace memory access will fail. For example on ARM64 with Privileged Access Never (PAN) and User Access Override (UAO) enabled the following crash occurs. Internal error: Accessing user space memory with fs=KERNEL_DS: 9600004f [#1] SMP Modules linked in: CPU: 2 PID: 1636 Comm: kworker/2:1 Not tainted 4.9.0-04081-g8ab2dfb-dirty #487 Hardware name: ZynqMP ZCU102 Rev1.0 (DT) Workqueue: events ffs_user_copy_worker task: ffffffc87afc8080 task.stack: ffffffc87a00c000 PC is at __arch_copy_to_user+0x190/0x220 LR is at copy_to_iter+0x78/0x3c8 [...] [] __arch_copy_to_user+0x190/0x220 [] ffs_user_copy_worker+0x70/0x130 [] process_one_work+0x1dc/0x460 [] worker_thread+0x50/0x4b0 [] kthread+0xd8/0xf0 [] ret_from_fork+0x10/0x50 Address this by placing a set_fs(USER_DS) before of the copy operation and revert it again once the copy operation has finished. This patch is analogous to commit d7ffde35e31a ("vhost: use USER_DS in vhost_worker thread") which addresses the same underlying issue. Signed-off-by: Lars-Peter Clausen Signed-off-by: Felipe Balbi Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- drivers/usb/gadget/function/f_fs.c | 4 ++++ 1 file changed, 4 insertions(+) --- a/drivers/usb/gadget/function/f_fs.c +++ b/drivers/usb/gadget/function/f_fs.c @@ -759,9 +759,13 @@ static void ffs_user_copy_worker(struct bool kiocb_has_eventfd = io_data->kiocb->ki_flags & IOCB_EVENTFD; if (io_data->read && ret > 0) { + mm_segment_t oldfs = get_fs(); + + set_fs(USER_DS); use_mm(io_data->mm); ret = ffs_copy_to_iter(io_data->buf, ret, &io_data->data); unuse_mm(io_data->mm); + set_fs(oldfs); } io_data->kiocb->ki_complete(io_data->kiocb, ret, ret);