a race in pid generation that causes pids to be reused immediatelly
A message that is not in the archive (sent off-list or not yet received): 456905695.757931291112868201.JavaMail.root@zmail06.collab.prod.int.phx2.redhat.com
PATCH 1/1 a race in pid generation that causes pids to be reused immediatelly
From the mainline commit 5fdee8c4a5e1800489ce61963208f8cc55e42ea1:
A program that repeatedly forks and waits is susceptible to having the same pid repeated, especially when it competes with another instance of the same program. This is really bad for bash implementation. Furthermore, many shell scripts assume that pid numbers will not be used for some length of time.
Race Description:
A B
// pid == offset == n // pid == offset == n + 1test_and_set_bit(offset, map->page)
test_and_set_bit(offset, map->page);
pid_ns->last_pid = pid;
pid_ns->last_pid = pid;
// pid == n + 1 is freed (wait())
// Next fork()...
last = pid_ns->last_pid; // == n
pid = last + 1;--- runtest/syscalls | 1 + testcases/kernel/syscalls/fork/fork13.c | 166 +++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+), 0 deletions(-) create mode 100644 testcases/kernel/syscalls/fork/fork13.c
runtest/syscalls +1 -0
@@ -274,6 +274,7 @@ fork08 fork08 fork09 fork09 fork10 fork10 fork11 fork11 +fork13 fork13 -c 2 -i 10000000 fpathconf01 fpathconf01
testcases/kernel/syscalls/fork/fork13.c new file +166 -0
166 lines not on this page · raw message
Signature · 1 line
1.7.1
From: CAI Qian <[email protected]>
Date: Tue, 30 Nov 2010 17:59:46 +0800
Subject: [PATCH] a race in pid generation that causes pids to be reused immediatelly
From the mainline commit 5fdee8c4a5e1800489ce61963208f8cc55e42ea1:
A program that repeatedly forks and waits is susceptible to having the same pid repeated, especially when it competes with another instance of the same program. This is really bad for bash implementation. Furthermore, many shell scripts assume that pid numbers will not be used for some length of time.
Race Description:
A B
// pid == offset == n // pid == offset == n + 1test_and_set_bit(offset, map->page)
test_and_set_bit(offset, map->page);
pid_ns->last_pid = pid;
pid_ns->last_pid = pid;
// pid == n + 1 is freed (wait())
// Next fork()...
last = pid_ns->last_pid; // == n
pid = last + 1;--- runtest/syscalls | 1 + testcases/kernel/syscalls/fork/fork13.c | 166 +++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+), 0 deletions(-) create mode 100644 testcases/kernel/syscalls/fork/fork13.c
runtest/syscalls +1 -0
@@ -274,6 +274,7 @@ fork08 fork08 fork09 fork09 fork10 fork10 fork11 fork11 +fork13 fork13 -c 2 -i 10000000 fpathconf01 fpathconf01
testcases/kernel/syscalls/fork/fork13.c new file +166 -0
166 lines not on this page · raw message
Signature · 1 line
1.7.1
------------------------------------------------------------------------------
Increase Visibility of Your 3D Game App & Earn a Chance To Win $500! Tap into the largest installed PC base & get more eyes on your game by optimizing for Intel(R) Graphics Technology. Get started today with the Intel(R) Software Partner Program. Five $500 cash prizes are up for grabs.
http://p.sf.net/sfu/intelisp-dev2dev
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list
Hi!
Quoted 5 lines
testcases/kernel/syscalls/fork/fork13.c new file +5
-0+#include <errno.h> +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <sys/types.h>
testcases/kernel/syscalls/fork/fork13.c line 62
Quoted 10 lines
testcases/kernel/syscalls/fork/fork13.c new file +10
-0+#include <sys/wait.h> +#include "test.h" +#include "usctest.h" + +char *TCID ="fork13" ; +int TST_TOTAL =1 ; +extern int Tst_count; + +static void setup (void ); +static int PidDistance (pid_t first,pid_t second);
Just to cite from linux kernel conding style:
"mixed-case names are frowned upon"
Quoted 12 lines
testcases/kernel/syscalls/fork/fork13.c new file +12
-0+static void cleanup(void) LTP_ATTRIBUTE_NORETURN; +static int check (void ); + +int main (int argc,char * argv[]) +{ +/* message returned from parse_opts */ +char *msg; + + msg =parse_opts (argc, argv,NULL ,NULL ); +if (msg !=NULL ) { +tst_brkm (TBROK , cleanup,"OPTION PARSING ERROR - %s" , + msg);
testcases/kernel/syscalls/fork/fork13.c line 84
Quoted 10 lines
testcases/kernel/syscalls/fork/fork13.c new file +10
-0+tst_exit (); + } +setup (); +TEST (check ()); +cleanup (); +} + +int check (void ) +{ +/* loop counter */
Once again, this comment is useless ;).
testcases/kernel/syscalls/fork/fork13.c line 91
Quoted 7 lines
testcases/kernel/syscalls/fork/fork13.c new file +7
-0+int lc; + +pid_t last_pid =0 ; +pid_t pid; +int child_exit_code, distance, reaped, status; + +/* Check looping state if -i option given */
Here as well.
testcases/kernel/syscalls/fork/fork13.c line 101
Quoted 10 lines
testcases/kernel/syscalls/fork/fork13.c new file +10
-0+for (lc =0 ;TEST_LOOPING (lc); lc++) { +if (lc %32786 ==0 ) +tst_resm (TINFO ,"Iter: %d" , lc/32768 ); + child_exit_code = lc %256 ; +switch (pid =fork ()) { +case -1 : +tst_brkm (TBROK |TERRNO , cleanup,"fork" ); +tst_exit (); +case 0 : +// Child
I would remove this one as well. Everybody who plays with lowlevel OS api should know how fork works.
testcases/kernel/syscalls/fork/fork13.c line 104
Quoted 3 lines
testcases/kernel/syscalls/fork/fork13.c new file +3
-0+exit (child_exit_code); +default : +// Parent
Here as well.
testcases/kernel/syscalls/fork/fork13.c line 108
Quoted 4 lines
testcases/kernel/syscalls/fork/fork13.c new file +4
-0+if (lc >0 ) { +printf ("last_pid = %d\npid = %d\n" , last_pid, pid); + distance =PidDistance (last_pid, pid); +if (distance ==0 || distance >30000 ) {
Hmm are you sure that distance > 30000 is good thing to assert? Who says that two pids keeps at least 2768 distance and even then, who says that 32768 is maximal pid (see below)?
Quoted 17 lines
testcases/kernel/syscalls/fork/fork13.c new file +17
-0+tst_resm (TFAIL , +"Unexpected pid " +"sequence: previous " +"fork: pid=%d, current " +"fork: pid=%d for " +"iteration=%d." , + last_pid, pid, lc); + } + } + last_pid = pid; + + reaped =wait (&status); +if (reaped != pid) { +tst_resm (TFAIL , +"Wait return value: expected " +"pid=%d, got %d, iteration %d." , + pid, reaped, lc);
testcases/kernel/syscalls/fork/fork13.c line 135
Quoted 10 lines
testcases/kernel/syscalls/fork/fork13.c new file +10
-0+ }else if (WEXITSTATUS (status != child_exit_code) + != child_exit_code) { +tst_resm (TFAIL , +"Unexpected exit status %x, " +"iteration %d." , +WEXITSTATUS (status), lc); + } + } + } +return 0 ;
Rather use tst_exit() here.
testcases/kernel/syscalls/fork/fork13.c line 146
Quoted 11 lines
testcases/kernel/syscalls/fork/fork13.c new file +11
-0+} + +void setup (void ) +{ +/* capture signals */ +tst_sig (FORK ,DEF_HANDLER , cleanup); + +/* Pause if that option was specified */ +TEST_PAUSE ; + +tst_tmpdir ();
Do you really need temporary directory to test fork() ?
Quoted 10 lines
testcases/kernel/syscalls/fork/fork13.c new file +10
-0+} + +void cleanup (void ) +{ +/* +* print timing stats if that option was specified. +* print errno log if that option was specified. +*/ +TEST_CLEANUP ; +
testcases/kernel/syscalls/fork/fork13.c line 166
Quoted 10 lines
testcases/kernel/syscalls/fork/fork13.c new file +9
-0+/* exit with return code appropriate for results */ +tst_rmdir (); +tst_exit (); +} +/* The distance mod 32768 between two pids, where the first pid is +expected to be smaller than the second. */ +int PidDistance (pid_t first,pid_t second) { +return (second +32768 - first) %32768 ; +}
The comment doesn't hold as when second is smaller than first the negative value would be smaller than 32768.
Moreover the maximal pid value may be changed by writing to "/proc/sys/kernel/pid_max", then this code is broken.
Also I'm not sure if the result of such computation couldn't overflow/underflow as int may not be the same size as pid_t.
Signature · 13 lines
Cyril Hrubis [email protected] ------------------------------------------------------------------------------ Increase Visibility of Your 3D Game App & Earn a Chance To Win $500! Tap into the largest installed PC base & get more eyes on your game by optimizing for Intel(R) Graphics Technology. Get started today with the Intel(R) Software Partner Program. Five $500 cash prizes are up for grabs. http://p.sf.net/sfu/intelisp-dev2dev _______________________________________________ Ltp-list mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ltp-list