From 05cc0054d8d3e07bd4ff62aab247bfa6ac070355 Mon Sep 17 00:00:00 2001 From: Vadim Vetrov Date: Sat, 12 Oct 2024 16:55:27 +0300 Subject: [PATCH] Fix getrandom on older versions --- args.h | 4 ---- tls.c | 22 ++++++++++++++++++---- types.h | 6 ++++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/args.h b/args.h index 0c3d52e..98d65cf 100644 --- a/args.h +++ b/args.h @@ -1,6 +1,3 @@ -#include "utils.h" -#include "tls.h" - #ifndef ARGS_H #define ARGS_H @@ -11,5 +8,4 @@ int parse_args(int argc, char *argv[]); /* Prints starting messages */ void print_welcome(); - #endif /* ARGS_H */ diff --git a/tls.c b/tls.c index b1e6099..6b13c3a 100644 --- a/tls.c +++ b/tls.c @@ -5,8 +5,8 @@ #include "utils.h" #ifndef KERNEL_SPACE -#include -#include +#include +#include #endif #define TLS_CONTENT_TYPE_HANDSHAKE 0x16 @@ -274,6 +274,8 @@ int gen_fake_sni(struct fake_type type, const struct tcphdr *tcph, uint32_t tcph_len, uint8_t *buf, uint32_t *buflen) { uint32_t data_len = type.fake_len; + int ret; + if (type.type == FAKE_PAYLOAD_RANDOM && data_len == 0) { data_len = (uint32_t)randint() % 1200; } @@ -317,9 +319,21 @@ int gen_fake_sni(struct fake_type type, default: // FAKE_PAYLOAD_RANDOM #ifdef KERNEL_SPACE get_random_bytes(bfdptr, data_len); -#else +#else /* KERNEL_SPACE */ +#if _NO_GETRANDOM + ret = open("/dev/urandom", O_RDONLY); + if (ret < 0) { + lgerror("Unable to open /dev/urandom", ret); + return ret; + } + + read(ret, bfdptr, data_len); + close(ret); + +#else /* _NO_GETRANDOM */ getrandom(bfdptr, data_len, 0); -#endif +#endif /* _NO_GETRANDOM */ +#endif /* KERNEL_SPACE */ } if (ipxv == IP4VERSION) { diff --git a/types.h b/types.h index 39f33cf..ab63f61 100644 --- a/types.h +++ b/types.h @@ -14,7 +14,13 @@ #include // IWYU pragma: export #include // IWYU pragma: export #include // IWYU pragma: export + + +#define _NO_GETRANDOM ((__GLIBC__ <= 2 && __GLIBC_MINOR__ < 25)) + +#if !_NO_GETRANDOM #include // IWYU pragma: export +#endif #endif /* SPACES */