Memory guard on dynamic allocations causes seg faults
Not sure if this belongs here or if there is a separate section for the Linux Subsystem for Windows, but here is the problem:
-
Brief description Dynamic allocations in C by default don't get memory guards on Linux. So if I do something that doesn't make sense, such as array[-1], I get garbage results (assuming the memory address is valid), but not a seg fault. The Linux Subsystem for Windows is causing seg faults in this scenario. If I load a copy of Ubuntu into a VM and run the same program I do not get this behavior.
-
Expected results Accessing negative indices in dynamically allocated arrays will not cause a seg fault.
-
Actual results (with terminal output if applicable) Accessing negative indices in dynamically allocated arrays will causes a seg fault.
-
Your Windows build number 14393
I can't repro this here. Behaves as expected on WSL; no segfault. This is on 15031 with gcc 5.4.0 with no compiler flags.
/* gcc foo.c -o foo && ./foo */
#include <stdio.h>
static char foo[1024];
int main(int argc, const char* argv[])
{
foo[-1] = 'a';
printf("foo[-1] = '%c'\n", foo[-1]);
return 0;
}
You're right, I was misinterpreting the code. This is a dynamic allocation, not a static one. I'm not sure if this is due to memory guards, or if the virtual memory just isn't allocated for the specified location.
@JoshCollinsMSFT -- I'm not able to reproduce this with a dynamic allocation either. On both Linux and WSL, the following code:
- Prints an integer at the first out-of-bounds read
- Segfaults at the second out-of-bounds read
- If the second out-of-bounds read is commented out so that it reaches the third, it triggers libc's out-of-bounds-write guard and aborts with a bunch of debug logging.
#include <stdio.h>
int main() {
int *array;
/* Allocate a small array from the heap */
array = (int)malloc(sizeof(int) * 3);
printf("%d\n", array[-1]);
free(array);
/* Allocate a large array; likely to get its own memory mapping */
array = (int)malloc(sizeof(int) * 100000000);
printf("%d\n", array[-1]);
free(array);
/* Overwrite some memory */
array = (int)malloc(sizeof(int) * 3);
array[-1] = 0x12345678;
free(array);
}
I'm not surprised to find that the behavior here is the same because WSL doesn't actually provide memory allocation. It provides abstracted page-table-manipulation facilities in the form of mmap(), sbrk(), etc. But actual memory allocation is, by default, managed by the "libc.so" shared library, which is a Linux library and which is the same binary on WSL and Ubuntu Linux systems.
That said, there are ways in which WSL-kernel behavior could cause visible differences in out-of-bound allocations. So -- do you have a reproducer that you could post here that repeatably illustrates different behavior?
Here is some repro code with makefile.
- test.c
#include <stdio.h>
#include <stdlib.h>
struct foo
{
char data[192];
};
int main() {
foo *array;
array = (foo*)malloc(253402 * sizeof(foo));
printf("%d\n", (int)array[-1].data[0]);
free(array);
}
- Makefile
CC = g++
CFLAGS = -g -Wno-write-strings -fpermissive -fno-stack-protector
RM = rm -f
default: all
all: test
test: test.c
$(CC) $(CFLAGS) -o test test.c
clean veryclean:
$(RM) test
@JoshCollinsMSFT -- that reproducer segfaults on both Ubuntu 16.04 and WSL for me. Do you have an example that behaves differently?
Ah, right, you're using build 14393, which is based on Ubuntu 14.04. (I'm testing on build 15031, which is based on Ubuntu 16.04.) I don't have an Ubuntu 14.04 machine handy to test on. Maybe this is a difference in newer versions of Ubuntu and/or WSL?
It segfaults on native Xenial too (happened to have one handy). Equivalent but simplified below. With foo - 1 you are just peeking at the malloc header. With foo - 192 (which is what the above case is doing), you are off into la-la land.
/* g++ foo.cc -o foo && ./foo */
#include <stdio.h>
#include <stdlib.h>
int main() {
const size_t sz = 61*4096*24*8; // 253402*192
char* foo = (char*)malloc(sz) - 192;
printf("%c\n", *foo);
}
So I'm verifying with some colleagues on which distro/version this doesn't repro. I'll get back to you soon.
Once you've tracked it, the next thing to do is an strace on native and WSL to see how the values returned from sbrk or mmap differ. That will show why the pointer is valid in one versus the other. That is where I was going with your test case before @aseering posted that 16.04 was the same.
Ok, verified the issue doesn't repro on Ubuntu 12.04 LTS. Not sure if that far down level compatibility is a priority for you. If not then feel free to close. If so I can do more investigation.
Hm... I'm not on the WSL team myself; that type of compatibility prioritization sounds like a question for someone who is. ( @benhillis ? Or anyone else on the team? )
For what it's worth, my personal opinion is that, in cases like this where there's apparently a behavior change between Ubuntu 12.04 and 14.04, WSL is pretending to be Ubuntu 14.04, so that's the behavior that it should emulate.
Also for what it's worth, I would be vaguely curious what would happen if you were to compile your test binary on Ubuntu 12.04 and statically link it against libc, and copy the resulting static binary to WSL.. It should run just fine, but I wonder whether the issue will repro or not. I'm basically wondering if this Linux behavior change was in the kernel (which WSL controls very directly) or GNU libc (which WSL is using unmodified from Linux). If the latter, then the obvious workaround would be to have affected code use the older libc.
I don't work there either, but yeah; MSFT can't really achieve "down level compatibility" with 12.04 even if they wanted to. Nor can Canonical. It's either a change in glibc, which MSFT doesn't control, or a change with the Real Linux kernel, which they also don't control, but must match on a forward basis.
Again, strace on Real Ubuntu 12.04 vs. 14.04 will elucidate the difference right away. Unfortunately I don't have a 12.04 handy. We're totally out of WSL territory here now, but I am also curious (in an equally vague way). I would have guessed (apparently incorrectly) that a test case which tried to read 192 bytes ahead of only one malloc() would fail on 12.04 too. But how malloc() allocates its space is entirely a black box. It isn't an "array guard" thing. Doing an array[-192] dereference is just pointer arithmetic, and you're allowed to peek at anything you feel like in your own address space.
@therealkenc -- WSL doesn't control glibc but in principle it could bundle old versions of it. And the team does very much control WSL's reimplementation of the kernel. Therefore, for example, WSL's ELF loader could scan each exec()'ed binary, look for glibc's compatibility symbols, then consult an internal lookup table to determine what compatibility behavior the kernel should expose to that binary and which build of glibc to map into the filesystem.
That said -- please don't to that! I expect Windows to sometimes jump through those sorts of hoops to maintain backwards compatibility, but I very much don't expect that of Linux.
The answer, if any, here would be to support installing 12.04 on bash for windows, then you'd get bug-for-bug compatability with 12.04's malloc.
You can't start attaching different glibcs to different processes in Linux, it tends to lead to horrible, horrible results (for a start, every library you load must be linked against the same libc). This is why when linux distros update glibc to a new major version, they have to do it in a "big bang", where everything is updated at once.
A suggestion here: If glibc is the culprit here, then submit some trouble ticket to the glibc team and/or do some research to see why this behavior exists in 14.04+ and not on 12.04. They may have a valid reason for changing the behavior.
Nah this one was just Linux-behavior; it was a malformed allocation. If it worked on any glibc it would be a fluke and in any case not recommended. Somehow this just got on a strange glibc backward compatibility tangent.
I mean, this is Microsoft's GitHub site. The company that allegedly fixed SimCity's use-after-free bug for them by adding a compatibility hack to their allocator.
Not saying WSL should do that :-) Just saying there's context.