summaryrefslogtreecommitdiffstats
path: root/fuzz/preload-snoop.c
blob: 34d57ade82f9e009e35abb27207e41b4adb87676 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Copyright (c) 2019 Yubico AB. All rights reserved.
 * Use of this source code is governed by a BSD-style
 * license that can be found in the LICENSE file.
 * SPDX-License-Identifier: BSD-2-Clause
 */

/*
 * cc -fPIC -D_GNU_SOURCE -shared -o preload-snoop.so preload-snoop.c
 * LD_PRELOAD=$(realpath preload-snoop.so)
 */

#include <sys/types.h>
#include <sys/stat.h>

#include <dlfcn.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define SNOOP_DEV_PREFIX	"/dev/hidraw"

struct fd_tuple {
	int snoop_in;
	int snoop_out;
	int real_dev;
};

static struct fd_tuple  *fd_tuple;
static int              (*open_f)(const char *, int, mode_t);
static int              (*close_f)(int);
static ssize_t          (*read_f)(int, void *, size_t);
static ssize_t          (*write_f)(int, const void *, size_t);

static int
get_fd(const char *hid_path, const char *suffix)
{
	char	*s = NULL;
	char	 path[PATH_MAX];
	int	 fd;
	int	 r;

	if ((s = strdup(hid_path)) == NULL) {
		warnx("%s: strdup", __func__);
		return (-1);
	}

	for (size_t i = 0; i < strlen(s); i++)
		if (s[i] == '/')
			s[i] = '_';

	if ((r = snprintf(path, sizeof(path), "%s-%s", s, suffix)) < 0 ||
	    (size_t)r >= sizeof(path)) {
		warnx("%s: snprintf", __func__);
		free(s);
		return (-1);
	}

	free(s);
	s = NULL;

	if ((fd = open_f(path, O_CREAT | O_WRONLY, 0644)) < 0) {
		warn("%s: open", __func__);
		return (-1);
	}

	return (fd);
}

int
open(const char *path, int flags, ...)
{
	va_list	ap;
	mode_t	mode;

	va_start(ap, flags);
	mode = va_arg(ap, mode_t);
	va_end(ap);

	if (open_f == NULL) {
		open_f = dlsym(RTLD_NEXT, "open");
		if (open_f == NULL) {
			warnx("%s: dlsym", __func__);
			errno = EACCES;
			return (-1);
		}
	}

	if (strncmp(path, SNOOP_DEV_PREFIX, strlen(SNOOP_DEV_PREFIX)) != 0)
		return (open_f(path, flags, mode));

	if (fd_tuple != NULL) {
		warnx("%s: fd_tuple != NULL", __func__);
		errno = EACCES;
		return (-1);
	}

	if ((fd_tuple = calloc(1, sizeof(*fd_tuple))) == NULL) {
		warn("%s: calloc", __func__);
		errno = ENOMEM;
		return (-1);
	}

	fd_tuple->snoop_in = -1;
	fd_tuple->snoop_out = -1;
	fd_tuple->real_dev = -1;

	if ((fd_tuple->snoop_in = get_fd(path, "in")) < 0 ||
	    (fd_tuple->snoop_out = get_fd(path, "out")) < 0 ||
	    (fd_tuple->real_dev = open_f(path, flags, mode)) < 0) {
		warn("%s: get_fd/open", __func__);
		goto fail;
	}

	return (fd_tuple->real_dev);
fail:
	if (fd_tuple->snoop_in != -1)
		close(fd_tuple->snoop_in);
	if (fd_tuple->snoop_out != -1)
		close(fd_tuple->snoop_out);
	if (fd_tuple->real_dev != -1)
		close(fd_tuple->real_dev);

	free(fd_tuple);
	fd_tuple = NULL;

	errno = EACCES;

	return (-1);
}

int
close(int fd)
{
	if (close_f == NULL) {
		close_f = dlsym(RTLD_NEXT, "close");
		if (close_f == NULL) {
			warnx("%s: dlsym", __func__);
			errno = EBADF;
			return (-1);
		}
	}

	if (fd_tuple == NULL || fd_tuple->real_dev != fd)
		return (close_f(fd));

	close_f(fd_tuple->snoop_in);
	close_f(fd_tuple->snoop_out);
	close_f(fd_tuple->real_dev);

	free(fd_tuple);
	fd_tuple = NULL;

	return (0);
}

ssize_t
read(int fd, void *buf, size_t nbytes)
{
	ssize_t n;

	if (read_f == NULL) {
		read_f = dlsym(RTLD_NEXT, "read");
		if (read_f == NULL) {
			warnx("%s: dlsym", __func__);
			errno = EBADF;
			return (-1);
		}
	}

	if (write_f == NULL) {
		write_f = dlsym(RTLD_NEXT, "write");
		if (write_f == NULL) {
			warnx("%s: dlsym", __func__);
			errno = EBADF;
			return (-1);
		}
	}

	if (fd_tuple == NULL || fd_tuple->real_dev != fd)
		return (read_f(fd, buf, nbytes));

	if ((n = read_f(fd, buf, nbytes)) < 0 ||
	    write_f(fd_tuple->snoop_in, buf, n) != n)
		return (-1);

	return (n);
}

ssize_t
write(int fd, const void *buf, size_t nbytes)
{
	ssize_t n;

	if (write_f == NULL) {
		write_f = dlsym(RTLD_NEXT, "write");
		if (write_f == NULL) {
			warnx("%s: dlsym", __func__);
			errno = EBADF;
			return (-1);
		}
	}

	if (fd_tuple == NULL || fd_tuple->real_dev != fd)
		return (write_f(fd, buf, nbytes));

	if ((n = write_f(fd, buf, nbytes)) < 0 ||
	    write_f(fd_tuple->snoop_out, buf, n) != n)
		return (-1);

	return (n);
}