public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] xacct_add_tsk: fix pure theoretical ->mm use-after-free
@ 2006-10-29 18:58 Oleg Nesterov
  2006-10-30 18:51 ` Jay Lan
  0 siblings, 1 reply; 4+ messages in thread
From: Oleg Nesterov @ 2006-10-29 18:58 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Shailabh Nagar, Balbir Singh, Jay Lan, linux-kernel

Paranoid fix. The task can free its ->mm after the 'if (p->mm)' check.

Signed-off-by: Oleg Nesterov <oleg@tv-sign.ru>

--- STATS/kernel/tsacct.c~3_mm	2006-10-27 01:03:26.000000000 +0400
+++ STATS/kernel/tsacct.c	2006-10-29 21:46:12.000000000 +0300
@@ -80,13 +80,17 @@ void bacct_add_tsk(struct taskstats *sta
  */
 void xacct_add_tsk(struct taskstats *stats, struct task_struct *p)
 {
+	struct mm_struct *mm;
+
 	/* convert pages-jiffies to Mbyte-usec */
 	stats->coremem = jiffies_to_usecs(p->acct_rss_mem1) * PAGE_SIZE / MB;
 	stats->virtmem = jiffies_to_usecs(p->acct_vm_mem1) * PAGE_SIZE / MB;
-	if (p->mm) {
+	mm = get_task_mm(p);
+	if (mm) {
 		/* adjust to KB unit */
-		stats->hiwater_rss   = p->mm->hiwater_rss * PAGE_SIZE / KB;
-		stats->hiwater_vm    = p->mm->hiwater_vm * PAGE_SIZE / KB;
+		stats->hiwater_rss   = mm->hiwater_rss * PAGE_SIZE / KB;
+		stats->hiwater_vm    = mm->hiwater_vm * PAGE_SIZE / KB;
+		mmput(mm);
 	}
 	stats->read_char	= p->rchar;
 	stats->write_char	= p->wchar;


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] xacct_add_tsk: fix pure theoretical ->mm use-after-free
  2006-10-29 18:58 [PATCH] xacct_add_tsk: fix pure theoretical ->mm use-after-free Oleg Nesterov
@ 2006-10-30 18:51 ` Jay Lan
  2006-10-30 20:41   ` Oleg Nesterov
  0 siblings, 1 reply; 4+ messages in thread
From: Jay Lan @ 2006-10-30 18:51 UTC (permalink / raw)
  To: Oleg Nesterov
  Cc: Andrew Morton, Shailabh Nagar, Balbir Singh, Jay Lan,
	linux-kernel

Oleg Nesterov wrote:
> Paranoid fix. The task can free its ->mm after the 'if (p->mm)' check.

Why this change is necessary? This routine is indirectly invoked by
taskstats_exit_send() routine called inside do_exit():

do_exit()
{
        ...
        taskstats_exit_send(tsk, tidstats, group_dead, mycpu);
        taskstats_exit_free(tidstats);
        delayacct_tsk_exit(tsk);

        exit_mm(tsk);
        ...
}

The mm is freed in exit_mm(), right?

- jay


> 
> Signed-off-by: Oleg Nesterov <oleg@tv-sign.ru>
> 
> --- STATS/kernel/tsacct.c~3_mm	2006-10-27 01:03:26.000000000 +0400
> +++ STATS/kernel/tsacct.c	2006-10-29 21:46:12.000000000 +0300
> @@ -80,13 +80,17 @@ void bacct_add_tsk(struct taskstats *sta
>   */
>  void xacct_add_tsk(struct taskstats *stats, struct task_struct *p)
>  {
> +	struct mm_struct *mm;
> +
>  	/* convert pages-jiffies to Mbyte-usec */
>  	stats->coremem = jiffies_to_usecs(p->acct_rss_mem1) * PAGE_SIZE / MB;
>  	stats->virtmem = jiffies_to_usecs(p->acct_vm_mem1) * PAGE_SIZE / MB;
> -	if (p->mm) {
> +	mm = get_task_mm(p);
> +	if (mm) {
>  		/* adjust to KB unit */
> -		stats->hiwater_rss   = p->mm->hiwater_rss * PAGE_SIZE / KB;
> -		stats->hiwater_vm    = p->mm->hiwater_vm * PAGE_SIZE / KB;
> +		stats->hiwater_rss   = mm->hiwater_rss * PAGE_SIZE / KB;
> +		stats->hiwater_vm    = mm->hiwater_vm * PAGE_SIZE / KB;
> +		mmput(mm);
>  	}
>  	stats->read_char	= p->rchar;
>  	stats->write_char	= p->wchar;
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] xacct_add_tsk: fix pure theoretical ->mm use-after-free
  2006-10-30 18:51 ` Jay Lan
@ 2006-10-30 20:41   ` Oleg Nesterov
  2006-10-31  3:08     ` Jay Lan
  0 siblings, 1 reply; 4+ messages in thread
From: Oleg Nesterov @ 2006-10-30 20:41 UTC (permalink / raw)
  To: Jay Lan; +Cc: Andrew Morton, Shailabh Nagar, Balbir Singh, Jay Lan,
	linux-kernel

On 10/30, Jay Lan wrote:
>
> Oleg Nesterov wrote:
> > Paranoid fix. The task can free its ->mm after the 'if (p->mm)' check.
> 
> Why this change is necessary? This routine is indirectly invoked by
> taskstats_exit_send() routine called inside do_exit():

Not only, please note

	taskstats_user_cmd(TASKSTATS_CMD_ATTR_PID)->fill_pid()->xacct_add_tsk()

path. This tsk is not 'current', it was found by find_task_by_pid(), and
it may exit at any time.

Oleg.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] xacct_add_tsk: fix pure theoretical ->mm use-after-free
  2006-10-30 20:41   ` Oleg Nesterov
@ 2006-10-31  3:08     ` Jay Lan
  0 siblings, 0 replies; 4+ messages in thread
From: Jay Lan @ 2006-10-31  3:08 UTC (permalink / raw)
  To: Oleg Nesterov
  Cc: Jay Lan, Andrew Morton, Shailabh Nagar, Balbir Singh,
	linux-kernel

Oleg Nesterov wrote:
> On 10/30, Jay Lan wrote:
>> Oleg Nesterov wrote:
>>> Paranoid fix. The task can free its ->mm after the 'if (p->mm)' check.
>> Why this change is necessary? This routine is indirectly invoked by
>> taskstats_exit_send() routine called inside do_exit():
> 
> Not only, please note
> 
> 	taskstats_user_cmd(TASKSTATS_CMD_ATTR_PID)->fill_pid()->xacct_add_tsk()
> 
> path. This tsk is not 'current', it was found by find_task_by_pid(), and
> it may exit at any time.

OK. Thanks!

Acked-by: Jay Lan <jlan@sgi.com>

> 
> Oleg.
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-10-31  3:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-10-29 18:58 [PATCH] xacct_add_tsk: fix pure theoretical ->mm use-after-free Oleg Nesterov
2006-10-30 18:51 ` Jay Lan
2006-10-30 20:41   ` Oleg Nesterov
2006-10-31  3:08     ` Jay Lan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox