[ Security ] Fixed-memory layout and related security issues
Hello.
Currently, AOT-compiled assembly images are preferably mapped to bases, which are fixed for each assembly. This seems to be the case for all platforms, except the x64:
src/pal/src/map/map.cpp:2897
#if !defined(_AMD64_)
loadedBase = mmap((void*)preferredBase, virtualSize, PROT_NONE, MAP_ANON|MAP_PRIVATE, -1, 0);
#else // defined(_AMD64_)
So, it is possible that, on some platforms, memory placement of AOT-compiled assemblies will be the same in several executions of a particular application.
This is similar to behaviour of kernel's memory subsystem, in case address space layout randomization (ASLR) is disabled. And, the ASLR is usually used to protect from buffer-overflow attacks.
What do you think about this behaviour from view point of security? Should it be considered a security issue in CoreCLR, or the related kinds of security attacks are supposed to be resisted by other means?
@jkotas @janvorli @cmckinsey Please take a look. cc: @parjong @hqueue @hseok-oh
We should make CoreCLR work well with ASLR as much as possible and not ever map code at fixed address.
https://github.com/dotnet/coreclr/issues/8480 is related to this.
@jkotas, thank you for your answer.
We should make CoreCLR work well with ASLR as much as possible and not ever map code at fixed address.
Currently, the
loadedBase = mmap((void*)preferredBase, virtualSize, PROT_NONE, MAP_ANON|MAP_PRIVATE, -1, 0);
doesn't map code to fixed address (MAP_FIXED is not set). However, it suggests the preferredBase address to kernel, so the address (which is fixed for particular assemblies) will most probably be used, if the mapping wouldn't overlap other mappings.
This leads to situation, which doesn't prevent ASLR. However, it creates address space, which has exactly same placement of some executable code parts in several executions of an application.
Do I understand correctly that the described behaviour is undesirable, and should be changed?
Yes, I think we should let the OS kernel choose random address for us. It will have some negative performance effect (pages with absolute pointers will need to be written to get the pointers updated).
Actually I was exploiting this vulnerability when debugging readytorun for ARM, e.g. setting breakpoints at memory address where native image will be loaded.
Yes, I think we should let the OS kernel choose random address for us. It will have some negative performance effect (pages with absolute pointers will need to be written to get the pointers updated).
@jkotas Is there any relocation mechanism available when loading native image in CoreCLR ? Because I observed that .text section of native image is loaded into random address for x64 as describse above, I wonder how relocation is done for x64 when loading native image.
The image has reloc section that has list of all places in the image that need to be updated when it does not get loaded at its preferred address.
@janvorli Can you PTAL?
@kouvel @jkotas Will fix for https://github.com/dotnet/coreclr/issues/8480 address this as well?
The fix for dotnet/runtime#7094 does not address this. dotnet/runtime#7094 refers to making CoreCLR work under security features that prevent mapping to a particular address, while this bug I believe refers to not trying to map native images to a particular address.
while this bug I believe refers to not trying to map native images to a particular address.
Does this not amount to the same thing (i.e. map to random address in both cases)?
Does this not amount to the same thing (i.e. map to random address in both cases)?
In my opinion, no, but it depends on how you want to look at it. The issues raise problems that may be considered related in areas concerned (security, native images, etc.), but they are distinct problems the root causes of which are not related.
I agree with @kouvel. The point of this issue is that we should never try to map native images at their preferred base addresses.
Fixed by dotnet/coreclr#11467