1 /*
2 * Check decoding of sockname family syscalls.
3 *
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@strace.io>
5 * Copyright (c) 2016-2022 The strace developers.
6 * All rights reserved.
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11 #include "tests.h"
12 #include <stddef.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <signal.h>
16 #include <unistd.h>
17 #include <sys/wait.h>
18 #include <sys/socket.h>
19 #include <sys/un.h>
20
21 #include "secontext.h"
22
23 #ifndef TEST_SYSCALL_NAME
24 # error TEST_SYSCALL_NAME must be defined
25 #endif
26
27 #ifndef TEST_SYSCALL_STR
28 # define TEST_SYSCALL_STR STRINGIFY_VAL(TEST_SYSCALL_NAME)
29 #endif
30 #define TEST_SOCKET TEST_SYSCALL_STR ".socket"
31
32 #ifdef TEST_SYSCALL_PREPARE
33 # define PREPARE_TEST_SYSCALL_INVOCATION do { TEST_SYSCALL_PREPARE; } while (0)
34 #else
35 # define PREPARE_TEST_SYSCALL_INVOCATION do {} while (0)
36 #endif
37
38 #ifndef PREFIX_S_ARGS
39 # define PREFIX_S_ARGS
40 #endif
41 #ifndef PREFIX_F_ARGS
42 # define PREFIX_F_ARGS
43 #endif
44 #ifndef PREFIX_S_STR
45 # define PREFIX_S_STR ""
46 #endif
47 #ifndef PREFIX_F_STR
48 # define PREFIX_F_STR ""
49 #endif
50 #ifndef SUFFIX_ARGS
51 # define SUFFIX_ARGS
52 #endif
53 #ifndef SUFFIX_STR
54 # define SUFFIX_STR ""
55 #endif
56
57 static void
58 test_sockname_syscall(const int fd)
59 {
60 TAIL_ALLOC_OBJECT_CONST_PTR(socklen_t, plen);
61 *plen = sizeof(struct sockaddr_un);
62 struct sockaddr_un *addr = tail_alloc(*plen);
63
64 char *my_secontext = SECONTEXT_PID_MY();
65 char *fd_secontext = SECONTEXT_FD(fd);
66
67 PREPARE_TEST_SYSCALL_INVOCATION;
68 int rc = TEST_SYSCALL_NAME(fd PREFIX_S_ARGS, (void *) addr,
69 plen SUFFIX_ARGS);
70 if (rc < 0)
71 perror_msg_and_skip(TEST_SYSCALL_STR);
72 printf("%s%s(%d%s%s, {sa_family=AF_UNIX, sun_path=\"%s\"%s}"
73 ", [%d => %d]%s) = %d\n",
74 my_secontext,
75 TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_S_STR,
76 addr->sun_path, SECONTEXT_FILE(addr->sun_path),
77 (int) sizeof(struct sockaddr_un), (int) *plen, SUFFIX_STR, rc);
78
79 memset(addr, 0, sizeof(*addr));
80 PREPARE_TEST_SYSCALL_INVOCATION;
81 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%s(%d%s%s, {sa_family=AF_UNIX, sun_path=\"%s\"%s}"
86 ", [%d]%s) = %d\n",
87 my_secontext,
88 TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_S_STR,
89 addr->sun_path, SECONTEXT_FILE(addr->sun_path),
90 (int) *plen, SUFFIX_STR, rc);
91
92 PREPARE_TEST_SYSCALL_INVOCATION;
93 rc = TEST_SYSCALL_NAME(fd PREFIX_F_ARGS, (void *) addr, 0 SUFFIX_ARGS);
94 printf("%s%s(%d%s%s, %p, NULL%s) = %s\n",
95 my_secontext,
96 TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_F_STR,
97 addr, SUFFIX_STR, sprintrc(rc));
98
99 PREPARE_TEST_SYSCALL_INVOCATION;
100 rc = TEST_SYSCALL_NAME(fd PREFIX_S_ARGS, 0, 0 SUFFIX_ARGS);
101 printf("%s%s(%d%s%s, NULL, NULL%s) = %s\n",
102 my_secontext,
103 TEST_SYSCALL_STR, fd, fd_secontext,
104 rc == -1 ? PREFIX_F_STR : PREFIX_S_STR,
105 SUFFIX_STR, sprintrc(rc));
106
107 PREPARE_TEST_SYSCALL_INVOCATION;
108 rc = TEST_SYSCALL_NAME(fd PREFIX_F_ARGS, (void *) addr,
109 plen + 1 SUFFIX_ARGS);
110 printf("%s%s(%d%s%s, %p, %p%s) = %s\n",
111 my_secontext,
112 TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_F_STR, addr,
113 plen + 1, SUFFIX_STR, sprintrc(rc));
114
115 const size_t offsetof_sun_path = offsetof(struct sockaddr_un, sun_path);
116 *plen = offsetof_sun_path;
117 memset(addr->sun_path, 'A', sizeof(addr->sun_path));
118
119 PREPARE_TEST_SYSCALL_INVOCATION;
120 rc = TEST_SYSCALL_NAME(fd PREFIX_S_ARGS, (void *) addr,
121 plen SUFFIX_ARGS);
122 if (rc < 0)
123 perror_msg_and_skip(TEST_SYSCALL_STR);
124 printf("%s%s(%d%s%s, {sa_family=AF_UNIX}, [%d => %d]%s) = %d\n",
125 my_secontext,
126 TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_S_STR,
127 (int) offsetof_sun_path, (int) *plen, SUFFIX_STR, rc);
128
129 ++addr;
130 *plen = sizeof(struct sockaddr);
131 addr = (void *) addr - *plen;
132
133 PREPARE_TEST_SYSCALL_INVOCATION;
134 rc = TEST_SYSCALL_NAME(fd PREFIX_S_ARGS, (void *) addr,
135 plen SUFFIX_ARGS);
136 if (rc < 0)
137 perror_msg_and_skip(TEST_SYSCALL_STR);
138 printf("%s%s(%d%s%s, {sa_family=AF_UNIX, sun_path=\"%.*s\"%s}"
139 ", [%d => %d]%s) = %d\n",
140 my_secontext,
141 TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_S_STR,
142 (int) (sizeof(struct sockaddr) - offsetof_sun_path),
143 addr->sun_path, SECONTEXT_FILE(addr->sun_path),
144 (int) sizeof(struct sockaddr), (int) *plen, SUFFIX_STR, rc);
145
146 PREPARE_TEST_SYSCALL_INVOCATION;
147 rc = TEST_SYSCALL_NAME(fd PREFIX_F_ARGS, (void *) addr,
148 plen SUFFIX_ARGS);
149 printf("%s%s(%d%s%s, %p, [%d]%s) = %s\n",
150 my_secontext,
151 TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_F_STR, addr,
152 *plen, SUFFIX_STR, sprintrc(rc));
153 }