vapier | 8435409 | 2008-09-26 23:01:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * check out-of-bound/unaligned addresses given to |
| 3 | * - {PEEK,POKE}{DATA,TEXT,USER} |
| 4 | * - {GET,SET}{,FG}REGS |
| 5 | * - {GET,SET}SIGINFO |
| 6 | * |
| 7 | * Copyright (c) 2008 Analog Devices Inc. |
| 8 | * |
| 9 | * Licensed under the GPL-2 or later |
| 10 | */ |
| 11 | |
vapier | 9a9a569 | 2008-12-11 09:56:27 +0000 | [diff] [blame] | 12 | #define _GNU_SOURCE |
| 13 | |
vapier | 8435409 | 2008-09-26 23:01:20 +0000 | [diff] [blame] | 14 | #include <errno.h> |
| 15 | #include <stdbool.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <unistd.h> |
vapier | 919dca8 | 2009-11-03 19:42:12 +0000 | [diff] [blame] | 19 | |
| 20 | #include <config.h> |
| 21 | #include "ptrace.h" |
vapier | 8435409 | 2008-09-26 23:01:20 +0000 | [diff] [blame] | 22 | |
| 23 | #include "test.h" |
subrata_modak | 90a8fb4 | 2008-10-15 14:38:51 +0000 | [diff] [blame] | 24 | #include "spawn_ptrace_child.h" |
yaberauneya | ef77253 | 2009-10-09 17:55:43 +0000 | [diff] [blame] | 25 | #include "config.h" |
vapier | 8435409 | 2008-09-26 23:01:20 +0000 | [diff] [blame] | 26 | |
vapier | e68d2eb | 2008-11-02 06:06:44 +0000 | [diff] [blame] | 27 | /* this should be sizeof(struct user), but that info is only found |
| 28 | * in the kernel asm/user.h which is not exported to userspace. |
| 29 | */ |
| 30 | #if defined(__i386__) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 31 | #define SIZEOF_USER 284 |
vapier | e68d2eb | 2008-11-02 06:06:44 +0000 | [diff] [blame] | 32 | #elif defined(__x86_64__) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 33 | #define SIZEOF_USER 928 |
vapier | e68d2eb | 2008-11-02 06:06:44 +0000 | [diff] [blame] | 34 | #else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 35 | #define SIZEOF_USER 0x1000 /* just pick a big number */ |
vapier | e68d2eb | 2008-11-02 06:06:44 +0000 | [diff] [blame] | 36 | #endif |
| 37 | |
vapier | 8435409 | 2008-09-26 23:01:20 +0000 | [diff] [blame] | 38 | char *TCID = "ptrace06"; |
| 39 | |
| 40 | struct test_case_t { |
| 41 | enum __ptrace_request request; |
| 42 | long addr; |
| 43 | long data; |
| 44 | } test_cases[] = { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 45 | { |
| 46 | PTRACE_PEEKDATA,.addr = 0}, { |
| 47 | PTRACE_PEEKDATA,.addr = 1}, { |
| 48 | PTRACE_PEEKDATA,.addr = 2}, { |
| 49 | PTRACE_PEEKDATA,.addr = 3}, { |
| 50 | PTRACE_PEEKDATA,.addr = -1}, { |
| 51 | PTRACE_PEEKDATA,.addr = -2}, { |
| 52 | PTRACE_PEEKDATA,.addr = -3}, { |
| 53 | PTRACE_PEEKDATA,.addr = -4}, { |
| 54 | PTRACE_PEEKTEXT,.addr = 0}, { |
| 55 | PTRACE_PEEKTEXT,.addr = 1}, { |
| 56 | PTRACE_PEEKTEXT,.addr = 2}, { |
| 57 | PTRACE_PEEKTEXT,.addr = 3}, { |
| 58 | PTRACE_PEEKTEXT,.addr = -1}, { |
| 59 | PTRACE_PEEKTEXT,.addr = -2}, { |
| 60 | PTRACE_PEEKTEXT,.addr = -3}, { |
| 61 | PTRACE_PEEKTEXT,.addr = -4}, { |
| 62 | PTRACE_PEEKUSER,.addr = SIZEOF_USER + 1}, { |
| 63 | PTRACE_PEEKUSER,.addr = SIZEOF_USER + 2}, { |
| 64 | PTRACE_PEEKUSER,.addr = SIZEOF_USER + 3}, { |
| 65 | PTRACE_PEEKUSER,.addr = SIZEOF_USER + 4}, { |
| 66 | PTRACE_PEEKUSER,.addr = -1}, { |
| 67 | PTRACE_PEEKUSER,.addr = -2}, { |
| 68 | PTRACE_PEEKUSER,.addr = -3}, { |
| 69 | PTRACE_PEEKUSER,.addr = -4}, { |
| 70 | PTRACE_POKEDATA,.addr = 0}, { |
| 71 | PTRACE_POKEDATA,.addr = 1}, { |
| 72 | PTRACE_POKEDATA,.addr = 2}, { |
| 73 | PTRACE_POKEDATA,.addr = 3}, { |
| 74 | PTRACE_POKEDATA,.addr = -1}, { |
| 75 | PTRACE_POKEDATA,.addr = -2}, { |
| 76 | PTRACE_POKEDATA,.addr = -3}, { |
| 77 | PTRACE_POKEDATA,.addr = -4}, { |
| 78 | PTRACE_POKETEXT,.addr = 0}, { |
| 79 | PTRACE_POKETEXT,.addr = 1}, { |
| 80 | PTRACE_POKETEXT,.addr = 2}, { |
| 81 | PTRACE_POKETEXT,.addr = 3}, { |
| 82 | PTRACE_POKETEXT,.addr = -1}, { |
| 83 | PTRACE_POKETEXT,.addr = -2}, { |
| 84 | PTRACE_POKETEXT,.addr = -3}, { |
| 85 | PTRACE_POKETEXT,.addr = -4}, { |
| 86 | PTRACE_POKEUSER,.addr = SIZEOF_USER + 1}, { |
| 87 | PTRACE_POKEUSER,.addr = SIZEOF_USER + 2}, { |
| 88 | PTRACE_POKEUSER,.addr = SIZEOF_USER + 3}, { |
| 89 | PTRACE_POKEUSER,.addr = SIZEOF_USER + 4}, { |
| 90 | PTRACE_POKEUSER,.addr = -1}, { |
| 91 | PTRACE_POKEUSER,.addr = -2}, { |
| 92 | PTRACE_POKEUSER,.addr = -3}, { |
| 93 | PTRACE_POKEUSER,.addr = -4}, |
subrata_modak | a6e0aa5 | 2008-10-15 14:17:03 +0000 | [diff] [blame] | 94 | #ifdef PTRACE_GETREGS |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 95 | { |
| 96 | PTRACE_GETREGS,.data = 0}, { |
| 97 | PTRACE_GETREGS,.data = 1}, { |
| 98 | PTRACE_GETREGS,.data = 2}, { |
| 99 | PTRACE_GETREGS,.data = 3}, { |
| 100 | PTRACE_GETREGS,.data = -1}, { |
| 101 | PTRACE_GETREGS,.data = -2}, { |
| 102 | PTRACE_GETREGS,.data = -3}, { |
| 103 | PTRACE_GETREGS,.data = -4}, |
subrata_modak | a6e0aa5 | 2008-10-15 14:17:03 +0000 | [diff] [blame] | 104 | #endif |
vapier | 8435409 | 2008-09-26 23:01:20 +0000 | [diff] [blame] | 105 | #ifdef PTRACE_GETFGREGS |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 106 | { |
| 107 | PTRACE_GETFGREGS,.data = 0}, { |
| 108 | PTRACE_GETFGREGS,.data = 1}, { |
| 109 | PTRACE_GETFGREGS,.data = 2}, { |
| 110 | PTRACE_GETFGREGS,.data = 3}, { |
| 111 | PTRACE_GETFGREGS,.data = -1}, { |
| 112 | PTRACE_GETFGREGS,.data = -2}, { |
| 113 | PTRACE_GETFGREGS,.data = -3}, { |
| 114 | PTRACE_GETFGREGS,.data = -4}, |
vapier | 8435409 | 2008-09-26 23:01:20 +0000 | [diff] [blame] | 115 | #endif |
subrata_modak | a6e0aa5 | 2008-10-15 14:17:03 +0000 | [diff] [blame] | 116 | #ifdef PTRACE_SETREGS |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 117 | { |
| 118 | PTRACE_SETREGS,.data = 0}, { |
| 119 | PTRACE_SETREGS,.data = 1}, { |
| 120 | PTRACE_SETREGS,.data = 2}, { |
| 121 | PTRACE_SETREGS,.data = 3}, { |
| 122 | PTRACE_SETREGS,.data = -1}, { |
| 123 | PTRACE_SETREGS,.data = -2}, { |
| 124 | PTRACE_SETREGS,.data = -3}, { |
| 125 | PTRACE_SETREGS,.data = -4}, |
subrata_modak | a6e0aa5 | 2008-10-15 14:17:03 +0000 | [diff] [blame] | 126 | #endif |
vapier | 8435409 | 2008-09-26 23:01:20 +0000 | [diff] [blame] | 127 | #ifdef PTRACE_SETFGREGS |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 128 | { |
| 129 | PTRACE_SETFGREGS,.data = 0}, { |
| 130 | PTRACE_SETFGREGS,.data = 1}, { |
| 131 | PTRACE_SETFGREGS,.data = 2}, { |
| 132 | PTRACE_SETFGREGS,.data = 3}, { |
| 133 | PTRACE_SETFGREGS,.data = -1}, { |
| 134 | PTRACE_SETFGREGS,.data = -2}, { |
| 135 | PTRACE_SETFGREGS,.data = -3}, { |
| 136 | PTRACE_SETFGREGS,.data = -4}, |
vapier | 8435409 | 2008-09-26 23:01:20 +0000 | [diff] [blame] | 137 | #endif |
yaberauneya | ef77253 | 2009-10-09 17:55:43 +0000 | [diff] [blame] | 138 | #if HAVE_DECL_PTRACE_GETSIGINFO |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 139 | { |
| 140 | PTRACE_GETSIGINFO,.data = 0}, { |
| 141 | PTRACE_GETSIGINFO,.data = 1}, { |
| 142 | PTRACE_GETSIGINFO,.data = 2}, { |
| 143 | PTRACE_GETSIGINFO,.data = 3}, { |
| 144 | PTRACE_GETSIGINFO,.data = -1}, { |
| 145 | PTRACE_GETSIGINFO,.data = -2}, { |
| 146 | PTRACE_GETSIGINFO,.data = -3}, { |
| 147 | PTRACE_GETSIGINFO,.data = -4}, |
yaberauneya | ef77253 | 2009-10-09 17:55:43 +0000 | [diff] [blame] | 148 | #endif |
yaberauneya | ef77253 | 2009-10-09 17:55:43 +0000 | [diff] [blame] | 149 | #if HAVE_DECL_PTRACE_SETSIGINFO |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 150 | { |
| 151 | PTRACE_SETSIGINFO,.data = 0}, { |
| 152 | PTRACE_SETSIGINFO,.data = 1}, { |
| 153 | PTRACE_SETSIGINFO,.data = 2}, { |
| 154 | PTRACE_SETSIGINFO,.data = 3}, { |
| 155 | PTRACE_SETSIGINFO,.data = -1}, { |
| 156 | PTRACE_SETSIGINFO,.data = -2}, { |
| 157 | PTRACE_SETSIGINFO,.data = -3}, { |
| 158 | PTRACE_SETSIGINFO,.data = -4}, |
yaberauneya | ef77253 | 2009-10-09 17:55:43 +0000 | [diff] [blame] | 159 | #endif |
vapier | a469a37 | 2009-05-26 02:53:28 +0000 | [diff] [blame] | 160 | }; |
vapier | 8435409 | 2008-09-26 23:01:20 +0000 | [diff] [blame] | 161 | |
| 162 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
| 163 | |
| 164 | int main(int argc, char *argv[]) |
| 165 | { |
| 166 | size_t i; |
| 167 | long ret; |
| 168 | int saved_errno; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 169 | const char *msg; |
vapier | 8435409 | 2008-09-26 23:01:20 +0000 | [diff] [blame] | 170 | |
| 171 | if ((msg = parse_opts(argc, argv, NULL, NULL))) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 172 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
vapier | 8435409 | 2008-09-26 23:01:20 +0000 | [diff] [blame] | 173 | |
| 174 | make_a_baby(argc, argv); |
| 175 | |
| 176 | for (i = 0; i < ARRAY_SIZE(test_cases); ++i) { |
| 177 | struct test_case_t *tc = &test_cases[i]; |
| 178 | |
| 179 | errno = 0; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 180 | ret = |
| 181 | ptrace(tc->request, pid, (void *)tc->addr, |
| 182 | (void *)tc->data); |
vapier | 8435409 | 2008-09-26 23:01:20 +0000 | [diff] [blame] | 183 | saved_errno = errno; |
| 184 | if (ret != -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 185 | tst_resm(TFAIL, |
| 186 | "ptrace(%s, ..., %li, %li) returned %li instead of -1", |
| 187 | strptrace(tc->request), tc->addr, tc->data, |
| 188 | ret); |
vapier | 8435409 | 2008-09-26 23:01:20 +0000 | [diff] [blame] | 189 | else if (saved_errno != EIO && saved_errno != EFAULT) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 190 | tst_resm(TFAIL, |
| 191 | "ptrace(%s, ..., %li, %li) expected errno EIO or EFAULT; actual: %i (%s)", |
| 192 | strptrace(tc->request), tc->addr, tc->data, |
| 193 | saved_errno, strerror(saved_errno)); |
vapier | 8435409 | 2008-09-26 23:01:20 +0000 | [diff] [blame] | 194 | else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 195 | tst_resm(TPASS, |
| 196 | "ptrace(%s, ..., %li, %li) failed as expected", |
| 197 | strptrace(tc->request), tc->addr, tc->data); |
vapier | 8435409 | 2008-09-26 23:01:20 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* hopefully this worked */ |
| 201 | ptrace(PTRACE_KILL, pid, NULL, NULL); |
| 202 | |
| 203 | tst_exit(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 204 | } |