1 /*
2 * Check decoding of sockname family syscalls.
3 *
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5 * Copyright (c) 2016-2017 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 #define TEST_SYSCALL_STR STRINGIFY_VAL(TEST_SYSCALL_NAME)
46 #define TEST_SOCKET TEST_SYSCALL_STR ".socket"
47
48 #ifdef TEST_SYSCALL_PREPARE
49 # define PREPARE_TEST_SYSCALL_INVOCATION do { TEST_SYSCALL_PREPARE; } while (0)
50 #else
51 # define PREPARE_TEST_SYSCALL_INVOCATION do {} while (0)
52 #endif
53
54 #ifndef PREFIX_S_ARGS
55 # define PREFIX_S_ARGS
56 #endif
57 #ifndef PREFIX_F_ARGS
58 # define PREFIX_F_ARGS
59 #endif
60 #ifndef PREFIX_S_STR
61 # define PREFIX_S_STR ""
62 #endif
63 #ifndef PREFIX_F_STR
64 # define PREFIX_F_STR ""
65 #endif
66 #ifndef SUFFIX_ARGS
67 # define SUFFIX_ARGS
68 #endif
69 #ifndef SUFFIX_STR
70 # define SUFFIX_STR ""
71 #endif
72
73 static void
test_sockname_syscall(const int fd)74 test_sockname_syscall(const int fd)
75 {
76 TAIL_ALLOC_OBJECT_CONST_PTR(socklen_t, plen);
77 *plen = sizeof(struct sockaddr_un);
78 struct sockaddr_un *addr = tail_alloc(*plen);
79
80 PREPARE_TEST_SYSCALL_INVOCATION;
81 int rc = TEST_SYSCALL_NAME(fd PREFIX_S_ARGS, (void *) addr,
82 plen SUFFIX_ARGS);
83 if (rc < 0)
84 perror_msg_and_skip(TEST_SYSCALL_STR);
85 printf("%s(%d%s, {sa_family=AF_UNIX, sun_path=\"%s\"}"
86 ", [%d->%d]%s) = %d\n",
87 TEST_SYSCALL_STR, fd, PREFIX_S_STR, addr->sun_path,
88 (int) sizeof(struct sockaddr_un), (int) *plen, SUFFIX_STR, rc);
89
90 memset(addr, 0, sizeof(*addr));
91 PREPARE_TEST_SYSCALL_INVOCATION;
92 rc = TEST_SYSCALL_NAME(fd PREFIX_S_ARGS, (void *) addr,
93 plen SUFFIX_ARGS);
94 if (rc < 0)
95 perror_msg_and_skip(TEST_SYSCALL_STR);
96 printf("%s(%d%s, {sa_family=AF_UNIX, sun_path=\"%s\"}"
97 ", [%d]%s) = %d\n",
98 TEST_SYSCALL_STR, fd, PREFIX_S_STR, addr->sun_path,
99 (int) *plen, SUFFIX_STR, rc);
100
101 PREPARE_TEST_SYSCALL_INVOCATION;
102 rc = TEST_SYSCALL_NAME(fd PREFIX_F_ARGS, (void *) addr, 0 SUFFIX_ARGS);
103 printf("%s(%d%s, %p, NULL%s) = %s\n",
104 TEST_SYSCALL_STR, fd, PREFIX_F_STR, addr, SUFFIX_STR,
105 sprintrc(rc));
106
107 PREPARE_TEST_SYSCALL_INVOCATION;
108 rc = TEST_SYSCALL_NAME(fd PREFIX_S_ARGS, 0, 0 SUFFIX_ARGS);
109 printf("%s(%d%s, NULL, NULL%s) = %s\n",
110 TEST_SYSCALL_STR, fd, rc == -1 ? PREFIX_F_STR : PREFIX_S_STR,
111 SUFFIX_STR, sprintrc(rc));
112
113 PREPARE_TEST_SYSCALL_INVOCATION;
114 rc = TEST_SYSCALL_NAME(fd PREFIX_F_ARGS, (void *) addr,
115 plen + 1 SUFFIX_ARGS);
116 printf("%s(%d%s, %p, %p%s) = %s\n",
117 TEST_SYSCALL_STR, fd, PREFIX_F_STR, addr,
118 plen + 1, SUFFIX_STR, sprintrc(rc));
119
120 const size_t offsetof_sun_path = offsetof(struct sockaddr_un, sun_path);
121 *plen = offsetof_sun_path;
122 memset(addr->sun_path, 'A', sizeof(addr->sun_path));
123
124 PREPARE_TEST_SYSCALL_INVOCATION;
125 rc = TEST_SYSCALL_NAME(fd PREFIX_S_ARGS, (void *) addr,
126 plen SUFFIX_ARGS);
127 if (rc < 0)
128 perror_msg_and_skip(TEST_SYSCALL_STR);
129 printf("%s(%d%s, {sa_family=AF_UNIX}, [%d->%d]%s) = %d\n",
130 TEST_SYSCALL_STR, fd, PREFIX_S_STR,
131 (int) offsetof_sun_path, (int) *plen, SUFFIX_STR, rc);
132
133 ++addr;
134 *plen = sizeof(struct sockaddr);
135 addr = (void *) addr - *plen;
136
137 PREPARE_TEST_SYSCALL_INVOCATION;
138 rc = TEST_SYSCALL_NAME(fd PREFIX_S_ARGS, (void *) addr,
139 plen SUFFIX_ARGS);
140 if (rc < 0)
141 perror_msg_and_skip(TEST_SYSCALL_STR);
142 printf("%s(%d%s, {sa_family=AF_UNIX, sun_path=\"%.*s\"}"
143 ", [%d->%d]%s) = %d\n",
144 TEST_SYSCALL_STR, fd, PREFIX_S_STR,
145 (int) (sizeof(struct sockaddr) - offsetof_sun_path),
146 addr->sun_path, (int) sizeof(struct sockaddr),
147 (int) *plen, SUFFIX_STR, rc);
148
149 PREPARE_TEST_SYSCALL_INVOCATION;
150 rc = TEST_SYSCALL_NAME(fd PREFIX_F_ARGS, (void *) addr,
151 plen SUFFIX_ARGS);
152 printf("%s(%d%s, %p, [%d]%s) = %s\n",
153 TEST_SYSCALL_STR, fd, PREFIX_F_STR, addr,
154 *plen, SUFFIX_STR, sprintrc(rc));
155 }
156