Skip to contentSkip to conversation

x86: early command-line parsing fixes / tests (v2)

PATCH In review Dave Hansen 3 participants 20 messages Dec 22, 2015 – Feb 3, 2016 x86

lore
Share & tools
lore
#0 lore raw

cover x86: early command-line parsing fixes / tests (v2)

The code here is unchanged except for fixing a compile error that I managed to introduce just before I sent it last time.

The tests for this code are in the last two patches. If folks think it is overkill, those can be left out. But, the first three are still needed.

#1 lore raw

PATCH 1/5 x86: fix early command-line parsing when matching at end

From: Dave Hansen <[email protected]>

The x86 early command line parsing in cmdline_find_option_bool() is buggy. If it matches a specified 'option' all the way to the end of the command-line, it will consider it a match.

For instance,

    	cmdline = "foo";
    	cmdline_find_option_bool(cmdline, "fool");

will return 1. This is particularly annoying since we have actual FPU options like "noxsave" and "noxsaves" So, command-line "foo bar noxsave" will match *BOTH* a "noxsave" and "noxsaves". (This turns out not to be an actual problem because "noxsave" implies "noxsaves", but it's still confusing.)

To fix this, we simplify the code and stop tracking 'len'. 'len' was trying to indicate either the NULL terminator *OR* the end of a non-NULL-terminated commandline at 'COMMAND_LINE_SIZE'. But, each of the three states is *already* checking 'cmdline' for a NULL terminator.

We _only_ need to check if we have overrun 'COMMAND_LINE_SIZE', and that we can do without keeping 'len' around.

Also add some commends to clarify what is going on.

---

 b/arch/x86/lib/cmdline.c |   34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)
arch/x86/lib/cmdline.c renamed from arch/x86/lib/cmdline.c~x86-broken-end-of-string-command-line-parsing +24 -10
diff -puN arch/x86/lib/cmdline.c~x86-broken-end-of-string-command-line-parsing arch/x86/lib/cmdline.c
@@ -21,12 +21,14 @@ static inline int myisspace(u8 c)
  * @option: option string to look for
  *
  * Returns the position of that @option (starts counting with 1)
