lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:	Thu, 28 May 2015 14:58:19 +0200
From:	Borislav Petkov <bp@...en8.de>
To:	Prarit Bhargava <prarit@...hat.com>
Cc:	Joe Perches <joe@...ches.com>, luto@...capital.net,
	peterz@...radead.org, dvlasenk@...hat.com,
	torvalds@...ux-foundation.org, imammedo@...hat.com,
	brgerst@...il.com, mingo@...nel.org, dave.hansen@...ux.intel.com,
	fenghua.yu@...el.com, hpa@...or.com, linux-kernel@...r.kernel.org,
	tglx@...utronix.de, bp@...e.de, linux-tip-commits@...r.kernel.org
Subject: Re: [tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field
 whitespace

On Thu, May 28, 2015 at 01:32:29PM +0200, Borislav Petkov wrote:
> +	while (*p) {
> +		/* Note the last non-whitespace index */
> +		if (!isspace(*p))
> +			s = q;
> +
> +		*q++ = *p++;

This should be optimized to not copy if there's no preceding whitespace
and p == q:

From: Borislav Petkov <bp@...e.de>
Date: Tue, 26 May 2015 10:28:17 +0200
Subject: [PATCH] x86/cpu: Trim model id whitespace

We did try trimming whitespace surrounding the 'model name' field
in /proc/cpuinfo since reportedly some userspace uses it in string
comparisons and there were discrepancies:

  [thetango@...rit ~]# grep "^model name" /proc/cpuinfo | uniq -c | sed 's/\ /_/g'
  ______1_model_name      :_AMD_Opteron(TM)_Processor_6272
  _____63_model_name      :_AMD_Opteron(TM)_Processor_6272_________________

However, there were issues with overlapping buffers, string sizes and
non-byte-sized copies in the previous proposed solutions; see Link tags
below for the whole farce.

So, instead of diddling with this more, let's simply extend what was
there originally with trimming any present trailing whitespace. Final
result is really simple and obvious.

Testing with the most insane model IDs qemu can generate, looks good:

  .model_id = "            My funny model ID CPU          ",
  ______4_model_name      :_My_funny_model_ID_CPU

  .model_id = "My funny model ID CPU          ",
  ______4_model_name      :_My_funny_model_ID_CPU

  .model_id = "            My funny model ID CPU",
  ______4_model_name      :_My_funny_model_ID_CPU

  .model_id = "            ",
  ______4_model_name      :__

  .model_id = "",
  ______4_model_name      :_15/02

Cc: Andy Lutomirski <luto@...capital.net>
Cc: Brian Gerst <brgerst@...il.com>
Cc: Dave Hansen <dave.hansen@...ux.intel.com>
Cc: Denys Vlasenko <dvlasenk@...hat.com>
Cc: Fenghua Yu <fenghua.yu@...el.com>
Cc: H. Peter Anvin <hpa@...or.com>
Cc: Igor Mammedov <imammedo@...hat.com>
Cc: Linus Torvalds <torvalds@...ux-foundation.org>
Cc: Peter Zijlstra <peterz@...radead.org>
Cc: Thomas Gleixner <tglx@...utronix.de>
Link: http://lkml.kernel.org/r/1432050210-32036-1-git-send-email-prarit@redhat.com
Link: http://lkml.kernel.org/r/1432628901-18044-15-git-send-email-bp@alien8.de
Signed-off-by: Borislav Petkov <bp@...e.de>
---
 arch/x86/kernel/cpu/common.c | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 41a8e9cb30bc..18120a33a2c1 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -5,6 +5,7 @@
 #include <linux/module.h>
 #include <linux/percpu.h>
 #include <linux/string.h>
+#include <linux/ctype.h>
 #include <linux/delay.h>
 #include <linux/sched.h>
 #include <linux/init.h>
@@ -419,6 +420,7 @@ static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
 static void get_model_name(struct cpuinfo_x86 *c)
 {
 	unsigned int *v;
+	char *p, *q, *s;
 
 	if (c->extended_cpuid_level < 0x80000004)
 		return;
@@ -429,11 +431,26 @@ static void get_model_name(struct cpuinfo_x86 *c)
 	cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
 	c->x86_model_id[48] = 0;
 
-	/*
-	 * Remove leading whitespace on Intel processors and trailing
-	 * whitespace on AMD processors.
-	 */
-	memmove(c->x86_model_id, strim(c->x86_model_id), 48);
+	/* Trim whitespace */
+	p = q = s = &c->x86_model_id[0];
+
+	while (*p == ' ')
+		p++;
+
+	while (*p) {
+		/* Note the last non-whitespace index: */
+		if (!isspace(*p))
+			s = q;
+
+		/* Only copy if p advanced due to whitespace: */
+		if (p != q)
+			*q = *p;
+
+		p++;
+		q++;
+	}
+
+	*(s + 1) = '\0';
 }
 
 void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
-- 
2.3.5

-- 
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
--
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