1*08b48e0bSAndroid Build Coastguard Worker /*
2*08b48e0bSAndroid Build Coastguard Worker * This is desock_dup.c from the amazing preeny project
3*08b48e0bSAndroid Build Coastguard Worker * https://github.com/zardus/preeny
4*08b48e0bSAndroid Build Coastguard Worker *
5*08b48e0bSAndroid Build Coastguard Worker * It is packaged in afl++ to have it at hand if needed
6*08b48e0bSAndroid Build Coastguard Worker *
7*08b48e0bSAndroid Build Coastguard Worker */
8*08b48e0bSAndroid Build Coastguard Worker
9*08b48e0bSAndroid Build Coastguard Worker #define _GNU_SOURCE
10*08b48e0bSAndroid Build Coastguard Worker
11*08b48e0bSAndroid Build Coastguard Worker #include <stdio.h>
12*08b48e0bSAndroid Build Coastguard Worker #include <stdlib.h>
13*08b48e0bSAndroid Build Coastguard Worker #include <unistd.h>
14*08b48e0bSAndroid Build Coastguard Worker #include <string.h>
15*08b48e0bSAndroid Build Coastguard Worker #include <sys/types.h> //
16*08b48e0bSAndroid Build Coastguard Worker #include <sys/socket.h> //
17*08b48e0bSAndroid Build Coastguard Worker #include <sys/stat.h> //
18*08b48e0bSAndroid Build Coastguard Worker #include <fcntl.h> //
19*08b48e0bSAndroid Build Coastguard Worker #include <netinet/in.h>
20*08b48e0bSAndroid Build Coastguard Worker #include <pthread.h>
21*08b48e0bSAndroid Build Coastguard Worker #include <signal.h>
22*08b48e0bSAndroid Build Coastguard Worker #include <dlfcn.h>
23*08b48e0bSAndroid Build Coastguard Worker #include <errno.h>
24*08b48e0bSAndroid Build Coastguard Worker #include <stdio.h>
25*08b48e0bSAndroid Build Coastguard Worker #include <poll.h>
26*08b48e0bSAndroid Build Coastguard Worker // #include "logging.h" // switched from preeny_info() to fprintf(stderr, "Info:
27*08b48e0bSAndroid Build Coastguard Worker // "
28*08b48e0bSAndroid Build Coastguard Worker
29*08b48e0bSAndroid Build Coastguard Worker //
30*08b48e0bSAndroid Build Coastguard Worker // originals
31*08b48e0bSAndroid Build Coastguard Worker //
32*08b48e0bSAndroid Build Coastguard Worker int (*original_close)(int);
33*08b48e0bSAndroid Build Coastguard Worker int (*original_dup2)(int, int);
preeny_desock_dup_orig()34*08b48e0bSAndroid Build Coastguard Worker __attribute__((constructor)) void preeny_desock_dup_orig() {
35*08b48e0bSAndroid Build Coastguard Worker
36*08b48e0bSAndroid Build Coastguard Worker original_close = dlsym(RTLD_NEXT, "close");
37*08b48e0bSAndroid Build Coastguard Worker original_dup2 = dlsym(RTLD_NEXT, "dup2");
38*08b48e0bSAndroid Build Coastguard Worker
39*08b48e0bSAndroid Build Coastguard Worker }
40*08b48e0bSAndroid Build Coastguard Worker
close(int sockfd)41*08b48e0bSAndroid Build Coastguard Worker int close(int sockfd) {
42*08b48e0bSAndroid Build Coastguard Worker
43*08b48e0bSAndroid Build Coastguard Worker if (sockfd <= 2) {
44*08b48e0bSAndroid Build Coastguard Worker
45*08b48e0bSAndroid Build Coastguard Worker fprintf(stderr, "Info: Disabling close on %d\n", sockfd);
46*08b48e0bSAndroid Build Coastguard Worker return 0;
47*08b48e0bSAndroid Build Coastguard Worker
48*08b48e0bSAndroid Build Coastguard Worker } else {
49*08b48e0bSAndroid Build Coastguard Worker
50*08b48e0bSAndroid Build Coastguard Worker return original_close(sockfd);
51*08b48e0bSAndroid Build Coastguard Worker
52*08b48e0bSAndroid Build Coastguard Worker }
53*08b48e0bSAndroid Build Coastguard Worker
54*08b48e0bSAndroid Build Coastguard Worker }
55*08b48e0bSAndroid Build Coastguard Worker
dup2(int old,int new)56*08b48e0bSAndroid Build Coastguard Worker int dup2(int old, int new) {
57*08b48e0bSAndroid Build Coastguard Worker
58*08b48e0bSAndroid Build Coastguard Worker if (new <= 2) {
59*08b48e0bSAndroid Build Coastguard Worker
60*08b48e0bSAndroid Build Coastguard Worker fprintf(stderr, "Info: Disabling dup from %d to %d\n", old, new);
61*08b48e0bSAndroid Build Coastguard Worker return 0;
62*08b48e0bSAndroid Build Coastguard Worker
63*08b48e0bSAndroid Build Coastguard Worker } else {
64*08b48e0bSAndroid Build Coastguard Worker
65*08b48e0bSAndroid Build Coastguard Worker return original_dup2(old, new);
66*08b48e0bSAndroid Build Coastguard Worker
67*08b48e0bSAndroid Build Coastguard Worker }
68*08b48e0bSAndroid Build Coastguard Worker
69*08b48e0bSAndroid Build Coastguard Worker }
70*08b48e0bSAndroid Build Coastguard Worker
accept(int sockfd,struct sockaddr * addr,socklen_t * addrlen)71*08b48e0bSAndroid Build Coastguard Worker int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
72*08b48e0bSAndroid Build Coastguard Worker
73*08b48e0bSAndroid Build Coastguard Worker (void)sockfd;
74*08b48e0bSAndroid Build Coastguard Worker (void)addr;
75*08b48e0bSAndroid Build Coastguard Worker (void)addrlen;
76*08b48e0bSAndroid Build Coastguard Worker fprintf(stderr, "Info: Emulating accept on %d\n", sockfd);
77*08b48e0bSAndroid Build Coastguard Worker return 0;
78*08b48e0bSAndroid Build Coastguard Worker
79*08b48e0bSAndroid Build Coastguard Worker }
80*08b48e0bSAndroid Build Coastguard Worker
bind(int sockfd,const struct sockaddr * addr,socklen_t addrlen)81*08b48e0bSAndroid Build Coastguard Worker int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
82*08b48e0bSAndroid Build Coastguard Worker
83*08b48e0bSAndroid Build Coastguard Worker (void)sockfd;
84*08b48e0bSAndroid Build Coastguard Worker (void)addr;
85*08b48e0bSAndroid Build Coastguard Worker (void)addrlen;
86*08b48e0bSAndroid Build Coastguard Worker fprintf(stderr, "Info: Emulating bind on port %d\n",
87*08b48e0bSAndroid Build Coastguard Worker ntohs(((struct sockaddr_in *)addr)->sin_port));
88*08b48e0bSAndroid Build Coastguard Worker return 0;
89*08b48e0bSAndroid Build Coastguard Worker
90*08b48e0bSAndroid Build Coastguard Worker }
91*08b48e0bSAndroid Build Coastguard Worker
listen(int sockfd,int backlog)92*08b48e0bSAndroid Build Coastguard Worker int listen(int sockfd, int backlog) {
93*08b48e0bSAndroid Build Coastguard Worker
94*08b48e0bSAndroid Build Coastguard Worker (void)sockfd;
95*08b48e0bSAndroid Build Coastguard Worker (void)backlog;
96*08b48e0bSAndroid Build Coastguard Worker return 0;
97*08b48e0bSAndroid Build Coastguard Worker
98*08b48e0bSAndroid Build Coastguard Worker }
99*08b48e0bSAndroid Build Coastguard Worker
setsockopt(int sockfd,int level,int optid,const void * optdata,socklen_t optdatalen)100*08b48e0bSAndroid Build Coastguard Worker int setsockopt(int sockfd, int level, int optid, const void *optdata,
101*08b48e0bSAndroid Build Coastguard Worker socklen_t optdatalen) {
102*08b48e0bSAndroid Build Coastguard Worker
103*08b48e0bSAndroid Build Coastguard Worker (void)sockfd;
104*08b48e0bSAndroid Build Coastguard Worker (void)level;
105*08b48e0bSAndroid Build Coastguard Worker (void)optid;
106*08b48e0bSAndroid Build Coastguard Worker (void)optdata;
107*08b48e0bSAndroid Build Coastguard Worker (void)optdatalen;
108*08b48e0bSAndroid Build Coastguard Worker return 0;
109*08b48e0bSAndroid Build Coastguard Worker
110*08b48e0bSAndroid Build Coastguard Worker }
111*08b48e0bSAndroid Build Coastguard Worker
112