From nobody Wed Sep 3 06:41:48 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2D6CFFC616E for ; Mon, 24 Oct 2022 12:31:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229658AbiJXMbE (ORCPT ); Mon, 24 Oct 2022 08:31:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233870AbiJXM2b (ORCPT ); Mon, 24 Oct 2022 08:28:31 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D4AF386F9D; Mon, 24 Oct 2022 05:02:12 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 117EA612BF; Mon, 24 Oct 2022 12:01:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1B474C433C1; Mon, 24 Oct 2022 12:01:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666612909; bh=ntOrOjNGLafpbrPL0IZcl4RkV1kMjXDeWbs/qhFGiXg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wyTZDyH62gFc1Vda9kVD4hWKSCQy+IroJvSy77meEoAfHNHPGjk72Vqfg2DugOQVq cXbN+z/sl57KXMbcLStkgQj7PE5hloheZ1FKtY9m5t6RNcns9U2OpxHenIwZxSuh+c vv/WOWA39QxVxjeEZLsFW+Zsed3ARxN77K6ILXAM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dan Carpenter , Robin Murphy , Laurent Pinchart , Joerg Roedel , Sasha Levin Subject: [PATCH 4.19 166/229] iommu/omap: Fix buffer overflow in debugfs Date: Mon, 24 Oct 2022 13:31:25 +0200 Message-Id: <20221024113004.450933795@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024112959.085534368@linuxfoundation.org> References: <20221024112959.085534368@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Dan Carpenter [ Upstream commit 184233a5202786b20220acd2d04ddf909ef18f29 ] There are two issues here: 1) The "len" variable needs to be checked before the very first write. Otherwise if omap2_iommu_dump_ctx() with "bytes" less than 32 it is a buffer overflow. 2) The snprintf() function returns the number of bytes that *would* have been copied if there were enough space. But we want to know the number of bytes which were *actually* copied so use scnprintf() instead. Fixes: bd4396f09a4a ("iommu/omap: Consolidate OMAP IOMMU modules") Signed-off-by: Dan Carpenter Reviewed-by: Robin Murphy Reviewed-by: Laurent Pinchart Link: https://lore.kernel.org/r/YuvYh1JbE3v+abd5@kili Signed-off-by: Joerg Roedel Signed-off-by: Sasha Levin --- drivers/iommu/omap-iommu-debug.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/omap-iommu-debug.c b/drivers/iommu/omap-iommu-de= bug.c index 5ce55fabc9d8..726702d01522 100644 --- a/drivers/iommu/omap-iommu-debug.c +++ b/drivers/iommu/omap-iommu-debug.c @@ -35,12 +35,12 @@ static inline bool is_omap_iommu_detached(struct omap_i= ommu *obj) ssize_t bytes; \ const char *str =3D "%20s: %08x\n"; \ const int maxcol =3D 32; \ - bytes =3D snprintf(p, maxcol, str, __stringify(name), \ + if (len < maxcol) \ + goto out; \ + bytes =3D scnprintf(p, maxcol, str, __stringify(name), \ iommu_read_reg(obj, MMU_##name)); \ p +=3D bytes; \ len -=3D bytes; \ - if (len < maxcol) \ - goto out; \ } while (0) =20 static ssize_t --=20 2.35.1