-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce Kernel Address Sanitizer #29
Comments
Thanks for the wonderful explanation! I went through the paper and I came up with the following roadmap:
Changing |
Good point. We need to update FYI, the stack area can be located by
|
Regarding |
I started off with trying to write a simple implementation for the heap. void shadow_chunk(struct malloc_chunk *chunk)
{
//Let's assume this function will take a chunk and mark appropriate places in shadow. For example -
paddr_t ptr_next = chunk->next;
for(size_t i = 0; i < 4; i++, ptr_next++)
{
shadow[ptr_next>>3] = SHADOW_UNADDRESSABLE;
}
} Will a chunk's address be a perfect multiple of 8 bytes? If that is not the case, I have to be very careful with updating the values of If it is so, every variable in the structure will have addresses multiples of 8 (obviously) and we can have different shadow values corresponding to different variables. #define SHADOW_NEXT_PTR -1
#define SHADOW_CAPACITY -2
#define SHADOW_SIZE -3
#define SHADOW_MAGIC -4
#define SHADOW_UNDERFLOW_REDZONE -5
#define SHADOW_DATA -6 |
Yes. It shall be aligned to 16 bytes since SSE instructions require the alignment. By the way, how about stop doing |
As we're having bits in every shadow array element, they're more than sufficient to store all the possible states of the corresponding 8 bytes, including our custom defined invalid-address states if required. I believe this shouldn't add much to the complexity/simplicity. Whatever you agree with, do tell me and I'll proceed that way |
I've reread the ASan wiki and finally I understood how we can have multiple states.
Please ignore this my comment. |
Thanks for the green signal :) |
I have a couple of points -
|
Perhaps we need to map the regions of the heap, stack etc. appropriately into shadow memory. I went through libs/resea/arch and from what I understand the starting position of heap region in the memory is dependent on the system architecture. Does it mean that we need different implementations for different architectures? |
Please ignore all the messages above except for the flags point (#1). I figured out that using |
No we don't have to. The starting address of the heap region, etc. are arch-dependent as you noticed, but they're defined in the linker script (example). Thus you can determine the addresses as an extern symbol: extern char __heap[];
extern char __heap_end[];
vaddr_t heap_start(void) {
return (vaddr_t) __heap;
} |
Yes, @nuta. I figured that out and wrote a simple implementation for the heap as well. It was fairly straightforward as everything was properly aligned to 8-bytes. I wrote these functions in malloc.c. These functions Also, it'd really help if you can throw some light on the stack part, or redirect me to the right files to go through and understand 😄 |
No. A good example is a string: a = "sample string";
b = malloc(8);
memcpy(&b[1], a, 1); I guess only
As the first step, let's ignore stack variable accesses unless it causes a problem for now (IIRC we can turn it off by compiler options). |
As of the current implementation, only the data region will be accessible. What is the memory region succeeded by? We do not have an overflow red-zone in Should we initialize |
I took help from this page and the OP-TEE's library to implement the hook functions. I have integrated it and everything seemed good until one of the assertions started to fail -
I have realized that the problem occurs when I try to access |
We have overflow redzones in malloc (
What does
That's caused by accessing an invalid memory access in the first task (i.e. |
A negative value means that it is unaddressable. -1 just means unaddressable. I've set
I was referring to my implementation. Do take a glance if possible. I have also updated malloc accordingly. |
Thanks for sharing. I'll have a look at later!
It is commented out because the length of |
Thanks for your time :). I'll try to still figure out myself why this paging error occurs. |
FWIW, here's a tip: if I were you, I'd debug unexpected page faults occurred in vm server in following steps:
Hope it help. |
Thanks for the tip! I did exactly that and this reveals the following -
It didn't make a lot of sense to me, at least not yet. The strange thing is that this error occurs even if I don't access I am investigating further. If something clicks after reading above, please do suggest. |
Might be not related to that bug, I noticed some pitfalls in your source code:
|
Thanks for pointing. I fixed that for the time being by changing
This didn't fix the problem though. After lots of experimentation, I feel this is due to something external as even keeping ONLY a
From what I could understand, the paper suggests marking the shadow memory region as "bad". I am not fully sure why they do that. Will the compiler also insert hook functions for shadow memory accesses? |
I checked the OP-TEE's implementation but it seems we don't have to care about that since they do nothing for that, like using a function attribute. Just ignore that comment. |
Kernel Address Sanitizer (KASAN) is a runtime memory error (e.g. use-after-free) checker. While it is "kernel" address sanitizer, we can use it in the userspace.
Briefly speaking, when KASAN is enabled, the compiler inserts code to call hook functions (
__asan_store8_noabort
) before each memory access (e.g.*ptr = 1;
). KASAN runtime (what we need to implement) is responsible for tracking how each memory bytes are valid.You don't need to implement as described in the paper. Just use it as memory access hooks.
Enabling KASAN
Add the following compiler options to
$CFLAGS
:Implementation Plan
Implement the KASAN runtime in
libs/common
.Furthermore, you need to update
shadow
inmalloc
,free
, and functions written in assembly likememcpy
.Good References
The text was updated successfully, but these errors were encountered: