The simple_strncmp_to_buffer() function provides an easier method for developers to compare a kernel space buffer against user space data. This process is done in a few drivers and may be simplified to a single function. Signed-off-by: Amber Thrall <amber@xxxxxxxxx> --- fs/libfs.c | 33 +++++++++++++++++++++++++++++++++ include/linux/fs.h | 1 + 2 files changed, 34 insertions(+) diff --git a/fs/libfs.c b/fs/libfs.c index 0ca80b2..d588f2d 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -639,6 +639,39 @@ ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos, EXPORT_SYMBOL(simple_write_to_buffer); +/** + * simple_strncmp_to_buffer - compare data from user space to a buffer + * @to: the buffer to compare to + * @from: the user space buffer to read from + * @count: the maximum number of bytes to check + * + * The simple_strncmp_to_buffer() function compares a buffer and a user space + * buffer. This is similar to strncmp() but between kernel and user space + * buffers. + * + * On success, an integer less than, equal to, or greater than zero if @to (or + * the first @count bytes) is found, respectively, to be less than, to match, or + * be greater than @from. A negative value is returned on error. + **/ +int simple_strncmp_to_buffer(void *to, const void __user *from, size_t count) +{ + size_t res; + char *data; + + data = kmalloc(count + 1, GFP_KERNEL); + if (!data) + return -ENOMEM; + res = copy_from_user(data, from, count); + if (res) { + kfree(data); + return -EFAULT; + } + + res = strncmp(data, to, count); + kfree(data); + return res; +} +EXPORT_SYMBOL(simple_strncmp_to_buffer); + /** * memory_read_from_buffer - copy data from the buffer * @to: the kernel space buffer to read to * @count: the maximum number of bytes to read diff --git a/include/linux/fs.h b/include/linux/fs.h index 2a15fe2..c7eac75 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2903,6 +2903,7 @@ extern ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos, const void *from, size_t available); extern ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos, const void __user *from, size_t count); +extern int simple_strncmp_to_buffer(void *to, const void __user *from, + size_t count); extern int __generic_file_fsync(struct file *, loff_t, loff_t, int); extern int generic_file_fsync(struct file *, loff_t, loff_t, int); -- 2.7.2 -- To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html