From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org
Cc: Ingo Molnar <mingo@elte.hu>,
Andrew Morton <akpm@linux-foundation.org>,
Peter Zijlstra <peterz@infradead.org>,
Frederic Weisbecker <fweisbec@gmail.com>,
"H. Peter Anvin" <hpa@zytor.com>,
Steven Rostedt <srostedt@redhat.com>
Subject: [PATCH v3 3/6] uaccess: add copy_strtok_from_user
Date: Thu, 26 Feb 2009 13:54:22 -0500 [thread overview]
Message-ID: <20090226185901.320018313@goodmis.org> (raw)
In-Reply-To: 20090226185419.371469287@goodmis.org
[-- Attachment #1: 0003-uaccess-add-copy_strtok_from_user.patch --]
[-- Type: text/plain, Size: 6289 bytes --]
From: Steven Rostedt <srostedt@redhat.com>
The ftrace utility reads delimited tokens from user space.
Andrew Morton did not like how ftrace open coded this. He had
a good point since more than one location performed this feature.
This patch creates a copy_strtok_from_user function that can copy
a delimited token from user space. This puts the code in the
lib/uaccess.c file. This keeps the code in a single location
and may be optimized in the future.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
---
include/linux/uaccess.h | 4 +
lib/uaccess.c | 152 ++++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 155 insertions(+), 1 deletions(-)
diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
index 6b58367..0a3faf5 100644
--- a/include/linux/uaccess.h
+++ b/include/linux/uaccess.h
@@ -106,4 +106,8 @@ extern long probe_kernel_read(void *dst, void *src, size_t size);
*/
extern long probe_kernel_write(void *dst, void *src, size_t size);
+extern int copy_strtok_from_user(void *to, const void __user *from,
+ unsigned int copy, unsigned int read,
+ unsigned int *copied, const char *delim);
+
#endif /* __LINUX_UACCESS_H__ */
diff --git a/lib/uaccess.c b/lib/uaccess.c
index ac40796..a55db23 100644
--- a/lib/uaccess.c
+++ b/lib/uaccess.c
@@ -1,8 +1,19 @@
/*
- * Access kernel memory without faulting.
+ * lib/uaccess.c
+ * Generic memory access without faulting.
+ *
+ * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
+ *
+ * Added copy_strtok_from_user -
+ * Copyright (C) 2009 Red Hat, Inc., Steven Rostedt <srostedt@redhat.com>
+ *
+ * This source code is licensed under the GNU General Public License,
+ * Version 2. See the file COPYING for more details.
+ *
*/
#include <linux/uaccess.h>
#include <linux/module.h>
+#include <linux/ctype.h>
#include <linux/mm.h>
/**
@@ -53,3 +64,142 @@ long probe_kernel_write(void *dst, void *src, size_t size)
return ret ? -EFAULT : 0;
}
EXPORT_SYMBOL_GPL(probe_kernel_write);
+
+#define USER_BUF_SIZE 127
+
+/**
+ * copy_strtok_from_user - copy a delimited token from user space
+ * @to: The location to copy to
+ * @from: The location to copy from
+ * @copy: The number of bytes to copy
+ * @read: The number of bytes to read
+ * @copied: The number of bytes actually written to @to
+ * @delim: NULL terminated string of character delimiters
+ *
+ * This reads from a user buffer, a delimited token.
+ * It places the token into @to if one is found, up to the number of
+ * bytes specified by @copy. It adds a "\0" terminating character
+ * into @to if a delimiter is found after the token. Otherwise the
+ * @to does not end with a \0 delimiter.
+ * If only delimiters are found, @to is not modified and
+ * @copied will be zero.
+ *
+ * It reads at most @read bytes from the user @from buffer, and
+ * will copy at most @copy bytes into the kernel @to buffer.
+ *
+ * Example of use:
+ *
+ * in user space a write(fd, "foo bar zot", 12) is done. We want to
+ * read three words.
+ *
+ * len = 12; - length of user buffer
+ * ret = copy_strtok_from_user(buf, ubuf, 100, len, @copied, " ");
+ * ret will equal 3 ("foo" read - " " is also read but not counted.)
+ * buf will contain "foo\0"
+ * copied will equal 4 ("foo\0" written)
+ *
+ * len -= ret; - 3 bytes was read
+ * read = ret;
+ * ret = copy_strtok_from_user(buf, ubuf+read, 100, len, @copied, " ");
+ * ret will equal 5 (" bar" read, notice the double space between
+ * foo and bar in the original write.)
+ * buf will contain "bar\0"
+ * copied will equal 4 ("bar\0" written)
+ *
+ * len -= ret; - 5 bytes read
+ * read += ret;
+ * ret = copy_strtok_from_user(buf, ubuf+read, 100, len, @copied, " ");
+ * ret = 4 (" zot" read)
+ * buf will contain "zot" (notice that it does not contain \0)
+ * copied will equal 3 ("zot" written)
+ * The buf only has "zot" because no terminating delimiter was
+ * encountered.
+ *
+ * Returns:
+ * The number of bytes read from user space (@from). This may or may not
+ * be the same as what was copied into @to.
+ *
+ * if (@to[*@copied - 1] == '\0')
+ * token ended with a delimiter
+ * else
+ * token in @from was not followed by a delimiter
+ *
+ * @copied will contain the number of bytes written into @to.
+ *
+ * Note, if the token that was found was bigger than @to,
+ * @copied will equal copy, and @to[*@copied - 1] will not be '\0'
+ *
+ * -EFAULT, if we faulted during any part of the copy.
+ * @copied will be undefined.
+ */
+int copy_strtok_from_user(void *to, const void __user *from,
+ unsigned int copy, unsigned int read,
+ unsigned int *copied, const char *delim)
+{
+ unsigned int have_read = 0;
+ unsigned int have_copied = 0;
+ char user[USER_BUF_SIZE + 1];
+ char *token, *ptr;
+ int ret, cnt;
+
+ do {
+ cnt = USER_BUF_SIZE;
+
+ if (cnt > read - have_read)
+ cnt = read - have_read;
+
+ ret = copy_from_user(user, from + have_read, cnt);
+ if (ret)
+ return ret;
+
+ user[cnt] = 0;
+ ptr = user;
+ have_read += cnt;
+
+ token = strtok_r(ptr, delim, &ptr);
+ if (token) {
+ strncpy(to + have_copied, token, copy - have_copied);
+ have_copied += strlen(token);
+
+ /*
+ * We do not want to report that we read past the
+ * token.
+ */
+ have_read -= cnt - (ptr - user);
+
+ /*
+ * Check if the token was larger than the supplied
+ * buffer.
+ */
+ if (have_copied >= copy) {
+ /* do not report the part not copied */
+ have_read -= have_copied - copy;
+ /* account for the delim read by ptr */
+ if (ptr[-1] == '\0')
+ have_read--;
+ have_copied = copy;
+ break;
+ }
+
+ /*
+ * Now the tricky part. We must determine if the token
+ * was split between two reads from userspace.
+ * If strtok_r found a delimiter after the token, then
+ * the previous character to ptr would be NULL.
+ * Note: if strtok_r returned a token, ptr must be
+ * greater than user.
+ */
+ if (!ptr[-1]) {
+ /* add '\0' */
+ have_copied++;
+ break;
+ }
+ }
+
+ } while (have_read < read && have_copied < copy);
+
+ *copied = have_copied;
+
+ return have_read;
+}
+EXPORT_SYMBOL_GPL(copy_strtok_from_user);
--
1.5.6.5
--
next prev parent reply other threads:[~2009-02-26 19:01 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-02-26 18:54 [PATCH v3 0/6] [RFC] copy_strtok_from_user Steven Rostedt
2009-02-26 18:54 ` [PATCH v3 1/6] uaccess: move probe_kernel_* functions to lib/uaccess.c Steven Rostedt
2009-02-26 18:54 ` [PATCH v3 2/6] string: add strtok_r to kernel Steven Rostedt
2009-02-26 18:54 ` Steven Rostedt [this message]
2009-02-26 18:54 ` [PATCH v3 4/6] tracing: convert event_trace to use copy_strtok_from_user Steven Rostedt
2009-02-26 18:54 ` [PATCH v3 5/6] tracing: convert ftrace_regex_write " Steven Rostedt
2009-02-26 18:54 ` [PATCH v3 6/6] tracing: convert ftrace_graph_write " Steven Rostedt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20090226185901.320018313@goodmis.org \
--to=rostedt@goodmis.org \
--cc=akpm@linux-foundation.org \
--cc=fweisbec@gmail.com \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@elte.hu \
--cc=peterz@infradead.org \
--cc=srostedt@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox