diff options
author | Kees Cook <keescook@chromium.org> | 2016-04-20 18:46:24 -0400 |
---|---|---|
committer | James Morris <james.l.morris@oracle.com> | 2016-04-20 20:47:26 -0400 |
commit | 0d0443288f2244d7054796086e481ddef6abdbba (patch) | |
tree | 3e7aa0126218b2bcec350ae3b3d1ec41e00e14a2 | |
parent | b53f27e4fa0d0e72d897830cc4f3f83d2a25d952 (diff) |
string_helpers: add kstrdup_quotable_cmdline
Provide an escaped (but readable: no inter-argument NULLs) commandline
safe for logging.
Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: James Morris <james.l.morris@oracle.com>
-rw-r--r-- | include/linux/string_helpers.h | 1 | ||||
-rw-r--r-- | lib/string_helpers.c | 34 |
2 files changed, 35 insertions, 0 deletions
diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h index 9de228af00c1..684d2695fc36 100644 --- a/include/linux/string_helpers.h +++ b/include/linux/string_helpers.h | |||
@@ -69,5 +69,6 @@ static inline int string_escape_str_any_np(const char *src, char *dst, | |||
69 | } | 69 | } |
70 | 70 | ||
71 | char *kstrdup_quotable(const char *src, gfp_t gfp); | 71 | char *kstrdup_quotable(const char *src, gfp_t gfp); |
72 | char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp); | ||
72 | 73 | ||
73 | #endif | 74 | #endif |
diff --git a/lib/string_helpers.c b/lib/string_helpers.c index aa00c9f989ee..b16ee85aaf87 100644 --- a/lib/string_helpers.c +++ b/lib/string_helpers.c | |||
@@ -10,6 +10,7 @@ | |||
10 | #include <linux/export.h> | 10 | #include <linux/export.h> |
11 | #include <linux/ctype.h> | 11 | #include <linux/ctype.h> |
12 | #include <linux/errno.h> | 12 | #include <linux/errno.h> |
13 | #include <linux/mm.h> | ||
13 | #include <linux/slab.h> | 14 | #include <linux/slab.h> |
14 | #include <linux/string.h> | 15 | #include <linux/string.h> |
15 | #include <linux/string_helpers.h> | 16 | #include <linux/string_helpers.h> |
@@ -562,3 +563,36 @@ char *kstrdup_quotable(const char *src, gfp_t gfp) | |||
562 | return dst; | 563 | return dst; |
563 | } | 564 | } |
564 | EXPORT_SYMBOL_GPL(kstrdup_quotable); | 565 | EXPORT_SYMBOL_GPL(kstrdup_quotable); |
566 | |||
567 | /* | ||
568 | * Returns allocated NULL-terminated string containing process | ||
569 | * command line, with inter-argument NULLs replaced with spaces, | ||
570 | * and other special characters escaped. | ||
571 | */ | ||
572 | char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp) | ||
573 | { | ||
574 | char *buffer, *quoted; | ||
575 | int i, res; | ||
576 | |||
577 | buffer = kmalloc(PAGE_SIZE, GFP_TEMPORARY); | ||
578 | if (!buffer) | ||
579 | return NULL; | ||
580 | |||
581 | res = get_cmdline(task, buffer, PAGE_SIZE - 1); | ||
582 | buffer[res] = '\0'; | ||
583 | |||
584 | /* Collapse trailing NULLs, leave res pointing to last non-NULL. */ | ||
585 | while (--res >= 0 && buffer[res] == '\0') | ||
586 | ; | ||
587 | |||
588 | /* Replace inter-argument NULLs. */ | ||
589 | for (i = 0; i <= res; i++) | ||
590 | if (buffer[i] == '\0') | ||
591 | buffer[i] = ' '; | ||
592 | |||
593 | /* Make sure result is printable. */ | ||
594 | quoted = kstrdup_quotable(buffer, gfp); | ||
595 | kfree(buffer); | ||
596 | return quoted; | ||
597 | } | ||
598 | EXPORT_SYMBOL_GPL(kstrdup_quotable_cmdline); | ||