From c03870c206dd07ded2c7dd4e7046d644f7c5c7fb Mon Sep 17 00:00:00 2001
From: Richard Hughes <richard@hughsie.com>
Date: Tue, 23 May 2023 12:12:31 +0100
Subject: [PATCH] Do not assume a file descriptor of zero is invalid

The open() call says than any valid result is 'a nonnegative integer'.

See also https://github.com/fwupd/fwupd/discussions/5841
---
 libfwupdplugin/fu-udev-device.c   | 17 +++++++++--------
 plugins/logind/fu-logind-plugin.c |  9 +++++----
 2 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/libfwupdplugin/fu-udev-device.c b/libfwupdplugin/fu-udev-device.c
index ffafc04c1..dd0d19c37 100644
--- a/libfwupdplugin/fu-udev-device.c
+++ b/libfwupdplugin/fu-udev-device.c
@@ -1432,7 +1432,7 @@ fu_udev_device_set_fd(FuUdevDevice *self, gint fd)
 	FuUdevDevicePrivate *priv = GET_PRIVATE(self);
 
 	g_return_if_fail(FU_IS_UDEV_DEVICE(self));
-	if (priv->fd > 0)
+	if (priv->fd >= 0)
 		close(priv->fd);
 	priv->fd = fd;
 }
@@ -1554,10 +1554,10 @@ fu_udev_device_close(FuDevice *device, GError **error)
 	FuUdevDevicePrivate *priv = GET_PRIVATE(self);
 
 	/* close device */
-	if (priv->fd > 0) {
+	if (priv->fd >= 0) {
 		if (!g_close(priv->fd, error))
 			return FALSE;
-		priv->fd = 0;
+		priv->fd = -1;
 	}
 
 	/* success */
@@ -1598,7 +1598,7 @@ fu_udev_device_ioctl(FuUdevDevice *self,
 	g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
 
 	/* not open! */
-	if (priv->fd == 0) {
+	if (priv->fd < 0) {
 		g_set_error(error,
 			    FWUPD_ERROR,
 			    FWUPD_ERROR_INTERNAL,
@@ -1679,7 +1679,7 @@ fu_udev_device_pread(FuUdevDevice *self, goffset port, guint8 *buf, gsize bufsz,
 	g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
 
 	/* not open! */
-	if (priv->fd == 0) {
+	if (priv->fd < 0) {
 		g_set_error(error,
 			    FWUPD_ERROR,
 			    FWUPD_ERROR_INTERNAL,
@@ -1730,7 +1730,7 @@ fu_udev_device_seek(FuUdevDevice *self, goffset offset, GError **error)
 	g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
 
 	/* not open! */
-	if (priv->fd == 0) {
+	if (priv->fd < 0) {
 		g_set_error(error,
 			    FWUPD_ERROR,
 			    FWUPD_ERROR_INTERNAL,
@@ -1787,7 +1787,7 @@ fu_udev_device_pwrite(FuUdevDevice *self,
 	g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
 
 	/* not open! */
-	if (priv->fd == 0) {
+	if (priv->fd < 0) {
 		g_set_error(error,
 			    FWUPD_ERROR,
 			    FWUPD_ERROR_INTERNAL,
@@ -2283,7 +2283,7 @@ fu_udev_device_finalize(GObject *object)
 	g_free(priv->device_file);
 	if (priv->udev_device != NULL)
 		g_object_unref(priv->udev_device);
-	if (priv->fd > 0)
+	if (priv->fd >= 0)
 		g_close(priv->fd, NULL);
 
 	G_OBJECT_CLASS(fu_udev_device_parent_class)->finalize(object);
@@ -2293,6 +2293,7 @@ static void
 fu_udev_device_init(FuUdevDevice *self)
 {
 	FuUdevDevicePrivate *priv = GET_PRIVATE(self);
+	priv->fd = -1;
 	priv->flags = FU_UDEV_DEVICE_FLAG_OPEN_READ | FU_UDEV_DEVICE_FLAG_OPEN_WRITE;
 	fu_device_set_acquiesce_delay(FU_DEVICE(self), 2500);
 }
diff --git a/plugins/logind/fu-logind-plugin.c b/plugins/logind/fu-logind-plugin.c
index cd9b8f1de..38ffb31a4 100644
--- a/plugins/logind/fu-logind-plugin.c
+++ b/plugins/logind/fu-logind-plugin.c
@@ -72,7 +72,7 @@ fu_logind_plugin_prepare(FuPlugin *plugin,
 			    "handle-hibernate-key:handle-lid-switch";
 
 	/* already inhibited */
-	if (self->logind_fd != 0)
+	if (self->logind_fd >= 0)
 		return TRUE;
 
 	/* not yet connected */
@@ -115,25 +115,26 @@ fu_logind_plugin_cleanup(FuPlugin *plugin,
 			 GError **error)
 {
 	FuLogindPlugin *self = FU_LOGIND_PLUGIN(plugin);
-	if (self->logind_fd == 0)
+	if (self->logind_fd < 0)
 		return TRUE;
 	g_debug("closed logind fd %i", self->logind_fd);
 	if (!g_close(self->logind_fd, error))
 		return FALSE;
-	self->logind_fd = 0;
+	self->logind_fd = -1;
 	return TRUE;
 }
 
 static void
 fu_logind_plugin_init(FuLogindPlugin *self)
 {
+	self->logind_fd = -1;
 }
 
 static void
 fu_logind_finalize(GObject *obj)
 {
 	FuLogindPlugin *self = FU_LOGIND_PLUGIN(obj);
-	if (self->logind_fd != 0)
+	if (self->logind_fd >= 0)
 		g_close(self->logind_fd, NULL);
 	if (self->logind_proxy != NULL)
 		g_object_unref(self->logind_proxy);
-- 
2.31.1

