Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp4473586ybz; Tue, 28 Apr 2020 11:54:30 -0700 (PDT) X-Google-Smtp-Source: APiQypJWQUemLkfAORLoBEM3UIG6EA+bvP2vIhy5QTgjzcLQyHafGWeJN1JqZGaPlY60E7uUrK1z X-Received: by 2002:a50:bb25:: with SMTP id y34mr23224587ede.237.1588100070439; Tue, 28 Apr 2020 11:54:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588100070; cv=none; d=google.com; s=arc-20160816; b=Qp7z5587YcWrooeINsZ1alLF/ZXlKnpieuKTIUtNSM6woiBNhbrC34CkdQFGSU9ScU N7zefBs8rLUI2tnzeWiCg2C86dICM5X3KEj22hfVe3aL9MWUENHZMsEVTCzzGHz+N+ZL RYHyl/MltgTwzqJzMuznnw7FEMQE2ugWPTn8+7cKeysAOvePUsW9+/cbZ6jOVex6i2uq UdAicrTDDdvp65R5EZ8lvbMAyNbnk2jTaHBKNrlpd1NDggklHTz/qCUlp/hMGeXhplOz ggAe1FpKGAf/ODd6yrVX9Guwzgnx0c3CrBLAYOQHJiWTiFD0gcpDC0/kRK0ICmsnX23K IwVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=h5lRxMwAmcGNCasTDJ5SyLQxgGrRrSSp4Y6OLRTLRow=; b=DegX7uOdRpo/lwQJQJDeTK18cuIXq7FCmVcD6o1dS0AY9X5FT6wg832RCq5ubc9t1A uJ6dxYlNBoPf0wySt4Zvg+m74Tk9WjCAZw47x0wWC9mfH1nJZWsxbNT5cT3LnTtQZA/d QGK80/idi1wtB1k05aagLv8oGlHLwl2Dm3GVo8eChffXmK3YyeusDWJTQdrz0p5KD0XF 7RvXJRW1a1E6lenoh0R5JSv+uSixkX0M8eFti+Qj4+MhQWqjxRgMgRtX625YLjLtc3Hy CA/fNdGxvCwss0C9PJKcu8LSpEdvTmh1OJpS16WE8o++OqVldYQFBC2X0QsT073hNJoT rLSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=BQefQJZG; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b26si2046655edw.447.2020.04.28.11.54.06; Tue, 28 Apr 2020 11:54:30 -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=default header.b=BQefQJZG; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730276AbgD1Swx (ORCPT + 99 others); Tue, 28 Apr 2020 14:52:53 -0400 Received: from mail.kernel.org ([198.145.29.99]:52952 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728662AbgD1Sfn (ORCPT ); Tue, 28 Apr 2020 14:35:43 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 BFAC2217D8; Tue, 28 Apr 2020 18:35:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588098942; bh=00ddRN8yKxFktcawj18Tti3yNrlFwEtVI74FidDtGrE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BQefQJZG5HWWD/9VzFBT/UNP9OT8YGXaipIBC5fpY3KuNpPCERYNegajcCbXqU2f8 /sZS1KPRYrUYgnaRGt1OGimjmeMu6lLtTm+k6SHvNypK8OdxMkT+ffZoZQHkJbFSKw pAgvnKD3g8kuCVqu/bAxGV0SvI9+oVEnBjkYNhCo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Nick Bowler , Christoph Hellwig , Sasha Levin Subject: [PATCH 5.4 034/168] nvme: fix compat address handling in several ioctls Date: Tue, 28 Apr 2020 20:23:28 +0200 Message-Id: <20200428182236.043979962@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200428182231.704304409@linuxfoundation.org> References: <20200428182231.704304409@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Nick Bowler [ Upstream commit c95b708d5fa65b4e51f088ee077d127fd5a57b70 ] On a 32-bit kernel, the upper bits of userspace addresses passed via various ioctls are silently ignored by the nvme driver. However on a 64-bit kernel running a compat task, these upper bits are not ignored and are in fact required to be zero for the ioctls to work. Unfortunately, this difference matters. 32-bit smartctl submits the NVME_IOCTL_ADMIN_CMD ioctl with garbage in these upper bits because it seems the pointer value it puts into the nvme_passthru_cmd structure is sign extended. This works fine on 32-bit kernels but fails on a 64-bit one because (at least on my setup) the addresses smartctl uses are consistently above 2G. For example: # smartctl -x /dev/nvme0n1 smartctl 7.1 2019-12-30 r5022 [x86_64-linux-5.5.11] (local build) Copyright (C) 2002-19, Bruce Allen, Christian Franke, www.smartmontools.org Read NVMe Identify Controller failed: NVME_IOCTL_ADMIN_CMD: Bad address Since changing 32-bit kernels to actually check all of the submitted address bits now would break existing userspace, this patch fixes the compat problem by explicitly zeroing the upper bits in the compat case. This enables 32-bit smartctl to work on a 64-bit kernel. Signed-off-by: Nick Bowler Signed-off-by: Christoph Hellwig Signed-off-by: Sasha Levin --- drivers/nvme/host/core.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c index b8fe42f4b3c5b..f97c48fd3edae 100644 --- a/drivers/nvme/host/core.c +++ b/drivers/nvme/host/core.c @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -1244,6 +1245,18 @@ static void nvme_enable_aen(struct nvme_ctrl *ctrl) queue_work(nvme_wq, &ctrl->async_event_work); } +/* + * Convert integer values from ioctl structures to user pointers, silently + * ignoring the upper bits in the compat case to match behaviour of 32-bit + * kernels. + */ +static void __user *nvme_to_user_ptr(uintptr_t ptrval) +{ + if (in_compat_syscall()) + ptrval = (compat_uptr_t)ptrval; + return (void __user *)ptrval; +} + static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio) { struct nvme_user_io io; @@ -1267,7 +1280,7 @@ static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio) length = (io.nblocks + 1) << ns->lba_shift; meta_len = (io.nblocks + 1) * ns->ms; - metadata = (void __user *)(uintptr_t)io.metadata; + metadata = nvme_to_user_ptr(io.metadata); if (ns->ext) { length += meta_len; @@ -1290,7 +1303,7 @@ static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio) c.rw.appmask = cpu_to_le16(io.appmask); return nvme_submit_user_cmd(ns->queue, &c, - (void __user *)(uintptr_t)io.addr, length, + nvme_to_user_ptr(io.addr), length, metadata, meta_len, lower_32_bits(io.slba), NULL, 0); } @@ -1410,9 +1423,9 @@ static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns, effects = nvme_passthru_start(ctrl, ns, cmd.opcode); status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c, - (void __user *)(uintptr_t)cmd.addr, cmd.data_len, - (void __user *)(uintptr_t)cmd.metadata, - cmd.metadata_len, 0, &result, timeout); + nvme_to_user_ptr(cmd.addr), cmd.data_len, + nvme_to_user_ptr(cmd.metadata), cmd.metadata_len, + 0, &result, timeout); nvme_passthru_end(ctrl, effects); if (status >= 0) { @@ -1457,8 +1470,8 @@ static int nvme_user_cmd64(struct nvme_ctrl *ctrl, struct nvme_ns *ns, effects = nvme_passthru_start(ctrl, ns, cmd.opcode); status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c, - (void __user *)(uintptr_t)cmd.addr, cmd.data_len, - (void __user *)(uintptr_t)cmd.metadata, cmd.metadata_len, + nvme_to_user_ptr(cmd.addr), cmd.data_len, + nvme_to_user_ptr(cmd.metadata), cmd.metadata_len, 0, &cmd.result, timeout); nvme_passthru_end(ctrl, effects); -- 2.20.1