diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2021-08-19 22:45:07 +0300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-08-19 23:49:53 +0200 |
commit | cf271183b4befeba7c96bd6ca25246b899e836bb (patch) | |
tree | 0361e7378750c2c4e45e5e01e273d6fe416555af /Kernel/Net | |
parent | 1259dc362376e8011d20d4d9e2fcc49d5b7f9dc9 (diff) | |
download | serenity-cf271183b4befeba7c96bd6ca25246b899e836bb.zip |
Kernel: Make Process::current() return a Process& instead of Process*
This has several benefits:
1) We no longer just blindly derefence a null pointer in various places
2) We will get nicer runtime error messages if the current process does
turn out to be null in the call location
3) GCC no longer complains about possible nullptr dereferences when
compiling without KUBSAN
Diffstat (limited to 'Kernel/Net')
-rw-r--r-- | Kernel/Net/IPv4Socket.cpp | 14 | ||||
-rw-r--r-- | Kernel/Net/LocalSocket.cpp | 16 | ||||
-rw-r--r-- | Kernel/Net/Socket.cpp | 4 |
3 files changed, 17 insertions, 17 deletions
diff --git a/Kernel/Net/IPv4Socket.cpp b/Kernel/Net/IPv4Socket.cpp index 9ca1731153..7b5c50c7a2 100644 --- a/Kernel/Net/IPv4Socket.cpp +++ b/Kernel/Net/IPv4Socket.cpp @@ -118,9 +118,9 @@ KResult IPv4Socket::bind(Userspace<const sockaddr*> user_address, socklen_t addr return set_so_error(EINVAL); auto requested_local_port = ntohs(address.sin_port); - if (!Process::current()->is_superuser()) { + if (!Process::current().is_superuser()) { if (requested_local_port > 0 && requested_local_port < 1024) { - dbgln("UID {} attempted to bind {} to port {}", Process::current()->uid(), class_name(), requested_local_port); + dbgln("UID {} attempted to bind {} to port {}", Process::current().uid(), class_name(), requested_local_port); return set_so_error(EACCES); } } @@ -603,7 +603,7 @@ KResult IPv4Socket::ioctl(FileDescription&, unsigned request, Userspace<void*> a switch (request) { case SIOCADDRT: - if (!Process::current()->is_superuser()) + if (!Process::current().is_superuser()) return EPERM; if (route.rt_gateway.sa_family != AF_INET) return EAFNOSUPPORT; @@ -628,7 +628,7 @@ KResult IPv4Socket::ioctl(FileDescription&, unsigned request, Userspace<void*> a switch (request) { case SIOCSARP: - if (!Process::current()->is_superuser()) + if (!Process::current().is_superuser()) return EPERM; if (arp_req.arp_pa.sa_family != AF_INET) return EAFNOSUPPORT; @@ -636,7 +636,7 @@ KResult IPv4Socket::ioctl(FileDescription&, unsigned request, Userspace<void*> a return KSuccess; case SIOCDARP: - if (!Process::current()->is_superuser()) + if (!Process::current().is_superuser()) return EPERM; if (arp_req.arp_pa.sa_family != AF_INET) return EAFNOSUPPORT; @@ -663,7 +663,7 @@ KResult IPv4Socket::ioctl(FileDescription&, unsigned request, Userspace<void*> a switch (request) { case SIOCSIFADDR: - if (!Process::current()->is_superuser()) + if (!Process::current().is_superuser()) return EPERM; if (ifr.ifr_addr.sa_family != AF_INET) return EAFNOSUPPORT; @@ -671,7 +671,7 @@ KResult IPv4Socket::ioctl(FileDescription&, unsigned request, Userspace<void*> a return KSuccess; case SIOCSIFNETMASK: - if (!Process::current()->is_superuser()) + if (!Process::current().is_superuser()) return EPERM; if (ifr.ifr_addr.sa_family != AF_INET) return EAFNOSUPPORT; diff --git a/Kernel/Net/LocalSocket.cpp b/Kernel/Net/LocalSocket.cpp index 7d23684d77..322d390913 100644 --- a/Kernel/Net/LocalSocket.cpp +++ b/Kernel/Net/LocalSocket.cpp @@ -61,7 +61,7 @@ KResultOr<SocketPair> LocalSocket::create_connected_pair(int type) socket->m_address.sun_family = AF_LOCAL; memcpy(socket->m_address.sun_path, "[socketpair]", 13); - auto& process = *Process::current(); + auto& process = Process::current(); socket->m_acceptor = { process.pid().value(), process.uid(), process.gid() }; socket->set_connected(true); @@ -80,9 +80,9 @@ LocalSocket::LocalSocket(int type, NonnullOwnPtr<DoubleBuffer> client_buffer, No , m_for_client(move(client_buffer)) , m_for_server(move(server_buffer)) { - auto current_process = Process::current(); - m_prebind_uid = current_process->euid(); - m_prebind_gid = current_process->egid(); + auto& current_process = Process::current(); + m_prebind_uid = current_process.euid(); + m_prebind_gid = current_process.egid(); m_prebind_mode = 0666; m_for_client->set_unblock_callback([this]() { @@ -137,7 +137,7 @@ KResult LocalSocket::bind(Userspace<const sockaddr*> user_address, socklen_t add mode_t mode = S_IFSOCK | (m_prebind_mode & 0777); UidAndGid owner { m_prebind_uid, m_prebind_gid }; - auto result = VirtualFileSystem::the().open(path, O_CREAT | O_EXCL | O_NOFOLLOW_NOERROR, mode, Process::current()->current_directory(), owner); + auto result = VirtualFileSystem::the().open(path, O_CREAT | O_EXCL | O_NOFOLLOW_NOERROR, mode, Process::current().current_directory(), owner); if (result.is_error()) { if (result.error() == EEXIST) return set_so_error(EADDRINUSE); @@ -179,7 +179,7 @@ KResult LocalSocket::connect(FileDescription& description, Userspace<const socka dbgln_if(LOCAL_SOCKET_DEBUG, "LocalSocket({}) connect({})", this, safe_address); - auto description_or_error = VirtualFileSystem::the().open(safe_address, O_RDWR, 0, Process::current()->current_directory()); + auto description_or_error = VirtualFileSystem::the().open(safe_address, O_RDWR, 0, Process::current().current_directory()); if (description_or_error.is_error()) return set_so_error(ECONNREFUSED); @@ -445,8 +445,8 @@ KResult LocalSocket::chown(FileDescription&, uid_t uid, gid_t gid) if (m_file) return m_file->chown(uid, gid); - auto current_process = Process::current(); - if (!current_process->is_superuser() && (current_process->euid() != uid || !current_process->in_group(gid))) + auto& current_process = Process::current(); + if (!current_process.is_superuser() && (current_process.euid() != uid || !current_process.in_group(gid))) return set_so_error(EPERM); m_prebind_uid = uid; diff --git a/Kernel/Net/Socket.cpp b/Kernel/Net/Socket.cpp index a3c13316ad..f67339dfb3 100644 --- a/Kernel/Net/Socket.cpp +++ b/Kernel/Net/Socket.cpp @@ -34,7 +34,7 @@ Socket::Socket(int domain, int type, int protocol) , m_type(type) , m_protocol(protocol) { - auto& process = *Process::current(); + auto& process = Process::current(); m_origin = { process.pid().value(), process.uid(), process.gid() }; } @@ -57,7 +57,7 @@ RefPtr<Socket> Socket::accept() dbgln_if(SOCKET_DEBUG, "Socket({}) de-queueing connection", this); auto client = m_pending.take_first(); VERIFY(!client->is_connected()); - auto& process = *Process::current(); + auto& process = Process::current(); client->m_acceptor = { process.pid().value(), process.uid(), process.gid() }; client->m_connected = true; client->m_role = Role::Accepted; |