Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp685454ybv; Thu, 13 Feb 2020 07:45:00 -0800 (PST) X-Google-Smtp-Source: APXvYqyeRtKAzawiTKAdrliEviNttFoKcMuafxi8yh/LxeIfL6HL1rFghC+xVLHgQfaath/b51IR X-Received: by 2002:a05:6830:114f:: with SMTP id x15mr13359902otq.291.1581608699879; Thu, 13 Feb 2020 07:44:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581608699; cv=none; d=google.com; s=arc-20160816; b=b84fjXumq4Rg3XO8OzITTa1Fn2Vka2gl24gayRoiK7VftutfGSNjrNwK5d2PRq2KA8 bI6FAOrzFrPmBKmPPhvnnCHOFL6onqRTQ2VElhe0h2ha4+S7aAcGOqRaqqr3jBJZmjzD oBFcfG7RIxWOC/mYHuUzyHH+hsI4a8iGOGqMibXeGX+L6hjElMB0/UL88cxnDcQxZiB7 I6LyzC9BmamOfK9WODibyz8xZmeVvmlooe2vm5X6hg52TlJeE47jBH2jcjbKVUFt3IDg v4I7C4bIMe8Ldz9QnAkxPl91S//chZNHSkXcyhTRI37fPlPYoCxXUlFkP8hYdY8Wqwmd KfLA== 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=2oi7o5LD64N5tVrsYyv5vpnqUF8pYpsA25JXOi2lgyg=; b=YhLhJKd67QvNU0Z1+hKDpFNBcFsULQTB3e2FidMa4b4fZWFRqvKK838ZGVP2It4jS8 cmaIiIMtQwCH0dWtN2kf4L2bJ9qMVish08d0TbhzjEG5hGEieyEWlcGShffAYZrdvq+g CyCdj379/dIUlbWHRRN8PDoji540rE90A3olf5TNHHbFI6XE2RxIPE/dvHL2lY4ApK/v DU78WXYj/MutDAdyxQ1K9N3QDJiQ30qo5WdV8J/sDWj8d2d8TIRqC62nOGrK6ZT3F0nA uYceq+xu4JdPi2bK00Aj/e2wzaYymm47tX6ymFbMigt09ihBsw7VUY9a0ivPOHyhpzBu giwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=xtank095; 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 k11si1378149otp.176.2020.02.13.07.44.47; Thu, 13 Feb 2020 07:44:59 -0800 (PST) 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=xtank095; 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 S1729121AbgBMPop (ORCPT + 99 others); Thu, 13 Feb 2020 10:44:45 -0500 Received: from mail.kernel.org ([198.145.29.99]:51704 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728152AbgBMP1i (ORCPT ); Thu, 13 Feb 2020 10:27:38 -0500 Received: from localhost (unknown [104.132.1.104]) (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 0E5E620661; Thu, 13 Feb 2020 15:27:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581607658; bh=1ISwekI7VhLWkjEB92+UJvz6A2OdVzEm3fUzbuydkLk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xtank095oVYxrg8T4czZYnJPg8ihe93ivYWOZ9JibVFy3glUVQja5xOWmxYrAYacF h8am3o8qvKr8DXKJhLNqi9StZaf1NuS35D6gQvt41yqSq2SdinVTT6oB4wZKZzhVXR MmqQ4x/K5hlt85Mki/RsnBVfIni65rs5dCgkQxL0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Tyrel Datwyler , Alexey Kardashevskiy , Michael Ellerman Subject: [PATCH 5.4 57/96] powerpc/pseries/vio: Fix iommu_table use-after-free refcount warning Date: Thu, 13 Feb 2020 07:21:04 -0800 Message-Id: <20200213151901.280644778@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200213151839.156309910@linuxfoundation.org> References: <20200213151839.156309910@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: Tyrel Datwyler commit aff8c8242bc638ba57247ae1ec5f272ac3ed3b92 upstream. Commit e5afdf9dd515 ("powerpc/vfio_spapr_tce: Add reference counting to iommu_table") missed an iommu_table allocation in the pseries vio code. The iommu_table is allocated with kzalloc and as a result the associated kref gets a value of zero. This has the side effect that during a DLPAR remove of the associated virtual IOA the iommu_tce_table_put() triggers a use-after-free underflow warning. Call Trace: [c0000002879e39f0] [c00000000071ecb4] refcount_warn_saturate+0x184/0x190 (unreliable) [c0000002879e3a50] [c0000000000500ac] iommu_tce_table_put+0x9c/0xb0 [c0000002879e3a70] [c0000000000f54e4] vio_dev_release+0x34/0x70 [c0000002879e3aa0] [c00000000087cfa4] device_release+0x54/0xf0 [c0000002879e3b10] [c000000000d64c84] kobject_cleanup+0xa4/0x240 [c0000002879e3b90] [c00000000087d358] put_device+0x28/0x40 [c0000002879e3bb0] [c0000000007a328c] dlpar_remove_slot+0x15c/0x250 [c0000002879e3c50] [c0000000007a348c] remove_slot_store+0xac/0xf0 [c0000002879e3cd0] [c000000000d64220] kobj_attr_store+0x30/0x60 [c0000002879e3cf0] [c0000000004ff13c] sysfs_kf_write+0x6c/0xa0 [c0000002879e3d10] [c0000000004fde4c] kernfs_fop_write+0x18c/0x260 [c0000002879e3d60] [c000000000410f3c] __vfs_write+0x3c/0x70 [c0000002879e3d80] [c000000000415408] vfs_write+0xc8/0x250 [c0000002879e3dd0] [c0000000004157dc] ksys_write+0x7c/0x120 [c0000002879e3e20] [c00000000000b278] system_call+0x5c/0x68 Further, since the refcount was always zero the iommu_tce_table_put() fails to call the iommu_table release function resulting in a leak. Fix this issue be initilizing the iommu_table kref immediately after allocation. Fixes: e5afdf9dd515 ("powerpc/vfio_spapr_tce: Add reference counting to iommu_table") Signed-off-by: Tyrel Datwyler Reviewed-by: Alexey Kardashevskiy Signed-off-by: Michael Ellerman Link: https://lore.kernel.org/r/1579558202-26052-1-git-send-email-tyreld@linux.ibm.com Signed-off-by: Greg Kroah-Hartman --- arch/powerpc/platforms/pseries/vio.c | 2 ++ 1 file changed, 2 insertions(+) --- a/arch/powerpc/platforms/pseries/vio.c +++ b/arch/powerpc/platforms/pseries/vio.c @@ -1176,6 +1176,8 @@ static struct iommu_table *vio_build_iom if (tbl == NULL) return NULL; + kref_init(&tbl->it_kref); + of_parse_dma_window(dev->dev.of_node, dma_window, &tbl->it_index, &offset, &size);