Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp198481imj; Thu, 14 Feb 2019 18:30:30 -0800 (PST) X-Google-Smtp-Source: AHgI3Ib1K19s/+TBqAy8MSdhxrtJxRkgq2N9WWgpav0ZGS+sZgtLCPpd8zp4NCG+TL4OcEjYdODD X-Received: by 2002:a62:2e46:: with SMTP id u67mr7295435pfu.3.1550197829924; Thu, 14 Feb 2019 18:30:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550197829; cv=none; d=google.com; s=arc-20160816; b=CH5vpUfk5ow0G18BXxpAYX3WxDasJCXzb9y/bzXY/RHDoHh8cyzjdNrdBOtLH7yjOQ aMhzlGGAfG4+s5ywIWCVCxGUUcHriGX8jjhBPcNC73g0Sa1Clzt3R9fXUrQ47L62nt9P bMDcJUa1bN6hwuK1Xm+zzMzFTfvkGsX74RNINeK0lSYs7y1qku1VIEWs5TMgw32sSmAX w93FX3MsARyWpGBVX2XDuQoL64OWaVmRI+4bJh6JvgbJEn/nAQmgYm4AwQttuTO3B/Pf JYVv0W3ZeeM0KNcFwfTDwXsWJlp8m70lMrsFkaJe349NjA4ho5/fsXrMXufgmlAJE2z2 5TtQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=gt+8qWR3KkvITeL4CxOWKn9aTBq9SqBJYZX8REz5mMc=; b=UzrDZg3kXpiB5EqTODPtnnbwnPulWiiC4Mq3h3Zw7jWipO0coJZb1OKbrHHuPEDzlb 8HGOQfLPYcUX7SKOB+uuyiKkOo3gXY4yIGbU3DVkpS0z1bJ0c8T01gdYMG6VlL2wqRGB b+z0kAiGh2zJ2k3YQoVdSEiOL48DiA8esy+4AL2qMaTapuM7+KcFFLhwDxODvuZorJyy CAjE30oRG/exC5NWl9WL2b7g7CdmD5qd+8TpGyrubA0gkFGEAKiDHIw9PP8ZCXmYYlUi jruks+3sqo6vqW/qFaRtrrmz3EQBGjL6ITygGeFAQ5b4VINpTRCPhSH7PDV7Dw2gYhNc M/oQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=lhfASkJ6; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s2si4054280plp.380.2019.02.14.18.30.14; Thu, 14 Feb 2019 18:30:29 -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=lhfASkJ6; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388840AbfBOCJo (ORCPT + 99 others); Thu, 14 Feb 2019 21:09:44 -0500 Received: from mail.kernel.org ([198.145.29.99]:49846 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388502AbfBOCJk (ORCPT ); Thu, 14 Feb 2019 21:09:40 -0500 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5EFD7206BA; Fri, 15 Feb 2019 02:09:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1550196580; bh=txooyQ8Rpj+IpdBFbXidhxz9DY3Rs8IPSu7DRDsgYtM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lhfASkJ67LOckDRe8T60TQnHqWOINMERR6yxh+aRHV+oCfl/BXNnQMBInjtwlnr+p AGTw5p+akwlBeP55+uam1jddIA/BgNDBDagTe/rW/DTQI0bobzwgu2HLgFtl5M8bH4 bMOVHRlBYPR3Ha4aL6uaMCtwZSGpl5azgpEgCIAo= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Silvio Cesare , Timur Tabi , Nicolin Chen , Mark Brown , Xiubo Li , Fabio Estevam , Dan Carpenter , Kees Cook , Will Deacon , Greg KH , Willy Tarreau , Sasha Levin , linuxppc-dev@lists.ozlabs.org Subject: [PATCH AUTOSEL 4.20 25/77] ASoC: imx-audmux: change snprintf to scnprintf for possible overflow Date: Thu, 14 Feb 2019 21:08:03 -0500 Message-Id: <20190215020855.176727-25-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190215020855.176727-1-sashal@kernel.org> References: <20190215020855.176727-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Silvio Cesare [ Upstream commit c407cd008fd039320d147088b52d0fa34ed3ddcb ] Change snprintf to scnprintf. There are generally two cases where using snprintf causes problems. 1) Uses of size += snprintf(buf, SIZE - size, fmt, ...) In this case, if snprintf would have written more characters than what the buffer size (SIZE) is, then size will end up larger than SIZE. In later uses of snprintf, SIZE - size will result in a negative number, leading to problems. Note that size might already be too large by using size = snprintf before the code reaches a case of size += snprintf. 2) If size is ultimately used as a length parameter for a copy back to user space, then it will potentially allow for a buffer overflow and information disclosure when size is greater than SIZE. When the size is used to index the buffer directly, we can have memory corruption. This also means when size = snprintf... is used, it may also cause problems since size may become large. Copying to userspace is mitigated by the HARDENED_USERCOPY kernel configuration. The solution to these issues is to use scnprintf which returns the number of characters actually written to the buffer, so the size variable will never exceed SIZE. Signed-off-by: Silvio Cesare Cc: Timur Tabi Cc: Nicolin Chen Cc: Mark Brown Cc: Xiubo Li Cc: Fabio Estevam Cc: Dan Carpenter Cc: Kees Cook Cc: Will Deacon Cc: Greg KH Signed-off-by: Willy Tarreau Acked-by: Nicolin Chen Reviewed-by: Kees Cook Signed-off-by: Mark Brown Signed-off-by: Sasha Levin --- sound/soc/fsl/imx-audmux.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/sound/soc/fsl/imx-audmux.c b/sound/soc/fsl/imx-audmux.c index 392d5eef356d..99e07b01a2ce 100644 --- a/sound/soc/fsl/imx-audmux.c +++ b/sound/soc/fsl/imx-audmux.c @@ -86,49 +86,49 @@ static ssize_t audmux_read_file(struct file *file, char __user *user_buf, if (!buf) return -ENOMEM; - ret = snprintf(buf, PAGE_SIZE, "PDCR: %08x\nPTCR: %08x\n", + ret = scnprintf(buf, PAGE_SIZE, "PDCR: %08x\nPTCR: %08x\n", pdcr, ptcr); if (ptcr & IMX_AUDMUX_V2_PTCR_TFSDIR) - ret += snprintf(buf + ret, PAGE_SIZE - ret, + ret += scnprintf(buf + ret, PAGE_SIZE - ret, "TxFS output from %s, ", audmux_port_string((ptcr >> 27) & 0x7)); else - ret += snprintf(buf + ret, PAGE_SIZE - ret, + ret += scnprintf(buf + ret, PAGE_SIZE - ret, "TxFS input, "); if (ptcr & IMX_AUDMUX_V2_PTCR_TCLKDIR) - ret += snprintf(buf + ret, PAGE_SIZE - ret, + ret += scnprintf(buf + ret, PAGE_SIZE - ret, "TxClk output from %s", audmux_port_string((ptcr >> 22) & 0x7)); else - ret += snprintf(buf + ret, PAGE_SIZE - ret, + ret += scnprintf(buf + ret, PAGE_SIZE - ret, "TxClk input"); - ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n"); + ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n"); if (ptcr & IMX_AUDMUX_V2_PTCR_SYN) { - ret += snprintf(buf + ret, PAGE_SIZE - ret, + ret += scnprintf(buf + ret, PAGE_SIZE - ret, "Port is symmetric"); } else { if (ptcr & IMX_AUDMUX_V2_PTCR_RFSDIR) - ret += snprintf(buf + ret, PAGE_SIZE - ret, + ret += scnprintf(buf + ret, PAGE_SIZE - ret, "RxFS output from %s, ", audmux_port_string((ptcr >> 17) & 0x7)); else - ret += snprintf(buf + ret, PAGE_SIZE - ret, + ret += scnprintf(buf + ret, PAGE_SIZE - ret, "RxFS input, "); if (ptcr & IMX_AUDMUX_V2_PTCR_RCLKDIR) - ret += snprintf(buf + ret, PAGE_SIZE - ret, + ret += scnprintf(buf + ret, PAGE_SIZE - ret, "RxClk output from %s", audmux_port_string((ptcr >> 12) & 0x7)); else - ret += snprintf(buf + ret, PAGE_SIZE - ret, + ret += scnprintf(buf + ret, PAGE_SIZE - ret, "RxClk input"); } - ret += snprintf(buf + ret, PAGE_SIZE - ret, + ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\nData received from %s\n", audmux_port_string((pdcr >> 13) & 0x7)); -- 2.19.1