Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp1947507imm; Thu, 24 May 2018 03:25:35 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrLMTENde7igMYiDYMXV/kJT6eLNsx/cbKJC0/43Xg6sjwbsbH2Cho6am4ENc/yGyxTV2Ha X-Received: by 2002:a63:7986:: with SMTP id u128-v6mr5326173pgc.127.1527157535200; Thu, 24 May 2018 03:25:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527157535; cv=none; d=google.com; s=arc-20160816; b=0/rcsgg+aw93l6jM+TJalqGIsbcZUEgTSWniLGNB2cR8SAlurHmQ98JKYZLAP7L9tM My7y0UVPmBSl6jHTKQ+bIiZz+ufZof0SHWgbSI/Uv9srT3bg70NcR8Q+jtapmnqF8BnX oNHX1i1s7MTAeHT5Rq+IZzqloHNKU5uevO7/hHyl50mnQXp7U1c8+Ig+R/S0kY9eLsH0 C3xaKO/dQN2UFvh6FFkoBHCbk9CQKVS5ivw6dTrt0O1ayTHp5ToZpVfdzxkSHg5d5lZH fPgsTVJ0/Byxkns4w2zN0nngmuyjjCQYOI1Ik5Mzrv2pQlz6Q02Kh6olWcnzfamyoWuD oNKA== 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=OdR19075Aes9nO5CDpwg1SJI3/i6PYOv9ESHxNM6E2k=; b=ObsiSUcR3ZIuK2HsdX8Q0D9b6FM79bdnO/Xm9C1qMcTkENMxaAvtujEyiFQ3V9BH41 Y7IlfZB7kZKqd+P6Wgag8DiE7q0FkpztmrGKpiTvvLLOZKrubTwtpIn0MxccS6Bi2JfX OagFwcC6GtDuThvcD4qpgtkUo5bFrMY6ykCFgUDZVrg69aqQtu6pQaUKf/J7EmuPXD8O O38PV0zDsvaGiYNqAa2UcHauLWPmm5M+M5qL7SvQ9OcGJR383q/VhLaVYr6+x+fSyAz5 +jQEmiwllhdeUKHglJt7Gglguf0nM3Lgse3eujk8dhSH1A/hBFU2AdU8sr4trB6lrzv4 5Mwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=HNw7SHgp; 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 g2-v6si19522052pll.525.2018.05.24.03.25.20; Thu, 24 May 2018 03:25:35 -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=HNw7SHgp; 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 S1031084AbeEXKCT (ORCPT + 99 others); Thu, 24 May 2018 06:02:19 -0400 Received: from mail.kernel.org ([198.145.29.99]:48532 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1031073AbeEXKCN (ORCPT ); Thu, 24 May 2018 06:02:13 -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 6632720899; Thu, 24 May 2018 10:02:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527156132; bh=NFJvIwdK63Ryya1qw32hi63AV3A2BSUMtHhha0IiTco=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HNw7SHgpvpkqkRMjXwVhW3/ojRuoKT2WM2ox/ppTpbTgpuu2tRGNEe+8yQDTYcgbY scrRNlaD+HXZr832pILLzpZ5ctGA52MshdIfMPtIDcPQMeMaZygvYAM6dCz8sbZPfU crfDRufcpgmmHts89fR56LN5LJQWRBCpF1ROhnxQ= 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.16 076/161] usb: gadget: ffs: Execute copy_to_user() with USER_DS set Date: Thu, 24 May 2018 11:38:21 +0200 Message-Id: <20180524093027.645208181@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180524093018.331893860@linuxfoundation.org> References: <20180524093018.331893860@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.16-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 @@ -758,9 +758,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);