From 392a2cba66be7a4b7ba9b8c60a06ba4a7b48d2b2 Mon Sep 17 00:00:00 2001 From: Johannes Schlatow Date: Thu, 10 Feb 2022 13:47:14 +0100 Subject: [PATCH] libc: fix page fault in socket_fs_plugin In the error case of socket_fs_accept() the Unconfirmed utility was incompletely applied with the result of executing the cleanup routines in the wrong order. Fixes #4417 --- .../src/lib/libc/internal/unconfirmed.h | 41 ------------------- .../libports/src/lib/libc/socket_fs_plugin.cc | 5 +-- 2 files changed, 1 insertion(+), 45 deletions(-) delete mode 100644 repos/libports/src/lib/libc/internal/unconfirmed.h diff --git a/repos/libports/src/lib/libc/internal/unconfirmed.h b/repos/libports/src/lib/libc/internal/unconfirmed.h deleted file mode 100644 index 280264f388..0000000000 --- a/repos/libports/src/lib/libc/internal/unconfirmed.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * \brief Utility to automatically unroll unconfirmed operations - * \author Christian Helmuth - * \date 2020-07-31 - */ - -/* - * Copyright (C) 2020 Genode Labs GmbH - * - * This file is part of the Genode OS framework, which is distributed - * under the terms of the GNU Affero General Public License version 3. - */ - -#ifndef _LIBC__INTERNAL__UNCONFIRMED_H_ -#define _LIBC__INTERNAL__UNCONFIRMED_H_ - -namespace Libc { - template struct Unconfirmed; - - template - Unconfirmed make_unconfirmed(FUNC const &cleanup) - { - return { cleanup }; - } -}; - - -template -struct Libc::Unconfirmed -{ - bool confirmed { false }; - - FUNC const &cleanup; - - Unconfirmed(FUNC const &cleanup) : cleanup(cleanup) { } - ~Unconfirmed() { if (!confirmed) cleanup(); } - - void confirm() { confirmed = true; } -}; - -#endif /* _LIBC__INTERNAL__UNCONFIRMED_H_ */ diff --git a/repos/libports/src/lib/libc/socket_fs_plugin.cc b/repos/libports/src/lib/libc/socket_fs_plugin.cc index d011ea3b84..7295ab3be7 100644 --- a/repos/libports/src/lib/libc/socket_fs_plugin.cc +++ b/repos/libports/src/lib/libc/socket_fs_plugin.cc @@ -43,7 +43,6 @@ #include #include #include -#include namespace Libc { @@ -604,12 +603,11 @@ extern "C" int socket_fs_accept(int libc_fd, sockaddr *addr, socklen_t *addrlen) return Errno(EMFILE); } - auto _accept_fd = make_unconfirmed([&] { file_descriptor_allocator()->free(accept_fd); }); - if (addr && addrlen) { Socket_fs::Remote_functor func(*accept_context, false); int ret = read_sockaddr_in(func, (sockaddr_in *)addr, addrlen); if (ret == -1) { + file_descriptor_allocator()->free(accept_fd); Libc::Allocator alloc { }; destroy(alloc, accept_context); return ret; @@ -619,7 +617,6 @@ extern "C" int socket_fs_accept(int libc_fd, sockaddr *addr, socklen_t *addrlen) /* inherit the O_NONBLOCK flag if set */ accept_context->fd_flags(listen_context->fd_flags()); - _accept_fd.confirm(); return accept_fd->libc_fd; }