From f884b549d299ee3388b47881fdca4e075bfcdb21 Mon Sep 17 00:00:00 2001 From: dartvader316 Date: Mon, 5 Aug 2024 19:49:51 +0300 Subject: [PATCH 1/4] fix usage of some misaligned pointers --- packets.c | 20 ++++++++++++++------ proxy.c | 2 +- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/packets.c b/packets.c index 52fd278..7e49dd5 100644 --- a/packets.c +++ b/packets.c @@ -137,10 +137,14 @@ int change_tls_sni(const char *host, char *buffer, size_t bsize) || free_sz < diff) { return -1; } - *(uint16_t *)(sni + 2) = htons(old_sz + diff + 5); - *(uint16_t *)(sni + 4) = htons(old_sz + diff + 3); - *(uint16_t *)(sni + 7) = htons(old_sz + diff); - *(uint16_t *)(pad + 2) = htons(free_sz - diff); + uint16_t htons_sni2 = htons(old_sz + diff + 5); + uint16_t htons_sni4 = htons(old_sz + diff + 3); + uint16_t htons_sni7 = htons(old_sz + diff); + uint16_t htons_pad2 = htons(old_sz - diff); + memcpy(sni + 2, &htons_sni2, sizeof(htons_sni2)); + memcpy(sni + 4, &htons_sni4, sizeof(htons_sni4)); + memcpy(sni + 7, &htons_sni7, sizeof(htons_sni7)); + memcpy(pad + 2, &htons_pad2, sizeof(htons_pad2)); char *host_end = sni + 9 + old_sz; int oth_sz = bsize - (sni_offs + 9 + old_sz); @@ -410,7 +414,11 @@ int part_tls(char *buffer, size_t bsize, ssize_t n, long pos) memmove(buffer + 5 + pos + 5, buffer + 5 + pos, n - (5 + pos)); memcpy(buffer + 5 + pos, buffer, 3); - *(uint16_t *)(buffer + 3) = htons(pos); - *(uint16_t *)(buffer + 5 + pos + 3) = htons(r_sz - pos); + uint16_t htons_pos = htons(pos); + memcpy(buffer + 3, &htons_pos, sizeof(htons_pos)); + + uint16_t htons_rsz_pos = htons(r_sz - pos); + memcpy(buffer + 5 + pos + 3, &htons_rsz_pos, sizeof(htons_rsz_pos)); + return 5; } diff --git a/proxy.c b/proxy.c index c631d88..de5ad7a 100644 --- a/proxy.c +++ b/proxy.c @@ -274,7 +274,7 @@ int s5_get_addr(char *buffer, size_t n, addr->in6.sin6_addr = r->i6; } } - addr->in.sin_port = *(uint16_t *)&buffer[o - 2]; + memcpy(&addr->in.sin_port, &buffer[o - 2], sizeof(uint16_t)); return o; } From 3fee8d5aed122f34ec13637f5f4b1502d13cc923 Mon Sep 17 00:00:00 2001 From: dartvader316 Date: Mon, 5 Aug 2024 20:19:10 +0300 Subject: [PATCH 2/4] fix compile without -O2 --- extend.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extend.c b/extend.c index 92abe93..b91edf1 100644 --- a/extend.c +++ b/extend.c @@ -90,7 +90,7 @@ int mode_add_get(struct sockaddr_ina *dst, int m) } -inline bool check_port(uint16_t *p, struct sockaddr_in6 *dst) +static inline bool check_port(uint16_t *p, struct sockaddr_in6 *dst) { return (dst->sin6_port >= p[0] && dst->sin6_port <= p[1]); From 0f2db6c2479639722f8c6c7f3fc37cc78f5e6bb3 Mon Sep 17 00:00:00 2001 From: dartvader316 Date: Mon, 5 Aug 2024 22:17:48 +0300 Subject: [PATCH 3/4] correct htons_pad2 --- packets.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packets.c b/packets.c index 7e49dd5..d3caa34 100644 --- a/packets.c +++ b/packets.c @@ -140,7 +140,7 @@ int change_tls_sni(const char *host, char *buffer, size_t bsize) uint16_t htons_sni2 = htons(old_sz + diff + 5); uint16_t htons_sni4 = htons(old_sz + diff + 3); uint16_t htons_sni7 = htons(old_sz + diff); - uint16_t htons_pad2 = htons(old_sz - diff); + uint16_t htons_pad2 = htons(free_sz - diff); memcpy(sni + 2, &htons_sni2, sizeof(htons_sni2)); memcpy(sni + 4, &htons_sni4, sizeof(htons_sni4)); memcpy(sni + 7, &htons_sni7, sizeof(htons_sni7)); From 0130b7deb5f7e9f5179212ca1524facf56ac46d8 Mon Sep 17 00:00:00 2001 From: ruti <> Date: Mon, 5 Aug 2024 22:46:39 +0300 Subject: [PATCH 4/4] create nhtona macros --- packets.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/packets.c b/packets.c index d3caa34..f669796 100644 --- a/packets.c +++ b/packets.c @@ -17,6 +17,10 @@ #define ANTOHS(data, i) \ (uint16_t)((data[i] << 8) + (uint8_t)data[i + 1]) +#define SHTONA(data, i, x) \ + data[i] = (uint8_t)(x >> 8); \ + data[i + 1] = x & 0xff; + char tls_data[517] = { "\x16\x03\x01\x02\x00\x01\x00\x01\xfc\x03\x03\x03\x5f" @@ -137,14 +141,10 @@ int change_tls_sni(const char *host, char *buffer, size_t bsize) || free_sz < diff) { return -1; } - uint16_t htons_sni2 = htons(old_sz + diff + 5); - uint16_t htons_sni4 = htons(old_sz + diff + 3); - uint16_t htons_sni7 = htons(old_sz + diff); - uint16_t htons_pad2 = htons(free_sz - diff); - memcpy(sni + 2, &htons_sni2, sizeof(htons_sni2)); - memcpy(sni + 4, &htons_sni4, sizeof(htons_sni4)); - memcpy(sni + 7, &htons_sni7, sizeof(htons_sni7)); - memcpy(pad + 2, &htons_pad2, sizeof(htons_pad2)); + SHTONA(sni, 2, old_sz + diff + 5); + SHTONA(sni, 4, old_sz + diff + 3); + SHTONA(sni, 7, old_sz + diff); + SHTONA(pad, 2, free_sz - diff); char *host_end = sni + 9 + old_sz; int oth_sz = bsize - (sni_offs + 9 + old_sz); @@ -414,11 +414,7 @@ int part_tls(char *buffer, size_t bsize, ssize_t n, long pos) memmove(buffer + 5 + pos + 5, buffer + 5 + pos, n - (5 + pos)); memcpy(buffer + 5 + pos, buffer, 3); - uint16_t htons_pos = htons(pos); - memcpy(buffer + 3, &htons_pos, sizeof(htons_pos)); - - uint16_t htons_rsz_pos = htons(r_sz - pos); - memcpy(buffer + 5 + pos + 3, &htons_rsz_pos, sizeof(htons_rsz_pos)); - + SHTONA(buffer, 3, pos); + SHTONA(buffer, 5 + pos + 3, r_sz - pos); return 5; }