From 963423df59ab55b30ceabae18183b49ccd519f7a Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Mon, 8 Jun 2026 16:11:00 +0800 Subject: [PATCH] add functions fc_srand and fc_rand for more security under Linux --- HISTORY | 3 +++ src/fast_allocator.c | 2 +- src/flat_skiplist.c | 3 ++- src/multi_skiplist.c | 3 ++- src/shared_func.c | 49 +++++++++++++++++++++++++++++++++----------- src/shared_func.h | 8 ++++++++ src/skiplist_set.c | 3 ++- src/uniq_skiplist.c | 3 ++- 8 files changed, 57 insertions(+), 17 deletions(-) diff --git a/HISTORY b/HISTORY index a910a17..2e5c358 100644 --- a/HISTORY +++ b/HISTORY @@ -1,4 +1,7 @@ +Version 1.85 2026-06-08 + * add functions fc_srand and fc_rand for more security under Linux + Version 1.84 2026-01-16 * fast_task_queue.h: add function free_queue_task_arg_offset * add function double2str and double_to_comma_str diff --git a/src/fast_allocator.c b/src/fast_allocator.c index ba8ba81..a11d75f 100644 --- a/src/fast_allocator.c +++ b/src/fast_allocator.c @@ -238,7 +238,7 @@ int fast_allocator_init_ex(struct fast_allocator_context *acontext, struct fast_region_info *pRegion; struct fast_region_info *region_end; - srand(time(NULL)); + set_rand_seed(); memset(acontext, 0, sizeof(*acontext)); if (region_count <= 0) { diff --git a/src/flat_skiplist.c b/src/flat_skiplist.c index 2d10a08..8d4ca72 100644 --- a/src/flat_skiplist.c +++ b/src/flat_skiplist.c @@ -22,6 +22,7 @@ #include #include #include "logger.h" +#include "shared_func.h" #include "fc_memory.h" #include "flat_skiplist.h" @@ -111,7 +112,7 @@ int flat_skiplist_init_ex(FlatSkiplist *sl, const int level_count, sl->compare_func = compare_func; sl->free_func = free_func; - srand(time(NULL)); + set_rand_seed(); return 0; } diff --git a/src/multi_skiplist.c b/src/multi_skiplist.c index 391d222..7b8eb5b 100644 --- a/src/multi_skiplist.c +++ b/src/multi_skiplist.c @@ -22,6 +22,7 @@ #include #include #include "logger.h" +#include "shared_func.h" #include "fc_memory.h" #include "multi_skiplist.h" @@ -118,7 +119,7 @@ int multi_skiplist_init_ex(MultiSkiplist *sl, const int level_count, sl->compare_func = compare_func; sl->free_func = free_func; - srand(time(NULL)); + set_rand_seed(); return 0; } diff --git a/src/shared_func.c b/src/shared_func.c index 313d336..88a7f1c 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -2486,19 +2486,15 @@ int parse_bytes(const char *pStr, const int default_unit_bytes, int64_t *bytes) int set_rand_seed() { - struct timeval tv; + time_t ts; + int64_t ns; + uint32_t seed; - if (gettimeofday(&tv, NULL) != 0) - { - logError("file: "__FILE__", line: %d, " \ - "call gettimeofday fail, " \ - "errno=%d, error info: %s", \ - __LINE__, errno, STRERROR(errno)); - return errno != 0 ? errno : EPERM; - } - - srand(tv.tv_sec ^ tv.tv_usec); - return 0; + ns = get_current_time_ns(); + time(&ts); + seed = ts ^ (ns & 0xFFFFFFFF); + srand(seed); + return 0; } int get_time_item_from_conf_ex(IniFullContext *ini_ctx, @@ -4555,3 +4551,32 @@ const char *double2str(const double d, const int scale, memcpy(buff + new_len, fragment, tail_len + 1); return buff; } + +#ifdef OS_LINUX + +#ifndef __NR_getrandom +#define __NR_getrandom 318 +#endif + +void fc_srand() +{ + uint32_t seed; + + if (syscall(__NR_getrandom, &seed, sizeof(seed), 0) != sizeof(seed)) { + set_rand_seed(); + } else { + srand(seed); + } +} + +int fc_rand() +{ + uint32_t n; + + if (syscall(__NR_getrandom, &n, sizeof(n), 0) != sizeof(n)) { + return rand(); + } + + return (n & RAND_MAX); +} +#endif diff --git a/src/shared_func.h b/src/shared_func.h index d7bac38..b6d00f8 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -1867,6 +1867,14 @@ static inline size_t fc_iov_get_bytes(const } } +#ifdef OS_LINUX +void fc_srand(); +int fc_rand(); +#else +#define fc_srand() set_rand_seed() +#define fc_rand() rand() +#endif + #ifdef __cplusplus } #endif diff --git a/src/skiplist_set.c b/src/skiplist_set.c index b7d9f7a..a34d6c8 100644 --- a/src/skiplist_set.c +++ b/src/skiplist_set.c @@ -22,6 +22,7 @@ #include #include #include "logger.h" +#include "shared_func.h" #include "fc_memory.h" #include "skiplist_set.h" @@ -110,7 +111,7 @@ int skiplist_set_init_ex(SkiplistSet *sl, const int level_count, sl->compare_func = compare_func; sl->free_func = free_func; - srand(time(NULL)); + set_rand_seed(); return 0; } diff --git a/src/uniq_skiplist.c b/src/uniq_skiplist.c index 2e3b856..3fbcc66 100644 --- a/src/uniq_skiplist.c +++ b/src/uniq_skiplist.c @@ -21,6 +21,7 @@ #include #include #include "logger.h" +#include "shared_func.h" #include "fc_memory.h" #include "uniq_skiplist.h" @@ -138,7 +139,7 @@ int uniq_skiplist_init_ex2(UniqSkiplistFactory *factory, factory->delay_free_seconds = delay_free_seconds; factory->arg = arg; - srand(time(NULL)); + set_rand_seed(); return 0; }