Skip to content

cgroup: fix memory issue when resizing array of strings#2109

Merged
giuseppe merged 1 commit into
containers:mainfrom
leonardomoreira00:bug/fix-oom-systemd-annotation
Jun 22, 2026
Merged

cgroup: fix memory issue when resizing array of strings#2109
giuseppe merged 1 commit into
containers:mainfrom
leonardomoreira00:bug/fix-oom-systemd-annotation

Conversation

@leonardomoreira00

@leonardomoreira00 leonardomoreira00 commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

The function append_systemd_annotation() initializes the array of strings pointers char ** using the correct byte size, but when it is resized, the code uses a hard-coded element count instead of the actual number of bytes required:

For details see the issue:

Closes: #2106

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes a memory allocation bug in src/libcrun/cgroup-systemd.c where xrealloc was being called with the number of elements instead of the total byte size. The fix correctly multiplies parts_size by sizeof(char *). There are no review comments, so I have no feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@packit-as-a-service

Copy link
Copy Markdown

TMT tests failed. @containers/packit-build please check.

@giuseppe giuseppe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@giuseppe

Copy link
Copy Markdown
Member

please amend the change suggested by make clang-format:

diff --git a/src/libcrun/cgroup-systemd.c b/src/libcrun/cgroup-systemd.c
index 9c43148..793da6f 100644
--- a/src/libcrun/cgroup-systemd.c
+++ b/src/libcrun/cgroup-systemd.c
@@ -671,7 +671,7 @@ append_systemd_annotation (sd_bus_message *m, const char *name, size_t name_len,
           if (n_parts == parts_size - 1)
             {
               parts_size += 32;
-              parts = xrealloc (parts, sizeof(char *) * parts_size);
+              parts = xrealloc (parts, sizeof (char *) * parts_size);
             }
           parts[n_parts] = NULL;
           if (next == NULL)

Signed-off-by: Leonardo Moreira <leonardo.moreira.coutinho@gmail.com>
@leonardomoreira00 leonardomoreira00 force-pushed the bug/fix-oom-systemd-annotation branch from 9e5a326 to 8d9551f Compare June 22, 2026 09:53

@giuseppe giuseppe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@leonardomoreira00

Copy link
Copy Markdown
Contributor Author

please amend the change suggested by make clang-format:

diff --git a/src/libcrun/cgroup-systemd.c b/src/libcrun/cgroup-systemd.c
index 9c43148..793da6f 100644
--- a/src/libcrun/cgroup-systemd.c
+++ b/src/libcrun/cgroup-systemd.c
@@ -671,7 +671,7 @@ append_systemd_annotation (sd_bus_message *m, const char *name, size_t name_len,
           if (n_parts == parts_size - 1)
             {
               parts_size += 32;
-              parts = xrealloc (parts, sizeof(char *) * parts_size);
+              parts = xrealloc (parts, sizeof (char *) * parts_size);
             }
           parts[n_parts] = NULL;
           if (next == NULL)

Fixed!
Sorry, I am still familiarizing myself with the repo in the small windows of time I get between newborn duties.
I overlook it when I was reading the contribution guide. Next time I will do it right 👍

@giuseppe giuseppe merged commit d17fd87 into containers:main Jun 22, 2026
44 of 46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cgroup: systemd annotation arrays can corrupt heap when more than 31 entries are parsed

2 participants