[PATCH] nproc: Verify the cgroup path isn't truncated.

Collin Funk <[email protected]>
Newsgroups gmane.comp.lib.gnulib.bugs
Message-ID <72654bda17604d1c16d3549245bc0692f8ab8a54.1786863057.git.collin.funk1@gmail.com>
I don't think it is actually possible for this case to happen, but
checking it does no harm.

Lasse Collin mentioned it to me while we were discussing some cgroup
stuff a while ago.

-- 8< --

* lib/nproc.c (get_cgroup2_cpu_quota): Check the return value of
snprintf to make sure the path isn't truncated.
---
 ChangeLog   | 6 ++++++
 lib/nproc.c | 7 ++++---
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 65793e578c..d56394b482 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2026-08-15  Collin Funk  <[email protected]>
+
+	nproc: Verify the cgroup path isn't truncated.
+	* lib/nproc.c (get_cgroup2_cpu_quota): Check the return value of
+	snprintf to make sure the path isn't truncated.
+
 2026-08-14  Bruno Haible  <[email protected]>
 
 	Add unit tests and doc for the %Lf large precision issue on Solaris.
diff --git a/lib/nproc.c b/lib/nproc.c
index ef86975fb1..e3dfeaa16d 100644
--- a/lib/nproc.c
+++ b/lib/nproc.c
@@ -437,10 +437,11 @@ get_cgroup2_cpu_quota (void)
          is not enabled for that part of the hierarchy.  */
 
       char cpu_max_file[PATH_MAX];
-      snprintf (cpu_max_file, sizeof (cpu_max_file),
-                "%s%s/cpu.max", mount, cgroup);
+      int n = snprintf (cpu_max_file, sizeof (cpu_max_file),
+                        "%s%s/cpu.max", mount, cgroup);
 
-      if ((fp = fopen (cpu_max_file, "r"))
+      if (0 <= n && n < sizeof cpu_max_file
+          && (fp = fopen (cpu_max_file, "r"))
           && getline (&quota_str, &quota_size, fp) != -1
           && strncmp (quota_str, "max", 3) != 0)
         {
-- 
2.55.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.