mailing list of musl libc
 help / color / mirror / code / Atom feed
88669b4907ee754ca2acedf7bc1e1fb42faa30b8 blob 12008 bytes (raw)

  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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
 
// SPDX-License-Identifier: GPL-2.0-only
/*
 * vsyscall_lockout.c - check that disabling vsyscall works
 * Copyright (C) 2021 Red Hat, Inc.
 */

#include <stddef.h>

#include <asm/prctl.h>
#include <asm/vsyscall.h>
#include <linux/signal.h>
#include <linux/time.h>
#include <linux/types.h>
#include <linux/unistd.h>

#ifndef ARCH_VSYSCALL_LOCKOUT
#define ARCH_VSYSCALL_LOCKOUT   0x5001
#elif ARCH_VSYSCALL_LOCKOUT != 0x5001
#error wrong vlaue for ARCH_VSYSCALL_LOCKOUT
#endif

static inline long syscall0(int nr)
{
        unsigned long result;

        __asm__ volatile ("syscall"
                          : "=a" (result)
                          : "0" (nr)
                          : "memory", "cc", "r11", "cx");
        return result;
}

static inline long syscall1(int nr, long arg0)
{
        register long rdi __asm__ ("rdi") = arg0;
        unsigned long result;

        __asm__ volatile ("syscall"
                          : "=a" (result)
                          : "0" (nr), "r" (rdi)
                          : "memory", "cc", "r11", "cx");
        return result;
}

static inline long syscall2(int nr, long arg0, long arg1)
{
        register long rdi __asm__ ("rdi") = arg0;
        register long rsi __asm__ ("rsi") = arg1;
        unsigned long result;

        __asm__ volatile ("syscall"
                          : "=a" (result)
                          : "0" (nr), "r" (rdi), "r" (rsi)
                          : "memory", "cc", "r11", "cx");
        return result;
}

static inline long syscall3(int nr, long arg0, long arg1, long arg2)
{
        register long rdi __asm__ ("rdi") = arg0;
        register long rsi __asm__ ("rsi") = arg1;
        register long rdx __asm__ ("rdx") = arg2;
        unsigned long result;

        __asm__ volatile ("syscall"
                          : "=a" (result)
                          : "0" (nr), "r" (rdi), "r" (rsi), "r" (rdx)
                          : "memory", "cc", "r11", "cx");
        return result;
}

static inline long syscall4(int nr, long arg0, long arg1, long arg2, long arg3)
{
        register long rdi __asm__ ("rdi") = arg0;
        register long rsi __asm__ ("rsi") = arg1;
        register long rdx __asm__ ("rdx") = arg2;
        register long r10 __asm__ ("r10") = arg2;
        unsigned long result;

        __asm__ volatile ("syscall"
                          : "=a" (result)
                          : "0" (nr), "r" (rdi), "r" (rsi), "r" (rdx),
                            "r" (r10)
                          : "memory", "cc", "r11", "cx");
        return result;
}

static inline long vsyscall1(long addr, long arg0)
{
        register long rdi __asm__ ("rdi") = arg0;
        unsigned long result;

        __asm__ volatile ("callq *%%rax"
                          : "=a" (result)
                          : "0" (addr), "r" (rdi)
                          : "memory", "cc", "r11", "cx");
        return result;
}

static void __attribute__ ((noreturn)) sys_exit(int status)
{
        syscall1(__NR_exit, status);
        __builtin_unreachable();
}

static void sigabrt(void)
{
        syscall2(__NR_kill, syscall0(__NR_getpid), SIGABRT);
}

static void print_char(char byte)
{
        if (syscall3(__NR_write, 1L, (long) &byte, 1L) < 0)
                sigabrt();
}

static void print_string(const char *p)
{
        while (*p) {
                print_char(*p);
                ++p;
        }
}

static void print_dec_1(unsigned long val)
{
        if (val != 0) {
                print_dec_1(val / 10);
                print_char('0' + (val % 10));
        }
}

static void print_dec(unsigned long val)
{
        if (val == 0)
                print_char('0');
        else
                print_dec_1(val);
}

static void print_signed_dec(long val)
{
        if (val < 0) {
                print_char('-');
                print_dec(-(unsigned long)val);
        } else
                print_dec(val);
}

