How does MMAP_PAGE_ZERO personality flag work?

mrn

According to linux man pages, MMAP_PAGE_ZERO flag used in personality system call makes the system emulates SVr4 behavior, meaning that zero page is mapped as read only. However, this a little naive trial (visible in the code below) does not prevent segmentation fault ocurrence.

#include <stdio.h>
#include <sys/personality.h>

int main(void)
{
  int *ptr = 0;
  (void)personality(MMAP_PAGE_ZERO);
  printf("%d\n", *ptr);
  return 0;
}

What should be the right usage of personality(MMAP_PAGE_ZERO) to achieve zero page mapped as read only for a process?

Joseph Sible-Reinstate Monica

There are two reasons that wasn't working for you:

  1. The kernel only checks whether MMAP_PAGE_ZERO is set in load_elf_binary, so setting it after a process has started will have no effect.
  2. That setting doesn't override the vm.mmap_min_addr sysctl, which isn't 0 on most systems.

To fix the first problem, try this program instead, which re-executes itself after setting that:

#include <stdio.h>
#include <sys/auxv.h>
#include <sys/personality.h>
#include <unistd.h>

int main(int argc, char **argv) {
    int persona = personality(0xffffffff);
    if(persona == -1) {
        perror("personality");
        return 1;
    }
    if(persona & MMAP_PAGE_ZERO) {
        int *ptr = 0;
        printf("%d\n", *ptr);
        return 0;
    } else {
        if(personality(persona | MMAP_PAGE_ZERO) == -1) {
            perror("personality");
            return 1;
        }
        execv((const char *)getauxval(AT_EXECFN), argv);
        perror("execv");
        return 1;
    }
}

To fix the second problem, you have three choices:

  1. Run the executable as root, with sudo or something
  2. Give the executable CAP_SYS_RAWIO
  3. Change the vm.mmap_min_addr sysctl to 0

Note that all of those choices will have security consequences.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How might work flag, that do not specified in Developer Android Docs?

How does browser page lifecycle sequence work?

How does mmap work?

How does cookie "Secure" flag work?

bash, bc modulo does not work with -l flag

Removing Activity from BackStack (Flag_Activity_no_history) does not work

FLAG_ACTIVITY_CLEAR_TOP does not work as expected

How to determine when zero flag, sign flag, overflow flag and carry flag are set?

NPM prefix flag does not work as expected on windows

How does .enforceQoS flag increase priority?

How do I get the --watch flag to work on OSX command line?

When does decompression process take place and how does solidbreak flag really work in Inno Setup?

GNU "install" -d flag -- how's it work?

FLAG_KEEP_SCREEN_ON does not work during keygaurd lock

-X flag (X11 Forwarding) does not appear to work in Windows

how does the Internet work

How does the web page - codebehind connection work?

How to set default personality for a kernel?

Tar --absolute-names flag does not work

What does the STICKY_TIMEOUTS flag for personality(2) do?

How exactly does a Chrome event page work?

How does mmap work with x32 ABI?

How is the "priority" flag in Jekyll plugins supposed to work?

how does the virtual memory and page cache work?

PasswordAuthentication=no flag does not work on one strange host

Docker flag "--gpu" does not work without sudo command

Rstudio gfortran flag -m64 doesn't work, but it does work on the than command line

On an AVR MCU, how does the S flag in the status register work?

Why does the Zero Flag exist?