Daniel Erat | 6782961 | 2019-03-15 15:29:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The Chromium OS Authors. |
| 3 | * |
| 4 | * Based on: |
| 5 | * http://bazaar.launchpad.net/~ubuntu-bugcontrol/qa-regression-testing/master/view/head:/scripts/kernel-security/ptrace/sleeper.c |
| 6 | * Copyright 2010 Canonical, Ltd |
| 7 | * License: GPLv3 |
| 8 | * Author: Kees Cook <kees.cook@canonical.com> |
| 9 | */ |
| 10 | #define _GNU_SOURCE |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <unistd.h> |
| 14 | #include <stdint.h> |
| 15 | #include <string.h> |
| 16 | #include <stdint.h> |
| 17 | #include <stddef.h> |
| 18 | #include <inttypes.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <sys/prctl.h> |
| 23 | #include <signal.h> |
| 24 | |
| 25 | #ifndef PR_SET_PTRACER |
| 26 | #define PR_SET_PTRACER 0x59616d61 |
| 27 | #endif |
| 28 | |
| 29 | int main(int argc, char* argv[]) { |
| 30 | long pid; |
| 31 | |
| 32 | if (argc < 3) { |
| 33 | fprintf(stderr, "Usage: %s TRACER_PID SLEEP_SECONDS\n", argv[0]); |
| 34 | /* Without arguments, send a SIGINT to ourself so that gdb can |
| 35 | * regain control without needing debugging symbols. |
| 36 | */ |
| 37 | kill(getpid(), SIGINT); |
| 38 | return 1; |
| 39 | } |
| 40 | |
| 41 | pid = strtol(argv[1], NULL, 10); |
| 42 | if (pid != -2) { |
| 43 | if (prctl(PR_SET_PTRACER, pid, 0, 0, 0)) { |
| 44 | perror("prctl"); |
| 45 | puts("failed"); |
| 46 | return 1; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | puts("ready"); |
| 51 | fflush(NULL); |
| 52 | sleep(atoi(argv[2])); |
| 53 | |
| 54 | return 0; |
| 55 | } |