1 /* 2 * Check decoding of name_to_handle_at and open_by_handle_at syscalls. 3 * 4 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv (at) altlinux.org> 5 * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr (at) gmail.com> 6 * Copyright (c) 2015-2017 The strace developers. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include "tests.h" 33 #include <asm/unistd.h> 34 35 #if defined __NR_name_to_handle_at && defined __NR_open_by_handle_at 36 37 # include <assert.h> 38 # include <errno.h> 39 # include <inttypes.h> 40 # include <fcntl.h> 41 # include <stdio.h> 42 # include <stdbool.h> 43 # include <unistd.h> 44 45 enum assert_rc { 46 ASSERT_NONE, 47 ASSERT_SUCCESS, 48 ASSERT_ERROR, 49 }; 50 51 # ifndef MAX_HANDLE_SZ 52 53 # define MAX_HANDLE_SZ 128 54 55 struct file_handle { 56 unsigned int handle_bytes; 57 int handle_type; 58 unsigned char f_handle[0]; 59 }; 60 # endif /* !MAX_HANDLE_SZ */ 61 62 63 void 64 print_handle_data(unsigned char *bytes, unsigned int size) 65 { 66 unsigned int i; 67 68 if (size > MAX_HANDLE_SZ) 69 size = MAX_HANDLE_SZ; 70 71 printf("0x"); 72 for (i = 0; i < size; ++i) 73 printf("%02x", bytes[i]); 74 } 75 76 void 77 do_name_to_handle_at(kernel_ulong_t dirfd, const char *dirfd_str, 78 kernel_ulong_t pathname, const char *pathname_str, 79 kernel_ulong_t handle, const char *handle_str, 80 kernel_ulong_t mount_id, 81 kernel_ulong_t flags, const char *flags_str, 82 enum assert_rc assert_rc, long assert_errno) 83 { 84 long rc; 85 const char *errstr; 86 87 rc = syscall(__NR_name_to_handle_at, dirfd, pathname, handle, mount_id, 88 flags); 89 errstr = sprintrc(rc); 90 91 if (assert_rc != ASSERT_NONE) 92 assert(rc == (assert_rc == ASSERT_SUCCESS ? 0 : -1)); 93 if (assert_errno) 94 assert(errno != assert_errno); 95 96 printf("name_to_handle_at(%s, %s, %s", 97 dirfd_str, pathname_str, handle_str); 98 99 if (rc != -1) { 100 struct file_handle *fh = 101 (struct file_handle *) (uintptr_t) handle; 102 int *mount_id_ptr = (int *) (uintptr_t) mount_id; 103 104 printf(" => %u, handle_type=%d, f_handle=", 105 fh->handle_bytes, fh->handle_type); 106 print_handle_data((unsigned char *) fh + 107 sizeof(struct file_handle), 108 fh->handle_bytes); 109 printf("}, [%d]", *mount_id_ptr); 110 } else { 111 if (mount_id) 112 printf(", %#llx", (unsigned long long) mount_id); 113 else 114 printf(", NULL"); 115 } 116 117 printf(", %s) = %s\n", flags_str, errstr); 118 } 119 120 void 121 do_open_by_handle_at(kernel_ulong_t mount_fd, 122 kernel_ulong_t handle, bool valid_handle, bool valid_data, 123 kernel_ulong_t flags, const char *flags_str) 124 { 125 long rc; 126 127 printf("open_by_handle_at(%d, ", (int) mount_fd); 128 if (valid_handle) { 129 struct file_handle *fh = 130 (struct file_handle *) (uintptr_t) handle; 131 132 printf("{handle_bytes=%u, handle_type=%d", fh->handle_bytes, 133 fh->handle_type); 134 135 if (valid_data) { 136 printf(", f_handle="); 137 print_handle_data((unsigned char *) fh + 138 sizeof(struct file_handle), 139 fh->handle_bytes); 140 } 141 142 printf("}"); 143 } else { 144 if (handle) 145 printf("%#llx", (unsigned long long) handle); 146 else 147 printf("NULL"); 148 } 149 printf(", %s) = ", flags_str); 150 151 rc = syscall(__NR_open_by_handle_at, mount_fd, handle, flags); 152 153 printf("%s\n", sprintrc(rc)); 154 } 155 156 struct strval { 157 kernel_ulong_t val; 158 const char *str; 159 }; 160 161 #define STR16 "0123456789abcdef" 162 #define STR64 STR16 STR16 STR16 STR16 163 164 int 165 main(void) 166 { 167 enum { 168 PATH1_SIZE = 64, 169 }; 170 171 static const kernel_ulong_t fdcwd = 172 (kernel_ulong_t) 0x87654321ffffff9cULL; 173 static const struct strval dirfds[] = { 174 { (kernel_ulong_t) 0xdeadca57badda7a1ULL, "-1159878751" }, 175 { (kernel_ulong_t) 0x12345678ffffff9cULL, "AT_FDCWD" }, 176 }; 177 static const struct strval name_flags[] = { 178 { (kernel_ulong_t) 0xdeadf15700000000ULL, "0" }, 179 { (kernel_ulong_t) 0xbadc0ded00001000ULL, 180 "AT_EMPTY_PATH" }, 181 { (kernel_ulong_t) 0xdeadc0deda7a1457ULL, 182 "AT_SYMLINK_FOLLOW|AT_EMPTY_PATH|0xda7a0057" }, 183 { (kernel_ulong_t) 0xdefaced1ffffebffULL, 184 "0xffffebff /* AT_??? */" }, 185 }; 186 static const kernel_ulong_t mount_fds[] = { 187 (kernel_ulong_t) 0xdeadca5701234567ULL, 188 (kernel_ulong_t) 0x12345678ffffff9cULL, 189 }; 190 static const struct strval open_flags[] = { 191 { F8ILL_KULONG_MASK, "O_RDONLY" }, 192 { (kernel_ulong_t) 0xdeadbeef80000001ULL, 193 "O_WRONLY|0x80000000" } 194 }; 195 196 static const char str64[] = STR64; 197 198 199 char *bogus_path1 = tail_memdup(str64, PATH1_SIZE); 200 char *bogus_path2 = tail_memdup(str64, sizeof(str64)); 201 202 struct file_handle *handle = 203 tail_alloc(sizeof(struct file_handle) + MAX_HANDLE_SZ); 204 struct file_handle *handle_0 = 205 tail_alloc(sizeof(struct file_handle) + 0); 206 struct file_handle *handle_8 = 207 tail_alloc(sizeof(struct file_handle) + 8); 208 struct file_handle *handle_128 = 209 tail_alloc(sizeof(struct file_handle) + 128); 210 struct file_handle *handle_256 = 211 tail_alloc(sizeof(struct file_handle) + 256); 212 TAIL_ALLOC_OBJECT_CONST_PTR(int, bogus_mount_id); 213 214 char handle_0_addr[sizeof("0x") + sizeof(void *) * 2]; 215 216 char bogus_path1_addr[sizeof("0x") + sizeof(void *) * 2]; 217 char bogus_path1_after_addr[sizeof("0x") + sizeof(void *) * 2]; 218 219 220 struct strval paths[] = { 221 { (kernel_ulong_t) 0, "NULL" }, 222 { (kernel_ulong_t) (uintptr_t) (bogus_path1 + PATH1_SIZE), 223 bogus_path1_after_addr }, 224 { (kernel_ulong_t) (uintptr_t) bogus_path1, bogus_path1_addr }, 225 { (kernel_ulong_t) (uintptr_t) bogus_path2, "\"" STR64 "\"" }, 226 }; 227 struct strval name_handles[] = { 228 { (uintptr_t) (handle_0 + sizeof(struct file_handle)), 229 handle_0_addr }, 230 { (uintptr_t) handle_0, "{handle_bytes=256}" }, 231 { (uintptr_t) handle_8, "{handle_bytes=0}" }, 232 { (uintptr_t) handle_128, "{handle_bytes=128}" }, 233 { (uintptr_t) handle_256, "{handle_bytes=256}" }, 234 }; 235 struct { 236 kernel_ulong_t addr; 237 bool valid; 238 bool valid_data; 239 } open_handles[] = { 240 { 0, false, false }, 241 { (uintptr_t) (handle_0 + sizeof(struct file_handle)), 242 false, false }, 243 { (uintptr_t) handle_0 + 4, false, false }, 244 { (uintptr_t) handle_0, true, false }, 245 { (uintptr_t) handle_8, true, true }, 246 { (uintptr_t) handle_128, true, true }, 247 { (uintptr_t) handle_256, true, true }, 248 }; 249 kernel_ulong_t mount_ids[] = { 250 0, 251 (kernel_ulong_t) (uintptr_t) (bogus_mount_id + 1), 252 (kernel_ulong_t) (uintptr_t) bogus_mount_id, 253 }; 254 255 const int flags = 0x400; 256 int mount_id; 257 unsigned int i; 258 unsigned int j; 259 unsigned int k; 260 unsigned int l; 261 unsigned int m; 262 263 264 snprintf(bogus_path1_addr, sizeof(bogus_path1_addr), "%p", bogus_path1); 265 snprintf(bogus_path1_after_addr, sizeof(bogus_path1_after_addr), "%p", 266 bogus_path1 + PATH1_SIZE); 267 268 handle_0->handle_bytes = 256; 269 handle_8->handle_bytes = 0; 270 handle_128->handle_bytes = 128; 271 handle_256->handle_bytes = 256; 272 273 fill_memory((char *) handle_128 + sizeof(struct file_handle), 128); 274 fill_memory((char *) handle_256 + sizeof(struct file_handle), 256); 275 276 snprintf(handle_0_addr, sizeof(handle_0_addr), "%p", 277 handle_0 + sizeof(struct file_handle)); 278 279 handle->handle_bytes = 0; 280 281 assert(syscall(__NR_name_to_handle_at, fdcwd, ".", handle, &mount_id, 282 flags | 1) == -1); 283 if (EINVAL != errno) 284 perror_msg_and_skip("name_to_handle_at"); 285 printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=0}, %p" 286 ", AT_SYMLINK_FOLLOW|0x1) = -1 EINVAL (%m)\n", &mount_id); 287 288 assert(syscall(__NR_name_to_handle_at, fdcwd, ".", handle, &mount_id, 289 flags) == -1); 290 if (EOVERFLOW != errno) 291 perror_msg_and_skip("name_to_handle_at"); 292 printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=0 => %u}" 293 ", %p, AT_SYMLINK_FOLLOW) = -1 EOVERFLOW (%m)\n", 294 handle->handle_bytes, &mount_id); 295 296 assert(syscall(__NR_name_to_handle_at, fdcwd, ".", handle, &mount_id, 297 flags) == 0); 298 printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=%u" 299 ", handle_type=%d, f_handle=0x", 300 handle->handle_bytes, handle->handle_type); 301 for (i = 0; i < handle->handle_bytes; ++i) 302 printf("%02x", handle->f_handle[i]); 303 printf("}, [%d], AT_SYMLINK_FOLLOW) = 0\n", mount_id); 304 305 printf("open_by_handle_at(-1, {handle_bytes=%u, handle_type=%d" 306 ", f_handle=0x", handle->handle_bytes, handle->handle_type); 307 for (i = 0; i < handle->handle_bytes; ++i) 308 printf("%02x", handle->f_handle[i]); 309 int rc = syscall(__NR_open_by_handle_at, -1, handle, 310 O_RDONLY | O_DIRECTORY); 311 printf("}, O_RDONLY|O_DIRECTORY) = %d %s (%m)\n", rc, errno2name()); 312 313 for (i = 0; i < ARRAY_SIZE(dirfds); i++) { 314 for (j = 0; j < ARRAY_SIZE(paths); j++) { 315 for (k = 0; k < ARRAY_SIZE(name_handles); k++) { 316 for (l = 0; l < ARRAY_SIZE(mount_ids); l++) { 317 for (m = 0; m < ARRAY_SIZE(name_flags); 318 m++) { 319 do_name_to_handle_at( 320 dirfds[i].val, 321 dirfds[i].str, 322 paths[j].val, 323 paths[j].str, 324 name_handles[k].val, 325 name_handles[k].str, 326 mount_ids[l], 327 name_flags[m].val, 328 name_flags[m].str, 329 ASSERT_ERROR, 0); 330 } 331 } 332 } 333 } 334 } 335 336 for (i = 0; i < ARRAY_SIZE(mount_fds); i++) { 337 for (j = 0; j < ARRAY_SIZE(open_handles); j++) { 338 for (k = 0; k < ARRAY_SIZE(open_flags); k++) { 339 do_open_by_handle_at(mount_fds[i], 340 open_handles[j].addr, 341 open_handles[j].valid, 342 open_handles[j].valid_data, 343 open_flags[k].val, 344 open_flags[k].str); 345 } 346 } 347 } 348 349 puts("+++ exited with 0 +++"); 350 return 0; 351 } 352 353 #else 354 355 SKIP_MAIN_UNDEFINED("__NR_name_to_handle_at && __NR_open_by_handle_at") 356 357 #endif 358