Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2182855imm; Mon, 28 May 2018 03:26:08 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoYm3lanmf+xlUFtu7U8dQ+/SL9j8EWw9UKoo7aD4JGRNIRMvPf9wlInLs2lytIj9jYWKld X-Received: by 2002:a65:5105:: with SMTP id f5-v6mr10250435pgq.232.1527503168351; Mon, 28 May 2018 03:26:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527503168; cv=none; d=google.com; s=arc-20160816; b=YvyVimEDPvlt9X1VU0YaA0ANh8siUtocWNpdGZuc+D9tMqzq6ph/hAu4whQmkW4RpC EeWElq2PzVCWOwuKKGXpFgK7wiI5QxQHAyrK87Oiy80z1M0rdlm9k09IydATb4a2ZLMm kXkZi4EUQXm2TDV/oRLxgAz78eIJp8LFLib0w3FFoZpSeQD9rJpiSJddjeN6zvye9YzS PnI7WRAWZvoaXnrNn1LB9qpDwmfztkzrKQ2MUFfjbNbP2159jkcffOCoH9K1BPPmWQeQ 48ksl2ZlX9JwvRlA7mNpKPkSjzTzOOw00ODGMNroJdngxHvBr/kLnEbdE4pilji7BiBj HpJw== 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=ns5VA//KzfIGlPaN3QekH2c+UlhJUHZykX/VY+jU0H4=; b=ohcm0cI3QDbkYx/ErHzWl1NsDFac5OMhFVy13JqtGyAKN5gPP2Y0KB9307TK72IFNk F4XhDCGPx+ClxxSwL2t5UWx3m6MPk90F3MNCgkT/+11Ft0q/TbA4Qb3r7oDhBZrhSfFN fk95aArOPJIMNzBaF+k53hg1LxvgyYtfFA4poa+kJLknYGBcnOHITzAGGgXqjMimw4LY R/YdJPYCPudf9mKSAgxgQX7PXZaPWQnjkqPdB2ES3fKaTHQ8pC7lRGy0qmjaoGZIgI6E UwQTaGCx90gYWuBHGUbDVRbscv97wJYS35tZu++ugpMa8i0wLsHZBrCWuJGCzHZfT6nS XMNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=HbZ3PFxc; 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 z16-v6si29129735pfe.255.2018.05.28.03.25.50; Mon, 28 May 2018 03:26:08 -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=HbZ3PFxc; 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 S1034049AbeE1KZp (ORCPT + 99 others); Mon, 28 May 2018 06:25:45 -0400 Received: from mail.kernel.org ([198.145.29.99]:45014 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1033745AbeE1KZl (ORCPT ); Mon, 28 May 2018 06:25:41 -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 2BA65208A1; Mon, 28 May 2018 10:25:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527503140; bh=mCG0MBt46UOZxEuYdkkdhTanPalMigdBbqux9VyBnII=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HbZ3PFxcrzcFZ1VJuwfz2ZyiOODWotFxnnXVrRaQCC9OUkYSLZ59wkD1ibl+aBjIL DP4tGellnNwEcc5du3uGHu7BHjTjh3brBJKmGY7AHj7gNOUXm69aGWmAT4Gl2QQ/TN v0DHc58kl3XtyAz/lYLdp/OyUFUwvAJlXhHSGfmk= 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.4 215/268] usb: gadget: ffs: Execute copy_to_user() with USER_DS set Date: Mon, 28 May 2018 12:03:09 +0200 Message-Id: <20180528100226.608230371@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180528100202.045206534@linuxfoundation.org> References: <20180528100202.045206534@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.4-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 @@ -649,11 +649,15 @@ 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 = copy_to_iter(io_data->buf, ret, &io_data->data); if (ret != io_data->req->actual && iov_iter_count(&io_data->data)) ret = -EFAULT; unuse_mm(io_data->mm); + set_fs(oldfs); } io_data->kiocb->ki_complete(io_data->kiocb, ret, ret);