• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2001
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * NAME
22  *	modify_ldt02.c
23  *
24  * DESCRIPTION
25  *	Testcase to check the error conditions for modify_ldt(2)
26  *
27  * ALGORITHM
28  *	block1:
29  *		Create a segment at entry 0 and a valid base address.
30  *		Read the contents of the segment thru' fs register.
31  *		Validate the data.
32  *		Write an invalid base address into entry 0.
33  *		Read the contents of entry 0 in the child process.
34  *		Verify that a SIGSEGV is incurred.
35  *
36  * USAGE
37  *	modify_ldt02
38  *
39  * HISTORY
40  *	07/2001 Ported by Wayne Boyer
41  *
42  * RESTRICTIONS
43  *	None
44  */
45 
46 #include "config.h"
47 #include "test.h"
48 
49 TCID_DEFINE(modify_ldt02);
50 int TST_TOTAL = 1;
51 
52 #if defined(__i386__) && defined(HAVE_MODIFY_LDT)
53 
54 #ifdef HAVE_ASM_LDT_H
55 #include <asm/ldt.h>
56 #endif
57 extern int modify_ldt(int, void *, unsigned long);
58 
59 #include <asm/unistd.h>
60 #include <string.h>
61 #include <sys/wait.h>
62 #include <errno.h>
63 
64 /* Newer ldt.h files use user_desc, instead of modify_ldt_ldt_s */
65 #ifdef HAVE_STRUCT_USER_DESC
66 typedef struct user_desc modify_ldt_s;
67 #elif  HAVE_STRUCT_MODIFY_LDT_LDT_S
68 typedef struct modify_ldt_ldt_s modify_ldt_s;
69 #else
70 typedef struct modify_ldt_ldt_t {
71 	unsigned int entry_number;
72 	unsigned long int base_addr;
73 	unsigned int limit;
74 	unsigned int seg_32bit:1;
75 	unsigned int contents:2;
76 	unsigned int read_exec_only:1;
77 	unsigned int limit_in_pages:1;
78 	unsigned int seg_not_present:1;
79 	unsigned int useable:1;
80 	unsigned int empty:25;
81 } modify_ldt_s;
82 #endif
83 
84 int create_segment(void *, size_t);
85 int read_segment(unsigned int);
86 void cleanup(void);
87 void setup(void);
88 
main(int ac,char ** av)89 int main(int ac, char **av)
90 {
91 	int lc;
92 
93 	int val, pid, status;
94 
95 	int seg[4];
96 
97 	tst_parse_opts(ac, av, NULL, NULL);
98 
99 	setup();
100 
101 	for (lc = 0; TEST_LOOPING(lc); lc++) {
102 		tst_count = 0;
103 
104 		seg[0] = 12345;
105 		if (create_segment(seg, sizeof(seg)) == -1) {
106 			tst_brkm(TBROK, cleanup, "Creation of segment failed");
107 		}
108 
109 		val = read_segment(0);
110 
111 		if (val != seg[0]) {
112 			tst_resm(TFAIL, "Invalid value read %d, expected %d",
113 				 val, seg[0]);
114 		} else
115 			tst_resm(TPASS, "value read as expected");
116 
117 		if (create_segment(0, 10) == -1) {
118 			tst_brkm(TBROK, cleanup, "Creation of segment failed");
119 		}
120 
121 		tst_old_flush();
122 		if ((pid = FORK_OR_VFORK()) == 0) {
123 			signal(SIGSEGV, SIG_DFL);
124 			val = read_segment(0);
125 			exit(1);
126 		}
127 
128 		(void)waitpid(pid, &status, 0);
129 
130 		if (WEXITSTATUS(status) != 0) {
131 			tst_resm(TFAIL, "Did not generate SEGV, child returned "
132 				 "unexpected status");
133 		} else {
134 			if (WIFSIGNALED(status) && (WTERMSIG(status) == SIGSEGV))
135 				tst_resm(TPASS, "generate SEGV as expected");
136 			else
137 				tst_resm(TFAIL, "Did not generate SEGV");
138 		}
139 	}
140 	cleanup();
141 	tst_exit();
142 
143 }
144 
create_segment(void * seg,size_t size)145 int create_segment(void *seg, size_t size)
146 {
147 	modify_ldt_s entry;
148 
149 	entry.entry_number = 0;
150 	entry.base_addr = (unsigned long)seg;
151 	entry.limit = size;
152 	entry.seg_32bit = 1;
153 	entry.contents = 0;
154 	entry.read_exec_only = 0;
155 	entry.limit_in_pages = 0;
156 	entry.seg_not_present = 0;
157 
158 	return modify_ldt(1, &entry, sizeof(entry));
159 }
160 
read_segment(unsigned int index)161 int read_segment(unsigned int index)
162 {
163 	int res;
164 	__asm__ __volatile__("\n\
165 			push    $0x0007;\n\
166 			pop     %%fs;\n\
167 			movl    %%fs:(%1), %0":"=r"(res)
168 			     :"r"(index * sizeof(int)));
169 	return res;
170 }
171 
setup(void)172 void setup(void)
173 {
174 	tst_sig(FORK, DEF_HANDLER, cleanup);
175 
176 	TEST_PAUSE;
177 }
178 
cleanup(void)179 void cleanup(void)
180 {
181 
182 }
183 #elif HAVE_MODIFY_LDT
main(void)184 int main(void)
185 {
186 	tst_brkm(TCONF,
187 		 NULL,
188 		 "modify_ldt is available but not tested on the platform than __i386__");
189 }
190 
191 #else /* if defined(__i386__) */
192 
main(void)193 int main(void)
194 {
195 	tst_resm(TINFO, "modify_ldt02 test only for ix86");
196 	tst_exit();
197 }
198 
199 #endif /* if defined(__i386__) */
200