From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752516AbcC3LGa (ORCPT ); Wed, 30 Mar 2016 07:06:30 -0400 Received: from mga14.intel.com ([192.55.52.115]:55843 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751172AbcC3LG3 (ORCPT ); Wed, 30 Mar 2016 07:06:29 -0400 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.24,415,1455004800"; d="scan'208";a="934626256" Message-ID: <1459336039.5907.37.camel@linux.intel.com> Subject: Re: [PATCH v2 2/5] string_helpers: add kstrdup_quotable_cmdline From: Andy Shevchenko To: Kees Cook , James Morris Cc: "Serge E. Hallyn" , Andrew Morton , Kalle Valo , Mauro Carvalho Chehab , Joe Perches , Guenter Roeck , Jiri Slaby , Paul Moore , Stephen Smalley , Mimi Zohar , Casey Schaufler , Andreas Gruenbacher , Rasmus Villemoes , Ulf Hansson , Vitaly Kuznetsov , linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org Date: Wed, 30 Mar 2016 14:07:19 +0300 In-Reply-To: <1459199662-16558-3-git-send-email-keescook@chromium.org> References: <1459199662-16558-1-git-send-email-keescook@chromium.org> <1459199662-16558-3-git-send-email-keescook@chromium.org> Organization: Intel Finland Oy Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.18.5.1-1 Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Intel Finland Oy