* [PATCH] unserialized task->files changing
@ 2006-08-08 10:07 Kirill Korotaev
2006-08-08 10:12 ` Christoph Hellwig
0 siblings, 1 reply; 4+ messages in thread
From: Kirill Korotaev @ 2006-08-08 10:07 UTC (permalink / raw)
To: Andrew Morton, Linux Kernel Mailing List
[-- Attachment #1: Type: text/plain, Size: 294 bytes --]
Fixed race on put_files_struct on exec with proc.
Restoring files on current on error path may lead
to proc having a pointer to already kfree-d files_struct.
Found during OpenVZ stress testing.
Signed-Off-By: Pavel Emelianov <xemul@openvz.org>
Signed-Off-By: Kirill Korotaev <dev@openvz.org>
[-- Attachment #2: diff-ms-files-race-fix --]
[-- Type: text/plain, Size: 1463 bytes --]
--- ./fs/binfmt_elf.c.fsfix 2006-03-27 14:25:59.000000000 +0400
+++ ./fs/binfmt_elf.c 2006-03-28 13:26:16.000000000 +0400
@@ -1027,8 +1027,13 @@ out_free_file:
sys_close(elf_exec_fileno);
out_free_fh:
if (files) {
- put_files_struct(current->files);
+ struct files_struct *old;
+
+ old = current->files;
+ task_lock(current);
current->files = files;
+ task_unlock(current);
+ put_files_struct(old);
}
out_free_ph:
kfree(elf_phdata);
--- ./fs/binfmt_misc.c.fsfix 2006-03-27 14:25:59.000000000 +0400
+++ ./fs/binfmt_misc.c 2006-03-28 13:27:06.000000000 +0400
@@ -216,8 +216,13 @@ _error:
bprm->interp_data = 0;
_unshare:
if (files) {
- put_files_struct(current->files);
+ struct files_struct *old;
+
+ old = current->files;
+ task_lock(current);
current->files = files;
+ task_unlock(current);
+ put_files_struct(old);
}
goto _ret;
}
--- ./fs/exec.c.fsfix 2006-03-27 14:25:59.000000000 +0400
+++ ./fs/exec.c 2006-03-28 13:28:10.000000000 +0400
@@ -865,7 +865,7 @@ int flush_old_exec(struct linux_binprm *
{
char * name;
int i, ch, retval;
- struct files_struct *files;
+ struct files_struct *files, *old;
char tcomm[sizeof(current->comm)];
/*
@@ -946,8 +946,11 @@ int flush_old_exec(struct linux_binprm *
return 0;
mmap_failed:
- put_files_struct(current->files);
+ old = current->files;
+ task_lock(current);
current->files = files;
+ task_unlock(current);
+ put_files_struct(old);
out:
return retval;
}
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] unserialized task->files changing
2006-08-08 10:07 [PATCH] unserialized task->files changing Kirill Korotaev
@ 2006-08-08 10:12 ` Christoph Hellwig
2006-08-08 10:23 ` Eric Dumazet
0 siblings, 1 reply; 4+ messages in thread
From: Christoph Hellwig @ 2006-08-08 10:12 UTC (permalink / raw)
To: Kirill Korotaev; +Cc: Andrew Morton, Linux Kernel Mailing List
On Tue, Aug 08, 2006 at 02:07:49PM +0400, Kirill Korotaev wrote:
> Fixed race on put_files_struct on exec with proc.
> Restoring files on current on error path may lead
> to proc having a pointer to already kfree-d files_struct.
This is three times the exact same code sequence, it should probably go into
a helper:
void reset_current_files(struct files_struct *files)
{
struct files_struct *old = current->files;
task_lock(current);
current->files = files;
task_unlock(current);
put_files_struct(old);
}
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] unserialized task->files changing
2006-08-08 10:12 ` Christoph Hellwig
@ 2006-08-08 10:23 ` Eric Dumazet
2006-08-08 10:43 ` Kirill Korotaev
0 siblings, 1 reply; 4+ messages in thread
From: Eric Dumazet @ 2006-08-08 10:23 UTC (permalink / raw)
To: Christoph Hellwig
Cc: Kirill Korotaev, Andrew Morton, Linux Kernel Mailing List
On Tuesday 08 August 2006 12:12, Christoph Hellwig wrote:
> On Tue, Aug 08, 2006 at 02:07:49PM +0400, Kirill Korotaev wrote:
> > Fixed race on put_files_struct on exec with proc.
> > Restoring files on current on error path may lead
> > to proc having a pointer to already kfree-d files_struct.
>
> This is three times the exact same code sequence, it should probably go
> into a helper:
>
> void reset_current_files(struct files_struct *files)
> {
> struct files_struct *old = current->files;
>
> task_lock(current);
> current->files = files;
> task_unlock(current);
> put_files_struct(old);
> }
More over I think you want to task_lock() before reading current->files
into 'old'
task_lock(current);
old = current->files;
current->files = files;
task_unlock(current);
put_files_struct(old);
or maybe a xchg() ?
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] unserialized task->files changing
2006-08-08 10:23 ` Eric Dumazet
@ 2006-08-08 10:43 ` Kirill Korotaev
0 siblings, 0 replies; 4+ messages in thread
From: Kirill Korotaev @ 2006-08-08 10:43 UTC (permalink / raw)
To: Eric Dumazet; +Cc: Christoph Hellwig, Andrew Morton, Linux Kernel Mailing List
Eric Dumazet wrote:
> On Tuesday 08 August 2006 12:12, Christoph Hellwig wrote:
>
>>On Tue, Aug 08, 2006 at 02:07:49PM +0400, Kirill Korotaev wrote:
>>
>>>Fixed race on put_files_struct on exec with proc.
>>>Restoring files on current on error path may lead
>>>to proc having a pointer to already kfree-d files_struct.
>>
>>This is three times the exact same code sequence, it should probably go
>>into a helper:
>>
>>void reset_current_files(struct files_struct *files)
>>{
>> struct files_struct *old = current->files;
>>
>> task_lock(current);
>> current->files = files;
>> task_unlock(current);
>> put_files_struct(old);
>>}
>
>
>
> More over I think you want to task_lock() before reading current->files
> into 'old'
>
> task_lock(current);
> old = current->files;
> current->files = files;
> task_unlock(current);
> put_files_struct(old);
>
> or maybe a xchg() ?
yeah, never do assignments in declarations :)
BTW, not sure about kthread_exit_files() yet, but looks like it suffers too.
unshare_files() changes current->files w/o locking as well. but I can't see
where it puts the old files... hmm...
Kirill
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2006-08-08 10:44 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-08-08 10:07 [PATCH] unserialized task->files changing Kirill Korotaev
2006-08-08 10:12 ` Christoph Hellwig
2006-08-08 10:23 ` Eric Dumazet
2006-08-08 10:43 ` Kirill Korotaev
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox