1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
/*
* Copyright (c) 2014-2021 Dmitry V. Levin <ldv@strace.io>
* All rights reserved.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "tests.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
int
read_int_from_file(const char *const fname, int *const pvalue)
{
const int fd = open(fname, O_RDONLY);
if (fd < 0)
return -1;
long lval;
char buf[sizeof(lval) * 3];
int n = read(fd, buf, sizeof(buf) - 1);
int saved_errno = errno;
close(fd);
if (n < 0) {
errno = saved_errno;
return -1;
}
buf[n] = '\0';
char *endptr = 0;
errno = 0;
lval = strtol(buf, &endptr, 10);
if (!endptr || (*endptr && '\n' != *endptr)
#if INT_MAX < LONG_MAX
|| lval > INT_MAX || lval < INT_MIN
#endif
|| ERANGE == errno) {
if (!errno)
errno = EINVAL;
return -1;
}
*pvalue = (int) lval;
return 0;
}
static void
check_overflow_id(const int id, const char *overflowid)
{
int n;
if (read_int_from_file(overflowid, &n)) {
if (ENOENT == errno)
return;
perror_msg_and_fail("read_int_from_file: %s", overflowid);
}
if (id == n)
error_msg_and_skip("%d matches %s", id, overflowid);
}
void
check_overflowuid(const int uid)
{
check_overflow_id(uid, "/proc/sys/kernel/overflowuid");
}
void
check_overflowgid(const int gid)
{
check_overflow_id(gid, "/proc/sys/kernel/overflowgid");
}
|