Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp200464imj; Thu, 14 Feb 2019 18:32:56 -0800 (PST) X-Google-Smtp-Source: AHgI3IZDq2nG0St7LCHfBTB0o0Chh10M7X5+4LfENnXYxiOs2fme5jP4QK6pFX3f7dZ3gBmroLZW X-Received: by 2002:a17:902:b60e:: with SMTP id b14mr7372500pls.301.1550197976024; Thu, 14 Feb 2019 18:32:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550197976; cv=none; d=google.com; s=arc-20160816; b=iiXh3USrLMvuGIp9mY//uLhZ4VaCkT1jFzsSSzGN1yL3441vCSkz6GjiabqqETldDV UkdbLBiCVv6a6hnw98822hx14UNUaMzL/azkGzhKMvc4qJhe4v9H1JFI6AE5K9mPnfQz C7bzoKX/67kMIYVWCDaptL/w5UR2pmJNDcHSHSs+v19i3W8t7Rz5cTIxS15a7eYKBZwO akvFs3wzUG2AMiPIS4eqQbYHT6Th0u8HaKFcPgTmvInXfsMt5ynIiQMfmg23IqAm+sCx eznZLLf7TmQ02XMQ9RmnxhlhqIkOUhW3Sc1zUhilL/6sQ5YH7UtdICzDxOFFrCTchc9r ezXw== 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=jnzq7yjfxVqyNv60VD/pFVflv6H/C3VUZzWwmJAXSxQ=; b=n4u4jL7v6yzbx9kF9/CbpZfCP4SK0aLxHtVhLliBUvJqLnjuN+hluNUxSY6q43EO11 POuo0ybjifFpSmnO3gRCW1fAI0NHZ7jcjOnMyRsmkY0XrT7oTWMq0ab54KDNIhB7rr3Z anT63ukL0ZIfgG/1Ob+QQOV7pZqpOgof1JVSDxHMnuOaLE6xKuen1JNsjs4TTpQzyIDE XJW1Tgyw86QYojWnXpBo5D+7sBYB530zC4/LwwRczkdEnEysPOxWra7sJiul6/6OIVK8 GTR+9AtfqCKJI/NvmPKp3XThA7I3RdFjNRuU8Ee/7lK+PmTVu/sl2oXl9jWWUSys56+W RwHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ZppKXPJw; 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 c8si3816515pgq.300.2019.02.14.18.32.40; Thu, 14 Feb 2019 18:32:56 -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=ZppKXPJw; 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 S1732514AbfBOCSm (ORCPT + 99 others); Thu, 14 Feb 2019 21:18:42 -0500 Received: from mail.kernel.org ([198.145.29.99]:55484 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2406037AbfBOCP6 (ORCPT ); Thu, 14 Feb 2019 21:15:58 -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 39DF9206BA; Fri, 15 Feb 2019 02:15:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1550196956; bh=S8N4n0zEOt7N8W+IJQFGv23/yCyOkM1/zip/UDFVjDI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZppKXPJw4fPk/foXewt6LfmpAVGPT8RGhcZYA+aAEnuDx1cwivsoVME8IJFKvkiRR L4cxo6ybn64JzYAFCKDPLPQRUQl51Wb54cgrlr1VjgZ3nLsFpw1yn3jY7Pu/LFr2+U l21ROHXtyqK7XPBdWW0mwgMJpeUca8Ep3q/eKFz8= 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 3.18 06/16] ASoC: imx-audmux: change snprintf to scnprintf for possible overflow Date: Thu, 14 Feb 2019 21:15:36 -0500 Message-Id: <20190215021546.179605-6-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190215021546.179605-1-sashal@kernel.org> References: <20190215021546.179605-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 46f9beb6b273..57d581d05de1 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