From 58665c7f50e34d83dc99baa0f3506e616478d8df Mon Sep 17 00:00:00 2001 From: rcitachi <68690849+rcitachi@users.noreply.github.com> Date: Sat, 2 Dec 2023 12:11:01 +0800 Subject: [PATCH] [AF_UNIX] Update AF_UNIX bind, connect entry (#8314) --- components/lwp/lwp_syscall.c | 59 ++++++++++++++++--------- components/net/sal/include/sal_socket.h | 3 +- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/components/lwp/lwp_syscall.c b/components/lwp/lwp_syscall.c index 73cff9577e..44c2bafc8e 100644 --- a/components/lwp/lwp_syscall.c +++ b/components/lwp/lwp_syscall.c @@ -3290,26 +3290,37 @@ sysret_t sys_accept(int socket, struct musl_sockaddr *addr, socklen_t *addrlen) sysret_t sys_bind(int socket, const struct musl_sockaddr *name, socklen_t namelen) { + rt_err_t ret = 0; struct sockaddr sa; struct musl_sockaddr kname; + struct sockaddr_un addr_un; + rt_uint16_t family = 0; if (!lwp_user_accessable((void *)name, namelen)) { return -EFAULT; } -#ifdef SAL_USING_AF_UNIX - if (name->sa_family == AF_UNIX) + lwp_get_from_user(&family, (void *)name, 2); + if (family == AF_UNIX) { - namelen = sizeof(struct sockaddr); + if (!lwp_user_accessable((void *)name, sizeof(struct sockaddr_un))) + { + return -EFAULT; + } + + lwp_get_from_user(&addr_un, (void *)name, sizeof(struct sockaddr_un)); + + ret = bind(socket, (struct sockaddr *)(&addr_un), namelen); + } + else + { + lwp_get_from_user(&kname, (void *)name, namelen); + sockaddr_tolwip(&kname, &sa); + ret = bind(socket, &sa, namelen); } -#endif /* SAL_USING_AF_UNIX */ - lwp_get_from_user(&kname, (void *)name, namelen); - - sockaddr_tolwip(&kname, &sa); - - return bind(socket, &sa, namelen); + return (ret < 0 ? GET_ERRNO() : ret); } sysret_t sys_shutdown(int socket, int how) @@ -3473,28 +3484,36 @@ sysret_t sys_setsockopt(int socket, int level, int optname, const void *optval, sysret_t sys_connect(int socket, const struct musl_sockaddr *name, socklen_t namelen) { - int ret; + int ret = 0; + rt_uint16_t family = 0; struct sockaddr sa; struct musl_sockaddr kname; + struct sockaddr_un addr_un; if (!lwp_user_accessable((void *)name, namelen)) { return -EFAULT; } -#ifdef SAL_USING_AF_UNIX - if (name->sa_family == AF_UNIX) + lwp_get_from_user(&family, (void *)name, 2); + if (family == AF_UNIX) { - namelen = sizeof(struct sockaddr); + if (!lwp_user_accessable((void *)name, sizeof(struct sockaddr_un))) + { + return -EFAULT; + } + + lwp_get_from_user(&addr_un, (void *)name, sizeof(struct sockaddr_un)); + ret = connect(socket, (struct sockaddr *)(&addr_un), namelen); + } + else + { + lwp_get_from_user(&kname, (void *)name, namelen); + sockaddr_tolwip(&kname, &sa); + ret = connect(socket, &sa, namelen); } -#endif /* SAL_USING_AF_UNIX */ - lwp_get_from_user(&kname, (void *)name, namelen); - - sockaddr_tolwip(&kname, &sa); - - ret = connect(socket, &sa, namelen); - return (ret < 0 ? GET_ERRNO() : ret); + return ret; } sysret_t sys_listen(int socket, int backlog) diff --git a/components/net/sal/include/sal_socket.h b/components/net/sal/include/sal_socket.h index 524ce86985..c5d4cc85ff 100644 --- a/components/net/sal/include/sal_socket.h +++ b/components/net/sal/include/sal_socket.h @@ -186,8 +186,7 @@ struct sockaddr /* Structure describing the address of an AF_LOCAL (aka AF_UNIX) socket. */ struct sockaddr_un { - uint8_t sa_len; - sa_family_t sa_family; + unsigned short sa_family; char sun_path[108]; /* Path name. */ };