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) International Business Machines Corp., 2002
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * 06/2002 Written by Paul Larson
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * Test for ENOMEM, EPERM errors.
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * 1) mlock(2) fails with ENOMEM if some of the specified address range
14*49cdfc7eSAndroid Build Coastguard Worker * does not correspond to mapped pages in the address space of
15*49cdfc7eSAndroid Build Coastguard Worker * the process.
16*49cdfc7eSAndroid Build Coastguard Worker *
17*49cdfc7eSAndroid Build Coastguard Worker * 2) mlock(2) fails with ENOMEM if the caller had a non-zero RLIMIT_MEMLOCK
18*49cdfc7eSAndroid Build Coastguard Worker * soft resource limit, but tried to lock more memory than the limit
19*49cdfc7eSAndroid Build Coastguard Worker * permitted. This limit is not enforced if the process is
20*49cdfc7eSAndroid Build Coastguard Worker * privileged (CAP_IPC_LOCK).
21*49cdfc7eSAndroid Build Coastguard Worker *
22*49cdfc7eSAndroid Build Coastguard Worker * 3) mlock(2) fails with EPERM if the caller was not privileged (CAP_IPC_LOCK)
23*49cdfc7eSAndroid Build Coastguard Worker * and its RLIMIT_MEMLOCK soft resource limit was 0.
24*49cdfc7eSAndroid Build Coastguard Worker */
25*49cdfc7eSAndroid Build Coastguard Worker
26*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
31*49cdfc7eSAndroid Build Coastguard Worker
32*49cdfc7eSAndroid Build Coastguard Worker static size_t len;
33*49cdfc7eSAndroid Build Coastguard Worker static struct rlimit original;
34*49cdfc7eSAndroid Build Coastguard Worker static struct passwd *ltpuser;
35*49cdfc7eSAndroid Build Coastguard Worker
test_enomem1(void)36*49cdfc7eSAndroid Build Coastguard Worker static void test_enomem1(void)
37*49cdfc7eSAndroid Build Coastguard Worker {
38*49cdfc7eSAndroid Build Coastguard Worker void *addr;
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker addr = SAFE_MMAP(NULL, len, PROT_READ,
41*49cdfc7eSAndroid Build Coastguard Worker MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
42*49cdfc7eSAndroid Build Coastguard Worker SAFE_MUNMAP(addr, len);
43*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL(mlock(addr, len), ENOMEM, "mlock(%p, %lu)", addr, len);
44*49cdfc7eSAndroid Build Coastguard Worker }
45*49cdfc7eSAndroid Build Coastguard Worker
test_enomem2(void)46*49cdfc7eSAndroid Build Coastguard Worker static void test_enomem2(void)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker void *addr;
49*49cdfc7eSAndroid Build Coastguard Worker struct rlimit rl;
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker rl.rlim_max = len - 1;
52*49cdfc7eSAndroid Build Coastguard Worker rl.rlim_cur = len - 1;
53*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETRLIMIT(RLIMIT_MEMLOCK, &rl);
54*49cdfc7eSAndroid Build Coastguard Worker addr = SAFE_MMAP(NULL, len, PROT_READ,
55*49cdfc7eSAndroid Build Coastguard Worker MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
56*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETEUID(ltpuser->pw_uid);
57*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL(mlock(addr, len), ENOMEM, "mlock(%p, %lu)", addr, len);
58*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETEUID(0);
59*49cdfc7eSAndroid Build Coastguard Worker SAFE_MUNMAP(addr, len);
60*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETRLIMIT(RLIMIT_MEMLOCK, &original);
61*49cdfc7eSAndroid Build Coastguard Worker }
62*49cdfc7eSAndroid Build Coastguard Worker
test_eperm(void)63*49cdfc7eSAndroid Build Coastguard Worker static void test_eperm(void)
64*49cdfc7eSAndroid Build Coastguard Worker {
65*49cdfc7eSAndroid Build Coastguard Worker void *addr;
66*49cdfc7eSAndroid Build Coastguard Worker struct rlimit rl;
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker rl.rlim_max = 0;
69*49cdfc7eSAndroid Build Coastguard Worker rl.rlim_cur = 0;
70*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETRLIMIT(RLIMIT_MEMLOCK, &rl);
71*49cdfc7eSAndroid Build Coastguard Worker addr = SAFE_MMAP(NULL, len, PROT_READ,
72*49cdfc7eSAndroid Build Coastguard Worker MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
73*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETEUID(ltpuser->pw_uid);
74*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL(mlock(addr, len), EPERM, "mlock(%p, %lu)", addr, len);
75*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETEUID(0);
76*49cdfc7eSAndroid Build Coastguard Worker SAFE_MUNMAP(addr, len);
77*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETRLIMIT(RLIMIT_MEMLOCK, &original);
78*49cdfc7eSAndroid Build Coastguard Worker }
79*49cdfc7eSAndroid Build Coastguard Worker
run(void)80*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
81*49cdfc7eSAndroid Build Coastguard Worker {
82*49cdfc7eSAndroid Build Coastguard Worker test_enomem1();
83*49cdfc7eSAndroid Build Coastguard Worker test_enomem2();
84*49cdfc7eSAndroid Build Coastguard Worker test_eperm();
85*49cdfc7eSAndroid Build Coastguard Worker }
86*49cdfc7eSAndroid Build Coastguard Worker
setup(void)87*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
88*49cdfc7eSAndroid Build Coastguard Worker {
89*49cdfc7eSAndroid Build Coastguard Worker ltpuser = SAFE_GETPWNAM("nobody");
90*49cdfc7eSAndroid Build Coastguard Worker len = getpagesize();
91*49cdfc7eSAndroid Build Coastguard Worker SAFE_GETRLIMIT(RLIMIT_MEMLOCK, &original);
92*49cdfc7eSAndroid Build Coastguard Worker }
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
95*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
96*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
97*49cdfc7eSAndroid Build Coastguard Worker .test_all = run,
98*49cdfc7eSAndroid Build Coastguard Worker };
99