static void print_time(const char *label, struct timeval tv)
{
        print_string(label);
        print_string(": ");
        print_dec(tv.tv_sec);
        print_char(' ');
        print_dec(tv.tv_usec);
        print_char('\n');
}

static void print_failure(const char *label, long ret)
{
        print_string("error: ");
        print_string(label);
        print_string(" failed: ");
        print_signed_dec(ret);
        print_char('\n');
}

static void xgettimeofday(struct timeval *tv)
{
        long ret = syscall1(__NR_gettimeofday, (long) tv);

        if (ret != 0) {
                print_failure("gettimeofday", ret);
                sigabrt();
        }
}

static void xvgettimeofday(struct timeval *tv)
{
        long ret = vsyscall1(VSYSCALL_ADDR, (long) tv);

        if (ret) {
                print_failure("vgettimeofday", ret);
                sigabrt();
        }
}

static int sys_arch_prctl(int code, unsigned long addr)
{
        return syscall2(__NR_arch_prctl, code, addr);
}

static __kernel_pid_t xfork(void)
{
        long ret = syscall0(__NR_fork);

        if (ret < 0) {
                print_failure("fork", ret);
                sigabrt();
        }
        return ret;
}

static void xexecve(const char *pathname, char **argv, char **envp)
{
        long ret;

        ret = syscall3(__NR_execve, (long) pathname, (long) argv, (long) envp);
        print_failure("execve", ret);
        sigabrt();
}

static __kernel_pid_t xwaitpid(__kernel_pid_t pid, int *status, int options)
{
        long ret = syscall4(__NR_wait4, pid, (long) status, options, 0);

        if (ret < 0) {
                print_failure("wait4", ret);
                sigabrt();
        }
        return ret;
}

static int
do_lockout(void)
{
        long ret = sys_arch_prctl(ARCH_VSYSCALL_LOCKOUT, 0);
        if (ret < 0)
                print_failure("arch_prctl(ARCH_VSYSCALL_LOCKOUT)", ret);
        return ret;
}

static long difftime(struct timeval first, struct timeval second)
{
        return second.tv_usec - first.tv_usec +
                (second.tv_sec - first.tv_sec) * 1000 * 1000;
}

/*
 * Second stage: Check that the lockout is not inherited across execve.
 */
static int main_2(void)
{
        struct timeval vsyscall_time = { -1, -1 };
        int status = 0;

        xvgettimeofday(&vsyscall_time);
        print_time("vsyscall gettimeofday after fork", vsyscall_time);
        if (vsyscall_time.tv_sec < 0 || vsyscall_time.tv_usec < 0)
                status = 1;

        return status;
}

static void check_lockout_after_fork(int *status, int twice)
{
        __kernel_pid_t pid;
        struct timeval vsyscall_time;
        int wstatus;

        if (twice) {
                __kernel_pid_t pid_outer;

                print_string("checking that lockout is inherited by fork\n");

                pid_outer = xfork();
                if (pid_outer == 0) {
                        if (do_lockout())
                                sys_exit(1);
                        /*
                         * Logic for the subprocess follows below.
                         */
                } else {
                        xwaitpid(pid_outer, &wstatus, 0);
                        if (wstatus != 0) {
                                print_string("error: unexpected exit status: ");
                                print_signed_dec(wstatus);
                                print_char('\n');
                                *status = 1;
                        }
                        return;
                }
        } else
                print_string("checking that lockout works after one fork\n");

        pid = xfork();
        if (pid == 0) {
                if (!twice && do_lockout())
                        sys_exit(1);
                /*
                 * This should trigger a fault.
                 */
                xvgettimeofday(&vsyscall_time);
                sys_exit(0);
        }
        xwaitpid(pid, &wstatus, 0);
        switch (wstatus) {
        case 0:
                print_string("error: no crash after lockout\n");
                *status = 1;
                break;
        case 0x0100:
                *status = 1;
                break;
        case SIGSEGV:
                print_string("termination after lockout\n");
                break;
        default:
                print_string("error: unexpected exit status: ");
                print_signed_dec(wstatus);
                print_char('\n');
                *status = 1;
        }

        if (twice)
                sys_exit(*status);

        /*
         * Status in the parent process should be unaffected.
         */
        xvgettimeofday(&vsyscall_time);
}

