Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1776607imu; Sat, 12 Jan 2019 07:30:45 -0800 (PST) X-Google-Smtp-Source: ALg8bN6VGc8aTtDsgKq+oyI+dPXOxSWA669qPVIlDG2BkCYFiQ0A+M4yVOvfw6SinbpZzKek/ZPg X-Received: by 2002:a17:902:bc44:: with SMTP id t4mr18813668plz.260.1547307044995; Sat, 12 Jan 2019 07:30:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547307044; cv=none; d=google.com; s=arc-20160816; b=qgfhMiZ443A4kO+eUV+DPgpJsWqpt9cmjLnRCUXy4sYxENQ5B0V9n40w9q6ldE5JWU kJ92AVhEfxv4lccyPQg1hbO4OEZKXjF5DoOBczC89UxZdZL7OSYMNZ8Y2IEgxVku3WN7 HMheDjsNEoWacpujyhgzXVsfpwhFT9mEuRzwzDciXO43+cbi0wqHeEoxsOAstUTxnKe9 mtLUahgUMmyxIABkIDPRL3riDEeCYWskAKAnMH2ahNKo/YBezAh61FCm74T7vVkLpD/f JjTNkoTtCUMYUkLSxOe63BhCANs6e/Ck9a2CPaNh0KA29C3/BSTUFYq4Vd+ewQlffV2F ZwWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=kYxan+aaIQd+x71mEGltShyZrSaYJJpHQ6BB0Ucdjd8=; b=P62xr14KX3fFcswgXuXMyNsNNQhKHBntWWc+Bs9dhcTXKTGY0uXDe2e+LtQUtRo+wI YV2lBjx8L90FMPqkDCS96q26BL2q4PkQtf9GyQ/50dWTNQN/xsh3L19XEK45NWmk9gNt 674pJRz4Vbt7NTHctjOGFCEojD7NRICWhwl8+VWcZwnZmfr068YIOoD26S2ZiEoqvuvC g/+mCo8YqJ6GYu8W75fDO/n7yKq19SCmzlmo0UgkQxYBLYWDSM4SXafgMrsoe3Qe+LGB WwRaT2sV4rklNrT9G5RHLnSKu6Te0MMhbwj67VTRSFwWxyBmGcFdTsgwl4wxwZ4lo0rc HZgg== ARC-Authentication-Results: i=1; mx.google.com; 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 d125si80531467pfc.114.2019.01.12.07.30.20; Sat, 12 Jan 2019 07:30:44 -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; 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 S1726474AbfALP3J (ORCPT + 99 others); Sat, 12 Jan 2019 10:29:09 -0500 Received: from wtarreau.pck.nerim.net ([62.212.114.60]:34294 "EHLO 1wt.eu" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726427AbfALP3J (ORCPT ); Sat, 12 Jan 2019 10:29:09 -0500 Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id x0CFSpnp026599; Sat, 12 Jan 2019 16:28:51 +0100 From: Willy Tarreau To: Silvio Cesare Cc: linux-kernel@vger.kernel.org, Timur Tabi , Nicolin Chen , Xiubo Li , Fabio Estevam , Dan Carpenter , Kees Cook , Will Deacon , Greg KH Subject: [PATCH 4/8] ASoC: change snprintf to scnprintf for possible overflow Date: Sat, 12 Jan 2019 16:28:40 +0100 Message-Id: <20190112152844.26550-4-w@1wt.eu> X-Mailer: git-send-email 2.9.0 In-Reply-To: <20190112152844.26550-1-w@1wt.eu> References: <20190112152844.26550-1-w@1wt.eu> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Silvio Cesare 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: Xiubo Li Cc: Fabio Estevam Cc: Dan Carpenter Cc: Kees Cook Cc: Will Deacon Cc: Greg KH Signed-off-by: Willy Tarreau --- 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.2