History

kprobes: Use helper to parse boolean input from userspace

The "enabled" file provides a debugfs interface to arm / disarm kprobes in the kernel. In order to parse the buffer containing the values written from userspace, the callback manually parses the user input to convert it to a boolean value. As taking a string value from userspace and converting it to boolean is a common operation, a helper kstrtobool_from_user() is already available in the kernel. Update the callback to use the common helper to parse the write buffer from userspace. Link: https://lkml.kernel.org/r/163163032637.489837.10678039554832855327.stgit@devnote2

Punit Agrawal committed 5 years ago

Commit 5d6de7d1 parent 8f7262c1 file · +6 −22Browse files

Showing the changes to kernel (1 of 1 file). Show all

Unified · Split

kernel/kprobes.c+6−22
@@ -2770,30 +2770,14 @@ static ssize_t read_enabled_file_bool(struct file *file, · 2769 unmodified lines
27702770 static ssize_t write_enabled_file_bool(struct file *file,
27712771 const char __user *user_buf, size_t count, loff_t *ppos)
27722772 {
2773− char buf[32];
2774− size_t buf_size;
2775− int ret = 0;
2776−
2777− buf_size = min(count, (sizeof(buf)-1));
2778− if (copy_from_user(buf, user_buf, buf_size))
2779− return -EFAULT;
2773+ bool enable;
2774+ int ret;
27802775
2781− buf[buf_size] = '\0';
2782− switch (buf[0]) {
2783− case 'y':
2784− case 'Y':
2785− case '1':
2786− ret = arm_all_kprobes();
2787− break;
2788− case 'n':
2789− case 'N':
2790− case '0':
2791− ret = disarm_all_kprobes();
2792− break;
2793− default:
2794− return -EINVAL;
2795− }
2776+ ret = kstrtobool_from_user(user_buf, count, &enable);
2777+ if (ret)
2778+ return ret;
27962779
2780+ ret = enable ? arm_all_kprobes() : disarm_all_kprobes();
27972781 if (ret)
27982782 return ret;
27992783