Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp2025208imm; Thu, 24 May 2018 04:43:31 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoCvOxgEqlR3HTilQuOZZ58w4+B5V6GAErktufFTgu20sQBX84VjUaNrJNANdx0XXm8mZVU X-Received: by 2002:a63:af13:: with SMTP id w19-v6mr1368729pge.47.1527162211331; Thu, 24 May 2018 04:43:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527162211; cv=none; d=google.com; s=arc-20160816; b=LNdCHa/PDlAK8dQKYboDO9WgPdQOdjDu3y2sgMu7Rr6rLKx7RpwAetSzl6EngVBjro G/8A/C+c7h3QNKuWG4jbHd2EtgMznfDW83wbMfIButUgaFMm1KT2rImGgOwZEtGBYZMF Sb7NElvLqpTuSwy0VmFr3a5PSQQUEOJY5yfks8Rp3cXxlgnKbm/iG6oJ5aYPowwsjLNA GS0cHMjnj4o9UWcXPjbFy5COZus6eqU8R4XK2X1aZCtD8fDYD+8w+sAoO4zxdfPBVrY+ smACQQc2doAeBGjnRCFNMH2V6ZalycRlaAidfTIgmX3cssKWikOBA427g/ZO0ECGem4H Rzwg== 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=8iwyZDHN5+5RyW2miMw1npPqCC2nCC1qs7Xyf7HrEaw=; b=d2YnB60wjYVwihtm4e9wAy35LJTI2S2YOK9cBXC4zLjaCS3SOPCF2lL7Z4h4SN56hv 21hcOX07BNlwHlnNVaF9/YT/AZ5i6kDEYnAIhgzMGp5q+csJ0p4WGdPH4/89U+Pfis6O wugHi7uYTcuwrspfnGjpWFDP92YhIR1vmoE2uwHee7yWD3li0qrjAuwZWZONeiGwvVA0 qwrXj4v449tuIE0dDH3gtrVSBn0P9XAQ1liPPOdbqMpJJ5TB5f0SkNbqp9SyA5QpHBkC asveYR93zFDOTAe6k5+JOaQLLuvQyWyhJIkQqYz4kFi2D55F8QZ0r+oDIh24KAWtoM9Z 5oCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Ib/rybUr; 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 i6-v6si21749934pfc.186.2018.05.24.04.43.16; Thu, 24 May 2018 04:43:31 -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=Ib/rybUr; 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 S967254AbeEXJsl (ORCPT + 99 others); Thu, 24 May 2018 05:48:41 -0400 Received: from mail.kernel.org ([198.145.29.99]:33968 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S967222AbeEXJsg (ORCPT ); Thu, 24 May 2018 05:48:36 -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 1D97620894; Thu, 24 May 2018 09:48:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527155315; bh=uhksLdlHyUr+kJtJZf2Zn0WXUEp4W/GTiQN34GoOg18=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ib/rybUrpkVyGIQbCNYoj/QrcjIY3ppJ1aN5DUHTnCb5xG1weE7SG5cATpkLUQwM4 Fk7xgVhCY15nSdGlnukP7rzyoDFoH0ZyyjH37sajdE0uORRBa4YBSQlZ0cxTd2+nMH k5wHgKFxBUO41KyMN49b0rAQyowv0PTR9txA12DI= 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.9 42/96] usb: gadget: ffs: Execute copy_to_user() with USER_DS set Date: Thu, 24 May 2018 11:38:25 +0200 Message-Id: <20180524093607.946948369@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180524093605.602125311@linuxfoundation.org> References: <20180524093605.602125311@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.9-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);