From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH master 3/7] sandbox: support escaping commas in --image filenames
Date: Mon, 14 Sep 2020 12:05:49 +0200 [thread overview]
Message-ID: <20200914100553.24808-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20200914100553.24808-1-a.fatoum@pengutronix.de>
Some tools like afl-fuzz generate file names containing commas.
Allow escaping the commas in the file names, so they can be passed
to barebox.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
arch/sandbox/os/common.c | 10 ++++++----
include/linux/string.h | 1 +
lib/string.c | 43 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 50 insertions(+), 4 deletions(-)
diff --git a/arch/sandbox/os/common.c b/arch/sandbox/os/common.c
index 9f26f8fa6e9a..437fe3ecdff8 100644
--- a/arch/sandbox/os/common.c
+++ b/arch/sandbox/os/common.c
@@ -212,6 +212,8 @@ int linux_execve(const char * filename, char *const argv[], char *const envp[])
extern void start_barebox(void);
extern void mem_malloc_init(void *start, void *end);
+extern char * strsep_unescaped(char **s, const char *ct);
+
static int add_image(char *str, char *devname_template, int *devname_number)
{
struct hf_info *hf = malloc(sizeof(struct hf_info));
@@ -225,15 +227,15 @@ static int add_image(char *str, char *devname_template, int *devname_number)
if (!hf)
return -1;
- filename = strtok(str, ",");
- while ((opt = strtok(NULL, ","))) {
+ filename = strsep_unescaped(&str, ",");
+ while ((opt = strsep_unescaped(&str, ","))) {
if (!strcmp(opt, "ro"))
readonly = 1;
}
/* parses: "devname=filename" */
- devname = strtok(filename, "=");
- filename = strtok(NULL, "=");
+ devname = strsep_unescaped(&filename, "=");
+ filename = strsep_unescaped(&filename, "=");
if (!filename) {
filename = devname;
snprintf(tmp, sizeof(tmp),
diff --git a/include/linux/string.h b/include/linux/string.h
index fd42f5020a07..763ef500e574 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -12,6 +12,7 @@ extern char * ___strtok;
extern char * strpbrk(const char *,const char *);
extern char * strtok(char *,const char *);
extern char * strsep(char **,const char *);
+extern char * strsep_unescaped(char **,const char *);
extern __kernel_size_t strspn(const char *,const char *);
diff --git a/lib/string.c b/lib/string.c
index 7548fd35810b..50f8e2f87c9f 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -455,6 +455,49 @@ char * strsep(char **s, const char *ct)
#endif
EXPORT_SYMBOL(strsep);
+/**
+ * strsep_unescaped - Split a string into tokens, while ignoring escaped delimiters
+ * @s: The string to be searched
+ * @ct: The delimiter characters to search for
+ *
+ * strsep_unescaped() behaves like strsep unless it meets an escaped delimiter.
+ * In that case, it shifts the string back in memory to overwrite the escape's
+ * backslash then continues the search until an unescaped delimiter is found.
+ */
+char *strsep_unescaped(char **s, const char *ct)
+{
+ char *sbegin = *s, *hay;
+ const char *needle;
+ size_t shift = 0;
+
+ if (sbegin == NULL)
+ return NULL;
+
+ for (hay = sbegin; *hay != '\0'; ++hay) {
+ *hay = hay[shift];
+
+ if (*hay == '\\') {
+ *hay = hay[++shift];
+ if (*hay != '\\')
+ continue;
+ }
+
+ for (needle = ct; *needle != '\0'; ++needle) {
+ if (*hay == *needle)
+ goto match;
+ }
+ }
+
+ *s = NULL;
+ return sbegin;
+
+match:
+ *hay = '\0';
+ *s = &hay[shift + 1];
+
+ return sbegin;
+}
+
#ifndef __HAVE_ARCH_STRSWAB
/**
* strswab - swap adjacent even and odd bytes in %NUL-terminated string
--
2.28.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next prev parent reply other threads:[~2020-09-14 10:06 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-14 10:05 [PATCH master 1/7] sandbox: hostfile: error out if file couldn't be opened Ahmad Fatoum
2020-09-14 10:05 ` [PATCH master 2/7] sandbox: add_image: support mmaping block devices on 32-bit hosts Ahmad Fatoum
2020-09-14 10:05 ` Ahmad Fatoum [this message]
2020-09-14 13:42 ` [PATCH master 3/7] sandbox: support escaping commas in --image filenames Ahmad Fatoum
2020-09-14 10:05 ` [PATCH master 4/7] readkey: fix buffer overflow handling longer escape sequences Ahmad Fatoum
2020-09-14 10:05 ` [PATCH master 5/7] sandbox: fix SANDBOX_UNWIND dependency to be KASAN only Ahmad Fatoum
2020-09-14 10:05 ` [PATCH master 6/7] fs: don't free device in remove callback Ahmad Fatoum
2020-09-14 10:05 ` [PATCH master 7/7] common: ubsan: replace pr_err with printf Ahmad Fatoum
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=20200914100553.24808-3-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/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