Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 1 | /* |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 2 | * This file is part of execveat strace test. |
| 3 | * |
Dmitry V. Levin | 71ac6ff | 2016-01-04 23:40:24 +0000 | [diff] [blame] | 4 | * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org> |
Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. The name of the author may not be used to endorse or promote products |
| 16 | * derived from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
Dmitry V. Levin | 0c8853c | 2016-01-02 13:28:43 +0000 | [diff] [blame] | 30 | #include "tests.h" |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
Dmitry V. Levin | 7c22101 | 2015-07-26 11:06:53 +0000 | [diff] [blame] | 32 | #include <unistd.h> |
| 33 | #include <sys/syscall.h> |
| 34 | |
| 35 | #ifdef __NR_execveat |
| 36 | |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 37 | # define FILENAME "test.execveat\nfilename" |
| 38 | # define Q_FILENAME "test.execveat\\nfilename" |
| 39 | |
| 40 | static const char * const argv[] = { |
| 41 | FILENAME, "first", "second", (const char *) -1L, |
| 42 | (const char *) -2L, (const char *) -3L |
| 43 | }; |
| 44 | static const char * const q_argv[] = { |
| 45 | Q_FILENAME, "first", "second" |
| 46 | }; |
| 47 | |
| 48 | static const char * const envp[] = { |
| 49 | "foobar=1", "foo\nbar=2", (const char *) -1L, |
| 50 | (const char *) -2L, (const char *) -3L |
| 51 | }; |
| 52 | static const char * const q_envp[] = { |
| 53 | "foobar=1", "foo\\nbar=2" |
| 54 | }; |
Dmitry V. Levin | 7c22101 | 2015-07-26 11:06:53 +0000 | [diff] [blame] | 55 | |
| 56 | int |
| 57 | main(void) |
| 58 | { |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 59 | const char ** const tail_argv = tail_memdup(argv, sizeof(argv)); |
| 60 | const char ** const tail_envp = tail_memdup(envp, sizeof(envp)); |
| 61 | |
| 62 | syscall(__NR_execveat, -100, FILENAME, tail_argv, tail_envp, 0x1100); |
| 63 | printf("execveat(AT_FDCWD, \"%s\"" |
| 64 | ", [\"%s\", \"%s\", \"%s\", %p, %p, %p, ???]" |
| 65 | #ifdef VERBOSE_EXECVEAT |
| 66 | ", [\"%s\", \"%s\", %p, %p, %p, ???]" |
| 67 | #else |
| 68 | ", [/* 5 vars, unterminated */]" |
| 69 | #endif |
| 70 | ", AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n", |
| 71 | Q_FILENAME, q_argv[0], q_argv[1], q_argv[2], |
| 72 | argv[3], argv[4], argv[5], |
| 73 | #ifdef VERBOSE_EXECVEAT |
| 74 | q_envp[0], q_envp[1], envp[2], envp[3], envp[4], |
| 75 | #endif |
Dmitry V. Levin | 9f6611b | 2016-04-21 17:49:32 +0000 | [diff] [blame] | 76 | errno2name()); |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 77 | |
| 78 | tail_argv[ARRAY_SIZE(q_argv)] = NULL; |
| 79 | tail_envp[ARRAY_SIZE(q_envp)] = NULL; |
| 80 | |
| 81 | syscall(__NR_execveat, -100, FILENAME, tail_argv, tail_envp, 0x1100); |
| 82 | printf("execveat(AT_FDCWD, \"%s\", [\"%s\", \"%s\", \"%s\"]" |
| 83 | #ifdef VERBOSE_EXECVEAT |
| 84 | ", [\"%s\", \"%s\"]" |
| 85 | #else |
| 86 | ", [/* 2 vars */]" |
| 87 | #endif |
| 88 | ", AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n", |
| 89 | Q_FILENAME, q_argv[0], q_argv[1], q_argv[2], |
| 90 | #ifdef VERBOSE_EXECVEAT |
| 91 | q_envp[0], q_envp[1], |
| 92 | #endif |
Dmitry V. Levin | 9f6611b | 2016-04-21 17:49:32 +0000 | [diff] [blame] | 93 | errno2name()); |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 94 | |
| 95 | syscall(__NR_execveat, -100, FILENAME, tail_argv + 2, tail_envp + 1, 0x1100); |
| 96 | printf("execveat(AT_FDCWD, \"%s\", [\"%s\"]" |
| 97 | #ifdef VERBOSE_EXECVEAT |
| 98 | ", [\"%s\"]" |
| 99 | #else |
| 100 | ", [/* 1 var */]" |
| 101 | #endif |
| 102 | ", AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n", |
| 103 | Q_FILENAME, q_argv[2], |
| 104 | #ifdef VERBOSE_EXECVEAT |
| 105 | q_envp[1], |
| 106 | #endif |
Dmitry V. Levin | 9f6611b | 2016-04-21 17:49:32 +0000 | [diff] [blame] | 107 | errno2name()); |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 108 | |
Dmitry V. Levin | 84be84d | 2016-02-13 01:23:26 +0000 | [diff] [blame] | 109 | char **const empty = tail_alloc(sizeof(*empty)); |
| 110 | char **const efault = empty + 1; |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 111 | *empty = NULL; |
| 112 | |
| 113 | syscall(__NR_execveat, -100, FILENAME, empty, empty, 0x1100); |
| 114 | printf("execveat(AT_FDCWD, \"%s\", []" |
| 115 | #ifdef VERBOSE_EXECVEAT |
| 116 | ", []" |
| 117 | #else |
| 118 | ", [/* 0 vars */]" |
| 119 | #endif |
| 120 | ", AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n", |
Dmitry V. Levin | 9f6611b | 2016-04-21 17:49:32 +0000 | [diff] [blame] | 121 | Q_FILENAME, errno2name()); |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 122 | |
| 123 | char str_a[] = "012345678901234567890123456789012"; |
| 124 | char str_b[] = "_abcdefghijklmnopqrstuvwxyz()[]{}"; |
| 125 | #define DEFAULT_STRLEN ((unsigned int) sizeof(str_a) - 2) |
| 126 | char **const a = tail_alloc(sizeof(*a) * (DEFAULT_STRLEN + 2)); |
| 127 | char **const b = tail_alloc(sizeof(*b) * (DEFAULT_STRLEN + 2)); |
| 128 | unsigned int i; |
| 129 | for (i = 0; i <= DEFAULT_STRLEN; ++i) { |
| 130 | a[i] = &str_a[i]; |
| 131 | b[i] = &str_b[i]; |
| 132 | } |
| 133 | a[i] = b[i] = NULL; |
| 134 | |
| 135 | syscall(__NR_execveat, -100, FILENAME, a, b, 0x1100); |
| 136 | printf("execveat(AT_FDCWD, \"%s\", [\"%.*s\"...", Q_FILENAME, DEFAULT_STRLEN, a[0]); |
| 137 | for (i = 1; i < DEFAULT_STRLEN; ++i) |
| 138 | printf(", \"%s\"", a[i]); |
| 139 | #ifdef VERBOSE_EXECVEAT |
| 140 | printf(", \"%s\"", a[i]); |
| 141 | #else |
| 142 | printf(", ..."); |
| 143 | #endif |
| 144 | #ifdef VERBOSE_EXECVEAT |
| 145 | printf("], [\"%.*s\"...", DEFAULT_STRLEN, b[0]); |
| 146 | for (i = 1; i <= DEFAULT_STRLEN; ++i) |
| 147 | printf(", \"%s\"", b[i]); |
| 148 | #else |
| 149 | printf("], [/* %u vars */", DEFAULT_STRLEN + 1); |
| 150 | #endif |
| 151 | printf("], AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n", |
Dmitry V. Levin | 9f6611b | 2016-04-21 17:49:32 +0000 | [diff] [blame] | 152 | errno2name()); |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 153 | |
| 154 | syscall(__NR_execveat, -100, FILENAME, a + 1, b + 1, 0x1100); |
| 155 | printf("execveat(AT_FDCWD, \"%s\", [\"%s\"", Q_FILENAME, a[1]); |
| 156 | for (i = 2; i <= DEFAULT_STRLEN; ++i) |
| 157 | printf(", \"%s\"", a[i]); |
| 158 | #ifdef VERBOSE_EXECVEAT |
| 159 | printf("], [\"%s\"", b[1]); |
| 160 | for (i = 2; i <= DEFAULT_STRLEN; ++i) |
| 161 | printf(", \"%s\"", b[i]); |
| 162 | #else |
| 163 | printf("], [/* %d vars */", DEFAULT_STRLEN); |
| 164 | #endif |
| 165 | printf("], AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n", |
Dmitry V. Levin | 9f6611b | 2016-04-21 17:49:32 +0000 | [diff] [blame] | 166 | errno2name()); |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 167 | |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 168 | syscall(__NR_execveat, -100, FILENAME, NULL, efault, 0x1100); |
| 169 | printf("execveat(AT_FDCWD, \"%s\", NULL, %p" |
| 170 | ", AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n", |
Dmitry V. Levin | 9f6611b | 2016-04-21 17:49:32 +0000 | [diff] [blame] | 171 | Q_FILENAME, efault, errno2name()); |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 172 | |
| 173 | syscall(__NR_execveat, -100, FILENAME, efault, NULL, 0x1100); |
| 174 | printf("execveat(AT_FDCWD, \"%s\", %p, NULL" |
| 175 | ", AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n", |
Dmitry V. Levin | 9f6611b | 2016-04-21 17:49:32 +0000 | [diff] [blame] | 176 | Q_FILENAME, efault, errno2name()); |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 177 | |
| 178 | puts("+++ exited with 0 +++"); |
Dmitry V. Levin | 7c22101 | 2015-07-26 11:06:53 +0000 | [diff] [blame] | 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | #else |
| 183 | |
Dmitry V. Levin | 71ac6ff | 2016-01-04 23:40:24 +0000 | [diff] [blame] | 184 | SKIP_MAIN_UNDEFINED("__NR_execveat") |
Dmitry V. Levin | 7c22101 | 2015-07-26 11:06:53 +0000 | [diff] [blame] | 185 | |
| 186 | #endif |