Home | History | Annotate | Line # | Download | only in fuzz
      1      1.1  christos /*
      2      1.1  christos  * Copyright (c) 2019 Yubico AB. All rights reserved.
      3      1.1  christos  * Use of this source code is governed by a BSD-style
      4      1.1  christos  * license that can be found in the LICENSE file.
      5  1.1.1.2  christos  * SPDX-License-Identifier: BSD-2-Clause
      6      1.1  christos  */
      7      1.1  christos 
      8      1.1  christos /*
      9      1.1  christos  * cc -fPIC -D_GNU_SOURCE -shared -o preload-snoop.so preload-snoop.c
     10      1.1  christos  * LD_PRELOAD=$(realpath preload-snoop.so)
     11      1.1  christos  */
     12      1.1  christos 
     13      1.1  christos #include <sys/types.h>
     14      1.1  christos #include <sys/stat.h>
     15      1.1  christos 
     16      1.1  christos #include <dlfcn.h>
     17      1.1  christos #include <err.h>
     18      1.1  christos #include <errno.h>
     19      1.1  christos #include <fcntl.h>
     20      1.1  christos #include <limits.h>
     21      1.1  christos #include <stdarg.h>
     22      1.1  christos #include <stdio.h>
     23      1.1  christos #include <stdlib.h>
     24      1.1  christos #include <string.h>
     25      1.1  christos #include <unistd.h>
     26      1.1  christos 
     27      1.1  christos #define SNOOP_DEV_PREFIX	"/dev/hidraw"
     28      1.1  christos 
     29      1.1  christos struct fd_tuple {
     30      1.1  christos 	int snoop_in;
     31      1.1  christos 	int snoop_out;
     32      1.1  christos 	int real_dev;
     33      1.1  christos };
     34      1.1  christos 
     35      1.1  christos static struct fd_tuple  *fd_tuple;
     36      1.1  christos static int              (*open_f)(const char *, int, mode_t);
     37      1.1  christos static int              (*close_f)(int);
     38      1.1  christos static ssize_t          (*read_f)(int, void *, size_t);
     39      1.1  christos static ssize_t          (*write_f)(int, const void *, size_t);
     40      1.1  christos 
     41      1.1  christos static int
     42      1.1  christos get_fd(const char *hid_path, const char *suffix)
     43      1.1  christos {
     44      1.1  christos 	char	*s = NULL;
     45      1.1  christos 	char	 path[PATH_MAX];
     46      1.1  christos 	int	 fd;
     47      1.1  christos 	int	 r;
     48      1.1  christos 
     49      1.1  christos 	if ((s = strdup(hid_path)) == NULL) {
     50      1.1  christos 		warnx("%s: strdup", __func__);
     51      1.1  christos 		return (-1);
     52      1.1  christos 	}
     53      1.1  christos 
     54      1.1  christos 	for (size_t i = 0; i < strlen(s); i++)
     55      1.1  christos 		if (s[i] == '/')
     56      1.1  christos 			s[i] = '_';
     57      1.1  christos 
     58      1.1  christos 	if ((r = snprintf(path, sizeof(path), "%s-%s", s, suffix)) < 0 ||
     59      1.1  christos 	    (size_t)r >= sizeof(path)) {
     60      1.1  christos 		warnx("%s: snprintf", __func__);
     61      1.1  christos 		free(s);
     62      1.1  christos 		return (-1);
     63      1.1  christos 	}
     64      1.1  christos 
     65      1.1  christos 	free(s);
     66      1.1  christos 	s = NULL;
     67      1.1  christos 
     68      1.1  christos 	if ((fd = open_f(path, O_CREAT | O_WRONLY, 0644)) < 0) {
     69      1.1  christos 		warn("%s: open", __func__);
     70      1.1  christos 		return (-1);
     71      1.1  christos 	}
     72      1.1  christos 
     73      1.1  christos 	return (fd);
     74      1.1  christos }
     75      1.1  christos 
     76      1.1  christos int
     77      1.1  christos open(const char *path, int flags, ...)
     78      1.1  christos {
     79      1.1  christos 	va_list	ap;
     80      1.1  christos 	mode_t	mode;
     81      1.1  christos 
     82      1.1  christos 	va_start(ap, flags);
     83      1.1  christos 	mode = va_arg(ap, mode_t);
     84      1.1  christos 	va_end(ap);
     85      1.1  christos 
     86      1.1  christos 	if (open_f == NULL) {
     87      1.1  christos 		open_f = dlsym(RTLD_NEXT, "open");
     88      1.1  christos 		if (open_f == NULL) {
     89      1.1  christos 			warnx("%s: dlsym", __func__);
     90      1.1  christos 			errno = EACCES;
     91      1.1  christos 			return (-1);
     92      1.1  christos 		}
     93      1.1  christos 	}
     94      1.1  christos 
     95      1.1  christos 	if (strncmp(path, SNOOP_DEV_PREFIX, strlen(SNOOP_DEV_PREFIX)) != 0)
     96      1.1  christos 		return (open_f(path, flags, mode));
     97      1.1  christos 
     98      1.1  christos 	if (fd_tuple != NULL) {
     99      1.1  christos 		warnx("%s: fd_tuple != NULL", __func__);
    100      1.1  christos 		errno = EACCES;
    101      1.1  christos 		return (-1);
    102      1.1  christos 	}
    103      1.1  christos 
    104      1.1  christos 	if ((fd_tuple = calloc(1, sizeof(*fd_tuple))) == NULL) {
    105      1.1  christos 		warn("%s: calloc", __func__);
    106      1.1  christos 		errno = ENOMEM;
    107      1.1  christos 		return (-1);
    108      1.1  christos 	}
    109      1.1  christos 
    110      1.1  christos 	fd_tuple->snoop_in = -1;
    111      1.1  christos 	fd_tuple->snoop_out = -1;
    112      1.1  christos 	fd_tuple->real_dev = -1;
    113      1.1  christos 
    114      1.1  christos 	if ((fd_tuple->snoop_in = get_fd(path, "in")) < 0 ||
    115      1.1  christos 	    (fd_tuple->snoop_out = get_fd(path, "out")) < 0 ||
    116      1.1  christos 	    (fd_tuple->real_dev = open_f(path, flags, mode)) < 0) {
    117      1.1  christos 		warn("%s: get_fd/open", __func__);
    118      1.1  christos 		goto fail;
    119      1.1  christos 	}
    120      1.1  christos 
    121      1.1  christos 	return (fd_tuple->real_dev);
    122      1.1  christos fail:
    123      1.1  christos 	if (fd_tuple->snoop_in != -1)
    124      1.1  christos 		close(fd_tuple->snoop_in);
    125      1.1  christos 	if (fd_tuple->snoop_out != -1)
    126      1.1  christos 		close(fd_tuple->snoop_out);
    127      1.1  christos 	if (fd_tuple->real_dev != -1)
    128      1.1  christos 		close(fd_tuple->real_dev);
    129      1.1  christos 
    130      1.1  christos 	free(fd_tuple);
    131      1.1  christos 	fd_tuple = NULL;
    132      1.1  christos 
    133      1.1  christos 	errno = EACCES;
    134      1.1  christos 
    135      1.1  christos 	return (-1);
    136      1.1  christos }
    137      1.1  christos 
    138      1.1  christos int
    139      1.1  christos close(int fd)
    140      1.1  christos {
    141      1.1  christos 	if (close_f == NULL) {
    142      1.1  christos 		close_f = dlsym(RTLD_NEXT, "close");
    143      1.1  christos 		if (close_f == NULL) {
    144      1.1  christos 			warnx("%s: dlsym", __func__);
    145      1.1  christos 			errno = EBADF;
    146      1.1  christos 			return (-1);
    147      1.1  christos 		}
    148      1.1  christos 	}
    149      1.1  christos 
    150      1.1  christos 	if (fd_tuple == NULL || fd_tuple->real_dev != fd)
    151      1.1  christos 		return (close_f(fd));
    152      1.1  christos 
    153      1.1  christos 	close_f(fd_tuple->snoop_in);
    154      1.1  christos 	close_f(fd_tuple->snoop_out);
    155      1.1  christos 	close_f(fd_tuple->real_dev);
    156      1.1  christos 
    157      1.1  christos 	free(fd_tuple);
    158      1.1  christos 	fd_tuple = NULL;
    159      1.1  christos 
    160      1.1  christos 	return (0);
    161      1.1  christos }
    162      1.1  christos 
    163      1.1  christos ssize_t
    164      1.1  christos read(int fd, void *buf, size_t nbytes)
    165      1.1  christos {
    166      1.1  christos 	ssize_t n;
    167      1.1  christos 
    168      1.1  christos 	if (read_f == NULL) {
    169      1.1  christos 		read_f = dlsym(RTLD_NEXT, "read");
    170      1.1  christos 		if (read_f == NULL) {
    171      1.1  christos 			warnx("%s: dlsym", __func__);
    172      1.1  christos 			errno = EBADF;
    173      1.1  christos 			return (-1);
    174      1.1  christos 		}
    175      1.1  christos 	}
    176      1.1  christos 
    177      1.1  christos 	if (write_f == NULL) {
    178      1.1  christos 		write_f = dlsym(RTLD_NEXT, "write");
    179      1.1  christos 		if (write_f == NULL) {
    180      1.1  christos 			warnx("%s: dlsym", __func__);
    181      1.1  christos 			errno = EBADF;
    182      1.1  christos 			return (-1);
    183      1.1  christos 		}
    184      1.1  christos 	}
    185      1.1  christos 
    186      1.1  christos 	if (fd_tuple == NULL || fd_tuple->real_dev != fd)
    187      1.1  christos 		return (read_f(fd, buf, nbytes));
    188      1.1  christos 
    189      1.1  christos 	if ((n = read_f(fd, buf, nbytes)) < 0 ||
    190      1.1  christos 	    write_f(fd_tuple->snoop_in, buf, n) != n)
    191      1.1  christos 		return (-1);
    192      1.1  christos 
    193      1.1  christos 	return (n);
    194      1.1  christos }
    195      1.1  christos 
    196      1.1  christos ssize_t
    197      1.1  christos write(int fd, const void *buf, size_t nbytes)
    198      1.1  christos {
    199      1.1  christos 	ssize_t n;
    200      1.1  christos 
    201      1.1  christos 	if (write_f == NULL) {
    202      1.1  christos 		write_f = dlsym(RTLD_NEXT, "write");
    203      1.1  christos 		if (write_f == NULL) {
    204      1.1  christos 			warnx("%s: dlsym", __func__);
    205      1.1  christos 			errno = EBADF;
    206      1.1  christos 			return (-1);
    207      1.1  christos 		}
    208      1.1  christos 	}
    209      1.1  christos 
    210      1.1  christos 	if (fd_tuple == NULL || fd_tuple->real_dev != fd)
    211      1.1  christos 		return (write_f(fd, buf, nbytes));
    212      1.1  christos 
    213      1.1  christos 	if ((n = write_f(fd, buf, nbytes)) < 0 ||
    214      1.1  christos 	    write_f(fd_tuple->snoop_out, buf, n) != n)
    215      1.1  christos 		return (-1);
    216      1.1  christos 
    217      1.1  christos 	return (n);
    218      1.1  christos }
    219