Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp207595imj; Thu, 14 Feb 2019 18:42:42 -0800 (PST) X-Google-Smtp-Source: AHgI3IYF84gv6VDrq24N1TMBEPwHSMRXGfK3YHZCMK+EQAK+IcUlVMZZFb/VAhWqwL51c9tac/yR X-Received: by 2002:a63:9b11:: with SMTP id r17mr3135539pgd.416.1550198562437; Thu, 14 Feb 2019 18:42:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550198562; cv=none; d=google.com; s=arc-20160816; b=IIN+MrcI4WuRe3IUVdlJ+D1bGXrYpfa/DhQnsXkW5D+mGvutbGsdG4Im21wYLmk/wn YbohB27N7oac7PDJ0canV3U+fvDeISxLjjr1O2XtqmX9utSHzQARExyU4mH3FzMhut4z bfjIwpL5UHjDxc3g2VUx7LzSf6f9qLNNscY2MCxohWfsXyy1nlwI0HZCCEw2ugprpZQE ZKKmZawBBUcx3+UnRRRHAWMUPj9PG1zpfz0+wnW1JvqimJYhF5nAJXiwaM/gm64qxcJ9 eys5CBXdjavtXlpRu+JWDR4DJlqacDxZynbI8AaEwFZE5LDY7nreLHbKnE6JV37UKsca pPuA== 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=JzER5kZnPNcKHNnMs9NYlk5ImNl+JI8rNZE3Zwo/D4I=; b=sGQWxPk8qpZ2mJ2GfsCj2NbSGzwHhk1OW6TjOf6P65jPfFP1jDXkueoBIezXfcd5z/ vmq7R8Me6lw8itWQUonCl16C4YgFinh3I76eJbjyhhaEVmY0SUBNeEfKrxavkYeBmqw+ QWHG9nvy3zj7OPz4pCq3JqpQ7SA/Peo14acBIUNkEgx50L+hRMcxbU3EOX9iOJVRrZnI 4R9drrp2jlpU0uFsSP9ehCs6LAYRrlfPYsXgWLQuI0VdBcHH8Mro6s9/qdnmOXabA0Rn x6hjkwfnOK06RKjiiKNy1oaBmxEf4XquyNgntuFyB6QEadfVNC/2oA+AidPa6jvDCBZu LrgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="XNsM/dDM"; 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 t20si4107178plo.360.2019.02.14.18.42.26; Thu, 14 Feb 2019 18:42:42 -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="XNsM/dDM"; 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 S1729485AbfBOCl3 (ORCPT + 99 others); Thu, 14 Feb 2019 21:41:29 -0500 Received: from mail.kernel.org ([198.145.29.99]:49798 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388137AbfBOCJi (ORCPT ); Thu, 14 Feb 2019 21:09:38 -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 C771E222C9; Fri, 15 Feb 2019 02:09:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1550196578; bh=Oeq7yqX15VgPc+LBpYeRDxiRoffITRc3K/P3etR9nbU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XNsM/dDMPCnCXKSyPccEKBm+mcIiywgzGmrVlYA0dUHFFqjrhvBFq/AaEtuTq7bLo NeDTTcMbkYUDo00G8j9zlDonrL3QrXo9ttvnrX9pcsvAqeokAvfU6dljxFKmXCzkgB TC31dOPWxt6at9Ls9MXVKzA7TUN3eB/XL8eTK8Fk= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Silvio Cesare , Liam Girdwood , Mark Brown , Dan Carpenter , Kees Cook , Will Deacon , Greg KH , Willy Tarreau , Sasha Levin Subject: [PATCH AUTOSEL 4.20 24/77] ASoC: dapm: change snprintf to scnprintf for possible overflow Date: Thu, 14 Feb 2019 21:08:02 -0500 Message-Id: <20190215020855.176727-24-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 e581e151e965bf1f2815dd94620b638fec4d0a7e ] 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: Liam Girdwood Cc: Mark Brown Cc: Dan Carpenter Cc: Kees Cook Cc: Will Deacon Cc: Greg KH Signed-off-by: Willy Tarreau Signed-off-by: Mark Brown Signed-off-by: Sasha Levin --- sound/soc/soc-dapm.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sound/soc/soc-dapm.c b/sound/soc/soc-dapm.c index a5178845065b..2c4c13419539 100644 --- a/sound/soc/soc-dapm.c +++ b/sound/soc/soc-dapm.c @@ -2019,19 +2019,19 @@ static ssize_t dapm_widget_power_read_file(struct file *file, out = is_connected_output_ep(w, NULL, NULL); } - ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d", + ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d", w->name, w->power ? "On" : "Off", w->force ? " (forced)" : "", in, out); if (w->reg >= 0) - ret += snprintf(buf + ret, PAGE_SIZE - ret, + ret += scnprintf(buf + ret, PAGE_SIZE - ret, " - R%d(0x%x) mask 0x%x", w->reg, w->reg, w->mask << w->shift); - ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n"); + ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n"); if (w->sname) - ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n", + ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n", w->sname, w->active ? "active" : "inactive"); @@ -2044,7 +2044,7 @@ static ssize_t dapm_widget_power_read_file(struct file *file, if (!p->connect) continue; - ret += snprintf(buf + ret, PAGE_SIZE - ret, + ret += scnprintf(buf + ret, PAGE_SIZE - ret, " %s \"%s\" \"%s\"\n", (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out", p->name ? p->name : "static", -- 2.19.1