diff --git a/CMakeLists.txt b/CMakeLists.txt index 56bce6b..6efbbda 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -90,6 +90,7 @@ endif() # Check platform API. chk_function_exists(explicit_bzero) +chk_function_exists(timingsafe_bcmp) chk_function_exists(memrchr) chk_function_exists(memmem) chk_function_exists(strlcpy) diff --git a/include/al/os.h b/include/al/os.h index cc3fc55..d9cbf2a 100644 --- a/include/al/os.h +++ b/include/al/os.h @@ -212,6 +212,24 @@ explicit_bzero(void *b, size_t len) { } #endif +#ifndef HAVE_TIMINGSAFE_BCMP +static inline int +timingsafe_bcmp(const void *b1, const void *b2, size_t len) { + int ret = 0; + const uint8_t *p1 = b1, *p2 = b2; + + if (0 == len || b1 == b2) + return (0); + if (NULL == b1 || + NULL == b2) + return (1); + for (size_t i = 0; i < len; i ++) { + ret |= (p1[i] ^ p2[i]); + } + return ((0 != ret)); +} +#endif + #ifndef HAVE_MEMRCHR static inline void * memrchr(const void *buf, const int what_find, const size_t buf_size) { @@ -292,7 +310,7 @@ freezero(void *ptr, const size_t size) { if (NULL == ptr) return; - memset_volatile(ptr, 0x00, size); + explicit_bzero(ptr, size); free(ptr); } #endif @@ -316,8 +334,6 @@ strlcpy(char * restrict dst, const char * restrict src, size_t size) { #endif -/* Syscalls. */ - /* pthread_create(2) can spuriously fail on Linux. This is a function * to wrap pthread_create(2) to retry if it fails with EAGAIN. */ static inline int @@ -360,6 +376,9 @@ pthread_self_name_set(const char *name) { } + +/* Syscalls. */ + #ifndef HAVE_PIPE2 static inline int pipe2(int fildes[2], int flags) { diff --git a/include/proto/radius.h b/include/proto/radius.h index e9ec8b2..5c40de5 100644 --- a/include/proto/radius.h +++ b/include/proto/radius.h @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2014 - 2020 Rozhuk Ivan + * Copyright (c) 2014-2024 Rozhuk Ivan * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -49,6 +49,7 @@ #include /* ntohs(), htons() */ #include #include +#include "al/os.h" #include "crypto/hash/md5.h" #ifndef ENOATTR @@ -585,21 +586,6 @@ typedef struct radius_pkt_hdr_s { /* Radius packet header. */ -/* Constatnt time memory comparation, prevent timing attacks - * http://www.cs.rice.edu/~dwallach/pub/crosby-timing2009.pdf */ -static inline int -radius_sec_memcmp(uint8_t const *a, uint8_t const *b, const size_t size) { - register int res = 0; - register size_t i; - - for (i = 0; i < size; i ++) { - res |= a[i] ^ b[i]; - } - - return (res); -} - - ////////////////////////////////////////////////////////////////////////// ////////////////////////Radius packet attribute/////////////////////////// ////////////////////////////////////////////////////////////////////////// @@ -959,7 +945,7 @@ radius_pkt_attr_msg_authenticator_chk(rad_pkt_hdr_p pkt, size_t offset, pkt_authenticator_inside, pkt_req, (uint8_t*)calc_msg_authr); if (0 != error) return (error); - if (0 != radius_sec_memcmp(RADIUS_PKT_ATTR_DATA(attr), calc_msg_authr, + if (0 != timingsafe_bcmp(RADIUS_PKT_ATTR_DATA(attr), calc_msg_authr, MD5_HASH_SIZE)) return (EBADMSG); @@ -1408,7 +1394,7 @@ radius_pkt_authenticator_chk(rad_pkt_hdr_p pkt, uint8_t *key, size_t key_len, if (0 != radius_pkt_authenticator_calc(pkt, key, key_len, pkt_authenticator_inside, pkt_req, (uint8_t*)calc_authr)) return (EINVAL); - if (0 != radius_sec_memcmp(pkt->authenticator, calc_authr, MD5_HASH_SIZE)) + if (0 != timingsafe_bcmp(pkt->authenticator, calc_authr, MD5_HASH_SIZE)) return (EBADMSG); return (0); diff --git a/include/utils/mem_utils.h b/include/utils/mem_utils.h index d566536..41c950f 100644 --- a/include/utils/mem_utils.h +++ b/include/utils/mem_utils.h @@ -393,27 +393,6 @@ mem_cmpn(const void *buf1, const size_t buf1_size, return (mem_cmp(buf1, buf2, buf1_size)); } -/* Secure version of memcmp(). */ -static inline int -mem_scmp(const void *buf1, const void *buf2, const size_t size) { - register int res = 0; - register size_t i; - register const uint8_t *a = (const uint8_t*)buf1; - register const uint8_t *b = (const uint8_t*)buf2; - - if (0 == size || buf1 == buf2) - return (0); - if (NULL == buf1) - return (-127); - if (NULL == buf2) - return (127); - for (i = 0; i < size; i ++) { - res |= (a[i] ^ b[i]); - } - - return (res); -} - //////////////////////////////////////////////////////////////////////// ////////////// Compare, ignory case, like strncasecmp() //////////////// diff --git a/lib.project b/lib.project index 5931ea0..0a94d64 100644 --- a/lib.project +++ b/lib.project @@ -137,7 +137,7 @@ - + diff --git a/src/proto/http_client.c b/src/proto/http_client.c index f5d4eb9..b7d37d4 100644 --- a/src/proto/http_client.c +++ b/src/proto/http_client.c @@ -1144,7 +1144,7 @@ http_cli_recv_done_cb(io_task_p iotask, int error, io_buf_p buf, int eof, host_port = UStr8ToUNum32(ptm, (cli->req.host_size - tm)); tm --; } - action = (0 == mem_cmpin_cstr(c"localhost", li->req.host, tm)); + action = (0 == mem_cmpin_cstr("localhost", li->req.host, tm)); /* Is connection to loopback from ext host? */ if (0 != action && 0 == sa_addr_is_loopback(&cli->addr)) /* from ext host? */ goto conn_from_net_to_loopback; diff --git a/tests/threadpool/test-threadpool.project b/tests/threadpool/test-threadpool.project index 8bce257..313c625 100644 --- a/tests/threadpool/test-threadpool.project +++ b/tests/threadpool/test-threadpool.project @@ -10,7 +10,7 @@ - +