Skip to contentSkip to conversation

kprobes: handle empty/invalid input to debugfs "enabled" file

PATCH In review Mathias Krause 2 participants 2 messages May 31, 2013 – Jun 3, 2013 kprobes

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kprobes: handle empty/invalid input to debugfs "enabled" file

When writing invalid input to 'debug/kprobes/enabled' it'll silently be ignored. Even worse, when writing an empty string to this file, the outcome is purely random as the switch statement will make its decision based on the value of an uninitialized stack variable.

Fix this by handling invalid/empty input as error returning -EINVAL.

Cc Ananth N Mavinakayanahalli <[email protected]>
Cc Anil S Keshavamurthy <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Andrew Morton <[email protected]>
---
 kernel/kprobes.c |    3 +++
 1 file changed, 3 insertions(+)
kernel/kprobes.c +3 -0
index 3fed7f0..948b597 100644
@@ -2322,6 +2322,7 @@ static ssize_t write_enabled_file_bool(struct file *file,
 	if (copy_from_user(buf, user_buf, buf_size))
 		return -EFAULT;
 
+	buf[buf_size] = '\0';
 	switch (buf[0]) {
 	case 'y':
 	case 'Y':
@@ -2333,6 +2334,8 @@ static ssize_t write_enabled_file_bool(struct file *file,
 	case '0':
 		disarm_all_kprobes();
 		break;
+	default:
+		return -EINVAL;
 	}
 
 	return count;
Signature · 1 line
1.7.10.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Mathias Krause

When writing invalid input to 'debug/kprobes/enabled' it'll silently be ignored. Even worse, when writing an empty string to this file, the outcome is purely random as the switch statement will make its decision based on the value of an uninitialized stack variable.

Oops, right.

Quoted 2 lines

Fix this by handling invalid/empty input as error returning -EINVAL.

Thanks!

Quoted 33 lines
Cc Ananth N Mavinakayanahalli <[email protected]>
Cc Anil S Keshavamurthy <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Andrew Morton <[email protected]>
---
 kernel/kprobes.c |    3 +++
 1 file changed, 3 insertions(+)
kernel/kprobes.c +3 -0
index 3fed7f0..948b597 100644
@@ -2322,6 +2322,7 @@ static ssize_t write_enabled_file_bool(struct file *file,
 	if (copy_from_user(buf, user_buf, buf_size))
 		return -EFAULT;
 
+	buf[buf_size] = '\0';
 	switch (buf[0]) {
 	case 'y':
 	case 'Y':
@@ -2333,6 +2334,8 @@ static ssize_t write_enabled_file_bool(struct file *file,
 	case '0':
 		disarm_all_kprobes();
 		break;
+	default:
+		return -EINVAL;
 	}
 
 	return count;
Signature · 4 lines
Masami HIRAMATSU
IT Management Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]