1 /* 2 * Check decoding of sockname family syscalls. 3 * 4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org> 5 * Copyright (c) 2016-2018 The strace developers. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "tests.h" 32 #include <stddef.h> 33 #include <stdio.h> 34 #include <string.h> 35 #include <signal.h> 36 #include <unistd.h> 37 #include <sys/wait.h> 38 #include <sys/socket.h> 39 #include <sys/un.h> 40 41 #ifndef TEST_SYSCALL_NAME 42 # error TEST_SYSCALL_NAME must be defined 43 #endif 44 45 #ifndef TEST_SYSCALL_STR 46 # define TEST_SYSCALL_STR STRINGIFY_VAL(TEST_SYSCALL_NAME) 47 #endif 48 #define TEST_SOCKET TEST_SYSCALL_STR ".socket" 49 50 #ifdef TEST_SYSCALL_PREPARE 51 # define PREPARE_TEST_SYSCALL_INVOCATION do { TEST_SYSCALL_PREPARE; } while (0) 52 #else 53 # define PREPARE_TEST_SYSCALL_INVOCATION do {} while (0) 54 #endif 55 56 #ifndef PREFIX_S_ARGS 57 # define PREFIX_S_ARGS 58 #endif 59 #ifndef PREFIX_F_ARGS 60 # define PREFIX_F_ARGS 61 #endif 62 #ifndef PREFIX_S_STR 63 # define PREFIX_S_STR "" 64 #endif 65 #ifndef PREFIX_F_STR 66 # define PREFIX_F_STR "" 67 #endif 68 #ifndef SUFFIX_ARGS 69 # define SUFFIX_ARGS 70 #endif 71 #ifndef SUFFIX_STR 72 # define SUFFIX_STR "" 73 #endif 74 75 static void 76 test_sockname_syscall(const int fd) 77 { 78 TAIL_ALLOC_OBJECT_CONST_PTR(socklen_t, plen); 79 *plen = sizeof(struct sockaddr_un); 80 struct sockaddr_un *addr = tail_alloc(*plen); 81 82 PREPARE_TEST_SYSCALL_INVOCATION; 83 int rc = TEST_SYSCALL_NAME(fd PREFIX_S_ARGS, (void *) addr, 84 plen SUFFIX_ARGS); 85 if (rc < 0) 86 perror_msg_and_skip(TEST_SYSCALL_STR); 87 printf("%s(%d%s, {sa_family=AF_UNIX, sun_path=\"%s\"}" 88 ", [%d->%d]%s) = %d\n", 89 TEST_SYSCALL_STR, fd, PREFIX_S_STR, addr->sun_path, 90 (int) sizeof(struct sockaddr_un), (int) *plen, SUFFIX_STR, rc); 91 92 memset(addr, 0, sizeof(*addr)); 93 PREPARE_TEST_SYSCALL_INVOCATION; 94 rc = TEST_SYSCALL_NAME(fd PREFIX_S_ARGS, (void *) addr, 95 plen SUFFIX_ARGS); 96 if (rc < 0) 97 perror_msg_and_skip(TEST_SYSCALL_STR); 98 printf("%s(%d%s, {sa_family=AF_UNIX, sun_path=\"%s\"}" 99 ", [%d]%s) = %d\n", 100 TEST_SYSCALL_STR, fd, PREFIX_S_STR, addr->sun_path, 101 (int) *plen, SUFFIX_STR, rc); 102 103 PREPARE_TEST_SYSCALL_INVOCATION; 104 rc = TEST_SYSCALL_NAME(fd PREFIX_F_ARGS, (void *) addr, 0 SUFFIX_ARGS); 105 printf("%s(%d%s, %p, NULL%s) = %s\n", 106 TEST_SYSCALL_STR, fd, PREFIX_F_STR, addr, SUFFIX_STR, 107 sprintrc(rc)); 108 109 PREPARE_TEST_SYSCALL_INVOCATION; 110 rc = TEST_SYSCALL_NAME(fd PREFIX_S_ARGS, 0, 0 SUFFIX_ARGS); 111 printf("%s(%d%s, NULL, NULL%s) = %s\n", 112 TEST_SYSCALL_STR, fd, rc == -1 ? PREFIX_F_STR : PREFIX_S_STR, 113 SUFFIX_STR, sprintrc(rc)); 114 115 PREPARE_TEST_SYSCALL_INVOCATION; 116 rc = TEST_SYSCALL_NAME(fd PREFIX_F_ARGS, (void *) addr, 117 plen + 1 SUFFIX_ARGS); 118 printf("%s(%d%s, %p, %p%s) = %s\n", 119 TEST_SYSCALL_STR, fd, PREFIX_F_STR, addr, 120 plen + 1, SUFFIX_STR, sprintrc(rc)); 121 122 const size_t offsetof_sun_path = offsetof(struct sockaddr_un, sun_path); 123 *plen = offsetof_sun_path; 124 memset(addr->sun_path, 'A', sizeof(addr->sun_path)); 125 126 PREPARE_TEST_SYSCALL_INVOCATION; 127 rc = TEST_SYSCALL_NAME(fd PREFIX_S_ARGS, (void *) addr, 128 plen SUFFIX_ARGS); 129 if (rc < 0) 130 perror_msg_and_skip(TEST_SYSCALL_STR); 131 printf("%s(%d%s, {sa_family=AF_UNIX}, [%d->%d]%s) = %d\n", 132 TEST_SYSCALL_STR, fd, PREFIX_S_STR, 133 (int) offsetof_sun_path, (int) *plen, SUFFIX_STR, rc); 134 135 ++addr; 136 *plen = sizeof(struct sockaddr); 137 addr = (void *) addr - *plen; 138 139 PREPARE_TEST_SYSCALL_INVOCATION; 140 rc = TEST_SYSCALL_NAME(fd PREFIX_S_ARGS, (void *) addr, 141 plen SUFFIX_ARGS); 142 if (rc < 0) 143 perror_msg_and_skip(TEST_SYSCALL_STR); 144 printf("%s(%d%s, {sa_family=AF_UNIX, sun_path=\"%.*s\"}" 145 ", [%d->%d]%s) = %d\n", 146 TEST_SYSCALL_STR, fd, PREFIX_S_STR, 147 (int) (sizeof(struct sockaddr) - offsetof_sun_path), 148 addr->sun_path, (int) sizeof(struct sockaddr), 149 (int) *plen, SUFFIX_STR, rc); 150 151 PREPARE_TEST_SYSCALL_INVOCATION; 152 rc = TEST_SYSCALL_NAME(fd PREFIX_F_ARGS, (void *) addr, 153 plen SUFFIX_ARGS); 154 printf("%s(%d%s, %p, [%d]%s) = %s\n", 155 TEST_SYSCALL_STR, fd, PREFIX_F_STR, addr, 156 *plen, SUFFIX_STR, sprintrc(rc)); 157 } 158