Replies: 2 comments
-
Explanations on the implementationWe already have ASAN enabled inside Gramine-SGX, and it can be easily adapted to Gramine-TDX. Here are the relevant ASAN files:
We don't want to modify any Gramine constants for ASAN. This means we are bound to use the Clearly, our 1:1 virtual-to-physical mapping does not work well in this case. So we need to have a separate set of page tables that will cover (a reasonable part) of this shadow memory region. Recall that ASAN's real-to-shadow memory mapping is like this:
If we assume that normal Gramine VMs will use up to 32GB of memory, then we only need a part of the shadow memory region that covers the top-most address, which is
So we have:
This gives us the size of the shadow memory region that we actually need (to cover first 32GB of app's + Gramine's really used memory):
This is 4GB. Which is no surprise because to cover 32GB, ASAN requires 1/8 of it, which is 4GB. How much memory space we need for page tables to cover 4GB of this actually-used shadow memory region? Since each 4KB-sized page in PT has 512 PTEs, and each PTE describes a 4KB of memory, this means that PTs ratio is 512:1. Thus:
This is 8MB. So in the end, to cover 32GB app memory with 4GB ASAN shadow memory, we need 8MB of page tables that must be correctly filled up (present, RW permissions, covering Gramine VM logic previously used 136MB of page tables, used to cover up to 64GB of memory. Technically, we need only 129MB to cover 63GB, but we added a bit more slack with additional 7MB here. However, 136MB of page tables was not enough if we want to have a 64GB addr space and also shadow addr space. So we need to increase the size of page tables to smth bigger. This should be trivial because adjacent to page tables, we have virtio queues shared memory. Virtio queues region was 248MB, which is way too much for our current usages of them (they maybe occupy up to 64MB, no more than that). So we can easily resize these two regions, increasing page tables by another e.g. 8MB and decreasing the virtio queues region. Since we already have a limit of 64GB on possible address space, it makes sense to allow the same limit on ASAN shadow memory. So ASAN shadow memory will need 8GB, and we'll require 16MB of page tables . So in general, we want to have these characteristics:
The former requires around 129MB, and the latter requires around 17MB. So a total of 146MB for page tables should be enough. |
Beta Was this translation helpful? Give feedback.
-
Here's a diff of how to test ASan manually: diff --git a/pal/src/host/vm-common/kernel_memory.c b/pal/src/host/vm-common/kernel_memory.c
index ebfb72dd..88209332 100644
--- a/pal/src/host/vm-common/kernel_memory.c
+++ b/pal/src/host/vm-common/kernel_memory.c
@@ -26,6 +26,33 @@
#include "kernel_multicore.h"
#include "kernel_virtio.h"
+/* ------------------- */
+__attribute__((no_sanitize("undefined")))
+static int run_test_asan_heap(void) {
+ char* buf = malloc(30);
+ char* volatile c = buf + 30;
+ *c = 1;
+ free(buf);
+ return 0;
+}
+
+__attribute__((no_sanitize("undefined")))
+static int run_test_asan_stack(void) {
+ char buf[30];
+ char* volatile c = buf + 30;
+ *c = 1;
+ return 0;
+}
+
+__attribute__((no_sanitize("undefined")))
+static int run_test_asan_global(void) {
+ static char buf[30];
+ char* volatile c = buf + 30;
+ *c = 1;
+ return 0;
+}
+/* ------------------- */
+
static_assert(PAGE_SIZE == 4096, "unexpected PAGE_SIZE (expected 4K)");
/* Beginning of the page table hierarchy */
@@ -550,6 +577,19 @@ int memory_tighten_permissions(void) {
return -PAL_ERROR_DENIED;
}
+
+ debug_serial_io_write("--- before testing Address Sanitizer");
+#if 1
+ (void)run_test_asan_heap();
+#endif
+#if 0
+ (void)run_test_asan_stack();
+#endif
+#if 0
+ (void)run_test_asan_global();
+#endif
+ debug_serial_io_write("--- after testing Address Sanitizer");
+
return 0;
} |
Beta Was this translation helpful? Give feedback.
-
Build:
NOTE: I needed at least Clang 12. Clang 10 compiled but incorrectly built the BIOS, which crashed QEMU.
Beta Was this translation helpful? Give feedback.
All reactions