diff mbox series

[meta-oe] libuio: fix FILE descriptor leak

Message ID 20260528004753.1193-1-qili00001@gmail.com
State New
Headers show
Series [meta-oe] libuio: fix FILE descriptor leak | expand

Commit Message

Qliangw May 28, 2026, 12:47 a.m. UTC
The function uio_line_from_file() fails to close the FILE pointer
when fgets() returns NULL, causing a file descriptor leak.

This can be triggered when reading from /sys files that return
empty content, leading to resource exhaustion over time.

Fix this by using goto-based error handling to ensure fclose()
is called on all exit paths.

Signed-off-by: Qliangw <qili00001@gmail.com>
---
 ...ix-fclose-leak-in-uio_line_from_file.patch | 31 +++++++++++++++++++
 .../libuio/libuio_0.2.1.bbappend              |  3 ++
 2 files changed, 34 insertions(+)
 create mode 100644 meta-oe/recipes-extended/libuio/libuio/fix-fclose-leak-in-uio_line_from_file.patch
 create mode 100644 meta-oe/recipes-extended/libuio/libuio_0.2.1.bbappend

Comments

Ankur Tyagi May 28, 2026, 10:34 a.m. UTC | #1
On Thu, May 28, 2026 at 5:13 PM Qliangw via lists.openembedded.org
<qili00001=gmail.com@lists.openembedded.org> wrote:
>
> The function uio_line_from_file() fails to close the FILE pointer
> when fgets() returns NULL, causing a file descriptor leak.
>
> This can be triggered when reading from /sys files that return
> empty content, leading to resource exhaustion over time.
>
> Fix this by using goto-based error handling to ensure fclose()
> is called on all exit paths.
>
> Signed-off-by: Qliangw <qili00001@gmail.com>
> ---
>  ...ix-fclose-leak-in-uio_line_from_file.patch | 31 +++++++++++++++++++
>  .../libuio/libuio_0.2.1.bbappend              |  3 ++
>  2 files changed, 34 insertions(+)
>  create mode 100644 meta-oe/recipes-extended/libuio/libuio/fix-fclose-leak-in-uio_line_from_file.patch
>  create mode 100644 meta-oe/recipes-extended/libuio/libuio_0.2.1.bbappend
>
> diff --git a/meta-oe/recipes-extended/libuio/libuio/fix-fclose-leak-in-uio_line_from_file.patch b/meta-oe/recipes-extended/libuio/libuio/fix-fclose-leak-in-uio_line_from_file.patch
> new file mode 100644
> index 0000000000..b5c984c23d
> --- /dev/null
> +++ b/meta-oe/recipes-extended/libuio/libuio/fix-fclose-leak-in-uio_line_from_file.patch
> @@ -0,0 +1,31 @@
> +From: Qliangw <qili00001@gmail.com>
> +Subject: [PATCH] Fix FILE descriptor leak in uio_line_from_file()
> +
> +The function uio_line_from_file() fails to close the FILE pointer
> +when fgets() returns NULL, causing a file descriptor leak.
> +
> +Upstream-Status: Pending

As per[1], this status is an exception. Please include valid
justification for it.

> +Signed-off-by: Qliangw <qili00001@gmail.com>
> +
> +--- a/src/uio_line_from_file.c
> ++++ b/src/uio_line_from_file.c
> +@@ -28,14 +28,17 @@
> + {
> +       char *s;
> +       int i;
> ++      int ret = 0;
> +       memset(linebuf, 0, UIO_MAX_NAME_SIZE);
> +       FILE* file = fopen(filename,"r");
> +       if (!file) return -1;
> +       s = fgets(linebuf,UIO_MAX_NAME_SIZE,file);
> +-      if (!s) return -2;
> ++      if (!s) { ret = -2; goto out; }
> +       for (i=0; (*s)&&(i<UIO_MAX_NAME_SIZE); i++) {
> +               if (*s == '\n') *s = 0;
> +               s++;
> +       }
> +-      return 0;
> ++out:
> ++      fclose(file);
> ++      return ret;
> + }
> diff --git a/meta-oe/recipes-extended/libuio/libuio_0.2.1.bbappend b/meta-oe/recipes-extended/libuio/libuio_0.2.1.bbappend

No need to add bbappend file, simply update SRC_URI in libuio_0.2.1bb
recipe file

> new file mode 100644
> index 0000000000..d0e0dbb9a8
> --- /dev/null
> +++ b/meta-oe/recipes-extended/libuio/libuio_0.2.1.bbappend
> @@ -0,0 +1,3 @@
> +FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
> +
> +SRC_URI += "file://fix-fclose-leak-in-uio_line_from_file.patch"
> --
> 2.53.0.windows.2
>
>
> -=-=-=-=-=-=-=-=-=-=-=-
> Links: You receive all messages sent to this group.
> View/Reply Online (#127271): https://lists.openembedded.org/g/openembedded-devel/message/127271
> Mute This Topic: https://lists.openembedded.org/mt/119525599/3619737
> Group Owner: openembedded-devel+owner@lists.openembedded.org
> Unsubscribe: https://lists.openembedded.org/g/openembedded-devel/unsub [ankur.tyagi85@gmail.com]
> -=-=-=-=-=-=-=-=-=-=-=-
>
diff mbox series

Patch

diff --git a/meta-oe/recipes-extended/libuio/libuio/fix-fclose-leak-in-uio_line_from_file.patch b/meta-oe/recipes-extended/libuio/libuio/fix-fclose-leak-in-uio_line_from_file.patch
new file mode 100644
index 0000000000..b5c984c23d
--- /dev/null
+++ b/meta-oe/recipes-extended/libuio/libuio/fix-fclose-leak-in-uio_line_from_file.patch
@@ -0,0 +1,31 @@ 
+From: Qliangw <qili00001@gmail.com>
+Subject: [PATCH] Fix FILE descriptor leak in uio_line_from_file()
+
+The function uio_line_from_file() fails to close the FILE pointer
+when fgets() returns NULL, causing a file descriptor leak.
+
+Upstream-Status: Pending
+Signed-off-by: Qliangw <qili00001@gmail.com>
+
+--- a/src/uio_line_from_file.c
++++ b/src/uio_line_from_file.c
+@@ -28,14 +28,17 @@
+ {
+ 	char *s;
+ 	int i;
++	int ret = 0;
+ 	memset(linebuf, 0, UIO_MAX_NAME_SIZE);
+ 	FILE* file = fopen(filename,"r");
+ 	if (!file) return -1;
+ 	s = fgets(linebuf,UIO_MAX_NAME_SIZE,file);
+-	if (!s) return -2;
++	if (!s) { ret = -2; goto out; }
+ 	for (i=0; (*s)&&(i<UIO_MAX_NAME_SIZE); i++) {
+ 		if (*s == '\n') *s = 0;
+ 		s++;
+ 	}
+-	return 0;
++out:
++	fclose(file);
++	return ret;
+ }
diff --git a/meta-oe/recipes-extended/libuio/libuio_0.2.1.bbappend b/meta-oe/recipes-extended/libuio/libuio_0.2.1.bbappend
new file mode 100644
index 0000000000..d0e0dbb9a8
--- /dev/null
+++ b/meta-oe/recipes-extended/libuio/libuio_0.2.1.bbappend
@@ -0,0 +1,3 @@ 
+FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
+
+SRC_URI += "file://fix-fclose-leak-in-uio_line_from_file.patch"