Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp412303pxb; Wed, 3 Mar 2021 06:30:37 -0800 (PST) X-Google-Smtp-Source: ABdhPJz9XluXvYiVLJ6l1QXGTJn4upxIWRQKJ4lR2kzq6YpA8+Ow/PUwRj53YcZLHLjwDuQE+x/X X-Received: by 2002:a17:906:7e12:: with SMTP id e18mr17151235ejr.316.1614781837132; Wed, 03 Mar 2021 06:30:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614781837; cv=none; d=google.com; s=arc-20160816; b=ydXEZgDQxH1CdS4sZ1hJTaghnlKoDmS7CCrKetQfVXpYHiqvgZt+cIjzn8W9p4KbFj vCnR8hR/mYPeBlr+TrNIfGIXAMk6IgxP8LHZF6VzP5/NORBc/hXfeljH3UM0pt7Jixhq AucneWMqJEth5gy0YjAEeE1BCBw7Z6gcEroNZVd3+0WYSTod1CbUbiW9sUqiTYDoEqqh F56hSu81mL22DH4pP9aiDV/uVHS1BXNFLkLCdmnwFYjSKgTTaVEkrx1wiXO/gyVZZ3zh FTI9HBVAZmi7QI36230d2wrIccgftPK6SAnJqXG3N6zNvQRbQM8E+WLp2HOGlKkuhFRF 9Hag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=EX1yby/iwDSYpmbsUkvK9kjdUmRzmk6eNwPB3pOqPQE=; b=AwtU3yep71h6yM8CqB2T6KX6RXZpzc3Mz0ZxiSHp1xe46OFpKUQpUMfyeyupaKe8Ga 5veZjDdxtE16WN+4bkGc6EuT+nLMqsQuHv+F4djN/fn0TUmZVgolg/MicjEMZ8lBR7bU rrl+NhREOTO6cpErb2WagPyILtHbfPKgI5ZyTM+AScvzzpF3+ljlfh85NN5iqAbe51or aieAAUMsv+WE+WZ/J+x6a00Wpxt7XpaF/RXaS65nLZ/hD/bh2iEtnqUYocTdgiWr6km7 +uyqr5hp2ZNDf9XYu7msic9D0sqssIgBM83oGc0Vvuft7V1AY/4NaHlzLp4SCGzg7ZkB jrZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Z4Wpotnv; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l20si14526814ejn.375.2021.03.03.06.29.59; Wed, 03 Mar 2021 06:30:37 -0800 (PST) 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=@linuxfoundation.org header.s=korg header.b=Z4Wpotnv; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240960AbhCAVaq (ORCPT + 99 others); Mon, 1 Mar 2021 16:30:46 -0500 Received: from mail.kernel.org ([198.145.29.99]:36604 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238035AbhCARS3 (ORCPT ); Mon, 1 Mar 2021 12:18:29 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1EC9C64EBE; Mon, 1 Mar 2021 16:46:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614617205; bh=GoN/ez8d53JBJoTOnaBAX2eFBXESWRbKo7b1nIeshwA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Z4WpotnvuNlDy+I2ufMpzAKKc0qcrCejZ2RBHimQvFqFYrWP4TKmYBq6ARRbBNXqM a7jpVsUoPo19sw+KNQM8L1Ye2KJ3FIF+T2H3iKhGA1t0UwBZ3Ttf58eC/SxkwbYYLA YKQjfqxCOSP7wDcEZnWvPWotPiPXQFzNJQQ8PENk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Vishal Verma , Dave Jiang , Ira Weiny , Coly Li , Richard Palethorpe , Dan Williams , Sudip Mukherjee Subject: [PATCH 4.19 222/247] libnvdimm/dimm: Avoid race between probe and available_slots_show() Date: Mon, 1 Mar 2021 17:14:02 +0100 Message-Id: <20210301161042.551402638@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210301161031.684018251@linuxfoundation.org> References: <20210301161031.684018251@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Dan Williams commit 7018c897c2f243d4b5f1b94bc6b4831a7eab80fb upstream Richard reports that the following test: (while true; do cat /sys/bus/nd/devices/nmem*/available_slots 2>&1 > /dev/null done) & while true; do for i in $(seq 0 4); do echo nmem$i > /sys/bus/nd/drivers/nvdimm/bind done for i in $(seq 0 4); do echo nmem$i > /sys/bus/nd/drivers/nvdimm/unbind done done ...fails with a crash signature like: divide error: 0000 [#1] SMP KASAN PTI RIP: 0010:nd_label_nfree+0x134/0x1a0 [libnvdimm] [..] Call Trace: available_slots_show+0x4e/0x120 [libnvdimm] dev_attr_show+0x42/0x80 ? memset+0x20/0x40 sysfs_kf_seq_show+0x218/0x410 The root cause is that available_slots_show() consults driver-data, but fails to synchronize against device-unbind setting up a TOCTOU race to access uninitialized memory. Validate driver-data under the device-lock. Fixes: 4d88a97aa9e8 ("libnvdimm, nvdimm: dimm driver and base libnvdimm device-driver infrastructure") Cc: Cc: Vishal Verma Cc: Dave Jiang Cc: Ira Weiny Cc: Coly Li Reported-by: Richard Palethorpe Acked-by: Richard Palethorpe Signed-off-by: Dan Williams [sudip: use device_lock()] Signed-off-by: Sudip Mukherjee Signed-off-by: Greg Kroah-Hartman --- drivers/nvdimm/dimm_devs.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) --- a/drivers/nvdimm/dimm_devs.c +++ b/drivers/nvdimm/dimm_devs.c @@ -359,16 +359,16 @@ static ssize_t state_show(struct device } static DEVICE_ATTR_RO(state); -static ssize_t available_slots_show(struct device *dev, - struct device_attribute *attr, char *buf) +static ssize_t __available_slots_show(struct nvdimm_drvdata *ndd, char *buf) { - struct nvdimm_drvdata *ndd = dev_get_drvdata(dev); + struct device *dev; ssize_t rc; u32 nfree; if (!ndd) return -ENXIO; + dev = ndd->dev; nvdimm_bus_lock(dev); nfree = nd_label_nfree(ndd); if (nfree - 1 > nfree) { @@ -380,6 +380,18 @@ static ssize_t available_slots_show(stru nvdimm_bus_unlock(dev); return rc; } + +static ssize_t available_slots_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + ssize_t rc; + + device_lock(dev); + rc = __available_slots_show(dev_get_drvdata(dev), buf); + device_unlock(dev); + + return rc; +} static DEVICE_ATTR_RO(available_slots); static struct attribute *nvdimm_attributes[] = {