- * or 0 on not found.
+ * or 0 on not found.  @option will only be found if it is found
+ * as an entire word in @cmdline.  For instance, if @option="car"
+ * then a cmdline which contains "cart" will not match.
  */
 int cmdline_find_option_bool(const char *cmdline, const char *option)
 {
 	char c;
-	int len, pos = 0, wstart = 0;
+	int pos = 0, wstart = 0;
 	const char *opptr = NULL;
 	enum {
 		st_wordstart = 0,	/* Start of word/after whitespace */
@@ -37,11 +39,14 @@ int cmdline_find_option_bool(const char
 	if (!cmdline)
 		return -1;      /* No command line */
 
-	len = min_t(int, strlen(cmdline), COMMAND_LINE_SIZE);
-	if (!len)
+	if (!strlen(cmdline))
 		return 0;
 
-	while (len--) {
+	/*
+	 * This 'pos' check ensures we do not overrun
+	 * a non-NULL-terminated 'cmdline'
+	 */
+	while (pos < COMMAND_LINE_SIZE) {
 		c = *(char *)cmdline++;
 		pos++;
 
@@ -58,17 +63,26 @@ int cmdline_find_option_bool(const char
 			/* fall through */
 
 		case st_wordcmp:
-			if (!*opptr)
+			if (!*opptr) {
+				/*
+				 * We matched all the way to the end of the
+				 * option we were looking for.  If the
+				 * command-line has a space _or_ ends, then
+				 * we matched!
+				 */
 				if (!c || myisspace(c))
 					return wstart;
 				else
 					state = st_wordskip;
-			else if (!c)
+			} else if (!c) {
+				/*
+				 * Hit the NULL terminator on the end of
+				 * cmdline.
+				 */
 				return 0;
-			else if (c != *opptr++)
+			} else if (c != *opptr++) {
 				state = st_wordskip;
-			else if (!len)		/* last word and is matching */
-				return wstart;
+			}
 			break;
 
 		case st_wordskip:

_

#2 lore raw

Re: [PATCH 1/5] x86: fix early command-line parsing when matching at end

Quoted 9 lines · Dave Hansen

From: Dave Hansen <[email protected]>

The x86 early command line parsing in cmdline_find_option_bool() is buggy. If it matches a specified 'option' all the way to the end of the command-line, it will consider it a match.

For instance,

Quoted 8 lines
    	cmdline = "foo";
    	cmdline_find_option_bool(cmdline, "fool");

will return 1. This is particularly annoying since we have actual FPU options like "noxsave" and "noxsaves" So, command-line "foo bar noxsave" will match *BOTH* a "noxsave" and "noxsaves". (This turns out not to be an actual problem because "noxsave" implies "noxsaves", but it's still confusing.)

So I booted a guest with this command line:

[    0.000000] Kernel command line: root=/dev/sda1 resume=/dev/sdb1 debug ignore_loglevel log_buf_len=16M earlyprintk=ttyS0,115200 console=ttyS0,115200 console=tty0 noxsave

after applying the debug hunk below. However, the only debug output I got is this:

[    0.000000] x86_noxsave_setup

If I supply "noxsaves", I get, as expected

[    0.000000] x86_noxsaves_setup

only. Ditto for "noxsaveopt".

So what am I missing?

---

arch/x86/kernel/fpu/init.c +6 -0
index 1a6adcb4fbce..472f6edfb8d9 100644
@@ -354,6 +354,8 @@ static int __init x86_noxsave_setup(char *s)
 	if (strlen(s))
 		return 0;
 
+	pr_info("%s\n", __func__);
+
 	fpu__xstate_clear_all_cpu_caps();
 
 	return 1;
@@ -367,6 +369,8 @@ static int __init x86_noxsaveopt_setup(char *s)
 {
 	setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
 
+	pr_info("%s\n", __func__);
+
 	return 1;
 }
 __setup("noxsaveopt", x86_noxsaveopt_setup);
@@ -378,6 +382,8 @@ static int __init x86_noxsaves_setup(char *s)
 {
 	setup_clear_cpu_cap(X86_FEATURE_XSAVES);
 
+	pr_info("%s\n", __func__);
+
 	return 1;
 }
 __setup("noxsaves", x86_noxsaves_setup);
Signature · 5 lines
Regards/Gruss,
    Boris.

SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
-- 
#3 lore raw

Re: [PATCH 1/5] x86: fix early command-line parsing when matching at end

Quoted 26 lines · Borislav Petkov
Quoted 7 lines · Dave Hansen

From: Dave Hansen <[email protected]>
The x86 early command line parsing in cmdline_find_option_bool() is buggy. If it matches a specified 'option' all the way to the end of the command-line, it will consider it a match.

For instance,

Quoted 8 lines
    	cmdline = "foo";
    	cmdline_find_option_bool(cmdline, "fool");

will return 1. This is particularly annoying since we have actual FPU options like "noxsave" and "noxsaves" So, command-line "foo bar noxsave" will match *BOTH* a "noxsave" and "noxsaves". (This turns out not to be an actual problem because "noxsave" implies "noxsaves", but it's still confusing.)

So I booted a guest with this command line:

[    0.000000] Kernel command line: root=/dev/sda1 resume=/dev/sdb1 debug ignore_loglevel log_buf_len=16M earlyprintk=ttyS0,115200 console=ttyS0,115200 console=tty0 noxsave

after applying the debug hunk below. However, the only debug output I got is this:

[    0.000000] x86_noxsave_setup
Quoted 7 lines

If I supply "noxsaves", I get, as expected

[    0.000000] x86_noxsaves_setup

only. Ditto for "noxsaveopt".

So what am I missing?

The current code uses __setup() which uses different parsing than cmdline_find_option_bool(). We're going to have to move the noxsave* ones over to cmdline_find_option_bool() since we need the parsing earlier than __setup() works.

The changelog should have been more clear on this, though.

#4 lore raw

Re: [PATCH 1/5] x86: fix early command-line parsing when matching at end

In reply to Dave Hansen
Quoted 65 lines · Dave Hansen

From: Dave Hansen <[email protected]>

The x86 early command line parsing in cmdline_find_option_bool() is buggy. If it matches a specified 'option' all the way to the end of the command-line, it will consider it a match.

For instance,

    	cmdline = "foo";
    	cmdline_find_option_bool(cmdline, "fool");

will return 1. This is particularly annoying since we have actual FPU options like "noxsave" and "noxsaves" So, command-line "foo bar noxsave" will match *BOTH* a "noxsave" and "noxsaves". (This turns out not to be an actual problem because "noxsave" implies "noxsaves", but it's still confusing.)

To fix this, we simplify the code and stop tracking 'len'. 'len' was trying to indicate either the NULL terminator *OR* the end of a non-NULL-terminated commandline at 'COMMAND_LINE_SIZE'. But, each of the three states is *already* checking 'cmdline' for a NULL terminator.

We _only_ need to check if we have overrun 'COMMAND_LINE_SIZE', and that we can do without keeping 'len' around.

Also add some commends to clarify what is going on.

---

 b/arch/x86/lib/cmdline.c |   34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)

diff -puN arch/x86/lib/cmdline.c~x86-broken-end-of-string-command-line-parsing arch/x86/lib/cmdline.c

arch/x86/lib/cmdline.c renamed from arch/x86/lib/cmdline.c~x86-broken-end-of-string-command-line-parsing +4 -3
@@ -21,12 +21,14 @@ static inline int myisspace(u8 c)
  * @option: option string to look for
  *
  * Returns the position of that @option (starts counting with 1)
- * or 0 on not found.
+ * or 0 on not found.  @option will only be found if it is found
+ * as an entire word in @cmdline.  For instance, if @option="car"
+ * then a cmdline which contains "cart" will not match.
  */
 int cmdline_find_option_bool(const char *cmdline, const char *option)
 {
 	char c;
-	int len, pos = 0, wstart = 0;
+	int pos = 0, wstart = 0;
 	const char *opptr = NULL;
 	enum {
 		st_wordstart = 0,	/* Start of word/after whitespace */
@@ -37,11 +39,14 @@ int cmdline_find_option_bool(const char
 	if (!cmdline)
 		return -1;      /* No command line */
 
-	len = min_t(int, strlen(cmdline), COMMAND_LINE_SIZE);

arch/x86/lib/cmdline.c int cmdline_find_option_bool(const char · line 49

Quoted 10 lines
arch/x86/lib/cmdline.c renamed from arch/x86/lib/cmdline.c~x86-broken-end-of-string-command-line-parsing +6 -2
-	if (!len)
+	if (!strlen(cmdline))
 		return 0;
 
-	while (len--) {
+	/*
+	 * This 'pos' check ensures we do not overrun
+	 * a non-NULL-terminated 'cmdline'
+	 */
+	while (pos < COMMAND_LINE_SIZE) {

So this is a little bit strange: you're checking strlen(cmdline) above but iterating until COMMAND_LINE_SIZE.

I think we should make it even more palatable:

while (pos < min_t(int, strlen(cmdline), COMMAND_LINE_SIZE))

so that it is obvious how far we iterate. I know, I know, it boils down to the same code because each state is checking !c but this way it is clearer what we're doing when someone looks at that code again.

Signature · 5 lines
Regards/Gruss,
    Boris.

SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
-- 
#5 lore raw

Re: [PATCH 1/5] x86: fix early command-line parsing when matching at end

In reply to Dave Hansen

Moving over to the actual patch we're talking about...

Quoted 5 lines · Dave Hansen
arch/x86/lib/cmdline.c renamed from arch/x86/lib/cmdline.c~x86-broken-end-of-string-command-line-parsing +0 -1
@@ -37,11 +39,14 @@ int cmdline_find_option_bool(const char
 	if (!cmdline)
 		return -1;      /* No command line */
 
-	len = min_t(int, strlen(cmdline), COMMAND_LINE_SIZE);

arch/x86/lib/cmdline.c int cmdline_find_option_bool(const char · line 49

Quoted 10 lines
arch/x86/lib/cmdline.c renamed from arch/x86/lib/cmdline.c~x86-broken-end-of-string-command-line-parsing +6 -2
-	if (!len)
+	if (!strlen(cmdline))
 		return 0;
 
-	while (len--) {
+	/*
+	 * This 'pos' check ensures we do not overrun
+	 * a non-NULL-terminated 'cmdline'
+	 */
+	while (pos < COMMAND_LINE_SIZE) {

Borislav says:

Quoted 7 lines
Quoted 7 lines

So why not leave the min_t:

	maxlen = min_t(int, strlen(cmdline), COMMAND_LINE_SIZE);

and then do:

while (pos < maxlen)

That works, and it's functionally equivalent to what I have (I think). I like what I have because it separates the 'strlen' checking from the COMMAND_LINE_SIZE checking, which makes it super duper obvious that the third patch here is OK.

So, after the third patch, the strlen() goes away any way you do this. The code ends up looking the same (the while() check is against the max-length variable alone).

I'm happy to rewrite this to have a different intermediate form and repost the thing, but the end result will be the same.

#6 lore raw

Re: [PATCH 1/5] x86: fix early command-line parsing when matching at end

Quoted 3 lines · Dave Hansen

So, after the third patch, the strlen() goes away any way you do this. The code ends up looking the same (the while() check is against the max-length variable alone).

True story.

Quoted 2 lines

I'm happy to rewrite this to have a different intermediate form and repost the thing, but the end result will be the same.

Yeah, no need.

Thanks.

Signature · 5 lines
Regards/Gruss,
    Boris.

SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
-- 
#7 lore raw

[tip:x86/boot] x86/boot: Fix early command-line parsing when matching at end

In reply to Dave Hansen
x86/boot: Fix early command-line parsing when matching at end
Commit-ID:  02afeaae9843733a39cd9b11053748b2d1dc5ae7
Gitweb:     http://git.kernel.org/tip/02afeaae9843733a39cd9b11053748b2d1dc5ae7
Author:     Dave Hansen <[email protected]>
AuthorDate: Tue, 22 Dec 2015 14:52:38 -0800
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 3 Feb 2016 12:03:15 +0100

x86/boot: Fix early command-line parsing when matching at end

The x86 early command line parsing in cmdline_find_option_bool() is buggy. If it matches a specified 'option' all the way to the end of the command-line, it will consider it a match.

For instance,

  cmdline = "foo";
  cmdline_find_option_bool(cmdline, "fool");

will return 1. This is particularly annoying since we have actual FPU options like "noxsave" and "noxsaves" So, command-line "foo bar noxsave" will match *BOTH* a "noxsave" and "noxsaves". (This turns out not to be an actual problem because "noxsave" implies "noxsaves", but it's still confusing.)

To fix this, we simplify the code and stop tracking 'len'. 'len' was trying to indicate either the NULL terminator *OR* the end of a non-NULL-terminated command line at 'COMMAND_LINE_SIZE'. But, each of the three states is *already* checking 'cmdline' for a NULL terminator.

We _only_ need to check if we have overrun 'COMMAND_LINE_SIZE', and that we can do without keeping 'len' around.

Also add some commends to clarify what is going on.

---
 arch/x86/lib/cmdline.c | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)
arch/x86/lib/cmdline.c +24 -10
index 422db00..49548be 100644
@@ -21,12 +21,14 @@ static inline int myisspace(u8 c)
  * @option: option string to look for
  *
  * Returns the position of that @option (starts counting with 1)
- * or 0 on not found.
+ * or 0 on not found.  @option will only be found if it is found
+ * as an entire word in @cmdline.  For instance, if @option="car"
+ * then a cmdline which contains "cart" will not match.
  */
 int cmdline_find_option_bool(const char *cmdline, const char *option)
 {
 	char c;
-	int len, pos = 0, wstart = 0;
+	int pos = 0, wstart = 0;
 	const char *opptr = NULL;
 	enum {
 		st_wordstart = 0,	/* Start of word/after whitespace */
@@ -37,11 +39,14 @@ int cmdline_find_option_bool(const char *cmdline, const char *option)
 	if (!cmdline)
 		return -1;      /* No command line */
 
-	len = min_t(int, strlen(cmdline), COMMAND_LINE_SIZE);
-	if (!len)
+	if (!strlen(cmdline))
 		return 0;
 
-	while (len--) {
+	/*
+	 * This 'pos' check ensures we do not overrun
+	 * a non-NULL-terminated 'cmdline'
+	 */
+	while (pos < COMMAND_LINE_SIZE) {
 		c = *(char *)cmdline++;
 		pos++;
 
@@ -58,17 +63,26 @@ int cmdline_find_option_bool(const char *cmdline, const char *option)
 			/* fall through */
 
 		case st_wordcmp:
-			if (!*opptr)
+			if (!*opptr) {
+				/*
+				 * We matched all the way to the end of the
+				 * option we were looking for.  If the
+				 * command-line has a space _or_ ends, then
+				 * we matched!
+				 */
 				if (!c || myisspace(c))
 					return wstart;
 				else
 					state = st_wordskip;
-			else if (!c)
+			} else if (!c) {
+				/*
+				 * Hit the NULL terminator on the end of
+				 * cmdline.
+				 */
 				return 0;
-			else if (c != *opptr++)
+			} else if (c != *opptr++) {
 				state = st_wordskip;
-			else if (!len)		/* last word and is matching */
-				return wstart;
+			}
 			break;
 
 		case st_wordskip:
#8 lore raw

PATCH 2/5 x86: fix early command-line parsing, when partial word match

In reply to Dave Hansen

From: Dave Hansen <[email protected]>

cmdline_find_option_bool() keeps track of position in two strings:
1. the command-line
2. the option we are searchign for in the command-line

We plow through each character in the command-line one at a time, always moving forward. We move forward in the option ('opptr') when we match characters in 'cmdline'. We reset the 'opptr' only when we go in to the 'st_wordstart' state.

But, if we fail to match an option because we see a space ( state=st_wordcmp, *opptr='\0',c=' '), we set state='st_wordskip' and 'break', moving to the next character. But, that move to the next character is the one *after* the ' '. This means that we will miss a 'st_wordstart' state.

For instance, if we have

	cmdline = "foo fool";

and are searching for "fool", we have:

	"fool"

opptr = ----^

"foo fool"
c = --------^

We see that 'l' != ' ', set state=st_wordskip, break, and then move 'c', so:

"foo fool"
c = ---------^

and are still in state=st_wordskip. We will stay in wordskip until we have skipped "fool", thus missing the option we were looking for. This *only* happens when you have a partially- matching word followed by a matching one.

To fix this, we always fall *into* the 'st_wordskip' state when we set it.

---

 b/arch/x86/lib/cmdline.c |   18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
arch/x86/lib/cmdline.c renamed from arch/x86/lib/cmdline.c~x86-mid-option-match-command-line-parsing +13 -5
diff -puN arch/x86/lib/cmdline.c~x86-mid-option-match-command-line-parsing arch/x86/lib/cmdline.c
@@ -72,18 +72,26 @@ int cmdline_find_option_bool(const char
 				 */
 				if (!c || myisspace(c))
 					return wstart;
-				else
-					state = st_wordskip;
+				/*
+				 * We hit the end of the option, but _not_
+				 * the end of a word on the cmdline.  Not
+				 * a match.
+				 */
 			} else if (!c) {
 				/*
 				 * Hit the NULL terminator on the end of
 				 * cmdline.
 				 */
 				return 0;
-			} else if (c != *opptr++) {
-				state = st_wordskip;
+			} else if (c == *opptr++) {
+				/*
+				 * We are currently matching, so continue
+				 * to the next character on the cmdline.
+				 */
+				break;
 			}
-			break;
+			state = st_wordskip;
+			/* fall through */
 
 		case st_wordskip:
 			if (!c)

_

#9 lore raw

[tip:x86/boot] x86/boot: Fix early command-line parsing when partial word matches

x86/boot: Fix early command-line parsing when partial word matches
Commit-ID:  abcdc1c694fa4055323cbec1cde4c2cb6b68398c
Gitweb:     http://git.kernel.org/tip/abcdc1c694fa4055323cbec1cde4c2cb6b68398c
Author:     Dave Hansen <[email protected]>
AuthorDate: Tue, 22 Dec 2015 14:52:39 -0800
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 3 Feb 2016 12:03:16 +0100

x86/boot: Fix early command-line parsing when partial word matches

cmdline_find_option_bool() keeps track of position in two strings:

1. the command-line
2. the option we are searchign for in the command-line

We plow through each character in the command-line one at a time, always moving forward. We move forward in the option ('opptr') when we match characters in 'cmdline'. We reset the 'opptr' only when we go in to the 'st_wordstart' state.

But, if we fail to match an option because we see a space (state=st_wordcmp, *opptr='\0',c=' '), we set state='st_wordskip' and 'break', moving to the next character. But, that move to the next character is the one *after* the ' '. This means that we will miss a 'st_wordstart' state.

For instance, if we have

  cmdline = "foo fool";

and are searching for "fool", we have:

	  "fool"

opptr = ----^

"foo fool"
c = --------^

We see that 'l' != ' ', set state=st_wordskip, break, and then move 'c', so:

"foo fool"
c = ---------^

and are still in state=st_wordskip. We will stay in wordskip until we have skipped "fool", thus missing the option we were looking for. This *only* happens when you have a partially- matching word followed by a matching one.

To fix this, we always fall *into* the 'st_wordskip' state when we set it.

---
 arch/x86/lib/cmdline.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
arch/x86/lib/cmdline.c +13 -5
index 49548be..ff8d1be 100644
@@ -72,18 +72,26 @@ int cmdline_find_option_bool(const char *cmdline, const char *option)
 				 */
 				if (!c || myisspace(c))
 					return wstart;
-				else
-					state = st_wordskip;
+				/*
+				 * We hit the end of the option, but _not_
+				 * the end of a word on the cmdline.  Not
+				 * a match.
+				 */
 			} else if (!c) {
 				/*
 				 * Hit the NULL terminator on the end of
 				 * cmdline.
 				 */
 				return 0;
-			} else if (c != *opptr++) {
-				state = st_wordskip;
+			} else if (c == *opptr++) {
+				/*
+				 * We are currently matching, so continue
+				 * to the next character on the cmdline.
+				 */
+				break;
 			}
-			break;
+			state = st_wordskip;
+			/* fall through */
 
 		case st_wordskip:
 			if (!c)
#10 lore raw

PATCH 3/5 x86: simplify early command line parsing

In reply to Dave Hansen

From: Dave Hansen <[email protected]>

__cmdline_find_option_bool() tries to account for both NULL-terminated and non-NULL-terminated strings. It keeps 'pos' to look for the end of the buffer and also looks for '!c' in a bunch of places to look for NULL termination.

But, it also calls strlen(). You can't call strlen on a non-NULL-terminated string.

If !strlen(cmdline), then cmdline[0]=='\0'. In that case, we will go in to the while() loop, set c='\0', hit st_wordstart, notice !c, and will immediately return 0.

So, remove the strlen(). It is unnecessary and unsafe.

---

 b/arch/x86/lib/cmdline.c |    3 ---
 1 file changed, 3 deletions(-)
arch/x86/lib/cmdline.c renamed from arch/x86/lib/cmdline.c~x86-early-command-line-non-term +0 -3
diff -puN arch/x86/lib/cmdline.c~x86-early-command-line-non-term arch/x86/lib/cmdline.c
@@ -39,9 +39,6 @@ int cmdline_find_option_bool(const char
 	if (!cmdline)
 		return -1;      /* No command line */
 
-	if (!strlen(cmdline))
-		return 0;
-
 	/*
 	 * This 'pos' check ensures we do not overrun
 	 * a non-NULL-terminated 'cmdline'

_

#11 lore raw

Re: [PATCH 3/5] x86: simplify early command line parsing

Quoted 32 lines · Dave Hansen

From: Dave Hansen <[email protected]>

__cmdline_find_option_bool() tries to account for both NULL-terminated and non-NULL-terminated strings. It keeps 'pos' to look for the end of the buffer and also looks for '!c' in a bunch of places to look for NULL termination.

But, it also calls strlen(). You can't call strlen on a non-NULL-terminated string.

If !strlen(cmdline), then cmdline[0]=='\0'. In that case, we will go in to the while() loop, set c='\0', hit st_wordstart, notice !c, and will immediately return 0.

So, remove the strlen(). It is unnecessary and unsafe.

---

 b/arch/x86/lib/cmdline.c |    3 ---
 1 file changed, 3 deletions(-)

diff -puN arch/x86/lib/cmdline.c~x86-early-command-line-non-term arch/x86/lib/cmdline.c

arch/x86/lib/cmdline.c int cmdline_find_option_bool(const char · line 44

Quoted 6 lines
arch/x86/lib/cmdline.c renamed from arch/x86/lib/cmdline.c~x86-early-command-line-non-term +0 -3
@@ -39,9 +39,6 @@ int cmdline_find_option_bool(const char
 	if (!cmdline)
 		return -1;      /* No command line */
 
-	if (!strlen(cmdline))
-		return 0;
-

Patch 1 adds the strlen(), this patch removes it. Please merge both patches.

Signature · 5 lines
Regards/Gruss,
    Boris.

SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
-- 
#12 lore raw

Re: [PATCH 3/5] x86: simplify early command line parsing

Quoted 11 lines · Borislav Petkov
Quoted 10 lines

arch/x86/lib/cmdline.c int cmdline_find_option_bool(const char · line 44

Quoted 10 lines

diff -puN arch/x86/lib/cmdline.c~x86-early-command-line-non-term arch/x86/lib/cmdline.c

arch/x86/lib/cmdline.c renamed from arch/x86/lib/cmdline.c~x86-early-command-line-non-term +0 -3
@@ -39,9 +39,6 @@ int cmdline_find_option_bool(const char
 	if (!cmdline)
 		return -1;      /* No command line */
 
-	if (!strlen(cmdline))
-		return 0;
-

Patch 1 adds the strlen(), this patch removes it. Please merge both patches.

Yes, it adds a line that uses strlen. But, there was already the use in the min_t() "call":

Review

Quoted 4 lines
(quoted diff) +1 -2
-	len = min_t(int, strlen(cmdline), COMMAND_LINE_SIZE);
-	if (!len)
+	if (!strlen(cmdline))
 		return 0;

I wanted to separate the two things because they really fix two separate issues. I also want to ensure that any problems that this patch causes can be isolated to the smallest change possible.

#13 lore raw

Re: [PATCH 3/5] x86: simplify early command line parsing

In reply to Borislav Petkov
Quoted 4 lines · Borislav Petkov
Quoted 3 lines

arch/x86/lib/cmdline.c int cmdline_find_option_bool(const char · line 44

Quoted 3 lines
arch/x86/lib/cmdline.c renamed from arch/x86/lib/cmdline.c~x86-early-command-line-non-term +0 -3
-	if (!strlen(cmdline))
-		return 0;
-

Patch 1 adds the strlen(), this patch removes it. Please merge both patches.

As I mentioned, it doesn't strictly add it.

Plus, if I go add this back to that patch, I'll end up having to paste most of the changelog in to that one. I think it's much more clear to have this hunk live on its own.

#14 lore raw

Re: [PATCH 3/5] x86: simplify early command line parsing

Quoted 11 lines · Dave Hansen
Quoted 4 lines · Borislav Petkov
Quoted 3 lines

arch/x86/lib/cmdline.c int cmdline_find_option_bool(const char · line 44

Quoted 3 lines
arch/x86/lib/cmdline.c renamed from arch/x86/lib/cmdline.c~x86-early-command-line-non-term +0 -3
-	if (!strlen(cmdline))
-		return 0;
-

Patch 1 adds the strlen(), this patch removes it. Please merge both patches.

As I mentioned, it doesn't strictly add it.

Plus, if I go add this back to that patch, I'll end up having to paste most of the changelog in to that one. I think it's much more clear to have this hunk live on its own.

So why not leave the min_t:

	maxlen = min_t(int, strlen(cmdline), COMMAND_LINE_SIZE);

and then do:

while (pos < maxlen)

?

This way it is absolutely clear what's going on.

Signature · 5 lines
Regards/Gruss,
    Boris.

SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
-- 
#15 lore raw

[tip:x86/boot] x86/boot: Simplify early command line parsing

In reply to Dave Hansen
x86/boot: Simplify early command line parsing
Commit-ID:  4de07ea481361b08fe13735004dafae862482d38
Gitweb:     http://git.kernel.org/tip/4de07ea481361b08fe13735004dafae862482d38
Author:     Dave Hansen <[email protected]>
AuthorDate: Tue, 22 Dec 2015 14:52:41 -0800
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 3 Feb 2016 12:03:17 +0100

x86/boot: Simplify early command line parsing

__cmdline_find_option_bool() tries to account for both NULL-terminated and non-NULL-terminated strings. It keeps 'pos' to look for the end of the buffer and also looks for '!c' in a bunch of places to look for NULL termination.

But, it also calls strlen(). You can't call strlen on a non-NULL-terminated string.

If !strlen(cmdline), then cmdline[0]=='\0'. In that case, we will go in to the while() loop, set c='\0', hit st_wordstart, notice !c, and will immediately return 0.

So, remove the strlen(). It is unnecessary and unsafe.

---
 arch/x86/lib/cmdline.c | 3 ---
 1 file changed, 3 deletions(-)
arch/x86/lib/cmdline.c +0 -3
index ff8d1be..945a639 100644
@@ -39,9 +39,6 @@ int cmdline_find_option_bool(const char *cmdline, const char *option)
 	if (!cmdline)
 		return -1;      /* No command line */
 
-	if (!strlen(cmdline))
-		return 0;
-
 	/*
 	 * This 'pos' check ensures we do not overrun
 	 * a non-NULL-terminated 'cmdline'
#16 lore raw

PATCH 4/5 x86: pass in size to early cmdline parsing

In reply to Dave Hansen

From: Dave Hansen <[email protected]>

We will use this in a few patches to implement tests for early parsing.

---

 b/arch/x86/lib/cmdline.c |   11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
arch/x86/lib/cmdline.c renamed from arch/x86/lib/cmdline.c~pass-in-cmdline-size +9 -2
diff -puN arch/x86/lib/cmdline.c~pass-in-cmdline-size arch/x86/lib/cmdline.c
@@ -25,7 +25,8 @@ static inline int myisspace(u8 c)
  * as an entire word in @cmdline.  For instance, if @option="car"
  * then a cmdline which contains "cart" will not match.
  */
-int cmdline_find_option_bool(const char *cmdline, const char *option)
+static int __cmdline_find_option_bool(const char *cmdline,
+		int max_cmdline_size, const char *option)
 {
 	char c;
 	int pos = 0, wstart = 0;
@@ -43,7 +44,7 @@ int cmdline_find_option_bool(const char
 	 * This 'pos' check ensures we do not overrun
 	 * a non-NULL-terminated 'cmdline'
 	 */
-	while (pos < COMMAND_LINE_SIZE) {
+	while (pos < max_cmdline_size) {
 		c = *(char *)cmdline++;
 		pos++;
 
@@ -101,3 +102,9 @@ int cmdline_find_option_bool(const char
 
 	return 0;	/* Buffer overrun */
 }
+
+int cmdline_find_option_bool(const char *cmdline, const char *option)
+{
+	return __cmdline_find_option_bool(cmdline, COMMAND_LINE_SIZE,
+			option);
+}

_

#17 lore raw

[tip:x86/boot] x86/boot: Pass in size to early cmdline parsing

x86/boot: Pass in size to early cmdline parsing
Commit-ID:  8c0517759a1a100a8b83134cf3c7f254774aaeba
Gitweb:     http://git.kernel.org/tip/8c0517759a1a100a8b83134cf3c7f254774aaeba
Author:     Dave Hansen <[email protected]>
AuthorDate: Tue, 22 Dec 2015 14:52:43 -0800
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 3 Feb 2016 12:03:18 +0100

x86/boot: Pass in size to early cmdline parsing

We will use this in a few patches to implement tests for early parsing.

[ Aligned args properly. ]
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 arch/x86/lib/cmdline.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
arch/x86/lib/cmdline.c +9 -2
index 945a639..5cc78bf 100644
@@ -25,7 +25,9 @@ static inline int myisspace(u8 c)
  * as an entire word in @cmdline.  For instance, if @option="car"
  * then a cmdline which contains "cart" will not match.
  */
-int cmdline_find_option_bool(const char *cmdline, const char *option)
+static int
+__cmdline_find_option_bool(const char *cmdline, int max_cmdline_size,
+			   const char *option)
 {
 	char c;
 	int pos = 0, wstart = 0;
@@ -43,7 +45,7 @@ int cmdline_find_option_bool(const char *cmdline, const char *option)
 	 * This 'pos' check ensures we do not overrun
 	 * a non-NULL-terminated 'cmdline'
 	 */
-	while (pos < COMMAND_LINE_SIZE) {
+	while (pos < max_cmdline_size) {
 		c = *(char *)cmdline++;
 		pos++;
 
@@ -101,3 +103,8 @@ int cmdline_find_option_bool(const char *cmdline, const char *option)
 
 	return 0;	/* Buffer overrun */
 }
+
+int cmdline_find_option_bool(const char *cmdline, const char *option)
+{
+	return __cmdline_find_option_bool(cmdline, COMMAND_LINE_SIZE, option);
+}
#18 lore raw

PATCH 5/5 x86: test early command-line code

In reply to Dave Hansen

From: Dave Hansen <[email protected]>

Here are some simple tests for the early command-line code. It had way more bugs than it should have, so let's make sure they never pop up again.

Note, there are a few failures in here now. We will fix those up in the next few patches.

This is complete overkill for this code, but I had to do it to convince myself that I wasn't making it any worse.

---

 b/arch/x86/Kconfig.debug  |    7 ++
 b/arch/x86/kernel/check.c |    1 
 b/arch/x86/kernel/setup.c |    9 +++
 b/arch/x86/lib/cmdline.c  |  122 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 138 insertions(+), 1 deletion(-)
arch/x86/Kconfig.debug renamed from arch/x86/Kconfig.debug~x86-early-command-line-test +7 -0
diff -puN arch/x86/Kconfig.debug~x86-early-command-line-test arch/x86/Kconfig.debug
@@ -400,4 +400,11 @@ config PUNIT_ATOM_DEBUG
 	  The current power state can be read from
 	  /sys/kernel/debug/punit_atom/dev_power_state
 
+config X86_TEST_EARLY_CMDLINE
+	bool "Early command line runtime tests"
+	---help---
+	  This creates some test command-lines and tries to parse
+	  a bunch of options from them.  The overhead is small both
+	  at boot and increased text/data sizes.
+
 endmenu
arch/x86/kernel/check.c renamed from arch/x86/kernel/check.c~x86-early-command-line-test +0 -1
diff -puN arch/x86/kernel/check.c~x86-early-command-line-test arch/x86/kernel/check.c
@@ -164,4 +164,3 @@ static int start_periodic_check_for_corr
 	return 0;
 }
 device_initcall(start_periodic_check_for_corruption);
-
arch/x86/lib/cmdline.c renamed from arch/x86/lib/cmdline.c~x86-early-command-line-test +122 -0

131 lines not on this page · raw message

arch/x86/kernel/setup.c renamed from arch/x86/kernel/setup.c~x86-early-command-line-test +9 -0
diff -puN arch/x86/kernel/setup.c~x86-early-command-line-test arch/x86/kernel/setup.c
@@ -1282,3 +1282,12 @@ static int __init register_kernel_offset
 	return 0;
 }
 __initcall(register_kernel_offset_dumper);
+
+/*
+ * This code is in lib/ and we do not link initcalls from there.
+ * Stash it here instead.
+ */
+#ifdef CONFIG_X86_TEST_EARLY_CMDLINE
+int test_early_cmdline(void);
+late_initcall(test_early_cmdline);
+#endif /* CONFIG_X86_TEST_EARLY_CMDLINE */

_

#19 lore raw

Re: [PATCH 5/5] x86: test early command-line code

Quoted 19 lines · Dave Hansen

From: Dave Hansen <[email protected]>

Here are some simple tests for the early command-line code. It had way more bugs than it should have, so let's make sure they never pop up again.

Note, there are a few failures in here now. We will fix those up in the next few patches.

This is complete overkill for this code, but I had to do it to convince myself that I wasn't making it any worse.

Quoted 6 lines

---

 b/arch/x86/Kconfig.debug  |    7 ++
 b/arch/x86/kernel/check.c |    1 
 b/arch/x86/kernel/setup.c |    9 +++
 b/arch/x86/lib/cmdline.c  |  122 ++++++++++++++++++++++++++++++++++++++++++++++

First 4 applied, thanks.

This one belongs into tools/testing/selftests/x86/, AFAICT, and can be run in userspace just the same.

Signature · 5 lines
Regards/Gruss,
    Boris.

SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
--