runtime icon indicating copy to clipboard operation
runtime copied to clipboard

[ Security ] Fixed-memory layout and related security issues

Open ruben-ayrapetyan opened this issue 9 years ago • 13 comments

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?

ruben-ayrapetyan avatar Mar 22 '17 10:03 ruben-ayrapetyan

@jkotas @janvorli @cmckinsey Please take a look. cc: @parjong @hqueue @hseok-oh

lemmaa avatar Mar 22 '17 14:03 lemmaa

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 avatar Mar 22 '17 15:03 jkotas

@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?

ruben-ayrapetyan avatar Mar 22 '17 18:03 ruben-ayrapetyan

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 avatar Mar 22 '17 23:03 jkotas

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.

hqueue avatar Mar 23 '17 06:03 hqueue

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.

jkotas avatar Mar 23 '17 07:03 jkotas

@janvorli Can you PTAL?

gkhanna79 avatar Apr 27 '17 19:04 gkhanna79

@kouvel @jkotas Will fix for https://github.com/dotnet/coreclr/issues/8480 address this as well?

gkhanna79 avatar May 05 '17 18:05 gkhanna79

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.

kouvel avatar May 05 '17 19:05 kouvel

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)?

gkhanna79 avatar May 05 '17 19:05 gkhanna79

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.

kouvel avatar May 05 '17 19:05 kouvel

I agree with @kouvel. The point of this issue is that we should never try to map native images at their preferred base addresses.

janvorli avatar May 09 '17 12:05 janvorli

Fixed by dotnet/coreclr#11467

janvorli avatar May 09 '17 18:05 janvorli