CCF
Loading...
Searching...
No Matches
mem.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#include <stdlib.h>
6
7#if !defined(INSIDE_ENCLAVE) || defined(VIRTUAL_ENCLAVE)
8# include <cstring>
9# include <limits>
10# include <sys/resource.h>
11#else
13
14# include <openenclave/advanced/mallinfo.h>
15# include <openenclave/bits/security.h>
16#endif
17
18namespace ccf::pal
19{
30
31#if !defined(INSIDE_ENCLAVE) || defined(VIRTUAL_ENCLAVE)
32
33 static inline void* safe_memcpy(void* dest, const void* src, size_t count)
34 {
35 return ::memcpy(dest, src, count);
36 }
37
38 static inline bool get_mallinfo(MallocInfo& info)
39 {
40 {
41 rusage ru;
42 auto rc = getrusage(RUSAGE_SELF, &ru);
43 if (rc != 0)
44 {
45 return false;
46 }
47 const auto heap_size = ru.ru_maxrss * 1024;
48
49 info.current_allocated_heap_size = heap_size;
50 info.peak_allocated_heap_size = heap_size;
51 }
52
53 {
54 rlimit rl;
55 auto rc = getrlimit(RLIMIT_AS, &rl);
56 if (rc != 0)
57 {
58 return false;
59 }
60
61 info.max_total_heap_size = rl.rlim_cur;
62 }
63
64 return true;
65 }
66
67 static bool require_alignment_for_untrusted_reads()
68 {
69# ifdef FORCE_ENABLE_XAPIC_MITIGATION
70 return true;
71# else
72 return false;
73# endif
74 }
75
76#else
77
78 static inline void* safe_memcpy(void* dest, const void* src, size_t count)
79 {
80 return oe_memcpy_with_barrier(dest, src, count);
81 }
82
83 static bool get_mallinfo(MallocInfo& info)
84 {
85 oe_mallinfo_t oe_info;
86 auto rc = oe_allocator_mallinfo(&oe_info);
87 if (rc != OE_OK)
88 {
89 return false;
90 }
91 info.max_total_heap_size = oe_info.max_total_heap_size;
92 info.current_allocated_heap_size = oe_info.current_allocated_heap_size;
93 info.peak_allocated_heap_size = oe_info.peak_allocated_heap_size;
94 return true;
95 }
96
97 static bool is_vulnerable_to_stale_xapic_read()
98 {
99 CpuidInfo info;
100
101 cpuid(&info, 1, 0);
102
103 // Ignores stepping, looks only at model and family: potentially
104 // includes safe instances which differ only by stepping from a vulnerable
105 // instance.
106 constexpr uint64_t proc_id_mask = 0x000F'0FF0;
107 const uint64_t proc_id = info.eax & proc_id_mask;
108
109 // https://www.intel.com/content/www/us/en/developer/topic-technology/software-security-guidance/processors-affected-consolidated-product-cpu-model.html
110 // 2022 tab, column "Stale Data Read from Legacy xAPIC, CVE-2022-21233,
111 // INTEL-SA-00657"
112 const std::set<uint64_t> vulnerable_proc_ids{
113 0x506C0, // Apollo Lake
114 0x506F0, // Denverton (Goldmont)
115 0x606A0, // Ice Lake Xeon-SP
116 0x606C0, // Ice Lake D
117 0x706A0, // Gemini Lake
118 0x706E0, // Ice Lake U, Y
119 0x80660, // Snow Ridge BTS (Tremont)
120 0x806A0, // Lakefield B-step (Tremont)
121 0x806C0, // Tiger Lake U
122 0x806D0, // Tiger Lake H
123 0x90660, // Elkhart Lake (Tremont)
124 0x90670, // Alder Lake S (Golden Cove, Gracemont)
125 0x906A0, // Alder Lake H (Golden Cove, Gracemont)
126 0x906C0, // Jasper Lake (Tremont)
127 0xA0670 // Rocket Lake
128 };
129
130 const auto it = vulnerable_proc_ids.find(proc_id);
131 return it != vulnerable_proc_ids.end();
132 }
133
134 static bool require_alignment_for_untrusted_reads()
135 {
136# ifdef FORCE_ENABLE_XAPIC_MITIGATION
137 return true;
138# else
139 static std::optional<bool> required = std::nullopt;
140 if (!required.has_value())
141 {
142 required = is_intel_cpu() && is_vulnerable_to_stale_xapic_read();
143 }
144 return required.value();
145# endif
146 }
147
148#endif
149}
Definition attestation.h:28
Definition mem.h:25
size_t current_allocated_heap_size
Definition mem.h:27
size_t max_total_heap_size
Definition mem.h:26
size_t peak_allocated_heap_size
Definition mem.h:28
constexpr oe_result_t OE_OK
Definition virtual_enclave.h:41