From 8c4cadec6d436b98902920d034afa4237455138c Mon Sep 17 00:00:00 2001 From: Mikolaj Zalewski Date: Thu, 11 Oct 2007 13:14:46 -0700 Subject: [PATCH] server: make the pids/tids more random --- server/main.c | 9 +++++++++ server/process.c | 48 +++++++++++++++++++++++++++++++----------------- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/server/main.c b/server/main.c index c839c18..ab2a4a4 100644 --- a/server/main.c +++ b/server/main.c @@ -115,6 +115,14 @@ static void sigterm_handler( int signum exit(1); /* make sure atexit functions get called */ } +static void init_random() +{ + time_t t; + + time(&t); + srandom(t); +} + int main( int argc, char *argv[] ) { parse_args( argc, argv ); @@ -132,6 +140,7 @@ int main( int argc, char *argv[] ) setvbuf( stderr, NULL, _IOLBF, 0 ); if (debug_level) fprintf( stderr, "wineserver: starting (pid=%ld)\n", (long) getpid() ); + init_random(); init_signals(); init_directories(); init_registry(); diff --git a/server/process.c b/server/process.c index 8ca20d8..3f44908 100644 --- a/server/process.c +++ b/server/process.c @@ -139,33 +139,44 @@ struct ptid_entry }; static struct ptid_entry *ptid_entries; /* array of ptid entries */ -static unsigned int used_ptid_entries; /* number of entries in use */ static unsigned int alloc_ptid_entries; /* number of allocated entries */ static unsigned int next_free_ptid; /* next free entry */ static unsigned int last_free_ptid; /* last free entry */ #define PTID_OFFSET 8 /* offset for first ptid value */ +/* some apps uses the pid/tid as random seed. This adds a bit of randomness to them. + * count must be a power of 2 */ +static void randomize_and_add_ptids( int base, int count ) +{ + int step, first, last; + int i; + + step = random() % (count/2); + step = 2*step + 1; + + for (i = 0; i < count; i++) + { + ptid_entries[base + i].next = base + ((i + step) % count) + PTID_OFFSET; + ptid_entries[base + i].ptr = NULL; + } + + last = base + (random() % count); + first = ptid_entries[last].next - PTID_OFFSET; + ptid_entries[last].next = 0; + last_free_ptid = last + PTID_OFFSET; + next_free_ptid = first + PTID_OFFSET; +} + /* allocate a new process or thread id */ unsigned int alloc_ptid( void *ptr ) { struct ptid_entry *entry; unsigned int id; - if (used_ptid_entries < alloc_ptid_entries) - { - id = used_ptid_entries + PTID_OFFSET; - entry = &ptid_entries[used_ptid_entries++]; - } - else if (next_free_ptid) + if (!next_free_ptid) /* need to grow the array */ { - id = next_free_ptid; - entry = &ptid_entries[id - PTID_OFFSET]; - if (!(next_free_ptid = entry->next)) last_free_ptid = 0; - } - else /* need to grow the array */ - { - unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2); + unsigned int count = 2*alloc_ptid_entries; if (!count) count = 64; if (!(entry = realloc( ptid_entries, count * sizeof(*entry) ))) { @@ -173,11 +184,14 @@ unsigned int alloc_ptid( void *ptr ) return 0; } ptid_entries = entry; + randomize_and_add_ptids(alloc_ptid_entries, count - alloc_ptid_entries); alloc_ptid_entries = count; - id = used_ptid_entries + PTID_OFFSET; - entry = &ptid_entries[used_ptid_entries++]; } + id = next_free_ptid; + entry = &ptid_entries[id - PTID_OFFSET]; + if (!(next_free_ptid = entry->next)) last_free_ptid = 0; + entry->ptr = ptr; return id; } @@ -201,7 +215,7 @@ void free_ptid( unsigned int id ) void *get_ptid_entry( unsigned int id ) { if (id < PTID_OFFSET) return NULL; - if (id - PTID_OFFSET >= used_ptid_entries) return NULL; + if (id - PTID_OFFSET >= alloc_ptid_entries) return NULL; return ptid_entries[id - PTID_OFFSET].ptr; } -- 1.4.1