Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp679456ybv; Thu, 13 Feb 2020 07:38:49 -0800 (PST) X-Google-Smtp-Source: APXvYqxA3IzjMVdpiyuRPV/phRZfiYSf6ycokeXFtUX8w97kiM3f05BLXskuRqFOSMdYu2wM31VE X-Received: by 2002:aca:4306:: with SMTP id q6mr3341869oia.54.1581608329662; Thu, 13 Feb 2020 07:38:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581608329; cv=none; d=google.com; s=arc-20160816; b=fJTrGZloWD8mWNdjAcD7eHwZTJKvyy5OOw0RY9LCRMaf5uzkEfQ9xMClcWi3Q47iPQ 9aBE7G/BjZdriGg01d3/+08ufa2BHPkHoBKGQv9PTuAmRQsDN8+42yOtTbtSCS1iNjQc wgTKkT1J0wtY+lf72FTJWzekQn8+aY9iBduykF7QPQ5ZvlV961qiQMVdfRHQinovNKyV VZ3WlMVkXZqeVcDqPHpimZLhareQwtLUHblUHG3Rg2xzlhU+PWy1m0AERJuUGITFgChk bHv3Qj6zmTH6WnFPSk0c0RpqJmVNhuI/o1z1Vheeg+ILa3DQGCiq9keYqRQpO1SHb50j cl5Q== 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=7BEDhvpponTugFrWe1izQG/8urURKYAkHlRluXuhN84=; b=AiaiP5UCNQdkrmvjMKCUn1bNe8xToFXr7jLGuvsJVI47YKFhu4a+1O0HzCqNyYSCuJ vF2fDT/YPXdaxB7ZXB5kNcIABxvN7W2ONQsBWCxSZJ/KtaHqKWIXhZjkkywzaE1W2fqK wHRh0j9hDSH/N44X2ckuxwqZrp7fHzEtoirbz2cvKH9NSqesCKBmuLML9upCMXa/v374 o7nyjcAP0vZWUUutKRhI5nCsyfqUCwIUku5M2J2oQ9KHjvnNv0NdUVPyhJmH6WsGSVji 4W04SxWAiQsOEkLldxqciqo0puPA0WH6rBMfegagqz69Mdbcxkaz4QoJzgxwpodvAWEm KnOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=fAETyESs; 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 v1si187448otp.91.2020.02.13.07.38.37; Thu, 13 Feb 2020 07:38:49 -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=fAETyESs; 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 S2387720AbgBMP3X (ORCPT + 99 others); Thu, 13 Feb 2020 10:29:23 -0500 Received: from mail.kernel.org ([198.145.29.99]:46220 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387608AbgBMP0g (ORCPT ); Thu, 13 Feb 2020 10:26:36 -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 C7B18222C2; Thu, 13 Feb 2020 15:26:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581607595; bh=mRN16gy+86886KrXaqWW/kvDoKgPKYfm3OlmM+N+Obk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fAETyESshAOZGrpGaFvya+k+YJYYGcTFmch6NMVVgC4GLB8cp1ExEYg/ZQvggNi3r v0d8B+HJSeH119Ahw/t5lylSeSOFTMEpjLp15kUqvcpbJj2rwhQhZlh2ydW7/AGSkm jK9yEE0sLHholgz/DE6+lwKqOg5IABWYa4un8dSE= 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 4.19 29/52] powerpc/pseries/vio: Fix iommu_table use-after-free refcount warning Date: Thu, 13 Feb 2020 07:21:10 -0800 Message-Id: <20200213151822.007654746@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200213151810.331796857@linuxfoundation.org> References: <20200213151810.331796857@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 @@ -1195,6 +1195,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);