Botan 3.5.0
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/cpuid.h>
14
15#include <algorithm>
16#include <chrono>
17#include <cstdlib>
18#include <iomanip>
19#include <sstream>
20
21#if defined(BOTAN_TARGET_OS_HAS_EXPLICIT_BZERO)
22 #include <string.h>
23#endif
24
25#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
26 #include <errno.h>
27 #include <pthread.h>
28 #include <setjmp.h>
29 #include <signal.h>
30 #include <stdlib.h>
31 #include <sys/mman.h>
32 #include <sys/resource.h>
33 #include <sys/types.h>
34 #include <termios.h>
35 #include <unistd.h>
36 #undef B0
37#endif
38
39#if defined(BOTAN_TARGET_OS_IS_EMSCRIPTEN)
40 #include <emscripten/emscripten.h>
41#endif
42
43#if defined(BOTAN_TARGET_OS_HAS_GETAUXVAL) || defined(BOTAN_TARGET_OS_IS_ANDROID) || \
44 defined(BOTAN_TARGET_OS_HAS_ELF_AUX_INFO)
45 #include <sys/auxv.h>
46#endif
47
48#if defined(BOTAN_TARGET_OS_HAS_AUXINFO)
49 #include <dlfcn.h>
50 #include <elf.h>
51#endif
52
53#if defined(BOTAN_TARGET_OS_HAS_WIN32)
54 #define NOMINMAX 1
55 #define _WINSOCKAPI_ // stop windows.h including winsock.h
56 #include <windows.h>
57 #if defined(BOTAN_BUILD_COMPILER_IS_MSVC)
58 #include <libloaderapi.h>
59 #include <stringapiset.h>
60 #endif
61#endif
62
63#if defined(BOTAN_TARGET_OS_IS_ANDROID)
64 #include <elf.h>
65extern "C" char** environ;
66#endif
67
68#if defined(BOTAN_TARGET_OS_IS_IOS) || defined(BOTAN_TARGET_OS_IS_MACOS)
69 #include <mach/vm_statistics.h>
70 #include <sys/sysctl.h>
71 #include <sys/types.h>
72#endif
73
74#if defined(BOTAN_TARGET_OS_HAS_PRCTL)
75 #include <sys/prctl.h>
76#endif
77
78#if defined(BOTAN_TARGET_OS_IS_FREEBSD) || defined(BOTAN_TARGET_OS_IS_OPENBSD) || defined(BOTAN_TARGET_OS_IS_DRAGONFLY)
79 #include <pthread_np.h>
80#endif
81
82#if defined(BOTAN_TARGET_OS_IS_HAIKU)
83 #include <kernel/OS.h>
84#endif
85
86namespace Botan {
87
88// Not defined in OS namespace for historical reasons
89void secure_scrub_memory(void* ptr, size_t n) {
90#if defined(BOTAN_TARGET_OS_HAS_RTLSECUREZEROMEMORY)
91 ::RtlSecureZeroMemory(ptr, n);
92
93#elif defined(BOTAN_TARGET_OS_HAS_EXPLICIT_BZERO)
94 ::explicit_bzero(ptr, n);
95
96#elif defined(BOTAN_TARGET_OS_HAS_EXPLICIT_MEMSET)
97 (void)::explicit_memset(ptr, 0, n);
98
99#elif defined(BOTAN_USE_VOLATILE_MEMSET_FOR_ZERO) && (BOTAN_USE_VOLATILE_MEMSET_FOR_ZERO == 1)
100 /*
101 Call memset through a static volatile pointer, which the compiler
102 should not elide. This construct should be safe in conforming
103 compilers, but who knows. I did confirm that on x86-64 GCC 6.1 and
104 Clang 3.8 both create code that saves the memset address in the
105 data segment and unconditionally loads and jumps to that address.
106 */
107 static void* (*const volatile memset_ptr)(void*, int, size_t) = std::memset;
108 (memset_ptr)(ptr, 0, n);
109#else
110
111 volatile uint8_t* p = reinterpret_cast<volatile uint8_t*>(ptr);
112
113 for(size_t i = 0; i != n; ++i)
114 p[i] = 0;
115#endif
116}
117
119#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
120 return ::getpid();
121#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
122 return ::GetCurrentProcessId();
123#elif defined(BOTAN_TARGET_OS_IS_LLVM) || defined(BOTAN_TARGET_OS_IS_NONE)
124 return 0; // truly no meaningful value
125#else
126 #error "Missing get_process_id"
127#endif
128}
129
130unsigned long OS::get_auxval(unsigned long id) {
131#if defined(BOTAN_TARGET_OS_HAS_GETAUXVAL)
132 return ::getauxval(id);
133#elif defined(BOTAN_TARGET_OS_IS_ANDROID) && defined(BOTAN_TARGET_ARCH_IS_ARM32)
134
135 if(id == 0)
136 return 0;
137
138 char** p = environ;
139
140 while(*p++ != nullptr)
141 ;
142
143 Elf32_auxv_t* e = reinterpret_cast<Elf32_auxv_t*>(p);
144
145 while(e != nullptr) {
146 if(e->a_type == id)
147 return e->a_un.a_val;
148 e++;
149 }
150
151 return 0;
152#elif defined(BOTAN_TARGET_OS_HAS_ELF_AUX_INFO)
153 unsigned long auxinfo = 0;
154 ::elf_aux_info(static_cast<int>(id), &auxinfo, sizeof(auxinfo));
155 return auxinfo;
156#elif defined(BOTAN_TARGET_OS_HAS_AUXINFO)
157 for(const AuxInfo* auxinfo = static_cast<AuxInfo*>(::_dlauxinfo()); auxinfo != AT_NULL; ++auxinfo) {
158 if(id == auxinfo->a_type)
159 return auxinfo->a_v;
160 }
161
162 return 0;
163#else
164 BOTAN_UNUSED(id);
165 return 0;
166#endif
167}
168
170#if defined(AT_SECURE)
171 return OS::get_auxval(AT_SECURE) != 0;
172#elif defined(BOTAN_TARGET_OS_HAS_POSIX1)
173 return (::getuid() != ::geteuid()) || (::getgid() != ::getegid());
174#else
175 return false;
176#endif
177}
178
180 uint64_t rtc = 0;
181
182#if defined(BOTAN_TARGET_OS_HAS_WIN32)
183 LARGE_INTEGER tv;
184 ::QueryPerformanceCounter(&tv);
185 rtc = tv.QuadPart;
186
187#elif defined(BOTAN_USE_GCC_INLINE_ASM)
188
189 #if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
190
191 if(CPUID::has_rdtsc()) {
192 uint32_t rtc_low = 0, rtc_high = 0;
193 asm volatile("rdtsc" : "=d"(rtc_high), "=a"(rtc_low));
194 rtc = (static_cast<uint64_t>(rtc_high) << 32) | rtc_low;
195 }
196
197 #elif defined(BOTAN_TARGET_ARCH_IS_PPC64)
198
199 for(;;) {
200 uint32_t rtc_low = 0, rtc_high = 0, rtc_high2 = 0;
201 asm volatile("mftbu %0" : "=r"(rtc_high));
202 asm volatile("mftb %0" : "=r"(rtc_low));
203 asm volatile("mftbu %0" : "=r"(rtc_high2));
204
205 if(rtc_high == rtc_high2) {
206 rtc = (static_cast<uint64_t>(rtc_high) << 32) | rtc_low;
207 break;
208 }
209 }
210
211 #elif defined(BOTAN_TARGET_ARCH_IS_ALPHA)
212 asm volatile("rpcc %0" : "=r"(rtc));
213
214 // OpenBSD does not trap access to the %tick register
215 #elif defined(BOTAN_TARGET_ARCH_IS_SPARC64) && !defined(BOTAN_TARGET_OS_IS_OPENBSD)
216 asm volatile("rd %%tick, %0" : "=r"(rtc));
217
218 #elif defined(BOTAN_TARGET_ARCH_IS_IA64)
219 asm volatile("mov %0=ar.itc" : "=r"(rtc));
220
221 #elif defined(BOTAN_TARGET_ARCH_IS_S390X)
222 asm volatile("stck 0(%0)" : : "a"(&rtc) : "memory", "cc");
223
224 #elif defined(BOTAN_TARGET_ARCH_IS_HPPA)
225 asm volatile("mfctl 16,%0" : "=r"(rtc)); // 64-bit only?
226
227 #else
228 //#warning "OS::get_cpu_cycle_counter not implemented"
229 #endif
230
231#endif
232
233 return rtc;
234}
235
237#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
238
239 #if defined(_SC_NPROCESSORS_ONLN)
240 const long cpu_online = ::sysconf(_SC_NPROCESSORS_ONLN);
241 if(cpu_online > 0) {
242 return static_cast<size_t>(cpu_online);
243 }
244 #endif
245
246 #if defined(_SC_NPROCESSORS_CONF)
247 const long cpu_conf = ::sysconf(_SC_NPROCESSORS_CONF);
248 if(cpu_conf > 0) {
249 return static_cast<size_t>(cpu_conf);
250 }
251 #endif
252
253#endif
254
255#if defined(BOTAN_TARGET_OS_HAS_THREADS)
256 // hardware_concurrency is allowed to return 0 if the value is not
257 // well defined or not computable.
258 const size_t hw_concur = std::thread::hardware_concurrency();
259
260 if(hw_concur > 0) {
261 return hw_concur;
262 }
263#endif
264
265 return 1;
266}
267
269 if(uint64_t cpu_clock = OS::get_cpu_cycle_counter()) {
270 return cpu_clock;
271 }
272
273#if defined(BOTAN_TARGET_OS_IS_EMSCRIPTEN)
274 return emscripten_get_now();
275#endif
276
277 /*
278 If we got here either we either don't have an asm instruction
279 above, or (for x86) RDTSC is not available at runtime. Try some
280 clock_gettimes and return the first one that works, or otherwise
281 fall back to std::chrono.
282 */
283
284#if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME)
285
286 // The ordering here is somewhat arbitrary...
287 const clockid_t clock_types[] = {
288 #if defined(CLOCK_MONOTONIC_HR)
289 CLOCK_MONOTONIC_HR,
290 #endif
291 #if defined(CLOCK_MONOTONIC_RAW)
292 CLOCK_MONOTONIC_RAW,
293 #endif
294 #if defined(CLOCK_MONOTONIC)
295 CLOCK_MONOTONIC,
296 #endif
297 #if defined(CLOCK_PROCESS_CPUTIME_ID)
298 CLOCK_PROCESS_CPUTIME_ID,
299 #endif
300 #if defined(CLOCK_THREAD_CPUTIME_ID)
301 CLOCK_THREAD_CPUTIME_ID,
302 #endif
303 };
304
305 for(clockid_t clock : clock_types) {
306 struct timespec ts;
307 if(::clock_gettime(clock, &ts) == 0) {
308 return (static_cast<uint64_t>(ts.tv_sec) * 1000000000) + static_cast<uint64_t>(ts.tv_nsec);
309 }
310 }
311#endif
312
313 // Plain C++11 fallback
314 auto now = std::chrono::high_resolution_clock::now().time_since_epoch();
315 return std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
316}
317
319#if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME)
320 struct timespec ts;
321 if(::clock_gettime(CLOCK_REALTIME, &ts) == 0) {
322 return (static_cast<uint64_t>(ts.tv_sec) * 1000000000) + static_cast<uint64_t>(ts.tv_nsec);
323 }
324#endif
325
326 auto now = std::chrono::system_clock::now().time_since_epoch();
327 return std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
328}
329
330std::string OS::format_time(time_t time, const std::string& format) {
331 std::tm tm;
332
333#if defined(BOTAN_TARGET_OS_HAS_WIN32)
334 localtime_s(&tm, &time);
335#elif defined(BOTAN_TARGET_OS_HAS_POSIX1)
336 localtime_r(&time, &tm);
337#else
338 if(auto tmp = std::localtime(&time)) {
339 tm = *tmp;
340 } else {
341 throw Encoding_Error("Could not convert time_t to localtime");
342 }
343#endif
344
345 std::ostringstream oss;
346 oss << std::put_time(&tm, format.c_str());
347 return oss.str();
348}
349
351 const size_t default_page_size = 4096;
352
353#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
354 long p = ::sysconf(_SC_PAGESIZE);
355 if(p > 1) {
356 return static_cast<size_t>(p);
357 } else {
358 return default_page_size;
359 }
360#elif defined(BOTAN_TARGET_OS_HAS_VIRTUAL_LOCK)
361 BOTAN_UNUSED(default_page_size);
362 SYSTEM_INFO sys_info;
363 ::GetSystemInfo(&sys_info);
364 return sys_info.dwPageSize;
365#else
366 return default_page_size;
367#endif
368}
369
371 /*
372 * Linux defaults to only 64 KiB of mlockable memory per process (too small)
373 * but BSDs offer a small fraction of total RAM (more than we need). Bound the
374 * total mlock size to 512 KiB which is enough to run the entire test suite
375 * without spilling to non-mlock memory (and thus presumably also enough for
376 * many useful programs), but small enough that we should not cause problems
377 * even if many processes are mlocking on the same machine.
378 */
379 const size_t max_locked_kb = 512;
380
381 /*
382 * If RLIMIT_MEMLOCK is not defined, likely the OS does not support
383 * unprivileged mlock calls.
384 */
385#if defined(RLIMIT_MEMLOCK) && defined(BOTAN_TARGET_OS_HAS_POSIX1) && defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)
386 const size_t mlock_requested =
387 std::min<size_t>(read_env_variable_sz("BOTAN_MLOCK_POOL_SIZE", max_locked_kb), max_locked_kb);
388
389 if(mlock_requested > 0) {
390 struct ::rlimit limits;
391
392 ::getrlimit(RLIMIT_MEMLOCK, &limits);
393
394 if(limits.rlim_cur < limits.rlim_max) {
395 limits.rlim_cur = limits.rlim_max;
396 ::setrlimit(RLIMIT_MEMLOCK, &limits);
397 ::getrlimit(RLIMIT_MEMLOCK, &limits);
398 }
399
400 return std::min<size_t>(limits.rlim_cur, mlock_requested * 1024);
401 }
402
403#elif defined(BOTAN_TARGET_OS_HAS_VIRTUAL_LOCK)
404 const size_t mlock_requested =
405 std::min<size_t>(read_env_variable_sz("BOTAN_MLOCK_POOL_SIZE", max_locked_kb), max_locked_kb);
406
407 SIZE_T working_min = 0, working_max = 0;
408 if(!::GetProcessWorkingSetSize(::GetCurrentProcess(), &working_min, &working_max)) {
409 return 0;
410 }
411
412 // According to Microsoft MSDN:
413 // 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
414 // In the book "Windows Internals Part 2": the maximum lockable pages are minimum working set size - 8 pages
415 // But the information in the book seems to be inaccurate/outdated
416 // I've tested this on Windows 8.1 x64, Windows 10 x64 and Windows 7 x86
417 // On all three OS the value is 11 instead of 8
418 const size_t overhead = OS::system_page_size() * 11;
419 if(working_min > overhead) {
420 const size_t lockable_bytes = working_min - overhead;
421 return std::min<size_t>(lockable_bytes, mlock_requested * 1024);
422 }
423#else
424 // Not supported on this platform
425 BOTAN_UNUSED(max_locked_kb);
426#endif
427
428 return 0;
429}
430
431bool OS::read_env_variable(std::string& value_out, std::string_view name_view) {
432 value_out = "";
433
435 return false;
436 }
437
438#if defined(BOTAN_TARGET_OS_HAS_WIN32) && defined(BOTAN_BUILD_COMPILER_IS_MSVC)
439 const std::string name(name_view);
440 char val[128] = {0};
441 size_t req_size = 0;
442 if(getenv_s(&req_size, val, sizeof(val), name.c_str()) == 0) {
443 // Microsoft's implementation always writes a terminating \0,
444 // and includes it in the reported length of the environment variable
445 // if a value exists.
446 if(req_size > 0 && val[req_size - 1] == '\0') {
447 value_out = std::string(val);
448 } else {
449 value_out = std::string(val, req_size);
450 }
451 return true;
452 }
453#else
454 const std::string name(name_view);
455 if(const char* val = std::getenv(name.c_str())) {
456 value_out = val;
457 return true;
458 }
459#endif
460
461 return false;
462}
463
464size_t OS::read_env_variable_sz(std::string_view name, size_t def) {
465 std::string value;
466 if(read_env_variable(value, name) && !value.empty()) {
467 try {
468 const size_t val = std::stoul(value, nullptr);
469 return val;
470 } catch(std::exception&) { /* ignore it */
471 }
472 }
473
474 return def;
475}
476
477#if defined(BOTAN_TARGET_OS_HAS_POSIX1) && defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)
478
479namespace {
480
481int get_locked_fd() {
482 #if defined(BOTAN_TARGET_OS_IS_IOS) || defined(BOTAN_TARGET_OS_IS_MACOS)
483 // On Darwin, tagging anonymous pages allows vmmap to track these.
484 // Allowed from 240 to 255 for userland applications
485 static constexpr int default_locked_fd = 255;
486 int locked_fd = default_locked_fd;
487
488 if(size_t locked_fdl = OS::read_env_variable_sz("BOTAN_LOCKED_FD", default_locked_fd)) {
489 if(locked_fdl < 240 || locked_fdl > 255) {
490 locked_fdl = default_locked_fd;
491 }
492 locked_fd = static_cast<int>(locked_fdl);
493 }
494 return VM_MAKE_TAG(locked_fd);
495 #else
496 return -1;
497 #endif
498}
499
500} // namespace
501
502#endif
503
504std::vector<void*> OS::allocate_locked_pages(size_t count) {
505 std::vector<void*> result;
506
507#if(defined(BOTAN_TARGET_OS_HAS_POSIX1) && defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)) || \
508 defined(BOTAN_TARGET_OS_HAS_VIRTUAL_LOCK)
509
510 result.reserve(count);
511
512 const size_t page_size = OS::system_page_size();
513
514 #if defined(BOTAN_TARGET_OS_HAS_POSIX1) && defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)
515 static const int locked_fd = get_locked_fd();
516 #endif
517
518 for(size_t i = 0; i != count; ++i) {
519 void* ptr = nullptr;
520
521 #if defined(BOTAN_TARGET_OS_HAS_POSIX1) && defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)
522
523 int mmap_flags = MAP_PRIVATE;
524
525 #if defined(MAP_ANONYMOUS)
526 mmap_flags |= MAP_ANONYMOUS;
527 #elif defined(MAP_ANON)
528 mmap_flags |= MAP_ANON;
529 #endif
530
531 #if defined(MAP_CONCEAL)
532 mmap_flags |= MAP_CONCEAL;
533 #elif defined(MAP_NOCORE)
534 mmap_flags |= MAP_NOCORE;
535 #endif
536
537 int mmap_prot = PROT_READ | PROT_WRITE;
538
539 #if defined(PROT_MAX)
540 mmap_prot |= PROT_MAX(mmap_prot);
541 #endif
542
543 ptr = ::mmap(nullptr,
544 3 * page_size,
545 mmap_prot,
546 mmap_flags,
547 /*fd=*/locked_fd,
548 /*offset=*/0);
549
550 if(ptr == MAP_FAILED) {
551 continue;
552 }
553
554 // lock the data page
555 if(::mlock(static_cast<uint8_t*>(ptr) + page_size, page_size) != 0) {
556 ::munmap(ptr, 3 * page_size);
557 continue;
558 }
559
560 #if defined(MADV_DONTDUMP)
561 // we ignore errors here, as DONTDUMP is just a bonus
562 ::madvise(static_cast<uint8_t*>(ptr) + page_size, page_size, MADV_DONTDUMP);
563 #endif
564
565 #elif defined(BOTAN_TARGET_OS_HAS_VIRTUAL_LOCK)
566 ptr = ::VirtualAlloc(nullptr, 3 * page_size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
567
568 if(ptr == nullptr)
569 continue;
570
571 if(::VirtualLock(static_cast<uint8_t*>(ptr) + page_size, page_size) == 0) {
572 ::VirtualFree(ptr, 0, MEM_RELEASE);
573 continue;
574 }
575 #endif
576
577 std::memset(ptr, 0, 3 * page_size); // zero data page and both guard pages
578
579 // Attempts to name the data page
580 page_named(ptr, 3 * page_size);
581 // Make guard page preceeding the data page
582 page_prohibit_access(static_cast<uint8_t*>(ptr));
583 // Make guard page following the data page
584 page_prohibit_access(static_cast<uint8_t*>(ptr) + 2 * page_size);
585
586 result.push_back(static_cast<uint8_t*>(ptr) + page_size);
587 }
588#else
589 BOTAN_UNUSED(count);
590#endif
591
592 return result;
593}
594
595void OS::page_allow_access(void* page) {
596#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
597 const size_t page_size = OS::system_page_size();
598 ::mprotect(page, page_size, PROT_READ | PROT_WRITE);
599#elif defined(BOTAN_TARGET_OS_HAS_VIRTUAL_LOCK)
600 const size_t page_size = OS::system_page_size();
601 DWORD old_perms = 0;
602 ::VirtualProtect(page, page_size, PAGE_READWRITE, &old_perms);
603 BOTAN_UNUSED(old_perms);
604#else
605 BOTAN_UNUSED(page);
606#endif
607}
608
609void OS::page_prohibit_access(void* page) {
610#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
611 const size_t page_size = OS::system_page_size();
612 ::mprotect(page, page_size, PROT_NONE);
613#elif defined(BOTAN_TARGET_OS_HAS_VIRTUAL_LOCK)
614 const size_t page_size = OS::system_page_size();
615 DWORD old_perms = 0;
616 ::VirtualProtect(page, page_size, PAGE_NOACCESS, &old_perms);
617 BOTAN_UNUSED(old_perms);
618#else
619 BOTAN_UNUSED(page);
620#endif
621}
622
623void OS::free_locked_pages(const std::vector<void*>& pages) {
624 const size_t page_size = OS::system_page_size();
625
626 for(size_t i = 0; i != pages.size(); ++i) {
627 void* ptr = pages[i];
628
629 secure_scrub_memory(ptr, page_size);
630
631 // ptr points to the data page, guard pages are before and after
632 page_allow_access(static_cast<uint8_t*>(ptr) - page_size);
633 page_allow_access(static_cast<uint8_t*>(ptr) + page_size);
634
635#if defined(BOTAN_TARGET_OS_HAS_POSIX1) && defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)
636 ::munlock(ptr, page_size);
637 ::munmap(static_cast<uint8_t*>(ptr) - page_size, 3 * page_size);
638#elif defined(BOTAN_TARGET_OS_HAS_VIRTUAL_LOCK)
639 ::VirtualUnlock(ptr, page_size);
640 ::VirtualFree(static_cast<uint8_t*>(ptr) - page_size, 0, MEM_RELEASE);
641#endif
642 }
643}
644
645void OS::page_named(void* page, size_t size) {
646#if defined(BOTAN_TARGET_OS_HAS_PRCTL) && defined(PR_SET_VMA) && defined(PR_SET_VMA_ANON_NAME)
647 static constexpr char name[] = "Botan mlock pool";
648 int r = prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, reinterpret_cast<uintptr_t>(page), size, name);
649 BOTAN_UNUSED(r);
650#else
651 BOTAN_UNUSED(page, size);
652#endif
653}
654
655#if defined(BOTAN_TARGET_OS_HAS_THREADS)
656void OS::set_thread_name(std::thread& thread, const std::string& name) {
657 #if defined(BOTAN_TARGET_OS_IS_LINUX) || defined(BOTAN_TARGET_OS_IS_FREEBSD) || defined(BOTAN_TARGET_OS_IS_DRAGONFLY)
658 static_cast<void>(pthread_setname_np(thread.native_handle(), name.c_str()));
659 #elif defined(BOTAN_TARGET_OS_IS_OPENBSD)
660 static_cast<void>(pthread_set_name_np(thread.native_handle(), name.c_str()));
661 #elif defined(BOTAN_TARGET_OS_IS_NETBSD)
662 static_cast<void>(pthread_setname_np(thread.native_handle(), "%s", const_cast<char*>(name.c_str())));
663 #elif defined(BOTAN_TARGET_OS_HAS_WIN32) && defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
664 static_cast<void>(pthread_setname_np(thread.native_handle(), name.c_str()));
665 #elif defined(BOTAN_TARGET_OS_HAS_WIN32) && defined(BOTAN_BUILD_COMPILER_IS_MSVC)
666 typedef HRESULT(WINAPI * std_proc)(HANDLE, PCWSTR);
667 HMODULE kern = GetModuleHandleA("KernelBase.dll");
668 std_proc set_thread_name = reinterpret_cast<std_proc>(GetProcAddress(kern, "SetThreadDescription"));
669 if(set_thread_name) {
670 std::wstring w;
671 auto sz = MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, nullptr, 0);
672 if(sz > 0) {
673 w.resize(sz);
674 if(MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, &w[0], sz) > 0) {
675 (void)set_thread_name(thread.native_handle(), w.c_str());
676 }
677 }
678 }
679 #elif defined(BOTAN_TARGET_OS_IF_HAIKU)
680 auto thread_id = get_pthread_thread_id(thread.native_handle());
681 static_cast<void>(rename_thread(thread_id, name.c_str()));
682 #else
683 // TODO other possible oses ?
684 // macOs does not seem to allow to name threads other than the current one.
685 BOTAN_UNUSED(thread, name);
686 #endif
687}
688#endif
689
690#if defined(BOTAN_TARGET_OS_HAS_POSIX1) && !defined(BOTAN_TARGET_OS_IS_EMSCRIPTEN)
691
692namespace {
693
694// NOLINTNEXTLINE(*-avoid-non-const-global-variables)
695::sigjmp_buf g_sigill_jmp_buf;
696
697void botan_sigill_handler(int /*unused*/) {
698 siglongjmp(g_sigill_jmp_buf, /*non-zero return value*/ 1);
699}
700
701} // namespace
702
703#endif
704
705int OS::run_cpu_instruction_probe(const std::function<int()>& probe_fn) {
706 volatile int probe_result = -3;
707
708#if defined(BOTAN_TARGET_OS_HAS_POSIX1) && !defined(BOTAN_TARGET_OS_IS_EMSCRIPTEN)
709 struct sigaction old_sigaction;
710 struct sigaction sigaction;
711
712 sigaction.sa_handler = botan_sigill_handler;
713 sigemptyset(&sigaction.sa_mask);
714 sigaction.sa_flags = 0;
715
716 int rc = ::sigaction(SIGILL, &sigaction, &old_sigaction);
717
718 if(rc != 0) {
719 throw System_Error("run_cpu_instruction_probe sigaction failed", errno);
720 }
721
722 rc = sigsetjmp(g_sigill_jmp_buf, /*save sigs*/ 1);
723
724 if(rc == 0) {
725 // first call to sigsetjmp
726 probe_result = probe_fn();
727 } else if(rc == 1) {
728 // non-local return from siglongjmp in signal handler: return error
729 probe_result = -1;
730 }
731
732 // Restore old SIGILL handler, if any
733 rc = ::sigaction(SIGILL, &old_sigaction, nullptr);
734 if(rc != 0) {
735 throw System_Error("run_cpu_instruction_probe sigaction restore failed", errno);
736 }
737
738#else
739 BOTAN_UNUSED(probe_fn);
740#endif
741
742 return probe_result;
743}
744
745std::unique_ptr<OS::Echo_Suppression> OS::suppress_echo_on_terminal() {
746#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
747 class POSIX_Echo_Suppression : public Echo_Suppression {
748 public:
749 POSIX_Echo_Suppression() {
750 m_stdin_fd = fileno(stdin);
751 if(::tcgetattr(m_stdin_fd, &m_old_termios) != 0) {
752 throw System_Error("Getting terminal status failed", errno);
753 }
754
755 struct termios noecho_flags = m_old_termios;
756 noecho_flags.c_lflag &= ~ECHO;
757 noecho_flags.c_lflag |= ECHONL;
758
759 if(::tcsetattr(m_stdin_fd, TCSANOW, &noecho_flags) != 0) {
760 throw System_Error("Clearing terminal echo bit failed", errno);
761 }
762 }
763
764 void reenable_echo() override {
765 if(m_stdin_fd > 0) {
766 if(::tcsetattr(m_stdin_fd, TCSANOW, &m_old_termios) != 0) {
767 throw System_Error("Restoring terminal echo bit failed", errno);
768 }
769 m_stdin_fd = -1;
770 }
771 }
772
773 ~POSIX_Echo_Suppression() override {
774 try {
775 reenable_echo();
776 } catch(...) {}
777 }
778
779 POSIX_Echo_Suppression(const POSIX_Echo_Suppression& other) = delete;
780 POSIX_Echo_Suppression(POSIX_Echo_Suppression&& other) = delete;
781 POSIX_Echo_Suppression& operator=(const POSIX_Echo_Suppression& other) = delete;
782 POSIX_Echo_Suppression& operator=(POSIX_Echo_Suppression&& other) = delete;
783
784 private:
785 int m_stdin_fd;
786 struct termios m_old_termios;
787 };
788
789 return std::make_unique<POSIX_Echo_Suppression>();
790
791#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
792
793 class Win32_Echo_Suppression : public Echo_Suppression {
794 public:
795 Win32_Echo_Suppression() {
796 m_input_handle = ::GetStdHandle(STD_INPUT_HANDLE);
797 if(::GetConsoleMode(m_input_handle, &m_console_state) == 0)
798 throw System_Error("Getting console mode failed", ::GetLastError());
799
800 DWORD new_mode = ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
801 if(::SetConsoleMode(m_input_handle, new_mode) == 0)
802 throw System_Error("Setting console mode failed", ::GetLastError());
803 }
804
805 void reenable_echo() override {
806 if(m_input_handle != INVALID_HANDLE_VALUE) {
807 if(::SetConsoleMode(m_input_handle, m_console_state) == 0)
808 throw System_Error("Setting console mode failed", ::GetLastError());
809 m_input_handle = INVALID_HANDLE_VALUE;
810 }
811 }
812
813 ~Win32_Echo_Suppression() override {
814 try {
815 reenable_echo();
816 } catch(...) {}
817 }
818
819 Win32_Echo_Suppression(const Win32_Echo_Suppression& other) = delete;
820 Win32_Echo_Suppression(Win32_Echo_Suppression&& other) = delete;
821 Win32_Echo_Suppression& operator=(const Win32_Echo_Suppression& other) = delete;
822 Win32_Echo_Suppression& operator=(Win32_Echo_Suppression&& other) = delete;
823
824 private:
825 HANDLE m_input_handle;
826 DWORD m_console_state;
827 };
828
829 return std::make_unique<Win32_Echo_Suppression>();
830
831#else
832
833 // Not supported on this platform, return null
834 return nullptr;
835#endif
836}
837
838} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:118
std::string name
bool running_in_privileged_state()
Definition os_utils.cpp:169
size_t get_memory_locking_limit()
Definition os_utils.cpp:370
uint64_t BOTAN_TEST_API get_high_resolution_clock()
Definition os_utils.cpp:268
size_t BOTAN_TEST_API get_cpu_available()
Definition os_utils.cpp:236
std::unique_ptr< Echo_Suppression > BOTAN_UNSTABLE_API suppress_echo_on_terminal()
Definition os_utils.cpp:745
size_t read_env_variable_sz(std::string_view var_name, size_t def_value=0)
Definition os_utils.cpp:464
void page_allow_access(void *page)
Definition os_utils.cpp:595
std::string BOTAN_TEST_API format_time(time_t time, const std::string &format)
Definition os_utils.cpp:330
bool read_env_variable(std::string &value_out, std::string_view var_name)
Definition os_utils.cpp:431
void page_prohibit_access(void *page)
Definition os_utils.cpp:609
int BOTAN_TEST_API run_cpu_instruction_probe(const std::function< int()> &probe_fn)
Definition os_utils.cpp:705
std::vector< void * > allocate_locked_pages(size_t count)
Definition os_utils.cpp:504
size_t system_page_size()
Definition os_utils.cpp:350
uint64_t BOTAN_TEST_API get_system_timestamp_ns()
Definition os_utils.cpp:318
unsigned long get_auxval(unsigned long id)
Definition os_utils.cpp:130
void free_locked_pages(const std::vector< void * > &pages)
Definition os_utils.cpp:623
void page_named(void *page, size_t size)
Definition os_utils.cpp:645
uint32_t BOTAN_TEST_API get_process_id()
Definition os_utils.cpp:118
uint64_t BOTAN_TEST_API get_cpu_cycle_counter()
Definition os_utils.cpp:179
void secure_scrub_memory(void *ptr, size_t n)
Definition os_utils.cpp:89