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