Skip to content

Commit

Permalink
Fix crash reproted via hackerone.
Browse files Browse the repository at this point in the history
In new_connection we didn't validate if proxy-protocol header had
correct number of items. Classic out-of-bounds.
  • Loading branch information
majek committed May 20, 2020
1 parent bf51031 commit 53ee896
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ format:

.PHONY: cloudflare-ip-ranges.txt
cloudflare-ip-ranges.txt:
curl -s https://www.cloudflare.com/ips-v4 https://www.cloudflare.com/ips-v6 > cloudflare-ip-ranges.txt
curl -s https://www.cloudflare.com/ips-v4 https://www.cloudflare.com/ips-v6 | sort > cloudflare-ip-ranges.txt
2 changes: 1 addition & 1 deletion cloudflare-ip-ranges.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
2405:b500::/32
2606:4700::/32
2803:f800::/32
2c0f:f248::/32
2a06:98c0::/29
2c0f:f248::/32
12 changes: 8 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ coroutine void new_connection(int cd, struct state *state)
return;
}

/* Chop buf on \r\n. */
const char **words = NULL;

/* Chop buf on \r\n. */
char *nl = memchr(buf, '\n', rbytes);
if (nl == NULL) {
goto parseerror;
Expand All @@ -95,7 +96,10 @@ coroutine void new_connection(int cd, struct state *state)
buf[nbytes - 1] = '\0';
buf[nbytes] = '\0';

const char **words = parse_argv(buf, ' ');
words = parse_argv(buf, ' ');
if (argv_len(words) != 6) {
goto parseerror;
}
if (strcasecmp(words[0], "PROXY") != 0) {
goto parseerror;
}
Expand Down Expand Up @@ -123,8 +127,6 @@ coroutine void new_connection(int cd, struct state *state)
goto parseerror;
}

free(words);

char rstr[IPADDR_MAXSTRLEN + 7];
ipaddrstr_port(remote_addr, rstr);

Expand Down Expand Up @@ -166,6 +168,7 @@ coroutine void new_connection(int cd, struct state *state)
free(ch);

disconnected:
free(words);
fdclean(cd);
close(cd);
fdclean(rs);
Expand All @@ -176,6 +179,7 @@ coroutine void new_connection(int cd, struct state *state)

parseerror:
printf("[?] %s broke with bad proxy-protocol header\n", lstr);
free(words);
fdclean(cd);
close(cd);
return;
Expand Down
2 changes: 1 addition & 1 deletion src/mmproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ int check_ip_rule(int ipv6, uint32_t mark, uint32_t table);
int check_ip_route(int ipv6, uint32_t table);
int set_nofile_max();
int read_subnets(const char *fname, network **ptr_networks, int *ptr_networks_len);
unsigned argv_len(const char **argv);

/* net.c */
ipaddr ipaddr_parse(const char *addr, int noport);
Expand All @@ -66,7 +67,6 @@ int net_find_match(network *networks, int networks_len, ipaddr addr);
int ipport(ipaddr addr);



#ifndef IP_TRANSPARENT
# define IP_TRANSPARENT 19
#endif
Expand Down

0 comments on commit 53ee896

Please sign in to comment.