aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKees Cook <keescook@chromium.org>2016-04-20 18:46:25 -0400
committerJames Morris <james.l.morris@oracle.com>2016-04-20 20:47:26 -0400
commit21985319add60b55fc27230d9421a3e5af7e998a (patch)
treeb6684d0914ddfbf34da20da2c7608e89694f7596
parent0d0443288f2244d7054796086e481ddef6abdbba (diff)
string_helpers: add kstrdup_quotable_file
Allocate a NULL-terminated file path with special characters escaped, 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.h3
-rw-r--r--lib/string_helpers.c30
2 files changed, 33 insertions, 0 deletions
diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h
index 684d2695fc36..5ce9538f290e 100644
--- a/include/linux/string_helpers.h
+++ b/include/linux/string_helpers.h
@@ -3,6 +3,8 @@
3 3
4#include <linux/types.h> 4#include <linux/types.h>
5 5
6struct file;
7
6/* Descriptions of the types of units to 8/* Descriptions of the types of units to
7 * print in */ 9 * print in */
8enum string_size_units { 10enum string_size_units {
@@ -70,5 +72,6 @@ static inline int string_escape_str_any_np(const char *src, char *dst,
70 72
71char *kstrdup_quotable(const char *src, gfp_t gfp); 73char *kstrdup_quotable(const char *src, gfp_t gfp);
72char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp); 74char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp);
75char *kstrdup_quotable_file(struct file *file, gfp_t gfp);
73 76
74#endif 77#endif
diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index b16ee85aaf87..ecaac2c0526f 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -10,6 +10,8 @@
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/fs.h>
14#include <linux/limits.h>
13#include <linux/mm.h> 15#include <linux/mm.h>
14#include <linux/slab.h> 16#include <linux/slab.h>
15#include <linux/string.h> 17#include <linux/string.h>
@@ -596,3 +598,31 @@ char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp)
596 return quoted; 598 return quoted;
597} 599}
598EXPORT_SYMBOL_GPL(kstrdup_quotable_cmdline); 600EXPORT_SYMBOL_GPL(kstrdup_quotable_cmdline);
601
602/*
603 * Returns allocated NULL-terminated string containing pathname,
604 * with special characters escaped, able to be safely logged. If
605 * there is an error, the leading character will be "<".
606 */
607char *kstrdup_quotable_file(struct file *file, gfp_t gfp)
608{
609 char *temp, *pathname;
610
611 if (!file)
612 return kstrdup("<unknown>", gfp);
613
614 /* We add 11 spaces for ' (deleted)' to be appended */
615 temp = kmalloc(PATH_MAX + 11, GFP_TEMPORARY);
616 if (!temp)
617 return kstrdup("<no_memory>", gfp);
618
619 pathname = file_path(file, temp, PATH_MAX + 11);
620 if (IS_ERR(pathname))
621 pathname = kstrdup("<too_long>", gfp);
622 else
623 pathname = kstrdup_quotable(pathname, gfp);
624
625 kfree(temp);
626 return pathname;
627}
628EXPORT_SYMBOL_GPL(kstrdup_quotable_file);