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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <cmocka.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/resource.h>
static int setup(void **state)
{
(void) state; /* unused */
setenv("PRIV_WRAPPER", "1", 1);
return 0;
}
static void test_RLIMIT_FSIZE(void **state)
{
int rc;
struct rlimit rlz, rli;
(void) state; /* unused */
rlz.rlim_cur = rlz.rlim_max = 0; /* zero */
rli.rlim_cur = rli.rlim_max = -1; /* RLIM64_INFINITY */
setenv("PRIV_WRAPPER_SETRLIMIT_DISABLE", "ALL", 1);
rc = setrlimit(RLIMIT_FSIZE, &rlz);
assert_return_code(rc, errno);
rc = setrlimit(RLIMIT_FSIZE, &rli);
assert_return_code(rc, errno);
unsetenv("PRIV_WRAPPER_SETRLIMIT_DISABLE");
setenv("PRIV_WRAPPER_SETRLIMIT_DISABLE", "RLIMIT_FSIZE", 1);
rc = setrlimit(RLIMIT_FSIZE, &rlz);
assert_return_code(rc, errno);
rc = setrlimit(RLIMIT_FSIZE, &rli);
assert_return_code(rc, errno);
unsetenv("PRIV_WRAPPER_SETRLIMIT_DISABLE");
}
/* Test with libc setrlimit() */
static void test_RLIMIT_FSIZE_fail(void **state)
{
int rc;
struct rlimit rli = {
.rlim_cur = 1000000,
.rlim_max = 1000000,
};
#ifdef __linux__
struct rlimit rlz = {
.rlim_cur = 1,
.rlim_max = 0, /* rlim_cur > rlim_max -> EINVAL */
};
#endif
(void) state; /* unused */
rc = setrlimit(RLIMIT_FSIZE, &rli);
assert_return_code(rc, errno);
#ifdef __linux__
/* This works on BSD and smells like a bug! */
rc = setrlimit(RLIMIT_FSIZE, &rlz);
assert_int_equal(rc, -1);
assert_int_equal(errno, EINVAL);
#endif
}
/*
* Test combination of these options :
* (first 5 are set in PRIV_WRAPPER_SETRLIMIT_DISABLE)
* RLIMIT_CPU
* RLIMIT_FSIZE
* RLIMIT_DATA
* RLIMIT_STACK
* RLIMIT_CORE
* RLIMIT_RSS
* RLIMIT_NOFILE
* RLIMIT_AS
* RLIMIT_NPROC
* RLIMIT_MEMLOCK
* RLIMIT_LOCKS
* RLIMIT_SIGPENDING
* RLIMIT_MSGQUEUE
* RLIMIT_NICE
* RLIMIT_RTPRIO
* RLIMIT_RTTIME
* RLIMIT_NLIMITS
*/
static void test_setrlimit_combination(void **state)
{
int rc;
struct rlimit rl_bad;
(void) state;
rl_bad.rlim_cur = 1000000;
rl_bad.rlim_max = 100000; /* rlim_cur > rlim_max ... EINVAL */
setenv("PRIV_WRAPPER_SETRLIMIT_DISABLE",
"RLIMIT_CPU|RLIMIT_FSIZE|RLIMIT_DATA|RLIMIT_STACK|RLIMIT_CORE",
1);
#define setrlimit_wrapped(o) \
rc = setrlimit(o, &rl_bad); \
assert_return_code(rc, errno);
#define setrlimit_unwrapped(o) \
rc = setrlimit(o, &rl_bad); \
assert_int_equal(rc, -1); \
assert_int_equal(errno, EINVAL);
setrlimit_wrapped(RLIMIT_CPU);
setrlimit_wrapped(RLIMIT_FSIZE);
setrlimit_wrapped(RLIMIT_DATA);
setrlimit_wrapped(RLIMIT_STACK);
setrlimit_wrapped(RLIMIT_CORE);
#ifdef __linux__
/* FreeBSD bugs (EINVAL not detected) */
setrlimit_unwrapped(RLIMIT_RSS);
setrlimit_unwrapped(RLIMIT_NOFILE);
setrlimit_unwrapped(RLIMIT_AS);
setrlimit_unwrapped(RLIMIT_NPROC);
setrlimit_unwrapped(RLIMIT_MEMLOCK);
#endif
#ifdef RLIMIT_LOCKS
setrlimit_unwrapped(RLIMIT_LOCKS);
#endif
#ifdef RLIMIT_SIGPENDING
setrlimit_unwrapped(RLIMIT_SIGPENDING);
#endif
#ifdef RLIMIT_MSGQUEUE
setrlimit_unwrapped(RLIMIT_MSGQUEUE);
#endif
#ifdef RLIMIT_NICE
setrlimit_unwrapped(RLIMIT_NICE);
#endif
#ifdef RLIMIT_RTPRIO
setrlimit_unwrapped(RLIMIT_RTPRIO);
#endif
#ifdef RLIMIT_RTTIME
setrlimit_unwrapped(RLIMIT_RTTIME);
#endif
#ifdef RLIMIT_NLIMITS
setrlimit_unwrapped(RLIMIT_NLIMITS);
#endif
unsetenv("PRIV_WRAPPER_SETRLIMIT_DISABLE");
}
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_RLIMIT_FSIZE),
cmocka_unit_test(test_RLIMIT_FSIZE_fail),
cmocka_unit_test(test_setrlimit_combination),
};
return cmocka_run_group_tests(tests, setup, NULL);
}
|