Messages in this thread | | | Subject | Re: [PATCH v2 2/5] string_helpers: add kstrdup_quotable_cmdline | From | Andy Shevchenko <> | Date | Wed, 30 Mar 2016 14:07:19 +0300 |
| |
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
| |