blob: da615746e5bbb1080b9587beade814d78959c38f [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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
subrata_modakae5eb2c2008-11-18 09:00:57 +000046#include "config.h"
plars865695b2001-08-27 22:15:12 +000047#include "test.h"
plars865695b2001-08-27 22:15:12 +000048
subrata_modakae5eb2c2008-11-18 09:00:57 +000049TCID_DEFINE(modify_ldt02);
plars865695b2001-08-27 22:15:12 +000050int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000051
subrata_modakae5eb2c2008-11-18 09:00:57 +000052#if defined(__i386__) && defined(HAVE_MODIFY_LDT)
53
54#ifdef HAVE_ASM_LDT_H
55#include <asm/ldt.h>
subrata_modak4bb656a2009-02-26 12:02:09 +000056#endif
subrata_modak56207ce2009-03-23 13:35:39 +000057extern int modify_ldt(int, void *, unsigned long);
subrata_modakae5eb2c2008-11-18 09:00:57 +000058
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
66typedef struct user_desc modify_ldt_s;
67#elif HAVE_STRUCT_MODIFY_LDT_LDT_S
subrata_modak4bb656a2009-02-26 12:02:09 +000068typedef struct modify_ldt_ldt_s modify_ldt_s;
subrata_modakae5eb2c2008-11-18 09:00:57 +000069#else
subrata_modak56207ce2009-03-23 13:35:39 +000070typedef 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;
subrata_modakae5eb2c2008-11-18 09:00:57 +000081} modify_ldt_s;
82#endif
83
84int create_segment(void *, size_t);
85int read_segment(unsigned int);
86void cleanup(void);
87void setup(void);
88
plars865695b2001-08-27 22:15:12 +000089#define FAILED 1
90
subrata_modak56207ce2009-03-23 13:35:39 +000091int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000092{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020093 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020094 const char *msg;
plars865695b2001-08-27 22:15:12 +000095
96 int val, pid, status;
97
subrata_modakae5eb2c2008-11-18 09:00:57 +000098 int flag;
99 int seg[4];
100
Garrett Cooper45e285d2010-11-22 12:19:25 -0800101 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800102 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800103 }
plars865695b2001-08-27 22:15:12 +0000104
subrata_modak56207ce2009-03-23 13:35:39 +0000105 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +0000106
107 /* The following loop checks looping state if -i option given */
108 for (lc = 0; TEST_LOOPING(lc); lc++) {
109
Caspar Zhangd59a6592013-03-07 14:59:12 +0800110 /* reset tst_count in case we are looping */
111 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000112
robbiew860b47b2003-03-26 23:13:58 +0000113//block1:
plars865695b2001-08-27 22:15:12 +0000114 tst_resm(TINFO, "Enter block 1");
115 flag = 0;
116
117 seg[0] = 12345;
118 if (create_segment(seg, sizeof(seg)) == -1) {
119 tst_brkm(TINFO, cleanup, "Creation of segment failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800120 }
plars865695b2001-08-27 22:15:12 +0000121
122 val = read_segment(0);
123
124 if (val != seg[0]) {
125 tst_resm(TFAIL, "Invalid value read %d, expected %d",
126 val, seg[0]);
127 flag = FAILED;
128 }
129
130 if (flag) {
131 tst_resm(TINFO, "block 1 FAILED");
132 } else {
133 tst_resm(TINFO, "block 1 PASSED");
134 }
135
136 tst_resm(TINFO, "Exit block 1");
137
robbiew860b47b2003-03-26 23:13:58 +0000138//block2:
plars865695b2001-08-27 22:15:12 +0000139 tst_resm(TINFO, "Enter block 2");
140 flag = 0;
141
142 if (create_segment(0, 10) == -1) {
143 tst_brkm(TINFO, cleanup, "Creation of segment failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800144 }
plars865695b2001-08-27 22:15:12 +0000145
vapier6c4e16b2006-05-26 05:37:27 +0000146 tst_flush();
robbiewd34d5812005-07-11 22:28:09 +0000147 if ((pid = FORK_OR_VFORK()) == 0) {
plars865695b2001-08-27 22:15:12 +0000148 val = read_segment(0);
149 exit(1);
150 }
151
152 (void)waitpid(pid, &status, 0);
153
154 if (WEXITSTATUS(status) != 0) {
155 flag = FAILED;
156 tst_resm(TFAIL, "Did not generate SEGV, child returned "
157 "unexpected status");
158 }
159
160 if (flag) {
161 tst_resm(TINFO, "block 2 FAILED");
162 } else {
163 tst_resm(TINFO, "block 2 PASSED");
164 }
165 }
subrata_modak56207ce2009-03-23 13:35:39 +0000166 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800167 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800168
plars865695b2001-08-27 22:15:12 +0000169}
170
subrata_modak56207ce2009-03-23 13:35:39 +0000171int create_segment(void *seg, size_t size)
plars865695b2001-08-27 22:15:12 +0000172{
mridgef7973a12004-07-07 19:17:31 +0000173 modify_ldt_s entry;
plars865695b2001-08-27 22:15:12 +0000174
175 entry.entry_number = 0;
176 entry.base_addr = (unsigned long)seg;
177 entry.limit = size;
178 entry.seg_32bit = 1;
179 entry.contents = 0;
180 entry.read_exec_only = 0;
181 entry.limit_in_pages = 0;
182 entry.seg_not_present = 0;
183
184 return modify_ldt(1, &entry, sizeof(entry));
185}
186
187int read_segment(unsigned int index)
188{
189 int res;
plars74948ad2002-11-14 16:16:14 +0000190 __asm__ __volatile__("\n\
191 push $0x0007;\n\
192 pop %%fs;\n\
subrata_modak56207ce2009-03-23 13:35:39 +0000193 movl %%fs:(%1), %0":"=r"(res)
194 :"r"(index * sizeof(int)));
plars865695b2001-08-27 22:15:12 +0000195 return res;
196}
197
subrata_modak56207ce2009-03-23 13:35:39 +0000198void sigsegv_handler(int sig)
plars865695b2001-08-27 22:15:12 +0000199{
200 tst_resm(TINFO, "received signal: %d", sig);
201 exit(0);
202}
subrata_modakae5eb2c2008-11-18 09:00:57 +0000203
plars865695b2001-08-27 22:15:12 +0000204/*
205 * setup() - performs all ONE TIME setup for this test
206 */
subrata_modak56207ce2009-03-23 13:35:39 +0000207void setup(void)
plars865695b2001-08-27 22:15:12 +0000208{
209 struct sigaction act;
210
vapier6c4e16b2006-05-26 05:37:27 +0000211 memset(&act, 0, sizeof(act));
212 sigemptyset(&act.sa_mask);
213
plars865695b2001-08-27 22:15:12 +0000214 tst_sig(FORK, DEF_HANDLER, cleanup);
215
216 act.sa_handler = sigsegv_handler;
217 (void)sigaction(SIGSEGV, &act, NULL);
218
plars865695b2001-08-27 22:15:12 +0000219 TEST_PAUSE;
220}
221
222/*
223 * cleanup() - performs all the ONE TIME cleanup for this test at completion
224 * or premature exit.
225 */
subrata_modak56207ce2009-03-23 13:35:39 +0000226void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000227{
plars865695b2001-08-27 22:15:12 +0000228
plars865695b2001-08-27 22:15:12 +0000229}
subrata_modakae5eb2c2008-11-18 09:00:57 +0000230#elif HAVE_MODIFY_LDT
Mike Frysingerc57fba52014-04-09 18:56:30 -0400231int main(void)
subrata_modakae5eb2c2008-11-18 09:00:57 +0000232{
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100233 tst_brkm(TCONF,
234 NULL,
subrata_modak56207ce2009-03-23 13:35:39 +0000235 "modify_ldt is available but not tested on the platform than __i386__");
subrata_modakae5eb2c2008-11-18 09:00:57 +0000236}
237
238#else /* if defined(__i386__) */
239
Mike Frysingerc57fba52014-04-09 18:56:30 -0400240int main(void)
subrata_modakae5eb2c2008-11-18 09:00:57 +0000241{
242 tst_resm(TINFO, "modify_ldt02 test only for ix86");
Garrett Cooper2c282152010-12-16 00:55:50 -0800243 tst_exit();
subrata_modakae5eb2c2008-11-18 09:00:57 +0000244}
245
Chris Dearmanec6edca2012-10-17 19:54:01 -0700246#endif /* if defined(__i386__) */