Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp128577ybm; Tue, 26 May 2020 12:29:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyF8XK4x91oXa9yBMPIMUP7juuJ1x7fwawG39smgNbTKq/P7hY6oxbINm42HSmwwtfezbg8 X-Received: by 2002:a17:906:6893:: with SMTP id n19mr2686126ejr.354.1590521342328; Tue, 26 May 2020 12:29:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590521342; cv=none; d=google.com; s=arc-20160816; b=It7OM0VtWXEopIVh0RfeT12Rb0rAfDwA93acUVuMpuioTRI8sBNlDhjvu1OYF6jYrl 5Sgps5K8ArOgytYV0whneCtHXL36RULfIks+OTnFit7abrCMTpoDYaOVh7Ip3xsM5GTq RRJEl0gNsUwFP/Il5xk4LB6EEwJ5u7cEBuS9vRRYT1COBNFxJaGFm5Ts6nM4u00uYgT6 J9sr1+vQclJjxyAV66V6S7xh+TN+aRP1lEjKSMGwy/VeQtI+HmKuRRVcP1U3FjTLWOtQ KuCDXx4Ck0vwyJ+HMgk8VBw6jiLqijd7pREsp+/GcCltLJ02ruo14ZpdePslLX2mhGzJ F+0w== 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=EUTEfu8fsTpIszZqpXP0RLj/scYKpVPur0K/tVu6vzY=; b=W9ucm3AV6pvpEmfFr31GTiRsfpDkdsXO1dRzXz/ROXZw35ak7DlKI1QUxV3y0CETHp T8MZkg5tJ/+DjBx3H+tznmvdqD+u7JSnY8ODXIkoeNYxN0nbgUdJPs0LZHg+4SiVG08n f9tUtVXY4wghQVitscZflbvhKhqD6GXNZwR0UJczHwcIZaOMu2G88bEtfIssG7J/Jkeg phT0IVL+JGMyl+3f92TS6FEg2PEP4pd5OrtU2NGeYu9HdlIMoTbzA/SIUc+ccRAURpbF Fw+PhAa6QISJmP+siR0oepUxOvPOFlQjIkmZFCaGM5MK5qhMLA+7aVHnbeWuX3Poaiaj fpDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=M2z7NTUU; 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 g8si387571eje.444.2020.05.26.12.28.39; Tue, 26 May 2020 12:29:02 -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=M2z7NTUU; 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 S2391648AbgEZT0A (ORCPT + 99 others); Tue, 26 May 2020 15:26:00 -0400 Received: from mail.kernel.org ([198.145.29.99]:34608 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390278AbgEZTGV (ORCPT ); Tue, 26 May 2020 15:06:21 -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 9DC3A20873; Tue, 26 May 2020 19:06:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1590519981; bh=cmVh0OPeml6D5NC6kDs0bsSrpH4XAy+2xRC/rnWJl84=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=M2z7NTUUdWCxY+J/baluyE9whPqhbZYQxiY7Jaj6TVfp63toiQz2Ye4BwLcxLbLaB EW4LYaNe7UfB6ef+xila9f5IXCA6AL0WrC4Bq/JgqD9p1v+5O0ro3Eww7pk4vc56IX D2F+47qin2ONSTPv0FOXHb20yPUPBxvyGkl6HBDY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alexander Monakov , Joerg Roedel , iommu@lists.linux-foundation.org, Joerg Roedel , Sasha Levin Subject: [PATCH 5.4 012/111] iommu/amd: Fix over-read of ACPI UID from IVRS table Date: Tue, 26 May 2020 20:52:30 +0200 Message-Id: <20200526183933.727706104@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200526183932.245016380@linuxfoundation.org> References: <20200526183932.245016380@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: Alexander Monakov [ Upstream commit e461b8c991b9202b007ea2059d953e264240b0c9 ] IVRS parsing code always tries to read 255 bytes from memory when retrieving ACPI device path, and makes an assumption that firmware provides a zero-terminated string. Both of those are bugs: the entry is likely to be shorter than 255 bytes, and zero-termination is not guaranteed. With Acer SF314-42 firmware these issues manifest visibly in dmesg: AMD-Vi: ivrs, add hid:AMDI0020, uid:\_SB.FUR0\xf0\xa5, rdevid:160 AMD-Vi: ivrs, add hid:AMDI0020, uid:\_SB.FUR1\xf0\xa5, rdevid:160 AMD-Vi: ivrs, add hid:AMDI0020, uid:\_SB.FUR2\xf0\xa5, rdevid:160 AMD-Vi: ivrs, add hid:AMDI0020, uid:\_SB.FUR3>\x83e\x8d\x9a\xd1... The first three lines show how the code over-reads adjacent table entries into the UID, and in the last line it even reads garbage data beyond the end of the IVRS table itself. Since each entry has the length of the UID (uidl member of ivhd_entry struct), use that for memcpy, and manually add a zero terminator. Avoid zero-filling hid and uid arrays up front, and instead ensure the uid array is always zero-terminated. No change needed for the hid array, as it was already properly zero-terminated. Fixes: 2a0cb4e2d423c ("iommu/amd: Add new map for storing IVHD dev entry type HID") Signed-off-by: Alexander Monakov Cc: Joerg Roedel Cc: iommu@lists.linux-foundation.org Link: https://lore.kernel.org/r/20200511102352.1831-1-amonakov@ispras.ru Signed-off-by: Joerg Roedel Signed-off-by: Sasha Levin --- drivers/iommu/amd_iommu_init.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/amd_iommu_init.c b/drivers/iommu/amd_iommu_init.c index ef14b00fa94b..135ae5222cf3 100644 --- a/drivers/iommu/amd_iommu_init.c +++ b/drivers/iommu/amd_iommu_init.c @@ -1331,8 +1331,8 @@ static int __init init_iommu_from_acpi(struct amd_iommu *iommu, } case IVHD_DEV_ACPI_HID: { u16 devid; - u8 hid[ACPIHID_HID_LEN] = {0}; - u8 uid[ACPIHID_UID_LEN] = {0}; + u8 hid[ACPIHID_HID_LEN]; + u8 uid[ACPIHID_UID_LEN]; int ret; if (h->type != 0x40) { @@ -1349,6 +1349,7 @@ static int __init init_iommu_from_acpi(struct amd_iommu *iommu, break; } + uid[0] = '\0'; switch (e->uidf) { case UID_NOT_PRESENT: @@ -1363,8 +1364,8 @@ static int __init init_iommu_from_acpi(struct amd_iommu *iommu, break; case UID_IS_CHARACTER: - memcpy(uid, (u8 *)(&e->uid), ACPIHID_UID_LEN - 1); - uid[ACPIHID_UID_LEN - 1] = '\0'; + memcpy(uid, &e->uid, e->uidl); + uid[e->uidl] = '\0'; break; default: -- 2.25.1