about summary refs log tree commit diff
path: root/sysdeps/unix/sysv/linux/i386/tst-bz21269.c
blob: 8b82ccf43b1fae1bd5b2e7046d22931f49904110 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* Test for i386 sigaction sa_restorer handling (BZ#21269)
   Copyright (C) 2017-2019 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

/* This is based on Linux test tools/testing/selftests/x86/ldt_gdt.c,
   more specifically in do_multicpu_tests function.  The main changes
   are:

   - C11 atomics instead of plain access.
   - Remove x86_64 support which simplifies the syscall handling
     and fallbacks.
   - Replicate only the test required to trigger the issue for the
     BZ#21269.  */

#include <stdatomic.h>

#include <asm/ldt.h>
#include <linux/futex.h>

#include <setjmp.h>
#include <signal.h>
#include <errno.h>
#include <sys/syscall.h>
#include <sys/mman.h>

#include <support/xunistd.h>
#include <support/check.h>
#include <support/xthread.h>

static int
xset_thread_area (struct user_desc *u_info)
{
  long ret = syscall (SYS_set_thread_area, u_info);
  TEST_VERIFY_EXIT (ret == 0);
  return ret;
}

static void
xmodify_ldt (int func, const void *ptr, unsigned long bytecount)
{
  TEST_VERIFY_EXIT (syscall (SYS_modify_ldt, 1, ptr, bytecount) == 0);
}

static int
futex (int *uaddr, int futex_op, int val, void *timeout, int *uaddr2,
	int val3)
{
  return syscall (SYS_futex, uaddr, futex_op, val, timeout, uaddr2, val3);
}

static void
xsethandler (int sig, void (*handler)(int, siginfo_t *, void *), int flags)
{
  struct sigaction sa = { 0 };
  sa.sa_sigaction = handler;
  sa.sa_flags = SA_SIGINFO | flags;
  TEST_VERIFY_EXIT (sigemptyset (&sa.sa_mask) == 0);
  TEST_VERIFY_EXIT (sigaction (sig, &sa, 0) == 0);
}

static jmp_buf jmpbuf;

static void
sigsegv_handler (int sig, siginfo_t *info, void *ctx_void)
{
  siglongjmp (jmpbuf, 1);
}

/* Points to an array of 1024 ints, each holding its own index.  */
static const unsigned int *counter_page;
static struct user_desc *low_user_desc;
static struct user_desc *low_user_desc_clear; /* Used to delete GDT entry.  */
static int gdt_entry_num;

static void
setup_counter_page (void)
{
  long page_size = sysconf (_SC_PAGE_SIZE);
  TEST_VERIFY_EXIT (page_size > 0);
  unsigned int *page = xmmap (NULL, page_size, PROT_READ | PROT_WRITE,
			      MAP_ANONYMOUS | MAP_PRIVATE | MAP_32BIT, -1);
  for (int i = 0; i < (page_size / sizeof (unsigned int)); i++)
    page[i] = i;
  counter_page = page;
}

static void
setup_low_user_desc (void)
{
  low_user_desc = xmmap (NULL, 2 * sizeof (struct user_desc),
			 PROT_READ | PROT_WRITE,
			 MAP_ANONYMOUS | MAP_PRIVATE | MAP_32BIT, -1);

  low_user_desc->entry_number    = -1;
  low_user_desc->base_addr       = (unsigned long) &counter_page[1];
  low_user_desc->limit           = 0xffff;
  low_user_desc->seg_32bit       = 1;
  low_user_desc->contents        = 0;
  low_user_desc->read_exec_only  = 0;
  low_user_desc->limit_in_pages  = 1;
  low_user_desc->seg_not_present = 0;
  low_user_desc->useable         = 0;

  xset_thread_area (low_user_desc);

  low_user_desc_clear = low_user_desc + 1;
  low_user_desc_clear->entry_number = gdt_entry_num;
  low_user_desc_clear->read_exec_only = 1;
  low_user_desc_clear->seg_not_present = 1;
}

/* Possible values of futex:
   0: thread is idle.
   1: thread armed.
   2: thread should clear LDT entry 0.
   3: thread should exit.  */
static atomic_uint ftx;

static void *
threadproc (void *ctx)
{
  while (1)
    {
      futex ((int *) &ftx, FUTEX_WAIT, 1, NULL, NULL, 0);
      while (atomic_load (&ftx) != 2)
	{
	  if (atomic_load (&ftx) >= 3)
	    return NULL;
	}

      /* clear LDT entry 0.  */
      const struct user_desc desc = { 0 };
      xmodify_ldt (1, &desc, sizeof (desc));

      /* If ftx == 2, set it to zero,  If ftx == 100, quit.  */
      if (atomic_fetch_add (&ftx, -2) != 2)
	return NULL;
    }
}


/* As described in testcase, for historical reasons x86_32 Linux (and compat
   on x86_64) interprets SA_RESTORER clear with nonzero sa_restorer as a
   request for stack switching if the SS segment is 'funny' (this is default
   scenario for vDSO system).  This means that anything that tries to mix
   signal handling with segmentation should explicit clear the sa_restorer.

   This testcase check if sigaction in fact does it by changing the local
   descriptor table (LDT) through the modify_ldt syscall and triggering
   a synchronous segfault on iret fault by trying to install an invalid
   segment.  With a correct zeroed sa_restorer it should not trigger an
   'real' SEGSEGV and allows the siglongjmp in signal handler.  */

static int
do_test (void)
{
  setup_counter_page ();
  setup_low_user_desc ();

  pthread_t thread;
  unsigned short orig_ss;

  xsethandler (SIGSEGV, sigsegv_handler, 0);
  /* 32-bit kernels send SIGILL instead of SIGSEGV on IRET faults.  */
  xsethandler (SIGILL, sigsegv_handler, 0);
  /* Some kernels send SIGBUS instead.  */
  xsethandler (SIGBUS, sigsegv_handler, 0);

  thread = xpthread_create (0, threadproc, 0);

  asm volatile ("mov %%ss, %0" : "=rm" (orig_ss));

  for (int i = 0; i < 5; i++)
    {
      if (sigsetjmp (jmpbuf, 1) != 0)
	continue;

      /* Make sure the thread is ready after the last test. */
      while (atomic_load (&ftx) != 0)
	;

      struct user_desc desc = {
	.entry_number       = 0,
	.base_addr          = 0,
	.limit              = 0xffff,
	.seg_32bit          = 1,
	.contents           = 0,
	.read_exec_only     = 0,
	.limit_in_pages     = 1,
	.seg_not_present    = 0,
	.useable            = 0
      };

      xmodify_ldt (0x11, &desc, sizeof (desc));

      /* Arm the thread.  */
      ftx = 1;
      futex ((int*) &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);

      asm volatile ("mov %0, %%ss" : : "r" (0x7));

      /* Fire up thread modify_ldt call.  */
      atomic_store (&ftx, 2);

      while (atomic_load (&ftx) != 0)
	;

      /* On success, modify_ldt will segfault us synchronously and we will
	 escape via siglongjmp.  */
      support_record_failure ();
    }

  atomic_store (&ftx, 100);
  futex ((int*) &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);

  xpthread_join (thread);

  return 0;
}

#include <support/test-driver.c>