Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1252011

[PATCH 03/13] lib/vsprintf.c: eliminate potential race in string()

Path csiph.com!news.mixmin.net!weretis.net!feeder1.news.weretis.net!newsfeed.CARNet.hr!news.spin.it!bofh.it!news.nic.it!robomod
From Rasmus Villemoes <linux@rasmusvillemoes.dk>
Newsgroups linux.kernel
Subject [PATCH 03/13] lib/vsprintf.c: eliminate potential race in string()
Date Tue, 20 Oct 2015 22:40:01 +0200
Message-ID <qlLRn-6h1-1@gated-at.bofh.it> (permalink)
References <qlLRn-6h1-3@gated-at.bofh.it>
X-Original-To Andrew Morton <akpm@linux-foundation.org>
Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=2HkydFP4c632EtG/I9G8zHG+sCwuv1MX70lZKWZCVP4=; b=EpSty12btcSEx7Lim0K76XJlQlY7LH/GwyzuOgbwIQuSUvyAwMssdBKofxnR1Z5EF2 TaPLaF76Ju0g9RgEZZCBHeV/Nn9xklAblj5iccS8eZEgDrJTLZxrBab2cuxxGcPvtzKO GTms3dGTkFyGUF88Pb6pm0EXYZEpEYy43cJJo=
X-Google-Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=2HkydFP4c632EtG/I9G8zHG+sCwuv1MX70lZKWZCVP4=; b=OoICVv/VoRcb3e6yN4WR5npVEZzQGXMcOWMwzJEr9CAfhxP21UxB5TCK0suWkcaWS6 s1g+FpGBS3kvZ3Ak4Dm9UlBULjSDa10V/fMXovcAw/Uk60h9Y41dWa8EXlzJJdNXpQDX sRGjjL0mH3X1PODbmNAoO/NpztVr8VWuHFujd6/59RZpa2H39yD5Fe20aj3/iMiqN+rs OjVgBrKoMkwNieEIHi0aFmZT38CtVfe7O+EHleaNtscHYz/e+VJXDVDAzzG0I9GM9L/K 6gYEkn/BbnZaw8cv2bgPnxLDQpB4ktcpQevp7FIiqhSM+OT3tV8ra7pkLnu5xPgSosiM IkcQ==
X-Gm-Message-State ALoCoQlKl9Xj5TLP/bkYLeEFD53T/o5onEZV9cf8uC9YI+FqqrOVpYCpohpfLMoIaKxR/QyEbRDp
X-Received by 10.25.143.72 with SMTP id r69mr1984516lfd.65.1445373032575; Tue, 20 Oct 2015 13:30:32 -0700 (PDT)
X-Mailer git-send-email 2.6.1
Sender robomod@news.nic.it
List-ID <linux-kernel.vger.kernel.org>
X-Mailing-List linux-kernel@vger.kernel.org
Approved robomod@news.nic.it
Lines 71
Organization linux.* mail to news gateway
X-Original-Cc Rasmus Villemoes <linux@rasmusvillemoes.dk>, Ingo Molnar <mingo@kernel.org>, linux-kernel@vger.kernel.org
X-Original-Date Tue, 20 Oct 2015 22:30:03 +0200
X-Original-Message-ID <1445373013-20207-4-git-send-email-linux@rasmusvillemoes.dk>
X-Original-References <1445373013-20207-1-git-send-email-linux@rasmusvillemoes.dk>
X-Original-Sender linux-kernel-owner@vger.kernel.org
Xref csiph.com linux.kernel:1252011

Show key headers only | View raw


If the string corresponding to a %s specifier can change under us, we
might end up copying a \0 byte to the output buffer. There might be
callers who expect the output buffer to contain a genuine C string
whose length is exactly the snprintf return value (assuming truncation
hasn't happened or has been checked for).

We can avoid this by only passing over the source string once,
stopping the first time we meet a nul byte (or when we reach the given
precision), and then letting widen_string() handle left/right space
padding.

Cc: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 lib/vsprintf.c | 28 +++++++++-------------------
 1 file changed, 9 insertions(+), 19 deletions(-)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index a0145b5d114e..ba00eab1e703 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -557,32 +557,22 @@ char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
 static noinline_for_stack
 char *string(char *buf, char *end, const char *s, struct printf_spec spec)
 {
-	int len, i;
+	int len = 0;
+	size_t lim = spec.precision;
 
 	if ((unsigned long)s < PAGE_SIZE)
 		s = "(null)";
 
-	len = strnlen(s, spec.precision);
-
-	if (!(spec.flags & LEFT)) {
-		while (len < spec.field_width--) {
-			if (buf < end)
-				*buf = ' ';
-			++buf;
-		}
-	}
-	for (i = 0; i < len; ++i) {
-		if (buf < end)
-			*buf = *s;
-		++buf; ++s;
-	}
-	while (len < spec.field_width--) {
+	while (lim--) {
+		char c = *s++;
+		if (!c)
+			break;
 		if (buf < end)
-			*buf = ' ';
+			*buf = c;
 		++buf;
+		++len;
 	}
-
-	return buf;
+	return widen_string(buf, len, end, spec);
 }
 
 static noinline_for_stack
-- 
2.6.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Back to linux.kernel | Previous | Next | Find similar | Unroll thread


Thread

[PATCH 03/13] lib/vsprintf.c: eliminate potential race in string() Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2015-10-20 22:40 +0200

csiph-web