clinuxvisual-studio-codeaslr

ASLR behavior differs between the VSCode Run button and the command line


The book said that ASLR is activated by default on Linux, but when I try to confirm it, I find a strange problem: when I click the run button in VSCode, it always outputs a fixed stack address and ASLR doesn't work, but when I run the executable file generated by VSCode in bash, ASLR works

The code to check the address of the stack:

#include <stdio.h>

int main() {
    long local;
    printf("local at %p\n", &local);
    return 0;
}

The output of clicking the "Run c/cpp file" button:

local at 0x7fffffffd250

The output of running the executable file in bash(several examples of random addresses):

local at 0x7ffed0965fb0

local at 0x7fff3eff7630

local at 0x7ffc0e083a00

I've checked that ASLR is open on my machine, I want to know what's the difference between clicking the button and running it with bash?


Solution

  • Debuggers temporarily disable ASLR to simplify the debugging process.

    This can be done via the personality function. Specifically, passing ADDR_NO_RANDOMIZE to this function does this for the current process.

    https://man7.org/linux/man-pages/man2/personality.2.html