[PATCH mptcp-next v14 01/12] selftests: mptcp: sockopt: reseed RNG after fork
Geliang Tang <[email protected]> Thu, 30 Jul 2026 11:15:12 +0800
| Newsgroups | dev.linux.lists.mptcp |
|---|---|
| Message-ID | <f15197064d657dccc64d2057cdd8ee28a6026788.1785380422.git.tanggeliang@kylinos.cn> |
From: Geliang Tang <[email protected]> xfork() does not reseed the RNG in the child, so server and client processes use the same rand() sequence. Restore the per-fork reseed from mptcp_inq.c by calling init_rng() in the child, ensuring independent random values for buffer contents and transfer sizes. init_rng() is now called from each forked child (server, client) so that rand() sequences don't diverge from a single parent seed. But the fallback path did 'srand(time(NULL))' - and time(NULL) has 1-second resolution, so the two children spawned back-to-back in main() almost always land in the same second and end up with the same seed, defeating the per-child re-seed. Mix in getpid() (per-process unique, kernel-guaranteed) with time(NULL) (cross-second entropy) so the fallback produces an independent seed for each child even when /dev/urandom is unavailable. Also hoist the 'foo' local to function scope and collapse the two srand() callsites into a single one at the end, so both branches consistently write the seed into 'foo' first. Signed-off-by: Geliang Tang <[email protected]> --- tools/testing/selftests/net/mptcp/mptcp_sockopt.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/net/mptcp/mptcp_sockopt.c b/tools/testing/selftests/net/mptcp/mptcp_sockopt.c index b6e58d936ebe..6c3d896d4a1b 100644 --- a/tools/testing/selftests/net/mptcp/mptcp_sockopt.c +++ b/tools/testing/selftests/net/mptcp/mptcp_sockopt.c @@ -128,6 +128,8 @@ struct so_state { #define MIN(a, b) ((a) < (b) ? (a) : (b)) #endif +static void init_rng(void); + static void __noreturn die_perror(const char *msg) { perror(msg); @@ -799,6 +801,8 @@ static pid_t xfork(void) if (p < 0) die_perror("fork"); + else if (p == 0) + init_rng(); return p; } @@ -822,9 +826,9 @@ static int rcheck(int wstatus, const char *what) static void init_rng(void) { int fd = open("/dev/urandom", O_RDONLY); + unsigned int foo; if (fd >= 0) { - unsigned int foo; ssize_t ret; /* can't fail */ @@ -832,10 +836,10 @@ static void init_rng(void) assert(ret == sizeof(foo)); close(fd); - srand(foo); } else { - srand(time(NULL)); + foo = getpid() ^ time(NULL); } + srand(foo); } int main(int argc, char *argv[]) -- 2.53.0