Botan 3.14.1
Crypto and TLS for C&
os_utils.cpp
Go to the documentation of this file.
1/*
2* OS and machine specific utility functions
3* (C) 2015,2016,2017,2018 Jack Lloyd
4* (C) 2016 Daniel Neus
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/internal/os_utils.h>
10
11#include <botan/exceptn.h>
12#include <botan/mem_ops.h>
13#include <botan/internal/parsing.h>
14#include <botan/internal/target_info.h>
15
16#if defined(BOTAN_HAS_CPUID)
17 #include <botan/internal/cpuid.h>
18#endif
19
20#include <algorithm>
21#include <chrono>
22#include <cstdlib>
23#include <iomanip>
24#include <sstream>
25
26#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
27 #include <errno.h>
28 #include <mutex>
29 #include <pthread.h>
30 #include <setjmp.h>
31 #include <signal.h>
32 #include <sys/mman.h>
33 #include <sys/resource.h>
34 #include <sys/types.h>
35 #include <termios.h>
36 #include <unistd.h>
37 #undef B0
38#endif
39
40#if defined(BOTAN_TARGET_OS_IS_EMSCRIPTEN)
41 #include <emscripten/emscripten.h>
42#endif
43
44#if defined(BOTAN_TARGET_OS_HAS_GETAUXVAL) || defined(BOTAN_TARGET_OS_HAS_ELF_AUX_INFO)
45 #include <sys/auxv.h>
46#endif
47
48#if defined(BOTAN_TARGET_OS_HAS_WIN32)
49 #define NOMINMAX 1
50 #define _WINSOCKAPI_ // stop windows.h including winsock.h
51 #include <windows.h>
52 #if defined(BOTAN_BUILD_COMPILER_IS_MSVC)
53 #include <libloaderapi.h>
54 #include <stringapiset.h>
55 #endif
56#endif
57
58#if defined(BOTAN_TARGET_OS_IS_IOS) || defined(BOTAN_TARGET_OS_IS_MACOS)
59 #include <mach/vm_statistics.h>
60 #include <sys/sysctl.h>
61 #include <sys/types.h>
62#endif
63
64#if defined(BOTAN_TARGET_OS_HAS_PRCTL)
65 #include <sys/prctl.h>
66#endif
67
68#if defined(BOTAN_TARGET_OS_IS_FREEBSD) || defined(BOTAN_TARGET_OS_IS_OPENBSD) || defined(BOTAN_TARGET_OS_IS_DRAGONFLY)
69 #include <pthread_np.h>
70#endif
71
72#if defined(BOTAN_TARGET_OS_IS_HAIKU)
73 #include <kernel/OS.h>
74#endif
75
76namespace Botan {
77
79#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
80 return ::getpid();
81#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
82 return ::GetCurrentProcessId();
83#elif defined(BOTAN_TARGET_OS_IS_LLVM) || defined(BOTAN_TARGET_OS_IS_NONE)
84 return 0; // truly no meaningful value
85#else
86 #error "Missing get_process_id"
87#endif
88}
89
90namespace {
91
92#if defined(BOTAN_TARGET_OS_HAS_GETAUXVAL) || defined(BOTAN_TARGET_OS_HAS_ELF_AUX_INFO)
93 #define BOTAN_TARGET_HAS_AUXVAL_INTERFACE
94#endif
95
96std::optional<unsigned long> auxval_hwcap() {
97#if defined(AT_HWCAP)
98 return AT_HWCAP;
99#elif defined(BOTAN_TARGET_HAS_AUXVAL_INTERFACE)
100 // If the value is not defined in a header we can see,
101 // but auxval is supported, return the Linux/Android value
102 return 16;
103#else
104 return {};
105#endif
106}
107
108std::optional<unsigned long> auxval_hwcap2() {
109#if defined(AT_HWCAP2)
110 return AT_HWCAP2;
111#elif defined(BOTAN_TARGET_HAS_AUXVAL_INTERFACE)
112 // If the value is not defined in a header we can see,
113 // but auxval is supported, return the Linux/Android value
114 return 26;
115#else
116 return {};
117#endif
118}
119
120std::optional<unsigned long> get_auxval(std::optional<unsigned long> id) {
121 if(id) {
122#if defined(BOTAN_TARGET_OS_HAS_GETAUXVAL)
123 return ::getauxval(*id);
124#elif defined(BOTAN_TARGET_OS_HAS_ELF_AUX_INFO)
125 unsigned long auxinfo = 0;
126 if(::elf_aux_info(static_cast<int>(*id), &auxinfo, sizeof(auxinfo)) == 0) {
127 return auxinfo;
128 }
129#endif
130 }
131
132 return {};
133}
134
135} // namespace
136
137std::optional<std::pair<unsigned long, unsigned long>> OS::get_auxval_hwcap() {
138 if(const auto hwcap = get_auxval(auxval_hwcap())) {
139 // If hwcap worked/was valid, we don't require hwcap2 to also
140 // succeed but instead will return zeros if it failed.
141 const auto hwcap2 = get_auxval(auxval_hwcap2()).value_or(0);
142 return std::make_pair(*hwcap, hwcap2);
143 } else {
144 return {};
145 }
146}
147
148namespace {
149
150/**
151* Test if we are currently running with elevated permissions
152* eg setuid, setgid, or with POSIX caps set.
153*/
154bool running_in_privileged_state() {
155#if defined(AT_SECURE)
156 if(const auto at_secure = get_auxval(AT_SECURE)) {
157 return at_secure != 0;
158 }
159#endif
160
161#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
162 return (::getuid() != ::geteuid()) || (::getgid() != ::getegid());
163#else
164 return false;
165#endif
166}
167
168} // namespace
169
171 uint64_t rtc = 0;
172
173#if defined(BOTAN_TARGET_OS_HAS_WIN32)
174 LARGE_INTEGER tv;
175 ::QueryPerformanceCounter(&tv);
176 rtc = tv.QuadPart;
177
178#elif defined(BOTAN_USE_GCC_INLINE_ASM)
179
180 // NOLINTBEGIN(*-no-assembler)
181
182 #if defined(BOTAN_TARGET_ARCH_IS_X86_64)
183
184 uint32_t rtc_low = 0; // NOLINT(*-const-correctness) clang-tidy doesn't understand inline asm
185 uint32_t rtc_high = 0; // NOLINT(*-const-correctness) clang-tidy doesn't understand inline asm
186 asm volatile("rdtsc" : "=d"(rtc_high), "=a"(rtc_low));
187 rtc = (static_cast<uint64_t>(rtc_high) << 32) | rtc_low;
188
189 #elif defined(BOTAN_TARGET_ARCH_IS_X86_FAMILY) && defined(BOTAN_HAS_CPUID)
190
192 uint32_t rtc_low = 0;
193 uint32_t rtc_high = 0;
194 asm volatile("rdtsc" : "=d"(rtc_high), "=a"(rtc_low));
195 rtc = (static_cast<uint64_t>(rtc_high) << 32) | rtc_low;
196 }
197
198 #elif defined(BOTAN_TARGET_ARCH_IS_PPC64)
199
200 for(;;) {
201 uint32_t rtc_low = 0;
202 uint32_t rtc_high = 0;
203 uint32_t rtc_high2 = 0;
204 asm volatile("mftbu %0" : "=r"(rtc_high));
205 asm volatile("mftb %0" : "=r"(rtc_low));
206 asm volatile("mftbu %0" : "=r"(rtc_high2));
207
208 if(rtc_high == rtc_high2) {
209 rtc = (static_cast<uint64_t>(rtc_high) << 32) | rtc_low;
210 break;
211 }
212 }
213
214 #elif defined(BOTAN_TARGET_ARCH_IS_ALPHA)
215 asm volatile("rpcc %0" : "=r"(rtc));
216
217 #elif defined(BOTAN_TARGET_ARCH_IS_SPARC64) && !defined(BOTAN_TARGET_OS_IS_OPENBSD)
218 // OpenBSD does not trap access to the %tick register so we avoid it there
219 asm volatile("rd %%tick, %0" : "=r"(rtc));
220
221 #elif defined(BOTAN_TARGET_ARCH_IS_IA64)
222 asm volatile("mov %0=ar.itc" : "=r"(rtc));
223
224 #elif defined(BOTAN_TARGET_ARCH_IS_S390X)
225 asm volatile("stck 0(%0)" : : "a"(&rtc) : "memory", "cc");
226
227 #elif defined(BOTAN_TARGET_ARCH_IS_HPPA)
228 asm volatile("mfctl 16,%0" : "=r"(rtc)); // 64-bit only?
229
230 #else
231 //#warning "OS::get_cpu_cycle_counter not implemented"
232 #endif
233
234 // NOLINTEND(*-no-assembler)
235
236#endif
237
238 return rtc;
239}
240
242#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
243
244 #if defined(_SC_NPROCESSORS_ONLN)
245 const long cpu_online = ::sysconf(_SC_NPROCESSORS_ONLN);
246 if(cpu_online > 0) {
247 return static_cast<size_t>(cpu_online);
248 }
249 #endif
250
251 #if defined(_SC_NPROCESSORS_CONF)
252 const long cpu_conf = ::sysconf(_SC_NPROCESSORS_CONF);
253 if(cpu_conf > 0) {
254 return static_cast<size_t>(cpu_conf);
255 }
256 #endif
257
258#endif
259
260#if defined(BOTAN_TARGET_OS_HAS_THREADS)
261 // hardware_concurrency is allowed to return 0 if the value is not
262 // well defined or not computable.
263 const size_t hw_concur = std::thread::hardware_concurrency();
264
265 if(hw_concur > 0) {
266 return hw_concur;
267 }
268#endif
269
270 return 1;
271}
272
274 if(const uint64_t cpu_clock = OS::get_cpu_cycle_counter()) {
275 return cpu_clock;
276 }
277
278#if defined(BOTAN_TARGET_OS_IS_EMSCRIPTEN)
279 return emscripten_get_now();
280#endif
281
282 /*
283 If we got here either we either don't have an asm instruction
284 above, or (for x86) RDTSC is not available at runtime. Try some
285 clock_gettimes and return the first one that works, or otherwise
286 fall back to std::chrono.
287 */
288
289#if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME)
290
291 // The ordering here is somewhat arbitrary...
292 const clockid_t clock_types[] = {
293 #if defined(CLOCK_MONOTONIC_HR)
294 CLOCK_MONOTONIC_HR,
295 #endif
296 #if defined(CLOCK_MONOTONIC_RAW)
297 CLOCK_MONOTONIC_RAW,
298 #endif
299 #if defined(CLOCK_MONOTONIC)
300 CLOCK_MONOTONIC,
301 #endif
302 #if defined(CLOCK_PROCESS_CPUTIME_ID)
303 CLOCK_PROCESS_CPUTIME_ID,
304 #endif
305 #if defined(CLOCK_THREAD_CPUTIME_ID)
306 CLOCK_THREAD_CPUTIME_ID,
307 #endif
308 };
309
310 for(const clockid_t clock : clock_types) {
311 struct timespec ts {};
312
313 if(::clock_gettime(clock, &ts) == 0) {
314 return (static_cast<uint64_t>(ts.tv_sec) * 1000000000) + static_cast<uint64_t>(ts.tv_nsec);
315 }
316 }
317#endif
318
319#if defined(BOTAN_TARGET_OS_HAS_SYSTEM_CLOCK)
320 // Plain C++11 fallback
321 const auto now = std::chrono::high_resolution_clock::now().time_since_epoch();
322 return std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
323#else
324 return 0;
325#endif
326}
327
329#if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME)
330 struct timespec ts {};
331
332 if(::clock_gettime(CLOCK_REALTIME, &ts) == 0) {
333 return (static_cast<uint64_t>(ts.tv_sec) * 1000000000) + static_cast<uint64_t>(ts.tv_nsec);
334 }
335#endif
336
337#if defined(BOTAN_TARGET_OS_HAS_SYSTEM_CLOCK)
338 const auto now = std::chrono::system_clock::now().time_since_epoch();
339 return std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
340#else
341 throw Not_Implemented("OS::get_system_timestamp_ns this system does not support a clock");
342#endif
343}
344
345std::optional<uint64_t> OS::get_thread_cpu_time_ns() {
346#if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME) && defined(CLOCK_THREAD_CPUTIME_ID)
347 // Tick-based accounting is useless for timing a single call, so only
348 // use this clock if it claims fine resolution
349 static const bool usable = []() {
350 struct timespec res {};
351 return ::clock_getres(CLOCK_THREAD_CPUTIME_ID, &res) == 0 && res.tv_sec == 0 && res.tv_nsec <= 10000;
352 }();
353
354 if(usable) {
355 struct timespec ts {};
356
357 if(::clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0) {
358 return (static_cast<uint64_t>(ts.tv_sec) * 1000000000) + static_cast<uint64_t>(ts.tv_nsec);
359 }
360 }
361#endif
362
363 return std::nullopt;
364}
365
366std::string OS::format_time(time_t time, const std::string& format) {
367 std::tm tm{};
368
369#if defined(BOTAN_TARGET_OS_HAS_WIN32)
370 if(::localtime_s(&tm, &time) != 0) {
371 throw Encoding_Error("Could not convert time_t to localtime");
372 }
373#elif defined(BOTAN_TARGET_OS_HAS_POSIX1)
374 if(::localtime_r(&time, &tm) == nullptr) {
375 throw Encoding_Error("Could not convert time_t to localtime");
376 }
377#else
378 if(auto tmp = std::localtime(&time)) {
379 tm = *tmp;
380 } else {
381 throw Encoding_Error("Could not convert time_t to localtime");
382 }
383#endif
384
385 std::ostringstream oss;
386 oss << std::put_time(&tm, format.c_str());
387 return oss.str();
388}
389
391 const size_t default_page_size = 4096;
392
393#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
394 const long p = ::sysconf(_SC_PAGESIZE);
395 if(p > 1) {
396 return static_cast<size_t>(p);
397 } else {
398 return default_page_size;
399 }
400#elif defined(BOTAN_TARGET_OS_HAS_VIRTUAL_LOCK)
401 BOTAN_UNUSED(default_page_size);
402 SYSTEM_INFO sys_info;
403 ::GetSystemInfo(&sys_info);
404 return sys_info.dwPageSize;
405#else
406 return default_page_size;
407#endif
408}
409
411 /*
412 * Linux defaults to only 64 KiB of mlockable memory per process (too small)
413 * but BSDs offer a small fraction of total RAM (more than we need). Bound the
414 * total mlock size to 512 KiB which is enough to run the entire test suite
415 * without spilling to non-mlock memory (and thus presumably also enough for
416 * many useful programs), but small enough that we should not cause problems
417 * even if many processes are mlocking on the same machine.
418 */
419 const size_t max_locked_kb = 512;
420
421 /*
422 * If RLIMIT_MEMLOCK is not defined, likely the OS does not support
423 * unprivileged mlock calls.
424 */
425#if defined(RLIMIT_MEMLOCK) && defined(BOTAN_TARGET_OS_HAS_POSIX1) && defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)
426 const size_t mlock_requested =
427 std::min<size_t>(read_env_variable_sz("BOTAN_MLOCK_POOL_SIZE", max_locked_kb), max_locked_kb);
428
429 if(mlock_requested > 0) {
430 struct ::rlimit limits {};
431
432 ::getrlimit(RLIMIT_MEMLOCK, &limits);
433
434 if(limits.rlim_cur < limits.rlim_max) {
435 limits.rlim_cur = limits.rlim_max;
436 ::setrlimit(RLIMIT_MEMLOCK, &limits);
437 ::getrlimit(RLIMIT_MEMLOCK, &limits);
438 }
439
440 return std::min<size_t>(limits.rlim_cur, mlock_requested * 1024);
441 }
442
443#elif defined(BOTAN_TARGET_OS_HAS_VIRTUAL_LOCK)
444 const size_t mlock_requested =
445 std::min<size_t>(read_env_variable_sz("BOTAN_MLOCK_POOL_SIZE", max_locked_kb), max_locked_kb);
446
447 SIZE_T working_min = 0, working_max = 0;
448 if(!::GetProcessWorkingSetSize(::GetCurrentProcess(), &working_min, &working_max)) {
449 return 0;
450 }
451
452 // According to Microsoft MSDN:
453 // The maximum number of pages that a process can lock is equal to the number of pages in its minimum working set minus a small overhead
454 // In the book "Windows Internals Part 2": the maximum lockable pages are minimum working set size - 8 pages
455 // But the information in the book seems to be inaccurate/outdated
456 // I've tested this on Windows 8.1 x64, Windows 10 x64 and Windows 7 x86
457 // On all three OS the value is 11 instead of 8
458 const size_t overhead = OS::system_page_size() * 11;
459 if(working_min > overhead) {
460 const size_t lockable_bytes = working_min - overhead;
461 return std::min<size_t>(lockable_bytes, mlock_requested * 1024);
462 }
463#else
464 // Not supported on this platform
465 BOTAN_UNUSED(max_locked_kb);
466#endif
467
468 return 0;
469}
470
471bool OS::read_env_variable(std::string& value_out, std::string_view name_view) {
472 value_out = "";
473
474 if(running_in_privileged_state()) {
475 return false;
476 }
477
478#if defined(BOTAN_TARGET_OS_HAS_WIN32) && \
479 (defined(BOTAN_BUILD_COMPILER_IS_MSVC) || defined(BOTAN_BUILD_COMPILER_IS_CLANGCL))
480 const std::string name(name_view);
481
482 // First query the required buffer size; req_size is 0 if the variable is not set
483 size_t req_size = 0;
484 if(getenv_s(&req_size, nullptr, 0, name.c_str()) != 0 || req_size == 0) {
485 return false;
486 }
487
488 // The reported size includes the terminating \0
489 std::string val(req_size, '\0');
490 if(getenv_s(&req_size, val.data(), val.size(), name.c_str()) != 0) {
491 return false;
492 }
493
494 value_out = std::string(val.c_str());
495 return true;
496#else
497 const std::string name(name_view);
498 if(const char* val = std::getenv(name.c_str())) {
499 value_out = val;
500 return true;
501 }
502#endif
503
504 return false;
505}
506
507size_t OS::read_env_variable_sz(std::string_view name, size_t def) {
508 std::string value;
509 if(read_env_variable(value, name) && !value.empty()) {
510 if(const auto sz = parse_sz(value)) {
511 return *sz;
512 }
513 }
514
515 return def;
516}
517
518#if defined(BOTAN_TARGET_OS_HAS_POSIX1) && defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)
519
520namespace {
521
522int get_locked_fd() {
523 #if defined(BOTAN_TARGET_OS_IS_IOS) || defined(BOTAN_TARGET_OS_IS_MACOS)
524 // On Darwin, tagging anonymous pages allows vmmap to track these.
525 // Allowed from 240 to 255 for userland applications
526 static constexpr int default_locked_fd = 255;
527 int locked_fd = default_locked_fd;
528
529 if(size_t locked_fdl = OS::read_env_variable_sz("BOTAN_LOCKED_FD", default_locked_fd)) {
530 if(locked_fdl < 240 || locked_fdl > 255) {
531 locked_fdl = default_locked_fd;
532 }
533 locked_fd = static_cast<int>(locked_fdl);
534 }
535 return VM_MAKE_TAG(locked_fd);
536 #else
537 return -1;
538 #endif
539}
540
541int mmap_flags() {
542 int flags = MAP_PRIVATE;
543
544 #if defined(MAP_ANONYMOUS)
545 flags |= MAP_ANONYMOUS;
546 #elif defined(MAP_ANON)
547 flags |= MAP_ANON;
548 #endif
549
550 #if defined(MAP_CONCEAL)
551 flags |= MAP_CONCEAL;
552 #elif defined(MAP_NOCORE)
553 flags |= MAP_NOCORE;
554 #endif
555
556 return flags;
557}
558
559int mmap_prot() {
560 int prot = PROT_READ | PROT_WRITE; // NOLINT(*-const-correctness)
561
562 #if defined(PROT_MAX)
563 prot |= PROT_MAX(prot);
564 #endif
565
566 return prot;
567}
568
569} // namespace
570
571#endif
572
573std::vector<void*> OS::allocate_locked_pages(size_t count) {
574 std::vector<void*> result;
575
576#if(defined(BOTAN_TARGET_OS_HAS_POSIX1) && defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)) || \
577 defined(BOTAN_TARGET_OS_HAS_VIRTUAL_LOCK)
578
579 result.reserve(count);
580
581 const size_t page_size = OS::system_page_size();
582
583 #if defined(BOTAN_TARGET_OS_HAS_POSIX1) && defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)
584 static const int locked_fd = get_locked_fd();
585 #endif
586
587 for(size_t i = 0; i != count; ++i) {
588 void* ptr = nullptr;
589
590 #if defined(BOTAN_TARGET_OS_HAS_POSIX1) && defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)
591 ptr = ::mmap(nullptr,
592 3 * page_size,
593 mmap_prot(),
594 mmap_flags(),
595 /*fd=*/locked_fd,
596 /*offset=*/0);
597
598 if(ptr == MAP_FAILED) {
599 continue;
600 }
601
602 // lock the data page
603 if(::mlock(static_cast<uint8_t*>(ptr) + page_size, page_size) != 0) {
604 ::munmap(ptr, 3 * page_size);
605 continue;
606 }
607
608 #if defined(MADV_DONTDUMP)
609 // we ignore errors here, as DONTDUMP is just a bonus
610 ::madvise(static_cast<uint8_t*>(ptr) + page_size, page_size, MADV_DONTDUMP);
611 #endif
612
613 #elif defined(BOTAN_TARGET_OS_HAS_VIRTUAL_LOCK)
614 ptr = ::VirtualAlloc(nullptr, 3 * page_size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
615
616 if(ptr == nullptr)
617 continue;
618
619 if(::VirtualLock(static_cast<uint8_t*>(ptr) + page_size, page_size) == 0) {
620 ::VirtualFree(ptr, 0, MEM_RELEASE);
621 continue;
622 }
623 #endif
624
625 std::memset(ptr, 0, 3 * page_size); // zero data page and both guard pages
626
627 #if defined(BOTAN_TARGET_OS_HAS_PRCTL) && defined(PR_SET_VMA) && defined(PR_SET_VMA_ANON_NAME)
628 // Attempt to name the data page
629 static constexpr char page_name[] = "Botan mlock pool";
630 // NOLINTNEXTLINE(*-vararg)
631 const int rc = ::prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ptr, 3 * page_size, page_name);
632 BOTAN_UNUSED(rc);
633 #endif
634
635 // Make guard page preceding the data page
636 page_prohibit_access(static_cast<uint8_t*>(ptr));
637 // Make guard page following the data page
638 page_prohibit_access(static_cast<uint8_t*>(ptr) + 2 * page_size);
639
640 result.push_back(static_cast<uint8_t*>(ptr) + page_size);
641 }
642#else
643 BOTAN_UNUSED(count);
644#endif
645
646 return result;
647}
648
649void OS::page_allow_access(void* page) {
650#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
651 const size_t page_size = OS::system_page_size();
652 ::mprotect(page, page_size, PROT_READ | PROT_WRITE);
653#elif defined(BOTAN_TARGET_OS_HAS_VIRTUAL_LOCK)
654 const size_t page_size = OS::system_page_size();
655 DWORD old_perms = 0;
656 ::VirtualProtect(page, page_size, PAGE_READWRITE, &old_perms);
657 BOTAN_UNUSED(old_perms);
658#else
659 BOTAN_UNUSED(page);
660#endif
661}
662
663void OS::page_prohibit_access(void* page) {
664#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
665 const size_t page_size = OS::system_page_size();
666 ::mprotect(page, page_size, PROT_NONE);
667#elif defined(BOTAN_TARGET_OS_HAS_VIRTUAL_LOCK)
668 const size_t page_size = OS::system_page_size();
669 DWORD old_perms = 0;
670 ::VirtualProtect(page, page_size, PAGE_NOACCESS, &old_perms);
671 BOTAN_UNUSED(old_perms);
672#else
673 BOTAN_UNUSED(page);
674#endif
675}
676
677#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
678namespace {
679
680// NOLINTNEXTLINE(*-avoid-non-const-global-variables,*-static-definition-in-anon*)
681static uint64_t g_fork_generation = 1;
682
683void atfork_in_child() {
684 g_fork_generation += 1;
685 // Leave generation 0 reserved for "no forks possible on this system"
686 if(g_fork_generation == 0) {
687 g_fork_generation = 1;
688 }
689}
690
691void setup_atfork() {
692 const int rc = ::pthread_atfork(/*prepare=*/nullptr,
693 /*parent=*/nullptr,
694 /*child=*/atfork_in_child);
695
696 if(rc != 0) {
697 throw Internal_Error("Failed to setup pthread_atfork handler");
698 }
699}
700
701} // namespace
702#endif
703
705#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
706 static std::once_flag g_atfork_initialized;
707 std::call_once(g_atfork_initialized, setup_atfork);
708 return g_fork_generation;
709#else
710 return 0;
711#endif
712}
713
714OS::Fork_Detector::Fork_Detector() : m_process_id(OS::get_process_id()), m_fork_generation(OS::get_fork_generation()) {}
715
717 *this = Fork_Detector();
718}
719
721 // The PID check also catches clones which bypass the pthread_atfork handlers
722 return OS::get_process_id() != m_process_id || OS::get_fork_generation() != m_fork_generation;
723}
724
725void OS::free_locked_pages(const std::vector<void*>& pages) {
726 const size_t page_size = OS::system_page_size();
727
728 for(void* ptr : pages) {
729 secure_scrub_memory(ptr, page_size);
730
731 // ptr points to the data page, guard pages are before and after
732 page_allow_access(static_cast<uint8_t*>(ptr) - page_size);
733 page_allow_access(static_cast<uint8_t*>(ptr) + page_size);
734
735#if defined(BOTAN_TARGET_OS_HAS_POSIX1) && defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)
736 ::munlock(ptr, page_size);
737 ::munmap(static_cast<uint8_t*>(ptr) - page_size, 3 * page_size);
738#elif defined(BOTAN_TARGET_OS_HAS_VIRTUAL_LOCK)
739 ::VirtualUnlock(ptr, page_size);
740 ::VirtualFree(static_cast<uint8_t*>(ptr) - page_size, 0, MEM_RELEASE);
741#endif
742 }
743}
744
745#if defined(BOTAN_TARGET_OS_HAS_THREADS)
746void OS::set_thread_name(std::thread& thread, const std::string& name) {
747 #if defined(BOTAN_TARGET_OS_IS_LINUX) || defined(BOTAN_TARGET_OS_IS_FREEBSD) || defined(BOTAN_TARGET_OS_IS_DRAGONFLY)
748 static_cast<void>(pthread_setname_np(thread.native_handle(), name.c_str()));
749 #elif defined(BOTAN_TARGET_OS_IS_OPENBSD)
750 static_cast<void>(pthread_set_name_np(thread.native_handle(), name.c_str()));
751 #elif defined(BOTAN_TARGET_OS_IS_NETBSD)
752 static_cast<void>(pthread_setname_np(thread.native_handle(), "%s", const_cast<char*>(name.c_str())));
753 #elif defined(BOTAN_TARGET_OS_HAS_WIN32) && defined(BOTAN_BUILD_COMPILER_IS_MSVC)
754 typedef HRESULT(WINAPI * std_proc)(HANDLE, PCWSTR);
755 HMODULE kern = GetModuleHandleA("KernelBase.dll");
756 std_proc set_thread_name = reinterpret_cast<std_proc>(GetProcAddress(kern, "SetThreadDescription"));
757 if(set_thread_name) {
758 std::wstring w;
759 auto sz = MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, nullptr, 0);
760 if(sz > 0) {
761 w.resize(sz);
762 if(MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, &w[0], sz) > 0) {
763 (void)set_thread_name(thread.native_handle(), w.c_str());
764 }
765 }
766 }
767 #elif defined(BOTAN_TARGET_OS_IS_HAIKU)
768 auto thread_id = get_pthread_thread_id(thread.native_handle());
769 static_cast<void>(rename_thread(thread_id, name.c_str()));
770 #else
771 // TODO other possible oses ?
772 // macOs does not seem to allow to name threads other than the current one.
773 BOTAN_UNUSED(thread, name);
774 #endif
775}
776#endif
777
778#if defined(BOTAN_TARGET_OS_HAS_POSIX1) && !defined(BOTAN_TARGET_OS_IS_EMSCRIPTEN)
779
780namespace {
781
782// NOLINTBEGIN(*-avoid-non-const-global-variables)
783
784/*
785* Probes are serialized (see run_cpu_instruction_probe) so these are only
786* written by a single thread at a time. The signal handler reads them, possibly
787* from another thread, in the (unlikely) event that some other thread faults
788* while a probe is running.
789*/
790::sigjmp_buf g_sigill_jmp_buf;
791::pthread_t g_sigill_probe_thread;
792struct sigaction g_sigill_prev_action;
793
794// NOLINTEND(*-avoid-non-const-global-variables)
795
796void botan_sigill_handler(int sig) {
797 if(::pthread_equal(::pthread_self(), g_sigill_probe_thread) != 0) {
798 siglongjmp(g_sigill_jmp_buf, /*non-zero return value*/ 1);
799 }
800
801 /*
802 * The SIGILL was raised on some other thread, so it was not caused by the
803 * probe. Restore the previous disposition and return; the faulting
804 * instruction is then re-executed and handled by the previous handler
805 * (or the default action) instead.
806 */
807 ::sigaction(sig, &g_sigill_prev_action, nullptr);
808}
809
810} // namespace
811
812#endif
813
814int OS::run_cpu_instruction_probe(const std::function<int()>& probe_fn) {
815 volatile int probe_result = -3;
816
817#if defined(BOTAN_TARGET_OS_HAS_POSIX1) && !defined(BOTAN_TARGET_OS_IS_EMSCRIPTEN)
818
819 #if defined(BOTAN_TARGET_OS_HAS_THREADS)
820 // Signal dispositions are process wide, so only run one probe at a time
821 static std::mutex g_sigill_probe_mutex;
822 const std::lock_guard<std::mutex> lock(g_sigill_probe_mutex);
823 #endif
824
825 struct sigaction sigaction {};
826
827 sigaction.sa_handler = botan_sigill_handler;
828 sigemptyset(&sigaction.sa_mask);
829 sigaction.sa_flags = 0;
830
831 g_sigill_probe_thread = ::pthread_self();
832
833 int rc = ::sigaction(SIGILL, &sigaction, &g_sigill_prev_action);
834
835 if(rc != 0) {
836 throw System_Error("run_cpu_instruction_probe sigaction failed", errno);
837 }
838
839 rc = sigsetjmp(g_sigill_jmp_buf, /*save sigs*/ 1);
840
841 if(rc == 0) {
842 // first call to sigsetjmp
843 probe_result = probe_fn();
844 } else if(rc == 1) {
845 // non-local return from siglongjmp in signal handler: return error
846 probe_result = -1;
847 }
848
849 // Restore old SIGILL handler, if any
850 rc = ::sigaction(SIGILL, &g_sigill_prev_action, nullptr);
851 if(rc != 0) {
852 throw System_Error("run_cpu_instruction_probe sigaction restore failed", errno);
853 }
854
855#else
856 BOTAN_UNUSED(probe_fn);
857#endif
858
859 return probe_result;
860}
861
862std::unique_ptr<OS::Echo_Suppression> OS::suppress_echo_on_terminal() {
863#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
864 class POSIX_Echo_Suppression final : public Echo_Suppression {
865 public:
866 POSIX_Echo_Suppression() : m_stdin_fd(fileno(stdin)), m_old_termios{} {
867 if(::tcgetattr(m_stdin_fd, &m_old_termios) != 0) {
868 throw System_Error("Getting terminal status failed", errno);
869 }
870
871 struct termios noecho_flags = m_old_termios;
872 noecho_flags.c_lflag &= ~ECHO;
873 noecho_flags.c_lflag |= ECHONL;
874
875 if(::tcsetattr(m_stdin_fd, TCSANOW, &noecho_flags) != 0) {
876 throw System_Error("Clearing terminal echo bit failed", errno);
877 }
878 }
879
880 void reenable_echo() override {
881 if(m_stdin_fd > 0) {
882 if(::tcsetattr(m_stdin_fd, TCSANOW, &m_old_termios) != 0) {
883 throw System_Error("Restoring terminal echo bit failed", errno);
884 }
885 m_stdin_fd = -1;
886 }
887 }
888
889 ~POSIX_Echo_Suppression() override {
890 try {
891 reenable_echo();
892 } catch(...) {}
893 }
894
895 POSIX_Echo_Suppression(const POSIX_Echo_Suppression& other) = delete;
896 POSIX_Echo_Suppression(POSIX_Echo_Suppression&& other) = delete;
897 POSIX_Echo_Suppression& operator=(const POSIX_Echo_Suppression& other) = delete;
898 POSIX_Echo_Suppression& operator=(POSIX_Echo_Suppression&& other) = delete;
899
900 private:
901 int m_stdin_fd;
902 struct termios m_old_termios;
903 };
904
905 return std::make_unique<POSIX_Echo_Suppression>();
906
907#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
908
909 class Win32_Echo_Suppression final : public Echo_Suppression {
910 public:
911 Win32_Echo_Suppression() : m_input_handle(::GetStdHandle(STD_INPUT_HANDLE)) {
912 if(::GetConsoleMode(m_input_handle, &m_console_state) == 0)
913 throw System_Error("Getting console mode failed", ::GetLastError());
914
915 DWORD new_mode = ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
916 if(::SetConsoleMode(m_input_handle, new_mode) == 0)
917 throw System_Error("Setting console mode failed", ::GetLastError());
918 }
919
920 void reenable_echo() override {
921 if(m_input_handle != INVALID_HANDLE_VALUE) {
922 if(::SetConsoleMode(m_input_handle, m_console_state) == 0)
923 throw System_Error("Setting console mode failed", ::GetLastError());
924 m_input_handle = INVALID_HANDLE_VALUE;
925 }
926 }
927
928 ~Win32_Echo_Suppression() override {
929 try {
930 reenable_echo();
931 } catch(...) {}
932 }
933
934 Win32_Echo_Suppression(const Win32_Echo_Suppression& other) = delete;
935 Win32_Echo_Suppression(Win32_Echo_Suppression&& other) = delete;
936 Win32_Echo_Suppression& operator=(const Win32_Echo_Suppression& other) = delete;
937 Win32_Echo_Suppression& operator=(Win32_Echo_Suppression&& other) = delete;
938
939 private:
940 HANDLE m_input_handle;
941 DWORD m_console_state;
942 };
943
944 return std::make_unique<Win32_Echo_Suppression>();
945
946#else
947
948 // Not supported on this platform, return null
949 return nullptr;
950#endif
951}
952
953} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
static bool has(CPUID::Feature feat)
Definition cpuid.h:93
bool fork_detected() const
Definition os_utils.cpp:720
const char * name
Definition ffi_tls.h:59
size_t get_memory_locking_limit()
Definition os_utils.cpp:410
uint64_t BOTAN_TEST_API get_high_resolution_clock()
Definition os_utils.cpp:273
size_t BOTAN_TEST_API get_cpu_available()
Definition os_utils.cpp:241
std::unique_ptr< Echo_Suppression > BOTAN_UNSTABLE_API suppress_echo_on_terminal()
Definition os_utils.cpp:862
size_t read_env_variable_sz(std::string_view var_name, size_t def_value=0)
Definition os_utils.cpp:507
void page_allow_access(void *page)
Definition os_utils.cpp:649
std::string BOTAN_TEST_API format_time(time_t time, const std::string &format)
Definition os_utils.cpp:366
bool read_env_variable(std::string &value_out, std::string_view var_name)
Definition os_utils.cpp:471
void page_prohibit_access(void *page)
Definition os_utils.cpp:663
std::optional< std::pair< unsigned long, unsigned long > > get_auxval_hwcap()
Definition os_utils.cpp:137
int BOTAN_TEST_API run_cpu_instruction_probe(const std::function< int()> &probe_fn)
Definition os_utils.cpp:814
std::vector< void * > allocate_locked_pages(size_t count)
Definition os_utils.cpp:573
size_t system_page_size()
Definition os_utils.cpp:390
uint64_t BOTAN_TEST_API get_system_timestamp_ns()
Definition os_utils.cpp:328
std::optional< uint64_t > BOTAN_TEST_API get_thread_cpu_time_ns()
Definition os_utils.cpp:345
void free_locked_pages(const std::vector< void * > &pages)
Definition os_utils.cpp:725
uint32_t BOTAN_TEST_API get_process_id()
Definition os_utils.cpp:78
uint64_t BOTAN_TEST_API get_cpu_cycle_counter()
Definition os_utils.cpp:170
uint64_t BOTAN_TEST_API get_fork_generation()
Definition os_utils.cpp:704
Flags flags(Flag flags)
Definition p11.h:1235
std::optional< size_t > parse_sz(std::string_view input, bool require_canonical)
Definition parsing.cpp:71
secure_vector< T > lock(const std::vector< T > &in)
Definition secmem.h:145
void secure_scrub_memory(void *ptr, size_t n)
Definition mem_utils.cpp:24