mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Neil Horman <nhorman@tuxdriver.com>
To: Xiaotian Feng <dfeng@redhat.com>
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	Andrew Morton <akpm@linux-foundation.org>,
	Oleg Nesterov <oleg@redhat.com>,
	KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>,
	Roland McGrath <roland@redhat.com>
Subject: Re: [RFC PATCH] core_pattern: fix long parameters was truncated by core_pattern handler
Date: Thu, 29 Jul 2010 09:31:22 -0400	[thread overview]
Message-ID: <20100729130707.GA31154@shamino.rdu.redhat.com> (raw)
In-Reply-To: <1280407364-32466-1-git-send-email-dfeng@redhat.com>

On Thu, Jul 29, 2010 at 08:42:44PM +0800, Xiaotian Feng wrote:
> We met a parameter truncated issue, consider following:
> > echo "|/root/core_pattern_pipe_test %p /usr/libexec/blah-blah-blah \
> %s %c %p %u %g %t 11 1234567890123456789012345678901234567890" > \
> /proc/sys/kernel/core_pattern
> 
> This is okay because the strings is less than CORENAME_MAX_SIZE.
> "cat /proc/sys/kernel/core_pattern" shows the whole string. but
> after we run core_pattern_pipe_test in man page, we found last
> parameter was truncated like below:
> 	argc[10]=<12345678901234567890123456789012345678>
> 
> The root cause is core_pattern allows % specifiers, which need to be
> replaced during parse time, but the replace may expand the strings
> to larger than CORENAME_MAX_SIZE.
> 
> This patch expands the size of parsing array, and makes the cursor
> out_end shift when we replace % specifiers.
> 
> Signed-off-by: Xiaotian Feng <dfeng@redhat.com>
> Cc: Alexander Viro <viro@zeniv.linux.org.uk>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Oleg Nesterov <oleg@redhat.com>
> Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
> Cc: Neil Horman <nhorman@tuxdriver.com>
> Cc: Roland McGrath <roland@redhat.com>
> ---
Thanks for looking at this, its definately a problem.  I don't think though,
that just extending the size of the corename array to be bigger is really going
to solve the problem, you're just running away from it.  To really fix it what
we should probably do is:
1) Modify the corename argument to format_corename to be char **corename

2) Dynamically allocate an array for corename inside format_corename, of size
CORENAME_MAX_SIZE*n, where n is variable holding the maximum number of times we
had to increment our allocation size in any previous call to format_corename

3) for each iteration through the while (*pat_ptr) loop in format_corename,
check to see if the remaining size can hold the next argument to be parsed.  If
we're going to overrun, use krealloc to extend the size of the array to another
multiple or CORENAME_MAX_SIZE, and increment the variable n in (2) by 1.


That should give us a decent heuristic to determine the size of the corename
array, so that we never overrun.

Regards
Neil

