lkml.org 
[lkml]   [2016]   [Mar]   [30]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectRe: [PATCH v2 2/5] string_helpers: add kstrdup_quotable_cmdline
From
Date
On Mon, 2016-03-28 at 14:14 -0700, Kees Cook wrote:
> Provide an escaped (but readable: no inter-argument NULLs)
> commandline
> safe for logging.

> +/*
> + * Returns allocated NULL-terminated string containing process
> + * command line, with inter-argument NULLs replaced with spaces,
> + * and other special characters escaped.
> + */
> +char *kstrdup_quotable_cmdline(struct task_struct *task)
> +{
> + char *buffer, *quoted;
> + int i, res;
> +
> + buffer = kmalloc(PAGE_SIZE, GFP_TEMPORARY);
> + if (!buffer)
> + return NULL;
> +
> + res = get_cmdline(task, buffer, PAGE_SIZE - 1);
> + buffer[res] = '\0';
> +
> + /* Collapse trailing NULLs. */
> + for (; res > 0; res--)
> + if (buffer[res-1] != '\0')
> + break;

/* buffer[res] is '\0', so, predecrement is safe here */
while (buffer[--res] == '\0')
 /* nothing */;

?

> +
> + /* Replace inter-argument NULLs. */
> + for (i = 0; i < res; i++)
> + if (buffer[i] == '\0')
> + buffer[i] = ' ';
> +
> + /* Make sure result is printable. */
> + quoted = kstrdup_quotable(buffer);
> + kfree(buffer);
> + return quoted;
> +}
> +EXPORT_SYMBOL_GPL(kstrdup_quotable_cmdline);

--
Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Intel Finland Oy

\
 
 \ /
  Last update: 2016-03-30 13:41    [W:1.188 / U:0.044 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site