prevent shmget from allocating objects that overflow ptrdiff_t

rather than returning an error, we have to increase the size argument
so high that the kernel will have no choice but to fail. this is
because POSIX only permits the EINVAL error for size errors when a new
shared memory segment would be created; if it already exists, the size
argument must be ignored. unfortunately Linux is non-conforming in
this regard, but I want to keep the code correct in userspace anyway
so that if/when Linux is fixed, the behavior applications see will be
conforming.
This commit is contained in:
Rich Felker 2013-06-29 00:02:38 -04:00
parent 062f40ef3e
commit 17aef0b41e
1 changed files with 2 additions and 0 deletions

View File

@ -1,9 +1,11 @@
#include <sys/shm.h> #include <sys/shm.h>
#include <stdint.h>
#include "syscall.h" #include "syscall.h"
#include "ipc.h" #include "ipc.h"
int shmget(key_t key, size_t size, int flag) int shmget(key_t key, size_t size, int flag)
{ {
if (size > PTRDIFF_MAX) size = SIZE_MAX;
#ifdef SYS_shmget #ifdef SYS_shmget
return syscall(SYS_shmget, key, size, flag); return syscall(SYS_shmget, key, size, flag);
#else #else