diff options
author | Jan Janssen <medhefgo@web.de> | 2023-01-07 22:16:52 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2023-01-09 18:58:54 +0100 |
commit | 3f92dc2fd4070b213e6bc85263a9bef06ec9a486 (patch) | |
tree | 63f4106441053a06868aab1c5a5e6d13c42a4b4f /src/fundamental | |
parent | f977356a82822612d82a8b4507b5140a7a6ffc40 (diff) | |
download | systemd-3f92dc2fd4070b213e6bc85263a9bef06ec9a486.tar.gz |
boot: Simplify object erasure
This erase_obj() machinery looks like voodoo and creates an awful lot of
noise as soon as we get back to building with -O0. We can do this in a
more simple way by introducing a struct that holds the information we
need on cleanup. When building with optimization enabled, all this gets
inlined and the eraser vanishes.
Diffstat (limited to 'src/fundamental')
-rw-r--r-- | src/fundamental/memory-util-fundamental.h | 42 | ||||
-rw-r--r-- | src/fundamental/meson.build | 1 |
2 files changed, 43 insertions, 0 deletions
diff --git a/src/fundamental/memory-util-fundamental.h b/src/fundamental/memory-util-fundamental.h new file mode 100644 index 0000000000..9015300ae8 --- /dev/null +++ b/src/fundamental/memory-util-fundamental.h @@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include <stddef.h> + +#ifdef SD_BOOT +# include "efi-string.h" +#else +# include <string.h> +#endif + +#include "macro-fundamental.h" + +#if defined(HAVE_EXPLICIT_BZERO) +static inline void *explicit_bzero_safe(void *p, size_t l) { + if (p && l > 0) + explicit_bzero(p, l); + + return p; +} +#else +static inline void *explicit_bzero_safe(void *p, size_t l) { + if (p && l > 0) { + memset(p, 0, l); + __asm__ __volatile__("" : : "r"(p) : "memory"); + } + return p; +} +#endif + +struct VarEraser { + void *p; + size_t size; +}; + +static inline void erase_var(struct VarEraser *e) { + explicit_bzero_safe(e->p, e->size); +} + +/* Mark var to be erased when leaving scope. */ +#define CLEANUP_ERASE(var) \ + _cleanup_(erase_var) _unused_ struct VarEraser CONCATENATE(_eraser_, UNIQ) = { .p = &var, .size = sizeof(var) } diff --git a/src/fundamental/meson.build b/src/fundamental/meson.build index 3810d6b456..4b8e32337d 100644 --- a/src/fundamental/meson.build +++ b/src/fundamental/meson.build @@ -6,6 +6,7 @@ fundamental_headers = files( 'bootspec-fundamental.h', 'efivars-fundamental.h', 'macro-fundamental.h', + 'memory-util-fundamental.h', 'sha256.h', 'string-util-fundamental.h', 'tpm-pcr.h', |