简体   繁体   中英

IPC SysV Shared memory - shmget(), shmat() does not work

cat /proc/sys/kernel/shmmax 
18446744073692774399

Trying to create shmem memory and later overwrite it unsing HUGE Dirty COW vulnerability.

Somehow allocation of MAXMYMEM shows an error "Cannot allocate memory", errorcode 12.

Why it does not work?

allocate.c

#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <string.h>

#define MAXMYMEM 0x200000 

int main(int argc, char **argv)
{
int shID;
char *myPtr;
int i;

    shID = shmget(2409, MAXMYMEM, IPC_CREAT | SHM_HUGETLB | 0666 );
    if (shID >= 0) {
        myPtr = shmat(shID, 0, 0);
        if (myPtr==(char *)-1) {
            perror("shmat");
        } else {
            for (i=0; i<MAXMYMEM; i++) {
                myPtr[i] = 'A'+i;
            }
            getchar();
            shmdt(myPtr);
        }
    } else {
        perror("shmget");
    }
}

Update 1:

grep -i huge /proc/meminfo
AnonHugePages:   1714176 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB

Update 2:

$cat /proc/sys/vm/hugetlb_shm_group
0
$uname -a
Linux 4.4.0-101-generic #124-Ubuntu SMP Fri Nov 10 18:29:59 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

The reason was that on Ubuntu 16.04 the number of Huge Pages was set to 0.

$sysctl vm.nr_hugepages
vm.nr_hugepages = 0

by changing it to a different value, the program worked!

$sysctl -w vm.nr_hugepages=512

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM