From c58a4a374300db6a547f3fe9d954d569598110ad Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Mon, 9 Sep 2024 15:19:50 +0200 Subject: [PATCH 1/2] Use macro expansion for maximum sockaddr storage length rather than hardcoded value --- libctru/source/services/soc/soc_accept.c | 6 +++--- libctru/source/services/soc/soc_bind.c | 6 +++--- libctru/source/services/soc/soc_common.h | 1 + libctru/source/services/soc/soc_connect.c | 6 +++--- libctru/source/services/soc/soc_getnameinfo.c | 4 ++-- libctru/source/services/soc/soc_getpeername.c | 6 +++--- libctru/source/services/soc/soc_getsockname.c | 6 +++--- libctru/source/services/soc/soc_recvfrom.c | 12 ++++++------ libctru/source/services/soc/soc_sendto.c | 12 ++++++------ 9 files changed, 30 insertions(+), 29 deletions(-) diff --git a/libctru/source/services/soc/soc_accept.c b/libctru/source/services/soc/soc_accept.c index 67b80d427..ef8be22db 100644 --- a/libctru/source/services/soc/soc_accept.c +++ b/libctru/source/services/soc/soc_accept.c @@ -6,10 +6,10 @@ int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) { int ret = 0; - int tmp_addrlen = 0x1c; + int tmp_addrlen = ADDR_STORAGE_LEN; int fd, dev; u32 *cmdbuf = getThreadCommandBuffer(); - u8 tmpaddr[0x1c]; + u8 tmpaddr[ADDR_STORAGE_LEN]; u32 saved_threadstorage[2]; sockfd = soc_get_fd(sockfd); @@ -27,7 +27,7 @@ int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) fd = __alloc_handle(dev); if(fd < 0) return fd; - memset(tmpaddr, 0, 0x1c); + memset(tmpaddr, 0, ADDR_STORAGE_LEN); cmdbuf[0] = IPC_MakeHeader(0x4,2,2); // 0x40082 cmdbuf[1] = (u32)sockfd; diff --git a/libctru/source/services/soc/soc_bind.c b/libctru/source/services/soc/soc_bind.c index 94b2ac528..3a5262217 100644 --- a/libctru/source/services/soc/soc_bind.c +++ b/libctru/source/services/soc/soc_bind.c @@ -8,7 +8,7 @@ int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) int ret = 0; int tmp_addrlen = 0; u32 *cmdbuf = getThreadCommandBuffer(); - u8 tmpaddr[0x1c]; + u8 tmpaddr[ADDR_STORAGE_LEN]; sockfd = soc_get_fd(sockfd); if(sockfd < 0) { @@ -16,12 +16,12 @@ int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) return -1; } - memset(tmpaddr, 0, 0x1c); + memset(tmpaddr, 0, ADDR_STORAGE_LEN); if(addr->sa_family == AF_INET) tmp_addrlen = 8; else - tmp_addrlen = 0x1c; + tmp_addrlen = ADDR_STORAGE_LEN; if(addrlen < tmp_addrlen) { errno = EINVAL; diff --git a/libctru/source/services/soc/soc_common.h b/libctru/source/services/soc/soc_common.h index e859ac5c4..85759eb7b 100644 --- a/libctru/source/services/soc/soc_common.h +++ b/libctru/source/services/soc/soc_common.h @@ -10,6 +10,7 @@ #include <3ds/services/soc.h> #define SYNC_ERROR ENODEV +#define ADDR_STORAGE_LEN sizeof(struct sockaddr_storage) extern Handle SOCU_handle; extern Handle socMemhandle; diff --git a/libctru/source/services/soc/soc_connect.c b/libctru/source/services/soc/soc_connect.c index e48f35da1..19955f10f 100644 --- a/libctru/source/services/soc/soc_connect.c +++ b/libctru/source/services/soc/soc_connect.c @@ -8,7 +8,7 @@ int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) int ret = 0; int tmp_addrlen = 0; u32 *cmdbuf = getThreadCommandBuffer(); - u8 tmpaddr[0x1c]; + u8 tmpaddr[ADDR_STORAGE_LEN]; sockfd = soc_get_fd(sockfd); if(sockfd < 0) { @@ -16,12 +16,12 @@ int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) return -1; } - memset(tmpaddr, 0, 0x1c); + memset(tmpaddr, 0, ADDR_STORAGE_LEN); if(addr->sa_family == AF_INET) tmp_addrlen = 8; else - tmp_addrlen = 0x1c; + tmp_addrlen = ADDR_STORAGE_LEN; if(addrlen < tmp_addrlen) { errno = EINVAL; diff --git a/libctru/source/services/soc/soc_getnameinfo.c b/libctru/source/services/soc/soc_getnameinfo.c index 08f37cbce..e6cf1116d 100644 --- a/libctru/source/services/soc/soc_getnameinfo.c +++ b/libctru/source/services/soc/soc_getnameinfo.c @@ -8,7 +8,7 @@ int getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, socklen_ int i,tmp_addrlen; u32 *cmdbuf = getThreadCommandBuffer(); u32 saved_threadstorage[4]; - u8 tmpaddr[0x1c]; // sockaddr size for the kernel is 0x1C (sockaddr_in6?) + u8 tmpaddr[ADDR_STORAGE_LEN]; // sockaddr size for the kernel is 0x1C (sockaddr_in6?) if((host == NULL || hostlen == 0) && (serv == NULL || servlen == 0)) { @@ -18,7 +18,7 @@ int getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, socklen_ if(sa->sa_family == AF_INET) tmp_addrlen = 8; else - tmp_addrlen = 0x1c; + tmp_addrlen = ADDR_STORAGE_LEN; if(salen < tmp_addrlen) { errno = EINVAL; diff --git a/libctru/source/services/soc/soc_getpeername.c b/libctru/source/services/soc/soc_getpeername.c index 269a781d0..10ed7094c 100644 --- a/libctru/source/services/soc/soc_getpeername.c +++ b/libctru/source/services/soc/soc_getpeername.c @@ -8,7 +8,7 @@ int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen) int ret = 0; u32 *cmdbuf = getThreadCommandBuffer(); u32 saved_threadstorage[2]; - u8 tmpaddr[0x1c]; + u8 tmpaddr[ADDR_STORAGE_LEN]; sockfd = soc_get_fd(sockfd); if(sockfd < 0) { @@ -18,14 +18,14 @@ int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen) cmdbuf[0] = IPC_MakeHeader(0x18,2,2); // 0x180082 cmdbuf[1] = (u32)sockfd; - cmdbuf[2] = 0x1c; + cmdbuf[2] = ADDR_STORAGE_LEN; cmdbuf[3] = IPC_Desc_CurProcessId(); u32 * staticbufs = getThreadStaticBuffers(); saved_threadstorage[0] = staticbufs[0]; saved_threadstorage[1] = staticbufs[1]; - staticbufs[0] = IPC_Desc_StaticBuffer(0x1c,0); + staticbufs[0] = IPC_Desc_StaticBuffer(ADDR_STORAGE_LEN,0); staticbufs[1] = (u32)tmpaddr; ret = svcSendSyncRequest(SOCU_handle); diff --git a/libctru/source/services/soc/soc_getsockname.c b/libctru/source/services/soc/soc_getsockname.c index eef6bf46a..87df82a30 100644 --- a/libctru/source/services/soc/soc_getsockname.c +++ b/libctru/source/services/soc/soc_getsockname.c @@ -8,7 +8,7 @@ int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen) int ret = 0; u32 *cmdbuf = getThreadCommandBuffer(); u32 saved_threadstorage[2]; - u8 tmpaddr[0x1c]; + u8 tmpaddr[ADDR_STORAGE_LEN]; sockfd = soc_get_fd(sockfd); if(sockfd < 0) { @@ -18,14 +18,14 @@ int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen) cmdbuf[0] = IPC_MakeHeader(0x17,2,2); // 0x170082 cmdbuf[1] = (u32)sockfd; - cmdbuf[2] = 0x1c; + cmdbuf[2] = ADDR_STORAGE_LEN; cmdbuf[3] = IPC_Desc_CurProcessId(); u32 * staticbufs = getThreadStaticBuffers(); saved_threadstorage[0] = staticbufs[0]; saved_threadstorage[1] = staticbufs[1]; - staticbufs[0] = IPC_Desc_StaticBuffer(0x1c,0); + staticbufs[0] = IPC_Desc_StaticBuffer(ADDR_STORAGE_LEN,0); staticbufs[1] = (u32)tmpaddr; ret = svcSendSyncRequest(SOCU_handle); diff --git a/libctru/source/services/soc/soc_recvfrom.c b/libctru/source/services/soc/soc_recvfrom.c index 30ffd57f4..047a720b0 100644 --- a/libctru/source/services/soc/soc_recvfrom.c +++ b/libctru/source/services/soc/soc_recvfrom.c @@ -8,13 +8,13 @@ ssize_t socuipc_cmd7(int sockfd, void *buf, size_t len, int flags, struct sockad int ret = 0; u32 *cmdbuf = getThreadCommandBuffer(); u32 tmp_addrlen = 0; - u8 tmpaddr[0x1c]; + u8 tmpaddr[ADDR_STORAGE_LEN]; u32 saved_threadstorage[2]; - memset(tmpaddr, 0, 0x1c); + memset(tmpaddr, 0, ADDR_STORAGE_LEN); if(src_addr) - tmp_addrlen = 0x1c; + tmp_addrlen = ADDR_STORAGE_LEN; cmdbuf[0] = IPC_MakeHeader(0x7,4,4); // 0x70104 cmdbuf[1] = (u32)sockfd; @@ -66,13 +66,13 @@ ssize_t socuipc_cmd8(int sockfd, void *buf, size_t len, int flags, struct sockad int ret = 0; u32 *cmdbuf = getThreadCommandBuffer(); u32 tmp_addrlen = 0; - u8 tmpaddr[0x1c]; + u8 tmpaddr[ADDR_STORAGE_LEN]; u32 saved_threadstorage[4]; if(src_addr) - tmp_addrlen = 0x1c; + tmp_addrlen = ADDR_STORAGE_LEN; - memset(tmpaddr, 0, 0x1c); + memset(tmpaddr, 0, ADDR_STORAGE_LEN); cmdbuf[0] = 0x00080102; cmdbuf[1] = (u32)sockfd; diff --git a/libctru/source/services/soc/soc_sendto.c b/libctru/source/services/soc/soc_sendto.c index fe7b5ba5c..42fce5c81 100644 --- a/libctru/source/services/soc/soc_sendto.c +++ b/libctru/source/services/soc/soc_sendto.c @@ -8,15 +8,15 @@ ssize_t socuipc_cmd9(int sockfd, const void *buf, size_t len, int flags, const s int ret = 0; u32 *cmdbuf = getThreadCommandBuffer(); u32 tmp_addrlen = 0; - u8 tmpaddr[0x1c]; + u8 tmpaddr[ADDR_STORAGE_LEN]; - memset(tmpaddr, 0, 0x1c); + memset(tmpaddr, 0, ADDR_STORAGE_LEN); if(dest_addr) { if(dest_addr->sa_family == AF_INET) tmp_addrlen = 8; else - tmp_addrlen = 0x1c; + tmp_addrlen = ADDR_STORAGE_LEN; if(addrlen < tmp_addrlen) { errno = EINVAL; @@ -62,15 +62,15 @@ ssize_t socuipc_cmda(int sockfd, const void *buf, size_t len, int flags, const s int ret = 0; u32 *cmdbuf = getThreadCommandBuffer(); u32 tmp_addrlen = 0; - u8 tmpaddr[0x1c]; + u8 tmpaddr[ADDR_STORAGE_LEN]; - memset(tmpaddr, 0, 0x1c); + memset(tmpaddr, 0, ADDR_STORAGE_LEN); if(dest_addr) { if(dest_addr->sa_family == AF_INET) tmp_addrlen = 8; else - tmp_addrlen = 0x1c; + tmp_addrlen = ADDR_STORAGE_LEN; if(addrlen < tmp_addrlen) { errno = EINVAL; From 6489f36915d02315a15115f42aacb31c6f7236c4 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Mon, 9 Sep 2024 16:48:51 +0200 Subject: [PATCH 2/2] Return sockaddr_in length from functions that have in/out addrlen Previously the socket funtions did not write the sockaddr_in struct fully and instead always returned the "internal" length, which did not account for the zeroed padding. --- libctru/source/services/soc/soc_accept.c | 11 +++++++--- libctru/source/services/soc/soc_getpeername.c | 14 ++++++++---- libctru/source/services/soc/soc_getsockname.c | 14 ++++++++---- libctru/source/services/soc/soc_recvfrom.c | 22 ++++++++++++++----- 4 files changed, 44 insertions(+), 17 deletions(-) diff --git a/libctru/source/services/soc/soc_accept.c b/libctru/source/services/soc/soc_accept.c index ef8be22db..d12be1fcf 100644 --- a/libctru/source/services/soc/soc_accept.c +++ b/libctru/source/services/soc/soc_accept.c @@ -61,9 +61,14 @@ int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) if(ret >= 0 && addr != NULL) { addr->sa_family = tmpaddr[1]; - if(*addrlen > tmpaddr[0]) - *addrlen = tmpaddr[0]; - memcpy(addr->sa_data, &tmpaddr[2], *addrlen - 2); + + socklen_t user_addrlen = tmpaddr[0]; + if(addr->sa_family == AF_INET) + user_addrlen += 8; // Accounting for the 8 bytes of sin_zero padding, which must be written for compatibility. + + if(*addrlen > user_addrlen) + *addrlen = user_addrlen; + memcpy(addr->sa_data, &tmpaddr[2], *addrlen - sizeof(addr->sa_family)); } if(ret < 0) { diff --git a/libctru/source/services/soc/soc_getpeername.c b/libctru/source/services/soc/soc_getpeername.c index 10ed7094c..d15bade70 100644 --- a/libctru/source/services/soc/soc_getpeername.c +++ b/libctru/source/services/soc/soc_getpeername.c @@ -16,6 +16,8 @@ int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen) return -1; } + memset(tmpaddr, 0, ADDR_STORAGE_LEN); + cmdbuf[0] = IPC_MakeHeader(0x18,2,2); // 0x180082 cmdbuf[1] = (u32)sockfd; cmdbuf[2] = ADDR_STORAGE_LEN; @@ -47,11 +49,15 @@ int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen) return -1; } - if(*addrlen > tmpaddr[0]) - *addrlen = tmpaddr[0]; - memset(addr, 0, sizeof(struct sockaddr)); addr->sa_family = tmpaddr[1]; - memcpy(addr->sa_data, &tmpaddr[2], *addrlen - 2); + + socklen_t user_addrlen = tmpaddr[0]; + if(addr->sa_family == AF_INET) + user_addrlen += 8; + + if(*addrlen > user_addrlen) + *addrlen = user_addrlen; + memcpy(addr->sa_data, &tmpaddr[2], *addrlen - sizeof(addr->sa_family)); return ret; } diff --git a/libctru/source/services/soc/soc_getsockname.c b/libctru/source/services/soc/soc_getsockname.c index 87df82a30..df75402f2 100644 --- a/libctru/source/services/soc/soc_getsockname.c +++ b/libctru/source/services/soc/soc_getsockname.c @@ -16,6 +16,8 @@ int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen) return -1; } + memset(tmpaddr, 0, ADDR_STORAGE_LEN); + cmdbuf[0] = IPC_MakeHeader(0x17,2,2); // 0x170082 cmdbuf[1] = (u32)sockfd; cmdbuf[2] = ADDR_STORAGE_LEN; @@ -47,11 +49,15 @@ int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen) return -1; } - if(*addrlen > tmpaddr[0]) - *addrlen = tmpaddr[0]; - memset(addr, 0, sizeof(struct sockaddr)); addr->sa_family = tmpaddr[1]; - memcpy(addr->sa_data, &tmpaddr[2], *addrlen - 2); + + socklen_t user_addrlen = tmpaddr[0]; + if(addr->sa_family == AF_INET) + user_addrlen += 8; + + if(*addrlen > user_addrlen) + *addrlen = user_addrlen; + memcpy(addr->sa_data, &tmpaddr[2], *addrlen - sizeof(addr->sa_family)); return ret; } diff --git a/libctru/source/services/soc/soc_recvfrom.c b/libctru/source/services/soc/soc_recvfrom.c index 047a720b0..eed0422a9 100644 --- a/libctru/source/services/soc/soc_recvfrom.c +++ b/libctru/source/services/soc/soc_recvfrom.c @@ -53,9 +53,14 @@ ssize_t socuipc_cmd7(int sockfd, void *buf, size_t len, int flags, struct sockad if(src_addr != NULL) { src_addr->sa_family = tmpaddr[1]; - if(*addrlen > tmpaddr[0]) - *addrlen = tmpaddr[0]; - memcpy(src_addr->sa_data, &tmpaddr[2], *addrlen - 2); + + socklen_t user_addrlen = tmpaddr[0]; + if(src_addr->sa_family == AF_INET) + user_addrlen += 8; + + if(*addrlen > user_addrlen) + *addrlen = user_addrlen; + memcpy(src_addr->sa_data, &tmpaddr[2], *addrlen - sizeof(src_addr->sa_family)); } return ret; @@ -113,9 +118,14 @@ ssize_t socuipc_cmd8(int sockfd, void *buf, size_t len, int flags, struct sockad if(src_addr != NULL) { src_addr->sa_family = tmpaddr[1]; - if(*addrlen > tmpaddr[0]) - *addrlen = tmpaddr[0]; - memcpy(src_addr->sa_data, &tmpaddr[2], *addrlen - 2); + + socklen_t user_addrlen = tmpaddr[0]; + if(src_addr->sa_family == AF_INET) + user_addrlen += 8; + + if(*addrlen > user_addrlen) + *addrlen = user_addrlen; + memcpy(src_addr->sa_data, &tmpaddr[2], *addrlen - sizeof(src_addr->sa_family)); } return ret;