>  fs/exec.c |   14 +++++++++++---
>  1 files changed, 11 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/exec.c b/fs/exec.c
> index e19de6a..e67e4b5 100644
> --- a/fs/exec.c
> +++ b/fs/exec.c
> @@ -1441,7 +1441,7 @@ EXPORT_SYMBOL(set_binfmt);
>  
>  /* format_corename will inspect the pattern parameter, and output a
>   * name into corename, which must have space for at least
> - * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
> + * CORENAME_MAX_SIZE * 3 bytes because of % specifiers.
>   */
>  static int format_corename(char *corename, long signr)
>  {
> @@ -1449,7 +1449,7 @@ static int format_corename(char *corename, long signr)
>  	const char *pat_ptr = core_pattern;
>  	int ispipe = (*pat_ptr == '|');
>  	char *out_ptr = corename;
> -	char *const out_end = corename + CORENAME_MAX_SIZE;
> +	char *out_end = corename + CORENAME_MAX_SIZE;
>  	int rc;
>  	int pid_in_pattern = 0;
>  
> @@ -1478,6 +1478,7 @@ static int format_corename(char *corename, long signr)
>  				if (rc > out_end - out_ptr)
>  					goto out;
>  				out_ptr += rc;
> +				out_end += rc - 2;
>  				break;
>  			/* uid */
>  			case 'u':
> @@ -1486,6 +1487,7 @@ static int format_corename(char *corename, long signr)
>  				if (rc > out_end - out_ptr)
>  					goto out;
>  				out_ptr += rc;
> +				out_end += rc - 2;
>  				break;
>  			/* gid */
>  			case 'g':
> @@ -1494,6 +1496,7 @@ static int format_corename(char *corename, long signr)
>  				if (rc > out_end - out_ptr)
>  					goto out;
>  				out_ptr += rc;
> +				out_end += rc - 2;
>  				break;
>  			/* signal that caused the coredump */
>  			case 's':
> @@ -1502,6 +1505,7 @@ static int format_corename(char *corename, long signr)
>  				if (rc > out_end - out_ptr)
>  					goto out;
>  				out_ptr += rc;
> +				out_end += rc - 2;
>  				break;
>  			/* UNIX time of coredump */
>  			case 't': {
> @@ -1512,6 +1516,7 @@ static int format_corename(char *corename, long signr)
>  				if (rc > out_end - out_ptr)
>  					goto out;
>  				out_ptr += rc;
> +				out_end += rc - 2;
>  				break;
>  			}
>  			/* hostname */
> @@ -1523,6 +1528,7 @@ static int format_corename(char *corename, long signr)
>  				if (rc > out_end - out_ptr)
>  					goto out;
>  				out_ptr += rc;
> +				out_end += rc - 2;
>  				break;
>  			/* executable */
>  			case 'e':
> @@ -1531,6 +1537,7 @@ static int format_corename(char *corename, long signr)
>  				if (rc > out_end - out_ptr)
>  					goto out;
>  				out_ptr += rc;
> +				out_end += rc - 2;
>  				break;
>  			/* core limit size */
>  			case 'c':
> @@ -1539,6 +1546,7 @@ static int format_corename(char *corename, long signr)
>  				if (rc > out_end - out_ptr)
>  					goto out;
>  				out_ptr += rc;
> +				out_end += rc - 2;
>  				break;
>  			default:
>  				break;
> @@ -1836,7 +1844,7 @@ static int umh_pipe_setup(struct subprocess_info *info)
>  void do_coredump(long signr, int exit_code, struct pt_regs *regs)
>  {
>  	struct core_state core_state;
> -	char corename[CORENAME_MAX_SIZE + 1];
> +	char corename[CORENAME_MAX_SIZE * 3];
>  	struct mm_struct *mm = current->mm;
>  	struct linux_binfmt * binfmt;
>  	const struct cred *old_cred;
> -- 
> 1.7.2
> 
> 

  reply	other threads:[~2010-07-29 13:31 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-07-29 12:42 Xiaotian Feng
2010-07-29 13:31 ` Neil Horman [this message]
2010-08-02 12:23   ` [RFC PATCH V2] " Xiaotian Feng
2010-08-02 13:50     ` Oleg Nesterov
2010-08-03 10:59       ` Neil Horman
2010-08-20  9:22         ` [RFC PATCH v3] " Xiaotian Feng
2010-08-20  9:35           ` Xiaotian Feng
2010-08-20  9:35         ` Xiaotian Feng
2010-08-23 11:07           ` Neil Horman
2010-08-23 23:02             ` KOSAKI Motohiro
2010-08-23 21:18           ` Andrew Morton
2010-08-24  6:18             ` Xiaotian Feng
2010-08-24  6:28               ` Andrew Morton
2010-08-24  9:42             ` [PATCH v4] " Xiaotian Feng
2010-08-24 22:47               ` Andrew Morton
2010-08-25  1:58                 ` Xiaotian Feng
2010-08-25  2:17                 ` [PATCH v5] " Xiaotian Feng
2010-08-02 14:30     ` [RFC PATCH V2] " Denys Vlasenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20100729130707.GA31154@shamino.rdu.redhat.com \
    --to=nhorman@tuxdriver.com \
    --cc=akpm@linux-foundation.org \
    --cc=dfeng@redhat.com \
    --cc=kosaki.motohiro@jp.fujitsu.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oleg@redhat.com \
    --cc=roland@redhat.com \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®