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 (at) 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 { 31 long pid; 32 33 if (argc<3) { 34 fprintf(stderr,"Usage: %s TRACER_PID SLEEP_SECONDS\n", argv[0]); 35 /* Without arguments, send a SIGINT to ourself so that gdb can 36 * regain control without needing debugging symbols. 37 */ 38 kill(getpid(), SIGINT); 39 return 1; 40 } 41 42 pid = strtol(argv[1], NULL, 10); 43 if (pid != -2) { 44 if (prctl(PR_SET_PTRACER, pid, 0, 0, 0)) { 45 perror("prctl"); 46 puts("failed"); 47 return 1; 48 } 49 } 50 51 puts("ready"); 52 fflush(NULL); 53 sleep(atoi(argv[2])); 54 55 return 0; 56 } 57