1
2
3
4
5
6
7
8
9
10
11#define _GNU_SOURCE
12
13#include <stdlib.h>
14#include <unistd.h>
15#include <stdio.h>
16#include <string.h>
17#include <sys/mman.h>
18#include <err.h>
19#include <stddef.h>
20#include <stdbool.h>
21#include <pthread.h>
22
23static void *threadproc(void *ctx)
24{
25
26
27
28
29 while (true)
30 ;
31
32 return NULL;
33}
34
35#ifdef __x86_64__
36extern unsigned long call32_from_64(void *stack, void (*function)(void));
37
38asm (".pushsection .text\n\t"
39 ".code32\n\t"
40 "test_ss:\n\t"
41 "pushl $0\n\t"
42 "popl %eax\n\t"
43 "ret\n\t"
44 ".code64");
45extern void test_ss(void);
46#endif
47
48int main()
49{
50
51
52
53
54
55 cpu_set_t cpuset;
56 CPU_ZERO(&cpuset);
57 CPU_SET(0, &cpuset);
58 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
59 printf("[WARN]\tsched_setaffinity failed\n");
60
61 pthread_t thread;
62 if (pthread_create(&thread, 0, threadproc, 0) != 0)
63 err(1, "pthread_create");
64
65#ifdef __x86_64__
66 unsigned char *stack32 = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
67 MAP_32BIT | MAP_ANONYMOUS | MAP_PRIVATE,
68 -1, 0);
69 if (stack32 == MAP_FAILED)
70 err(1, "mmap");
71#endif
72
73 printf("[RUN]\tSyscalls followed by SS validation\n");
74
75 for (int i = 0; i < 1000; i++) {
76
77
78
79
80
81
82
83
84
85 usleep(2);
86
87#ifdef __x86_64__
88
89
90
91
92
93 call32_from_64(stack32 + 4088, test_ss);
94#endif
95 }
96
97 printf("[OK]\tWe survived\n");
98
99#ifdef __x86_64__
100 munmap(stack32, 4096);
101#endif
102
103 return 0;
104}
105