xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mlock2/mlock202.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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) 2018 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  * Description:
8*49cdfc7eSAndroid Build Coastguard Worker  * Check various errnos for mlock2(2) since kernel v2.6.9:
9*49cdfc7eSAndroid Build Coastguard Worker  * 1) mlock2() fails and returns EINVAL if unknown flag is specified.
10*49cdfc7eSAndroid Build Coastguard Worker  * 2) mlock2() fails and returns ENOMEM if the caller is not
11*49cdfc7eSAndroid Build Coastguard Worker  *    privileged(CAP_IPC_LOCK) and tries to lock more memory than the
12*49cdfc7eSAndroid Build Coastguard Worker  *    RLIMIT_MEMLOCK limit.
13*49cdfc7eSAndroid Build Coastguard Worker  * 3) mlock2() fails and returns EPERM if the caller is not
14*49cdfc7eSAndroid Build Coastguard Worker  *    privileged(CAP_IPC_LOCK) and its RLIMIT_MEMLOCK limit is 0.
15*49cdfc7eSAndroid Build Coastguard Worker  * 4) mlock2() fails and returns ENOMEM if some of the specified address
16*49cdfc7eSAndroid Build Coastguard Worker  *    range does not correspond to mapped pages in the address space
17*49cdfc7eSAndroid Build Coastguard Worker  *    of the caller.
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <sys/time.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <sys/resource.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
27*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker #define PAGES 8
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker static size_t pgsz;
32*49cdfc7eSAndroid Build Coastguard Worker static size_t max_sz1, max_sz2;
33*49cdfc7eSAndroid Build Coastguard Worker static char *addr, *unmapped_addr;
34*49cdfc7eSAndroid Build Coastguard Worker static struct passwd *nobody;
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
37*49cdfc7eSAndroid Build Coastguard Worker 	char **taddr;
38*49cdfc7eSAndroid Build Coastguard Worker 	int flag;
39*49cdfc7eSAndroid Build Coastguard Worker 	size_t *max_size;
40*49cdfc7eSAndroid Build Coastguard Worker 	/* 1: nobody 0: root */
41*49cdfc7eSAndroid Build Coastguard Worker 	int user;
42*49cdfc7eSAndroid Build Coastguard Worker 	int exp_err;
43*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
44*49cdfc7eSAndroid Build Coastguard Worker 	{&addr, -1, NULL, 0, EINVAL},
45*49cdfc7eSAndroid Build Coastguard Worker 	{&addr, 0, &max_sz1, 1, ENOMEM},
46*49cdfc7eSAndroid Build Coastguard Worker 	{&addr, 0, &max_sz2, 1, EPERM},
47*49cdfc7eSAndroid Build Coastguard Worker 	{&unmapped_addr, 0, NULL, 0, ENOMEM},
48*49cdfc7eSAndroid Build Coastguard Worker };
49*49cdfc7eSAndroid Build Coastguard Worker 
verify_mlock2(unsigned int n)50*49cdfc7eSAndroid Build Coastguard Worker static void verify_mlock2(unsigned int n)
51*49cdfc7eSAndroid Build Coastguard Worker {
52*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
53*49cdfc7eSAndroid Build Coastguard Worker 	struct rlimit orig_limit, new_limit;
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->user) {
56*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_GETRLIMIT(RLIMIT_MEMLOCK, &orig_limit);
57*49cdfc7eSAndroid Build Coastguard Worker 		new_limit.rlim_cur = *tc->max_size;
58*49cdfc7eSAndroid Build Coastguard Worker 		new_limit.rlim_max = *tc->max_size;
59*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SETRLIMIT(RLIMIT_MEMLOCK, &new_limit);
60*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SETEUID(nobody->pw_uid);
61*49cdfc7eSAndroid Build Coastguard Worker 	}
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	TEST(tst_syscall(__NR_mlock2, *tc->taddr, pgsz, tc->flag));
64*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != -1) {
65*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "mlock2() succeeded");
66*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_MUNLOCK(*tc->taddr, pgsz);
67*49cdfc7eSAndroid Build Coastguard Worker 		goto end;
68*49cdfc7eSAndroid Build Coastguard Worker 	}
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR != tc->exp_err) {
71*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO,
72*49cdfc7eSAndroid Build Coastguard Worker 			"mlock2() failed unexpectedly, expected %s",
73*49cdfc7eSAndroid Build Coastguard Worker 			tst_strerrno(tc->exp_err));
74*49cdfc7eSAndroid Build Coastguard Worker 	} else {
75*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TTERRNO, "mlock2() failed as expected");
76*49cdfc7eSAndroid Build Coastguard Worker 	}
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker end:
79*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->user) {
80*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SETEUID(0);
81*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SETRLIMIT(RLIMIT_MEMLOCK, &orig_limit);
82*49cdfc7eSAndroid Build Coastguard Worker 	}
83*49cdfc7eSAndroid Build Coastguard Worker }
84*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)85*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
86*49cdfc7eSAndroid Build Coastguard Worker {
87*49cdfc7eSAndroid Build Coastguard Worker 	pgsz = getpagesize();
88*49cdfc7eSAndroid Build Coastguard Worker 	nobody = SAFE_GETPWNAM("nobody");
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker 	addr = SAFE_MMAP(NULL, pgsz, PROT_WRITE,
91*49cdfc7eSAndroid Build Coastguard Worker 			 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
92*49cdfc7eSAndroid Build Coastguard Worker 	unmapped_addr = SAFE_MMAP(NULL, pgsz * PAGES, PROT_READ,
93*49cdfc7eSAndroid Build Coastguard Worker 				  MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
94*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MUNMAP(unmapped_addr, pgsz * PAGES);
95*49cdfc7eSAndroid Build Coastguard Worker 	unmapped_addr = unmapped_addr + pgsz * PAGES / 2;
96*49cdfc7eSAndroid Build Coastguard Worker 
97*49cdfc7eSAndroid Build Coastguard Worker 	max_sz1 = pgsz - 1;
98*49cdfc7eSAndroid Build Coastguard Worker }
99*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)100*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
101*49cdfc7eSAndroid Build Coastguard Worker {
102*49cdfc7eSAndroid Build Coastguard Worker 	if (addr)
103*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_MUNMAP(addr, pgsz);
104*49cdfc7eSAndroid Build Coastguard Worker }
105*49cdfc7eSAndroid Build Coastguard Worker 
106*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
107*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
108*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_mlock2,
109*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
110*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
111*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
112*49cdfc7eSAndroid Build Coastguard Worker };
113