From b9f30c6f2a69b0c07d9a61e5dd81f439a1eafc8f Mon Sep 17 00:00:00 2001 From: Daniel Bertalan Date: Thu, 1 Jul 2021 11:29:28 +0200 Subject: Everywhere: Fix some alignment issues When creating uninitialized storage for variables, we need to make sure that the alignment is correct. Fixes a KUBSAN failure when running kernels compiled with Clang. In `Syscalls/socket.cpp`, we can simply use local variables, as `sockaddr_un` is a POD type. Along with moving the `alignas` specifier to the correct member, `AK::Optional`'s internal buffer has been made non-zeroed by default. GCC emitted bogus uninitialized memory access warnings, so we now use `__builtin_launder` to tell the compiler that we know what we are doing. This might disable some optimizations, but judging by how GCC failed to notice that the memory's initialization is dependent on `m_has_value`, I'm not sure that's a bad thing. --- Kernel/Syscalls/socket.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'Kernel/Syscalls/socket.cpp') diff --git a/Kernel/Syscalls/socket.cpp b/Kernel/Syscalls/socket.cpp index 108e3967f1..f6f15384e5 100644 --- a/Kernel/Syscalls/socket.cpp +++ b/Kernel/Syscalls/socket.cpp @@ -119,10 +119,10 @@ KResultOr Process::sys$accept4(Userspace(address_size)); - accepted_socket->get_peer_address((sockaddr*)address_buffer, &address_size); - if (!copy_to_user(user_address, address_buffer, address_size)) + accepted_socket->get_peer_address((sockaddr*)&address_buffer, &address_size); + if (!copy_to_user(user_address, &address_buffer, address_size)) return EFAULT; if (!copy_to_user(user_address_size, &address_size)) return EFAULT; @@ -311,13 +311,13 @@ int Process::get_sock_or_peer_name(const Params& params) auto& socket = *description->socket(); REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain()); - u8 address_buffer[sizeof(sockaddr_un)]; + sockaddr_un address_buffer; addrlen_value = min(sizeof(sockaddr_un), static_cast(addrlen_value)); if constexpr (sockname) - socket.get_local_address((sockaddr*)address_buffer, &addrlen_value); + socket.get_local_address((sockaddr*)&address_buffer, &addrlen_value); else - socket.get_peer_address((sockaddr*)address_buffer, &addrlen_value); - if (!copy_to_user(params.addr, address_buffer, addrlen_value)) + socket.get_peer_address((sockaddr*)&address_buffer, &addrlen_value); + if (!copy_to_user(params.addr, &address_buffer, addrlen_value)) return EFAULT; if (!copy_to_user(params.addrlen, &addrlen_value)) return EFAULT; -- cgit v1.2.3