Compare commits

...

3 commits

Author SHA1 Message Date
cgzones
95c8641aee
Merge 96836f463b into 1d7fc7ffe0 2025-04-04 18:45:51 +00:00
Christian Göttsche
1d7fc7ffe0 support GCC15
GCC 15 starts warning about non NUL-terminated string literals:

    chacha.c:44:31: error: initializer-string for array of ‘char’ truncates NUL terminator but destination lacks ‘nonstring’ attribute (17 chars into 16 available) [-Werror=unterminated-string-initialization]
       44 | static const char sigma[16] = "expand 32-byte k";
          |                               ^~~~~~~~~~~~~~~~~~
2025-04-03 18:31:55 -04:00
Christian Göttsche
96836f463b linux: make use of mseal(2)
Instead of protecting the global read-only data structure after startup
via the read-only flag, which can be reverted, use the in Linux 6.10
introduced irreversible syscall mseal(2).
2024-07-27 11:37:59 +02:00
5 changed files with 32 additions and 1 deletions

View file

@ -41,7 +41,7 @@ static const unsigned rounds = 8;
a = PLUS(a, b); d = ROTATE(XOR(d, a), 8); \ a = PLUS(a, b); d = ROTATE(XOR(d, a), 8); \
c = PLUS(c, d); b = ROTATE(XOR(b, c), 7); c = PLUS(c, d); b = ROTATE(XOR(b, c), 7);
static const char sigma[16] = "expand 32-byte k"; static const char sigma[16] NONSTRING = "expand 32-byte k";
void chacha_keysetup(chacha_ctx *x, const u8 *k) { void chacha_keysetup(chacha_ctx *x, const u8 *k) {
x->input[0] = U8TO32_LITTLE(sigma + 0); x->input[0] = U8TO32_LITTLE(sigma + 0);

View file

@ -1295,7 +1295,12 @@ COLD static void init_slow_path(void) {
atomic_store_explicit(&ro.slab_region_end, slab_region_end, memory_order_release); atomic_store_explicit(&ro.slab_region_end, slab_region_end, memory_order_release);
#if defined(__ANDROID__) && defined(HAS_ARM_MTE)
/* Do not seal to support disabling memory tagging */
if (unlikely(memory_protect_ro(&ro, sizeof(ro)))) { if (unlikely(memory_protect_ro(&ro, sizeof(ro)))) {
#else
if (unlikely(memory_protect_seal(&ro, sizeof(ro)))) {
#endif
fatal_error("failed to protect allocator data"); fatal_error("failed to protect allocator data");
} }
memory_set_name(&ro, sizeof(ro), "malloc read-only after init"); memory_set_name(&ro, sizeof(ro), "malloc read-only after init");

View file

@ -1,6 +1,8 @@
#include <errno.h> #include <errno.h>
#include <unistd.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/syscall.h>
#ifdef LABEL_MEMORY #ifdef LABEL_MEMORY
#include <sys/prctl.h> #include <sys/prctl.h>
@ -91,6 +93,22 @@ bool memory_protect_rw_metadata(void *ptr, size_t size) {
return memory_protect_prot(ptr, size, PROT_READ|PROT_WRITE, get_metadata_key()); return memory_protect_prot(ptr, size, PROT_READ|PROT_WRITE, get_metadata_key());
} }
COLD bool memory_protect_seal(void *ptr, size_t size) {
#if defined(__linux__) && defined(__NR_mseal)
/* supported since Linux 6.10 */
int ret = syscall(__NR_mseal, ptr, size, 0);
if (ret == 0)
return false;
if (unlikely(errno == ENOMEM))
return true;
if (errno == ENOSYS)
return memory_protect_ro(ptr, size);
fatal_error("non-ENOMEM and non-ENOSYS mseal failure");
#else
return memory_protect_ro(ptr, size);
#endif
}
#ifdef HAVE_COMPATIBLE_MREMAP #ifdef HAVE_COMPATIBLE_MREMAP
bool memory_remap(void *old, size_t old_size, size_t new_size) { bool memory_remap(void *old, size_t old_size, size_t new_size) {
void *ptr = mremap(old, old_size, new_size, 0); void *ptr = mremap(old, old_size, new_size, 0);

View file

@ -22,6 +22,7 @@ bool memory_unmap(void *ptr, size_t size);
bool memory_protect_ro(void *ptr, size_t size); bool memory_protect_ro(void *ptr, size_t size);
bool memory_protect_rw(void *ptr, size_t size); bool memory_protect_rw(void *ptr, size_t size);
bool memory_protect_rw_metadata(void *ptr, size_t size); bool memory_protect_rw_metadata(void *ptr, size_t size);
bool memory_protect_seal(void *ptr, size_t size);
#ifdef HAVE_COMPATIBLE_MREMAP #ifdef HAVE_COMPATIBLE_MREMAP
bool memory_remap(void *old, size_t old_size, size_t new_size); bool memory_remap(void *old, size_t old_size, size_t new_size);
bool memory_remap_fixed(void *old, size_t old_size, void *new, size_t new_size); bool memory_remap_fixed(void *old, size_t old_size, void *new, size_t new_size);

7
util.h
View file

@ -32,6 +32,13 @@
#define STRINGIFY(s) #s #define STRINGIFY(s) #s
#define ALIAS(f) __attribute__((alias(STRINGIFY(f)))) #define ALIAS(f) __attribute__((alias(STRINGIFY(f))))
// supported since GCC 15
#if __has_attribute (nonstring)
# define NONSTRING __attribute__ ((nonstring))
#else
# define NONSTRING
#endif
typedef uint8_t u8; typedef uint8_t u8;
typedef uint16_t u16; typedef uint16_t u16;
typedef uint32_t u32; typedef uint32_t u32;