From patchwork Sat May 7 23:54:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Depoire--Ferrer X-Patchwork-Id: 12842206 Received: from gregdf.com (gregdf.com [152.228.171.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9CB047F for ; Sun, 8 May 2022 00:00:23 +0000 (UTC) Received: from localhost.localdomain (unknown [37.167.202.147]) by gregdf.com (Postfix) with ESMTPSA id A910D796F3C; Sun, 8 May 2022 01:49:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gregdf.com; s=mail; t=1651967370; bh=8mX4dgoR696QuxGx3YYhXhk6x7C/d7sbAi6Ruz/96Ms=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=ICzokZ0iQGXvr4TCZyHAE9OdkMkhKE35wgDJkXVHZ5QwvKRthUEzFUi78ZPIXgpDQ +KpG0JnqoD83OMy4XzW+dwss+aM+F2Hcva0ExXNyKFeOWFQH6olRHWFeGBSeCF9M0P 9RUDmG1JL+rfTiru9l6CtJKO06fmmBVplWh48GNs= From: Greg Depoire--Ferrer To: ell@lists.linux.dev Cc: Greg Depoire--Ferrer Subject: [PATCH 1/3] main: Accept FD 0 as epoll_fd Date: Sun, 8 May 2022 01:54:32 +0200 Message-Id: <20220507235434.11693-2-greg@gregdf.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220507235434.11693-1-greg@gregdf.com> References: <20220507235434.11693-1-greg@gregdf.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When create_epoll was called and the file descriptor 0 was not open, the kernel would return it as the epoll FD, but ell would consider it as an error because it used the value 0 in epoll_fd to signify failure. Use -1 instead of 0 to signify error to fix the issue. --- ell/main.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/ell/main.c b/ell/main.c index 1a6cd60..6f9a074 100644 --- a/ell/main.c +++ b/ell/main.c @@ -61,7 +61,7 @@ #define WATCHDOG_TRIGGER_FREQ 2 -static int epoll_fd; +static int epoll_fd = -1; static bool epoll_running; static bool epoll_terminate; static int idle_id; @@ -99,10 +99,8 @@ static inline bool __attribute__ ((always_inline)) create_epoll(void) unsigned int i; epoll_fd = epoll_create1(EPOLL_CLOEXEC); - if (epoll_fd < 0) { - epoll_fd = 0; + if (epoll_fd < 0) return false; - } watch_list = malloc(DEFAULT_WATCH_ENTRIES * sizeof(void *)); if (!watch_list) @@ -121,7 +119,7 @@ static inline bool __attribute__ ((always_inline)) create_epoll(void) close_epoll: close(epoll_fd); - epoll_fd = 0; + epoll_fd = -1; return false; } @@ -136,7 +134,7 @@ int watch_add(int fd, uint32_t events, watch_event_cb_t callback, if (unlikely(fd < 0 || !callback)) return -EINVAL; - if (!epoll_fd) + if (epoll_fd < 0) return -EIO; if ((unsigned int) fd > watch_entries - 1) @@ -284,7 +282,7 @@ int idle_add(idle_event_cb_t callback, void *user_data, uint32_t flags, if (unlikely(!callback)) return -EINVAL; - if (!epoll_fd) + if (epoll_fd < 0) return -EIO; data = l_new(struct idle_data, 1); @@ -509,7 +507,7 @@ LIB_EXPORT int l_main_run(void) int timeout; /* Has l_main_init() been called? */ - if (unlikely(!epoll_fd)) + if (unlikely(epoll_fd < 0)) return EXIT_FAILURE; if (unlikely(epoll_running)) @@ -577,7 +575,7 @@ LIB_EXPORT bool l_main_exit(void) idle_list = NULL; close(epoll_fd); - epoll_fd = 0; + epoll_fd = -1; return true; } From patchwork Sat May 7 23:54:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Depoire--Ferrer X-Patchwork-Id: 12842207 Received: from gregdf.com (gregdf.com [152.228.171.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A41113D9F for ; Sun, 8 May 2022 00:00:23 +0000 (UTC) Received: from localhost.localdomain (unknown [37.167.202.147]) by gregdf.com (Postfix) with ESMTPSA id 3E3D3796F3E; Sun, 8 May 2022 01:49:32 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gregdf.com; s=mail; t=1651967372; bh=IZAUY/ehHHduqNH8QXXKtaHrM2CdtmyC/XW26xesmPo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=AA7jyDApTD1/El5vzeUoesCTL++B5fYTxOh8iFbTZAkDt3OqTzA8/IHPCf0MnrVJL OBwtzfSsNCffLG+YkFC0zYCgQ4aPrPuHlztogU3cDhU2dQVAq37rEMP4rEP5XcJ8Yb gdJA8maGLXNyytX1K26E3cREs1ocyLwTLk5tthqc= From: Greg Depoire--Ferrer To: ell@lists.linux.dev Cc: Greg Depoire--Ferrer Subject: [PATCH 2/3] dhcp6-transport: Remove udp_fd field Date: Sun, 8 May 2022 01:54:33 +0200 Message-Id: <20220507235434.11693-3-greg@gregdf.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220507235434.11693-1-greg@gregdf.com> References: <20220507235434.11693-1-greg@gregdf.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 _dhcp6_default_transport_new forgot to initialize the udp_fd field to a negative value. Because of this, _dhcp6_default_transport_close would try to close it. For instance, this could end up closing FD 0 (stdin) if memory allocation returned was zeroed. However, udp_fd is not actually used and is a left over from from the dhcp-transport.c file that was copied to implement DHCPv6 in commit b384838d1a79182f09b71a7a48d103ff790b8ae6 ("dhcp6: Add dhcp6 transport abstraction and default"), so it can just be removed. --- ell/dhcp6-transport.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/ell/dhcp6-transport.c b/ell/dhcp6-transport.c index 04bf4fb..8d15606 100644 --- a/ell/dhcp6-transport.c +++ b/ell/dhcp6-transport.c @@ -45,7 +45,6 @@ struct dhcp6_default_transport { struct dhcp6_transport super; struct l_io *io; - int udp_fd; uint16_t port; struct in6_addr local; }; @@ -214,11 +213,6 @@ static void _dhcp6_default_transport_close(struct dhcp6_transport *s) l_io_destroy(transport->io); transport->io = NULL; - - if (transport->udp_fd >= 0) { - L_TFR(close(transport->udp_fd)); - transport->udp_fd = -1; - } } void _dhcp6_transport_set_rx_callback(struct dhcp6_transport *transport, From patchwork Sat May 7 23:54:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Depoire--Ferrer X-Patchwork-Id: 12842205 Received: from gregdf.com (gregdf.com [152.228.171.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9ED6E3D9D for ; Sun, 8 May 2022 00:00:23 +0000 (UTC) Received: from localhost.localdomain (unknown [37.167.202.147]) by gregdf.com (Postfix) with ESMTPSA id B3C2D796F40; Sun, 8 May 2022 01:49:33 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gregdf.com; s=mail; t=1651967374; bh=wznD7WNT8+0b+9LntRTHA1HAXzL+b8R6BARGm0URa9s=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=KY4XEKIGfpU5OeKO4gp+gqXqAB0eQpJoEAx3DFnP6DyGYtvKIj2mtRTtBRfAYpiCY JHr/ttnVSRsT5isZFK5BXGfq0KdUB8FMwmlbv2loToxgGg7V8b2VvUc/7LWGXudG9Y Rc7h+TIDZ8UbhAdhknLCSYm+ftAgLBsq2fjXGYLk= From: Greg Depoire--Ferrer To: ell@lists.linux.dev Cc: Greg Depoire--Ferrer Subject: [PATCH 3/3] dhcp-transport: Initialize the udp_fd field Date: Sun, 8 May 2022 01:54:34 +0200 Message-Id: <20220507235434.11693-4-greg@gregdf.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220507235434.11693-1-greg@gregdf.com> References: <20220507235434.11693-1-greg@gregdf.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Initialize the udp_fd field to -1 in _dhcp_default_transport_new to prevent future mistakes where a random FD is closed when the transport is closed before the field is initialized. --- ell/dhcp-transport.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ell/dhcp-transport.c b/ell/dhcp-transport.c index d73930b..8ab6327 100644 --- a/ell/dhcp-transport.c +++ b/ell/dhcp-transport.c @@ -550,6 +550,7 @@ struct dhcp_transport *_dhcp_default_transport_new(uint32_t ifindex, transport->super.ifindex = ifindex; l_strlcpy(transport->ifname, ifname, IFNAMSIZ); transport->port = port; + transport->udp_fd = -1; return &transport->super; }