From c8d9f1b9c920e0314bb9ca67f183c8df743e845a Mon Sep 17 00:00:00 2001 From: Tom Date: Fri, 11 Sep 2020 21:11:07 -0600 Subject: Kernel: Make copy_to/from_user safe and remove unnecessary checks Since the CPU already does almost all necessary validation steps for us, we don't really need to attempt to do this. Doing it ourselves doesn't really work very reliably, because we'd have to account for other processors modifying virtual memory, and we'd have to account for e.g. pages not being able to be allocated due to insufficient resources. So change the copy_to/from_user (and associated helper functions) to use the new safe_memcpy, which will return whether it succeeded or not. The only manual validation step needed (which the CPU can't perform for us) is making sure the pointers provided by user mode aren't pointing to kernel mappings. To make it easier to read/write from/to either kernel or user mode data add the UserOrKernelBuffer helper class, which will internally either use copy_from/to_user or directly memcpy, or pass the data through directly using a temporary buffer on the stack. Last but not least we need to keep syscall params trivial as we need to copy them from/to user mode using copy_from/to_user. --- Kernel/Net/UDPSocket.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'Kernel/Net/UDPSocket.cpp') diff --git a/Kernel/Net/UDPSocket.cpp b/Kernel/Net/UDPSocket.cpp index c770a4e184..22db799af7 100644 --- a/Kernel/Net/UDPSocket.cpp +++ b/Kernel/Net/UDPSocket.cpp @@ -79,18 +79,19 @@ NonnullRefPtr UDPSocket::create(int protocol) return adopt(*new UDPSocket(protocol)); } -KResultOr UDPSocket::protocol_receive(const KBuffer& packet_buffer, void* buffer, size_t buffer_size, int flags) +KResultOr UDPSocket::protocol_receive(const KBuffer& packet_buffer, UserOrKernelBuffer& buffer, size_t buffer_size, int flags) { (void)flags; auto& ipv4_packet = *(const IPv4Packet*)(packet_buffer.data()); auto& udp_packet = *static_cast(ipv4_packet.payload()); ASSERT(udp_packet.length() >= sizeof(UDPPacket)); // FIXME: This should be rejected earlier. ASSERT(buffer_size >= (udp_packet.length() - sizeof(UDPPacket))); - memcpy(buffer, udp_packet.payload(), udp_packet.length() - sizeof(UDPPacket)); + if (!buffer.write(udp_packet.payload(), udp_packet.length() - sizeof(UDPPacket))) + return KResult(-EFAULT); return udp_packet.length() - sizeof(UDPPacket); } -KResultOr UDPSocket::protocol_send(const void* data, size_t data_length) +KResultOr UDPSocket::protocol_send(const UserOrKernelBuffer& data, size_t data_length) { auto routing_decision = route_to(peer_address(), local_address(), bound_interface()); if (routing_decision.is_zero()) @@ -100,9 +101,11 @@ KResultOr UDPSocket::protocol_send(const void* data, size_t data_length) udp_packet.set_source_port(local_port()); udp_packet.set_destination_port(peer_port()); udp_packet.set_length(sizeof(UDPPacket) + data_length); - memcpy(udp_packet.payload(), data, data_length); + if (!data.read(udp_packet.payload(), data_length)) + return KResult(-EFAULT); klog() << "sending as udp packet from " << routing_decision.adapter->ipv4_address().to_string().characters() << ":" << local_port() << " to " << peer_address().to_string().characters() << ":" << peer_port() << "!"; - routing_decision.adapter->send_ipv4(routing_decision.next_hop, peer_address(), IPv4Protocol::UDP, buffer, ttl()); + auto udp_packet_buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)&udp_packet); + routing_decision.adapter->send_ipv4(routing_decision.next_hop, peer_address(), IPv4Protocol::UDP, udp_packet_buffer, buffer.size(), ttl()); return data_length; } -- cgit v1.2.3