Skip to contentSkip to conversation

Race condition?

RFC Kasper Dupont 4 participants 12 messages Aug 2, 2002 – Aug 3, 2002

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Is there a race condition in this piece of code from do_fork in linux/kernel/fork.c? I cannot see what prevents two processes from calling this at the same time and both successfully fork even though the user had only one process left.

        if (atomic_read(&p->user->processes) >= p->rlim[RLIMIT_NPROC].rlim_cur
                      && !capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE))
                goto bad_fork_free;

        atomic_inc(&p->user->__count);
        atomic_inc(&p->user->processes);
Signature · 3 lines
Kasper Dupont -- der bruger for meget tid på usenet.
For sending spam use mailto:[email protected]
or mailto:[email protected]
#1 lore raw
Quoted 11 lines · Kasper Dupont

Is there a race condition in this piece of code from do_fork in linux/kernel/fork.c? I cannot see what prevents two processes from calling this at the same time and both successfully fork even though the user had only one process left.

        if (atomic_read(&p->user->processes) >= p->rlim[RLIMIT_NPROC].rlim_cur
                      && !capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE))
                goto bad_fork_free;

        atomic_inc(&p->user->__count);
        atomic_inc(&p->user->processes);

I don't see any locking in the call chain leading to this function, so I think you're right. The attached patch fixes this. It costs an extra 2 atomic ops in the failure case, but otherwise just makes the processes++ operation earlier.

Patch is against 2.5.27, but applies against 30.

Signature · 2 lines
Dave Hansen
[email protected]
kernel/fork.c +5 -3
@@ -628,13 +628,15 @@
 		goto fork_out;
 
 	retval = -EAGAIN;
-	if (atomic_read(&p->user->processes) >= p->rlim[RLIMIT_NPROC].rlim_cur) {
-		if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE))
+	atomic_inc(&p->user->processes);
+	if (atomic_read(&p->user->processes) > p->rlim[RLIMIT_NPROC].rlim_cur) {
+		if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE)) {
+			atomic_dec(&p->user->processes);
 			goto bad_fork_free;
+		}
 	}
 
 	atomic_inc(&p->user->__count);
