Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1776606imu; Sat, 12 Jan 2019 07:30:45 -0800 (PST) X-Google-Smtp-Source: ALg8bN7rziHIFOliUOJaPoojVARjpQ/8S5GLPBxZANvBRu3HWFLngbMsmfk+dn2LqDFhQNwLbvab X-Received: by 2002:a63:bd51:: with SMTP id d17mr17424201pgp.443.1547307044988; 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=tuz9mKZAKq5L3YNjgljgwGeter0DgRcRD09nOummjDUmScCM15qrjW8AOuNio28Aag 1IlW4SIIwvXFokdADN3MGZpgnlwOmtEoEII2TfNukrh8g/n4vRBJPgIAH2+P4ZIeQ5qw zoh4jzn9FxC1/AEiKCkqWQB7t5hrcuUEO5VLwxRp1Qc3PyyMehbrVYpVoIzgTmuZ0/C2 5JU4F7/s20M8wUhCJXeJbcHOows78PefDrSJHGjapfKy+EH/tMiDKEwjSauwLL94Vpf/ ZW9mcMVQWiRPBOe5dYcpW0EzHFy+dqpLp3Q1Q4hs6WQvAKRYz8fx4mjQUI/Hu+A8E6AE mfGA== 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=wqP3QXo3AMCCUkKfGs3ZinA6RGXdgu74wM2YpXf4YM8=; b=g6eNtwkSPlezqAcw/xbiPof4JVARPqWpHSPLPtTYKzHw4dgXNNRpGQx9axOVPiX4Aa JR/hF7IB3WTYGHTrEjIE7DgEqq3UWOdKVRm7TC3DvJxVic/5VZZQlgtVN/P7l3K8xmXF 5Zlb6Ad6/AoKqDSqpuPez5Vl3kB6cdfJJHJFuRv8e4Aj9TFuDQJo4cFi1OVnICGpXAU2 u6aOEhcafQOGYGMI5lQ4jX4ckq901DISyK1qvOIzS7sCMhciAFFoiHNERCCLvSbSG/VU wfnmkZme4rt1foK4mn3yyzFAiMX4sK5sqgYJmNuCSVz9Q3mm4rp533LSsNangmXL5Nri zBcw== 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 d142si9161897pfd.93.2019.01.12.07.30.08; 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 S1726399AbfALP3E (ORCPT + 99 others); Sat, 12 Jan 2019 10:29:04 -0500 Received: from wtarreau.pck.nerim.net ([62.212.114.60]:34285 "EHLO 1wt.eu" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725839AbfALP3E (ORCPT ); Sat, 12 Jan 2019 10:29:04 -0500 Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id x0CFSo1l026597; Sat, 12 Jan 2019 16:28:50 +0100 From: Willy Tarreau To: Silvio Cesare Cc: linux-kernel@vger.kernel.org, Kalle Valo , Dan Carpenter , Kees Cook , Will Deacon , Greg KH Subject: [PATCH 2/8] libertas: change snprintf to scnprintf for possible overflow Date: Sat, 12 Jan 2019 16:28:38 +0100 Message-Id: <20190112152844.26550-2-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: Kalle Valo Cc: Dan Carpenter Cc: Kees Cook Cc: Will Deacon Cc: Greg KH Signed-off-by: Willy Tarreau --- drivers/net/wireless/marvell/libertas/debugfs.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/drivers/net/wireless/marvell/libertas/debugfs.c b/drivers/net/wireless/marvell/libertas/debugfs.c index c83f44f9ddf1..ec73bd3a10db 100644 --- a/drivers/net/wireless/marvell/libertas/debugfs.c +++ b/drivers/net/wireless/marvell/libertas/debugfs.c @@ -41,9 +41,9 @@ static ssize_t lbs_dev_info(struct file *file, char __user *userbuf, if (!buf) return -ENOMEM; - pos += snprintf(buf+pos, len-pos, "state = %s\n", + pos += scnprintf(buf+pos, len-pos, "state = %s\n", szStates[priv->connect_status]); - pos += snprintf(buf+pos, len-pos, "region_code = %02x\n", + pos += scnprintf(buf+pos, len-pos, "region_code = %02x\n", (u32) priv->regioncode); res = simple_read_from_buffer(userbuf, count, ppos, buf, pos); @@ -105,7 +105,7 @@ static ssize_t lbs_sleepparams_read(struct file *file, char __user *userbuf, if (ret) goto out_unlock; - pos += snprintf(buf, len, "%d %d %d %d %d %d\n", sp.sp_error, + pos += scnprintf(buf, len, "%d %d %d %d %d %d\n", sp.sp_error, sp.sp_offset, sp.sp_stabletime, sp.sp_calcontrol, sp.sp_extsleepclk, sp.sp_reserved); @@ -170,7 +170,7 @@ static ssize_t lbs_host_sleep_read(struct file *file, char __user *userbuf, if (!buf) return -ENOMEM; - pos += snprintf(buf, len, "%d\n", priv->is_host_sleep_activated); + pos += scnprintf(buf, len, "%d\n", priv->is_host_sleep_activated); ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos); @@ -251,7 +251,7 @@ static ssize_t lbs_threshold_read(uint16_t tlv_type, uint16_t event_mask, freq = got->freq; events = le16_to_cpu(subscribed->events); - pos += snprintf(buf, len, "%d %d %d\n", value, freq, + pos += scnprintf(buf, len, "%d %d %d\n", value, freq, !!(events & event_mask)); } @@ -446,7 +446,7 @@ static ssize_t lbs_rdmac_read(struct file *file, char __user *userbuf, ret = lbs_get_reg(priv, CMD_MAC_REG_ACCESS, priv->mac_offset, &val); mdelay(10); if (!ret) { - pos = snprintf(buf, len, "MAC[0x%x] = 0x%08x\n", + pos = scnprintf(buf, len, "MAC[0x%x] = 0x%08x\n", priv->mac_offset, val); ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos); } @@ -516,7 +516,7 @@ static ssize_t lbs_rdbbp_read(struct file *file, char __user *userbuf, ret = lbs_get_reg(priv, CMD_BBP_REG_ACCESS, priv->bbp_offset, &val); mdelay(10); if (!ret) { - pos = snprintf(buf, len, "BBP[0x%x] = 0x%08x\n", + pos = scnprintf(buf, len, "BBP[0x%x] = 0x%08x\n", priv->bbp_offset, val); ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos); } @@ -588,7 +588,7 @@ static ssize_t lbs_rdrf_read(struct file *file, char __user *userbuf, ret = lbs_get_reg(priv, CMD_RF_REG_ACCESS, priv->rf_offset, &val); mdelay(10); if (!ret) { - pos = snprintf(buf, len, "RF[0x%x] = 0x%08x\n", + pos = scnprintf(buf, len, "RF[0x%x] = 0x%08x\n", priv->rf_offset, val); ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos); } -- 2.19.2