1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2017 FUJITSU LIMITED. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker * Author: Xiao Yang <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker * Test for EFAULT when rlim points outside the accessible address space.
9*49cdfc7eSAndroid Build Coastguard Worker */
10*49cdfc7eSAndroid Build Coastguard Worker
11*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
12*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
13*49cdfc7eSAndroid Build Coastguard Worker #include <sys/resource.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <sys/time.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker static void *bad_addr;
21*49cdfc7eSAndroid Build Coastguard Worker
verify_setrlimit(void)22*49cdfc7eSAndroid Build Coastguard Worker static void verify_setrlimit(void)
23*49cdfc7eSAndroid Build Coastguard Worker {
24*49cdfc7eSAndroid Build Coastguard Worker int status;
25*49cdfc7eSAndroid Build Coastguard Worker pid_t pid;
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker pid = SAFE_FORK();
28*49cdfc7eSAndroid Build Coastguard Worker if (!pid) {
29*49cdfc7eSAndroid Build Coastguard Worker TEST(setrlimit(RLIMIT_NOFILE, bad_addr));
30*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != -1) {
31*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "setrlimit() succeeded unexpectedly");
32*49cdfc7eSAndroid Build Coastguard Worker exit(0);
33*49cdfc7eSAndroid Build Coastguard Worker }
34*49cdfc7eSAndroid Build Coastguard Worker
35*49cdfc7eSAndroid Build Coastguard Worker /* Usually, setrlimit() should return EFAULT */
36*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR == EFAULT) {
37*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO,
38*49cdfc7eSAndroid Build Coastguard Worker "setrlimit() failed as expected");
39*49cdfc7eSAndroid Build Coastguard Worker } else {
40*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO,
41*49cdfc7eSAndroid Build Coastguard Worker "setrlimit() should fail with EFAULT, got");
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker exit(0);
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker
47*49cdfc7eSAndroid Build Coastguard Worker SAFE_WAITPID(pid, &status, 0);
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker /* If glibc has to convert between 32bit and 64bit struct rlimit
50*49cdfc7eSAndroid Build Coastguard Worker * in some cases, it is possible to get SegFault.
51*49cdfc7eSAndroid Build Coastguard Worker */
52*49cdfc7eSAndroid Build Coastguard Worker if (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) {
53*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "setrlimit() caused SIGSEGV");
54*49cdfc7eSAndroid Build Coastguard Worker return;
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
58*49cdfc7eSAndroid Build Coastguard Worker return;
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "child %s", tst_strstatus(status));
61*49cdfc7eSAndroid Build Coastguard Worker }
62*49cdfc7eSAndroid Build Coastguard Worker
setup(void)63*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
64*49cdfc7eSAndroid Build Coastguard Worker {
65*49cdfc7eSAndroid Build Coastguard Worker bad_addr = tst_get_bad_addr(NULL);
66*49cdfc7eSAndroid Build Coastguard Worker }
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
69*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_setrlimit,
70*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
71*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
72*49cdfc7eSAndroid Build Coastguard Worker };
73