-	atomic_inc(&p->user->processes);
 
 	/*
 	 * Counter increases are protected by
#2 lore raw

Am Freitag, 2. August 2002 19:00 schrieb Dave Hansen:

Quoted 13 lines
Quoted 11 lines · Kasper Dupont

Is there a race condition in this piece of code from do_fork in linux/kernel/fork.c? I cannot see what prevents two processes from calling this at the same time and both successfully fork even though the user had only one process left.

        if (atomic_read(&p->user->processes) >=

p->rlim[RLIMIT_NPROC].rlim_cur && !capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE)) goto bad_fork_free;

        atomic_inc(&p->user->__count);
        atomic_inc(&p->user->processes);
Quoted 6 lines

I don't see any locking in the call chain leading to this function, so I think you're right. The attached patch fixes this. It costs an extra 2 atomic ops in the failure case, but otherwise just makes the processes++ operation earlier.

Patch is against 2.5.27, but applies against 30.

It has the opposite failure mode. Forks only some of which should succeed may all fail.

Regards
Oliver

#3 lore raw
Quoted 21 lines · Oliver Neukum

Am Freitag, 2. August 2002 19:00 schrieb Dave Hansen:

Quoted 19 lines
Quoted 11 lines · Kasper Dupont

Is there a race condition in this piece of code from do_fork in linux/kernel/fork.c? I cannot see what prevents two processes from calling this at the same time and both successfully fork even though the user had only one process left.

       if (atomic_read(&p->user->processes) >=

p->rlim[RLIMIT_NPROC].rlim_cur && !capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE)) goto bad_fork_free;

       atomic_inc(&p->user->__count);
       atomic_inc(&p->user->processes);

I don't see any locking in the call chain leading to this function, so I think you're right. The attached patch fixes this. It costs an extra 2 atomic ops in the failure case, but otherwise just makes the processes++ operation earlier.

Quoted 4 lines
Quoted 1 line

Patch is against 2.5.27, but applies against 30.

It has the opposite failure mode. Forks only some of which should succeed may all fail.

You beat me to it. I haven't had a chance to test it yet.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
       if (atomic_read(&p->user->processes) >=

p->rlim[RLIMIT_NPROC].rlim_cur && !capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE)) goto bad_fork_free;

Signature · 2 lines
Dave Hansen
[email protected]
#5 lore raw
In reply to Dave Hansen
Quoted 10 lines · Dave Hansen
Quoted 9 lines · Kasper Dupont

Is there a race condition in this piece of code from do_fork in linux/kernel/fork.c? I cannot see what prevents two processes from calling this at the same time and both successfully fork even though the user had only one process left.

        if (atomic_read(&p->user->processes) >= p->rlim[RLIMIT_NPROC].rlim_cur
                      && !capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE))
                goto bad_fork_free;
Quoted 7 lines
Quoted 2 lines
        atomic_inc(&p->user->__count);
        atomic_inc(&p->user->processes);

I don't see any locking in the call chain leading to this function, so I think you're right. The attached patch fixes this. It costs an extra 2 atomic ops in the failure case, but otherwise just makes the processes++ operation earlier.

Does this race really justify extra locks? AFAICT the worst case is that a user can go slightly over their RLIMIT_NPROC, and that will only occur if they fork on multiple cpus "at the same time". Given the timing constraints on that small window, I would be surprised if this race could be exploited to gain more than a couple of extra processes. This looks like a case where close enough is good enough.

#6 lore raw
In reply to Kasper Dupont

Am Freitag, 2. August 2002 15:46 schrieb Kasper Dupont:

Quoted 1 line

Is there a race condition in this piece of code from do_fork in

It would seem so. Perhaps the BKL was taken previously.

Regards
Oliver

#7 lore raw
Quoted 5 lines · Oliver Neukum

Am Freitag, 2. August 2002 15:46 schrieb Kasper Dupont:

Quoted 1 line

Is there a race condition in this piece of code from do_fork in

It would seem so. Perhaps the BKL was taken previously.

I guess you are right. Looking closer on the following lines I find a comment stating that nr_threads is protected by the kernel lock, but I don't see a lock_kernel() anywhere in this code. How about the next code using the nr_threads variable, is that safe?

Signature · 3 lines
Kasper Dupont -- der bruger for meget tid på usenet.
For sending spam use mailto:[email protected]
or mailto:[email protected]
#8 lore raw

Am Freitag, 2. August 2002 19:13 schrieb Kasper Dupont:

Quoted 11 lines
Quoted 4 lines · Oliver Neukum

Am Freitag, 2. August 2002 15:46 schrieb Kasper Dupont:

Quoted 1 line

Is there a race condition in this piece of code from do_fork in

It would seem so. Perhaps the BKL was taken previously.

I guess you are right. Looking closer on the following lines I find a comment stating that nr_threads is protected by the kernel lock, but I don't see a lock_kernel() anywhere in this code. How about the next code using the nr_threads variable, is that safe?

No, it isn't either.
In fact you can even lose updates as it isn't atomic_t.

Regards
Oliver

#9 lore raw
In reply to Oliver Neukum
Quoted 6 lines · Oliver Neukum

Am Freitag, 2. August 2002 15:46 schrieb Kasper Dupont:

Quoted 1 line

Is there a race condition in this piece of code from do_fork in

It would seem so. Perhaps the BKL was taken previously.

Even if it was, I doubt the code ever knowingly relied upon it. If I know that I'm protected under a lock, I rarely go to the trouble of atomic operations.

The root of the problem is that the reference count is being relied on for the wrong thing. There is a race on p->user between the
dup_task_struct() and whenever the atomic_inc(&p->user->__count) occcurs. The user reference count needs to be incremented in dup_task_struct(), before the copy occurs.

Signature · 2 lines
Dave Hansen
[email protected]
#10 lore raw

Am Freitag, 2. August 2002 19:37 schrieb Dave Hansen:

Quoted 9 lines
Quoted 4 lines · Oliver Neukum

Am Freitag, 2. August 2002 15:46 schrieb Kasper Dupont:

Quoted 1 line

Is there a race condition in this piece of code from do_fork in

It would seem so. Perhaps the BKL was taken previously.

Even if it was, I doubt the code ever knowingly relied upon it. If I know that I'm protected under a lock, I rarely go to the trouble of atomic operations.

That depends on where else you need these variables.

Quoted 5 lines

The root of the problem is that the reference count is being relied on for the wrong thing. There is a race on p->user between the dup_task_struct() and whenever the atomic_inc(&p->user->__count)
occcurs. The user reference count needs to be incremented in
dup_task_struct(), before the copy occurs.

I don't get you. The user_struct can hardly go away while we are forking.

IMHO you should add a spinlock to user_struct and take it. A clear solution that doesn't hurt the common case.

Regards
Oliver

#11 lore raw
Quoted 8 lines · Oliver Neukum
Quoted 5 lines

The root of the problem is that the reference count is being relied on for the wrong thing. There is a race on p->user between the dup_task_struct() and whenever the atomic_inc(&p->user->__count)
occcurs. The user reference count needs to be incremented in
dup_task_struct(), before the copy occurs.

I don't get you. The user_struct can hardly go away while we are forking.

Good point. I was figuring that it could disappear when the task clearly can't be exiting or setuid'ing while forking.

Quoted 2 lines

IMHO you should add a spinlock to user_struct and take it. A clear solution that doesn't hurt the common case.

That _is_ a pretty clear solution. It looks like there are grand plans for struct user, so it might come in handy in the future. But, a spinlock _will_ hurt the common case. With the atomic incs, we have 2 of them in the common case and, at most, 4 in the failure case. Adding a spinlock will require more lock instructions, which are the most costly operations in either a spinlock or atomic op.

Either of these are _incredibly_ small prices to pay in any case. Forks are slow anyway. A spinlock would be just fine with me.

Signature · 2 lines
Dave Hansen
[email protected]