static void check_no_lockout_after_execve(char **argv, int *status)
{
        __kernel_pid_t pid;
        int wstatus;

        print_string("checking that lockout is not inherited by execve\n");
        pid = xfork();
        if (pid == 0) {
                struct timeval vsyscall_time;
                char *new_argv[] = { argv[0], "2", NULL };

                xvgettimeofday(&vsyscall_time);
                if (do_lockout())
                        sys_exit(1);

                /*
                 * Re-exec the second stage.  See main_2 above.
                 */
                xexecve(argv[0], new_argv, new_argv + 2);
        }

        xwaitpid(pid, &wstatus, 0);
        if (wstatus != 0) {
                print_string("error: unexpected exit status: ");
                print_signed_dec(wstatus);
                print_char('\n');
                *status = 1;
        }
}

static int main(int argc, char **argv)
{
        struct timeval initial_time = { -1, -1 };
        struct timeval vsyscall_time = { -1, -1 };
        struct timeval final_time = { -1, -1 };
        long vsyscall_diff, final_diff;
        int status = 0;

        if (argc > 1)
                switch (*argv[1]) {
                case '2':
                        return main_2();
                default:
                        print_string("usage: ");
                        print_string(argv[0]);
                        print_string("\n");
                        return 1;
                }


        xgettimeofday(&initial_time);
        xvgettimeofday(&vsyscall_time);
        xgettimeofday(&final_time);
        vsyscall_diff = difftime(initial_time, vsyscall_time);
        final_diff = difftime(vsyscall_time, final_time);

        print_time("initial gettimeofday", initial_time);
        print_time("vsyscall gettimeofday", vsyscall_time);
        print_time("final gettimeofday", final_time);

        if (initial_time.tv_sec < 0 || initial_time.tv_usec < 0 ||
            vsyscall_time.tv_sec < 0 || vsyscall_time.tv_usec < 0 ||
            final_time.tv_sec < 0 || final_time.tv_usec < 0) {
                print_string("error: negative time\n");
                status = 1;
        }

        print_string("differences: ");
        print_signed_dec(vsyscall_diff);
        print_char(' ');
        print_signed_dec(final_diff);
        print_char('\n');

        if (vsyscall_diff < 0 || final_diff < 0) {
                /*
                 * This may produce false positives if there is an active NTP.
                 */
                print_string("error: time went backwards\n");
                status = 1;
        }

        check_lockout_after_fork(&status, 0);
        check_lockout_after_fork(&status, 1);
        check_no_lockout_after_execve(argv, &status);

        print_string("testing done, exit status: ");
        print_signed_dec(status);
        print_char('\n');
        return status;
}

static void __attribute__ ((used)) main_trampoline(long *rsp)
{
        sys_exit(main(*rsp, (char **) (rsp + 1)));
}

__asm__ (".text\n\t"
         ".globl _start\n"
         "_start:\n\t"
         ".cfi_startproc\n\t"
         ".cfi_undefined rip\n\t"
         "movq %rsp, %rdi\n\t"
         "callq main_trampoline\n\t" /* Results in psABI %rsp alignment.  */
         ".cfi_endproc\n\t"
         ".type _start, @function\n\t"
         ".size _start, . - _start\n\t"
         ".previous");
debug log:

solving 88669b4907ee ...
found 88669b4907ee in https://inbox.vuxu.org/musl/87h7bzjaer.fsf@oldenburg.str.redhat.com/

applying [1/1] https://inbox.vuxu.org/musl/87h7bzjaer.fsf@oldenburg.str.redhat.com/
diff --git a/tools/testing/selftests/x86/vsyscall_lockout.c b/tools/testing/selftests/x86/vsyscall_lockout.c
new file mode 100644
index 000000000000..88669b4907ee

Checking patch tools/testing/selftests/x86/vsyscall_lockout.c...
Applied patch tools/testing/selftests/x86/vsyscall_lockout.c cleanly.

index at:
100644 88669b4907ee754ca2acedf7bc1e1fb42faa30b8	tools/testing/selftests/x86/vsyscall_lockout.c

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).