From patchwork Sun Nov 14 17:06:08 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vasiliy Kulikov X-Patchwork-Id: 71140 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 26D6EB7114 for ; Mon, 15 Nov 2010 04:06:50 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756393Ab0KNRGR (ORCPT ); Sun, 14 Nov 2010 12:06:17 -0500 Received: from mail-ey0-f174.google.com ([209.85.215.174]:50665 "EHLO mail-ey0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752986Ab0KNRGP (ORCPT ); Sun, 14 Nov 2010 12:06:15 -0500 Received: by eye27 with SMTP id 27so2632760eye.19 for ; Sun, 14 Nov 2010 09:06:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:sender:from:to:cc:subject :date:message-id:x-mailer; bh=7v/JL4lnWoyFDyG8oWv73yGr4V6m/pTAVWU8Acqzdho=; b=iVkKsq3yLLmuqifTxOXUEEt05MkiBSw2dnVa3PgDUXfNw4NYkNy5cDdB69HJaoy0gK Qyr0zZKjQtdgCKCA10KjXM1hydMEnYHLEvGV+PAtF1U/GWH6Lg8hZvYSObTnWhMvtNx8 eLi9Kp3ojM79W2tes3aR/99no3seO7gDPO0u8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=sender:from:to:cc:subject:date:message-id:x-mailer; b=EPwnvClMzxh06eIhOtkaj92hNlHEbp3EATqwcve1c7QBeTbcwjKEbEalsLM+MjUDox M8gYzmBnHCOwMhaJUKQQeAmdFx459Pk73yqAaG4tbwykCI6Wl43lgU6gHKSDuURHnSTL 0UQvBeij39Agq5vCIFyYkktyCshv0SR3fYoqI= Received: by 10.213.28.134 with SMTP id m6mr4064526ebc.49.1289754373390; Sun, 14 Nov 2010 09:06:13 -0800 (PST) Received: from localhost (ppp85-140-234-48.pppoe.mtu-net.ru [85.140.234.48]) by mx.google.com with ESMTPS id w20sm5655555eeh.0.2010.11.14.09.06.10 (version=TLSv1/SSLv3 cipher=RC4-MD5); Sun, 14 Nov 2010 09:06:12 -0800 (PST) From: Vasiliy Kulikov To: kernel-janitors@vger.kernel.org Cc: "David S. Miller" , Alexey Kuznetsov , "Pekka Savola (ipv6)" , James Morris , Hideaki YOSHIFUJI , Patrick McHardy , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] net: ipv4: tcp_probe: cleanup snprintf() use Date: Sun, 14 Nov 2010 20:06:08 +0300 Message-Id: <1289754368-31660-1-git-send-email-segoon@openwall.com> X-Mailer: git-send-email 1.7.0.4 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org snprintf() returns number of bytes that were copied if there is no overflow. This code uses return value as number of copied bytes. Theoretically format string '%lu.%09lu %pI4:%u %pI4:%u %d %#x %#x %u %u %u %u\n' may be expanded up to 163 bytes. In reality tv.tv_sec is just few bytes instead of 20, 2 ports are just 5 bytes each instead of 10, length is 5 bytes instead of 10. The rest is an unstrusted input. Theoretically if tv_sec is big then copy_to_user() would overflow tbuf. tbuf was increased to fit in 163 bytes. snprintf() is used to follow return value semantic. Signed-off-by: Vasiliy Kulikov --- Compile tested. Format length: 20 for '%lu' 1 for '.' 9 for '%09lu' 1 for ' ' 15 for '%pI4' 1 for ':' 10 for '%u' 1 for ' ' 15 for '%pI4' 1 for ':' 10 for '%u' 1 for ' ' 11 for '%d' 1 for ' ' 10 for '%#x' 1 for ' ' 10 for '%#x' 1 for ' ' 10 for '%u' 1 for ' ' 10 for '%u' 1 for ' ' 10 for '%u' 1 for ' ' 10 for '%u' 1 for '\n' 163 for '%lu.%09lu %pI4:%u %pI4:%u %d %#x %#x %u %u %u %u\n' net/ipv4/tcp_probe.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-) diff --git a/net/ipv4/tcp_probe.c b/net/ipv4/tcp_probe.c index 6211e21..3b7bf19 100644 --- a/net/ipv4/tcp_probe.c +++ b/net/ipv4/tcp_probe.c @@ -154,7 +154,7 @@ static int tcpprobe_sprint(char *tbuf, int n) struct timespec tv = ktime_to_timespec(ktime_sub(p->tstamp, tcp_probe.start)); - return snprintf(tbuf, n, + return scnprintf(tbuf, n, "%lu.%09lu %pI4:%u %pI4:%u %d %#x %#x %u %u %u %u\n", (unsigned long) tv.tv_sec, (unsigned long) tv.tv_nsec, @@ -174,7 +174,7 @@ static ssize_t tcpprobe_read(struct file *file, char __user *buf, return -EINVAL; while (cnt < len) { - char tbuf[128]; + char tbuf[164]; int width; /* Wait for data in buffer */