• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2004
4  * Copyright (c) Linux Test Project, 2004-2017
5  */
6 
7 /*
8  * DESCRIPTION
9  *	hugeshmctl03 - check for EACCES, and EPERM errors
10  *
11  * ALGORITHM
12  *	create a large shared memory segment with root only read & write
13  *	permissions fork a child process
14  *	if child
15  *	  set the ID of the child process to that of "ltpuser1"
16  *	  call do_child()
17  *	  loop if that option was specified
18  *	    call shmctl() using three different invalid cases
19  *	    check the errno value
20  *	      issue a PASS message if we get EACCES or EPERM
21  *	    otherwise, the tests fails
22  *	      issue a FAIL message
23  *	  call cleanup
24  *	if parent
25  *	  wait for child to exit
26  *	  remove the large shared memory segment
27  *
28  * HISTORY
29  *	03/2001 - Written by Wayne Boyer
30  *	04/2004 - Updated by Robbie Williamson
31  *
32  * RESTRICTIONS
33  *	test must be run as root
34  */
35 
36 #include <sys/types.h>
37 #include <sys/wait.h>
38 #include <limits.h>
39 #include "hugetlb.h"
40 
41 static size_t shm_size;
42 static int shm_id_1 = -1;
43 static struct shmid_ds buf;
44 static uid_t ltp_uid;
45 static char *ltp_user = "nobody";
46 
47 static struct tst_option options[] = {
48 	{"s:", &nr_opt, "-s num   Set the number of the been allocated hugepages"},
49 	{NULL, NULL, NULL}
50 };
51 
52 struct tcase {
53 	int *shmid;
54 	int cmd;
55 	struct shmid_ds *sbuf;
56 	int error;
57 } tcases[] = {
58 	/* EACCES - child has no read permission for segment */
59 	{&shm_id_1, IPC_STAT, &buf, EACCES},
60 	/* EPERM - IPC_SET - child doesn't have permission to change segment */
61 	{&shm_id_1, IPC_SET, &buf, EPERM},
62 	/* EPERM - IPC_RMID - child can not remove the segment */
63 	{&shm_id_1, IPC_RMID, &buf, EPERM},
64 };
65 
66 static void do_child(void);
67 
test_hugeshmctl(void)68 static void test_hugeshmctl(void)
69 {
70 	pid_t pid;
71 	int status;
72 
73 	switch (pid = SAFE_FORK()) {
74 	case 0:
75 		/* set the user ID of the child to the non root user */
76 		SAFE_SETUID(ltp_uid);
77 		do_child();
78 		exit(0);
79 	default:
80 		SAFE_WAITPID(pid, &status, 0);
81 	}
82 }
83 
do_child(void)84 static void do_child(void)
85 {
86 	unsigned int i;
87 
88 	for (i = 0; i < ARRAY_SIZE(tcases); i++) {
89 		TEST(shmctl(*(tcases[i].shmid), tcases[i].cmd, tcases[i].sbuf));
90 		if (TST_RET != -1) {
91 			tst_res(TFAIL, "shmctl succeeded "
92 					"unexpectedly");
93 			continue;
94 		}
95 		if (TST_ERR == tcases[i].error)
96 			tst_res(TPASS | TTERRNO, "shmctl failed "
97 					"as expected");
98 		else
99 			tst_res(TFAIL | TTERRNO, "shmctl failed "
100 					"unexpectedly - expect errno = "
101 					"%d, got", tcases[i].error);
102 	}
103 }
104 
setup(void)105 void setup(void)
106 {
107 	long hpage_size;
108 
109 	if (tst_hugepages == 0)
110 		tst_brk(TCONF, "No enough hugepages for testing.");
111 
112 	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
113 
114 	shm_size = hpage_size * tst_hugepages / 2;
115 	update_shm_size(&shm_size);
116 	shmkey = getipckey();
117 	shm_id_1 = shmget(shmkey, shm_size,
118 			  SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
119 	if (shm_id_1 == -1)
120 		tst_brk(TBROK | TERRNO, "shmget");
121 
122 	/* get the userid for a non root user */
123 	ltp_uid = getuserid(ltp_user);
124 }
125 
cleanup(void)126 void cleanup(void)
127 {
128 	rm_shm(shm_id_1);
129 }
130 
131 static struct tst_test test = {
132 	.needs_root = 1,
133 	.forks_child = 1,
134 	.needs_tmpdir = 1,
135 	.options = options,
136 	.setup = setup,
137 	.cleanup = cleanup,
138 	.test_all = test_hugeshmctl,
139 	.request_hugepages = 128,
140 };
141