/* * Copyright (c) 2018-2020, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include #include Kernel::KResultOr> try_copy_kstring_from_user(Userspace user_str, size_t user_str_size) { bool is_user = Kernel::Memory::is_user_range(VirtualAddress(user_str), user_str_size); if (!is_user) return EFAULT; Kernel::SmapDisabler disabler; void* fault_at; ssize_t length = Kernel::safe_strnlen(user_str.unsafe_userspace_ptr(), user_str_size, fault_at); if (length < 0) { dbgln("copy_kstring_from_user({:p}, {}) failed at {} (strnlen)", static_cast(user_str.unsafe_userspace_ptr()), user_str_size, VirtualAddress { fault_at }); return EFAULT; } char* buffer; auto new_string = TRY(Kernel::KString::try_create_uninitialized(length, buffer)); buffer[length] = '\0'; if (length == 0) return new_string; if (!Kernel::safe_memcpy(buffer, user_str.unsafe_userspace_ptr(), (size_t)length, fault_at)) { dbgln("copy_kstring_from_user({:p}, {}) failed at {} (memcpy)", static_cast(user_str.unsafe_userspace_ptr()), user_str_size, VirtualAddress { fault_at }); return EFAULT; } return new_string; } KResultOr