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

Unified · Split

kernel/kprobes.c+6−22
@@ -2770,30 +2770,14 @@ static ssize_t read_enabled_file_bool(struct file *file, · 2769 unmodified lines
2770static ssize_t write_enabled_file_bool(struct file *file,2770static ssize_t write_enabled_file_bool(struct file *file,
2771 const char __user *user_buf, size_t count, loff_t *ppos)2771 const char __user *user_buf, size_t count, loff_t *ppos)
2772{2772{
2773 char buf[32];2773 bool enable;
2774 size_t buf_size;2774 int ret;
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;
2780 2775
2781 buf[buf_size] = '\0';2776 ret = kstrtobool_from_user(user_buf, count, &enable);
2782 switch (buf[0]) {2777 if (ret)
2783 case 'y':2778 return ret;
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 }
2796 2779
2780 ret = enable ? arm_all_kprobes() : disarm_all_kprobes();
2797 if (ret)2781 if (ret)
2798 return ret;2782 return ret;
2799 2783