musl/src/ipc/semget.c
Rich Felker 062f40ef3e work around wrong kernel type for sem_nsems member of struct semid_ds
rejecting invalid values for n is fine even in the case where a new
sem will not be created, since the kernel does its range checks on n
even in this case as well.

by default, the kernel will bound the limit well below USHRT_MAX
anyway, but it's presumably possible that an administrator could
override this limit and break things.
2013-06-28 23:57:58 -04:00

19 lines
520 B
C

#include <sys/sem.h>
#include <limits.h>
#include <errno.h>
#include "syscall.h"
#include "ipc.h"
int semget(key_t key, int n, int fl)
{
/* The kernel uses the wrong type for the sem_nsems member
* of struct semid_ds, and thus might not check that the
* n fits in the correct (per POSIX) userspace type, so
* we have to check here. */
if (n > USHRT_MAX) return __syscall_ret(-EINVAL);
#ifdef SYS_semget
return syscall(SYS_semget, key, n, fl);
#else
return syscall(SYS_ipc, IPCOP_semget, key, n, fl);
#endif
}