Skip to contentSkip to conversation

lib/vsprintf: add default case to 'i' specifier

PATCH In review Tobin C. Harding 3 participants 5 messages Oct 9, 2017 – Oct 10, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/vsprintf: add default case to 'i' specifier

%pi leaks kernel addresses if incorrectly specified.

Currently the printk specifier %pi (%pI) contains a switch statement without a default clause. The %pi specifier requires a subsequent character (4, 6, or S) controlling the output. If the specifier is incomplete the switch statement will fall through and print the variable argument address in hex instead of the value of the argument (as an IP address).

If uncaught this leaks kernel addresses into dmesg. We can return an error string to make the bug visible and stop addresses leaking.

Add a default clause returning an error string, stops leaking addresses and makes the buggy code

---
 lib/vsprintf.c | 2 ++
 1 file changed, 2 insertions(+)
lib/vsprintf.c +2 -0
index 86c3385b9eb3..155702f05b14 100644
@@ -1775,6 +1775,8 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
 			default:
 				return string(buf, end, "(invalid address)", spec);
 			}}
+		default:
+			return string(buf, end, "(invalid specifier, form: %pi4)", spec);
 		}
 		break;
 	case 'E':
Signature · 1 line
2.7.4
#1 lore raw
Quoted 9 lines · Tobin C. Harding

%pi leaks kernel addresses if incorrectly specified.

Currently the printk specifier %pi (%pI) contains a switch statement without a default clause. The %pi specifier requires a subsequent character (4, 6, or S) controlling the output. If the specifier is incomplete the switch statement will fall through and print the variable argument address in hex instead of the value of the argument (as an IP address).

Quoted 5 lines

If uncaught this leaks kernel addresses into dmesg. We can return an error string to make the bug visible and stop addresses leaking.

Add a default clause returning an error string, stops leaking addresses and makes the buggy code

...? :)

Quoted 10 lines
---
 lib/vsprintf.c | 2 ++
 1 file changed, 2 insertions(+)
lib/vsprintf.c +0 -0
index 86c3385b9eb3..155702f05b14 100644

lib/vsprintf.c char *pointer(const char *fmt, char *buf, char *end, void *ptr, · line 1778

Quoted 4 lines
lib/vsprintf.c +1 -0
@@ -1775,6 +1775,8 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
 			default:
 				return string(buf, end, "(invalid address)", spec);
 			}}
+		default:

Maybe a WARN(1, "invalid pointer format")? That way it'll be easy for people to figure out where to fix.

Cheers,

Tycho

Quoted 7 lines
lib/vsprintf.c +1 -0
+			return string(buf, end, "(invalid specifier, form: %pi4)", spec);
 		}
 		break;
 	case 'E':
Signature · 1 line
2.7.4
#2 lore raw
Quoted 23 lines · Tycho Andersen
Quoted 9 lines · Tobin C. Harding

%pi leaks kernel addresses if incorrectly specified.

Currently the printk specifier %pi (%pI) contains a switch statement without a default clause. The %pi specifier requires a subsequent character (4, 6, or S) controlling the output. If the specifier is incomplete the switch statement will fall through and print the variable argument address in hex instead of the value of the argument (as an IP address).

Quoted 5 lines

If uncaught this leaks kernel addresses into dmesg. We can return an error string to make the bug visible and stop addresses leaking.

Add a default clause returning an error string, stops leaking addresses and makes the buggy code

...? :)

Quoted 5 lines
---
 lib/vsprintf.c | 2 ++
 1 file changed, 2 insertions(+)
Quoted 12 lines

lib/vsprintf.c char *pointer(const char *fmt, char *buf, char *end, void *ptr, · line 1778

Quoted 9 lines
lib/vsprintf.c +1 -0
index 86c3385b9eb3..155702f05b14 100644
@@ -1775,6 +1775,8 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
 			default:
 				return string(buf, end, "(invalid address)", spec);
 			}}
+		default:

Maybe a WARN(1, "invalid pointer format")? That way it'll be easy for people to figure out where to fix.

Thanks for the review. vsprintf.c uses custom error return logic so as not to create a function call cycle, I assume the printf versions of WARN call into vsprintf to do their formatting also. Hence (and without studying the WARN code) I avoided the printf versions of WARN.

Open to correction if I am wrong.

thanks,
Tobin.

#3 lore raw
In reply to Tobin C. Harding
Quoted 1 line · Tobin C. Harding

%pi leaks kernel addresses if incorrectly specified.

Are there any uses that are incorrectly specified? grep doesn't show any.

Quoted 7 lines

Currently the printk specifier %pi (%pI) contains a switch statement without a default clause. The %pi specifier requires a subsequent character (4, 6, or S) controlling the output. If the specifier is incomplete the switch statement will fall through and print the variable argument address in hex instead of the value of the argument (as an IP address).

Quoted 5 lines

If uncaught this leaks kernel addresses into dmesg. We can return an error string to make the bug visible and stop addresses leaking.

Add a default clause returning an error string, stops leaking addresses and makes the buggy code

[]

lib/vsprintf.c

Quoted 1 line
lib/vsprintf.c +0 -0

[]

lib/vsprintf.c char *pointer(const char *fmt, char *buf, char *end, void *ptr, · line 1782

Quoted 9 lines
lib/vsprintf.c +2 -0
@@ -1775,6 +1775,8 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
 			default:
 				return string(buf, end, "(invalid address)", spec);
 			}}
+		default:
+			return string(buf, end, "(invalid specifier, form: %pi4)", spec);
 		}
 		break;
 	case 'E':

I'm not sure this is a big deal and
maybe a better way to handle it is to
move the %pK, case 'K': block and add
a fallthrough or keep the case 'K':
block where it is and add a goto.

#4 lore raw
Quoted 5 lines · Joe Perches
Quoted 1 line · Tobin C. Harding

%pi leaks kernel addresses if incorrectly specified.

Are there any uses that are incorrectly specified? grep doesn't show any.

You are correct I don't see any.

Quoted 15 lines
Quoted 7 lines

Currently the printk specifier %pi (%pI) contains a switch statement without a default clause. The %pi specifier requires a subsequent character (4, 6, or S) controlling the output. If the specifier is incomplete the switch statement will fall through and print the variable argument address in hex instead of the value of the argument (as an IP address).

Quoted 5 lines

If uncaught this leaks kernel addresses into dmesg. We can return an error string to make the bug visible and stop addresses leaking.

Add a default clause returning an error string, stops leaking addresses and makes the buggy code

[]

lib/vsprintf.c

Quoted 1 line
lib/vsprintf.c +0 -0

[]

Quoted 15 lines

lib/vsprintf.c char *pointer(const char *fmt, char *buf, char *end, void *ptr, · line 1782

Quoted 9 lines
lib/vsprintf.c +2 -0
@@ -1775,6 +1775,8 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
 			default:
 				return string(buf, end, "(invalid address)", spec);
 			}}
+		default:
+			return string(buf, end, "(invalid specifier, form: %pi4)", spec);
 		}
 		break;
 	case 'E':

I'm not sure this is a big deal and
maybe a better way to handle it is to
move the %pK, case 'K': block and add
a fallthrough or keep the case 'K':
block where it is and add a goto.

Thanks for suggestions.

Tobin.