linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] fs: strncmp() for user space buffers
@ 2016-02-28 22:50 Amber Thrall
  2016-02-28 23:03 ` Al Viro
  0 siblings, 1 reply; 6+ messages in thread
From: Amber Thrall @ 2016-02-28 22:50 UTC (permalink / raw)
  To: Alexander Viro; +Cc: linux-fsdevel, Amber Thrall

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@thrall.me>
---
 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

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-02-29 15:41 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-02-28 22:50 [PATCH 1/1] fs: strncmp() for user space buffers Amber Thrall
2016-02-28 23:03 ` Al Viro
2016-02-28 23:10   ` Al Viro
2016-02-28 23:39   ` Amber Thrall
2016-02-29  2:10     ` Al Viro
2016-02-29 15:41       ` Amber Thrall

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).