From 1feff0c0378ac0509e38115f385171ca130eb32b Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Mon, 15 Jul 2024 16:46:35 -0400 Subject: [PATCH] libroot: Initialize the stack protector guard value with generic_syscall. Avoids open/read/close of a FD on every application startup. May help with #18947 (especially as opening files on devfs is somewhat more expensive than on a regular filesystem.) --- src/system/libroot/os/stack_protector.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/system/libroot/os/stack_protector.cpp b/src/system/libroot/os/stack_protector.cpp index 21fdff5e9e..d441d03cb9 100644 --- a/src/system/libroot/os/stack_protector.cpp +++ b/src/system/libroot/os/stack_protector.cpp @@ -8,7 +8,9 @@ #include #include #include +#include +#include "private/system/random_defs.h" #include "private/system/symbol_visibility.h" @@ -23,15 +25,14 @@ __init_stack_protector() if (__stack_chk_guard != 0) return; - bool done = false; - int fd = open("/dev/random", O_RDONLY, 0); - if (fd >= 0) { - done = read(fd, &__stack_chk_guard, sizeof(__stack_chk_guard)) - == sizeof(__stack_chk_guard); - close(fd); - } + struct random_get_entropy_args args; + args.buffer = &__stack_chk_guard; + args.length = sizeof(__stack_chk_guard); - if (!done) { + status_t status = _kern_generic_syscall(RANDOM_SYSCALLS, RANDOM_GET_ENTROPY, + &args, sizeof(args)); + + if (status != B_OK || args.length != sizeof(__stack_chk_guard)) { unsigned char* p = (unsigned char *)&__stack_chk_guard; p[0] = 0; p[1] = 0;