Linux Security Modules development
 help / color / mirror / Atom feed
From: Peng Hao <flyingpenghao@gmail.com>
To: mic@digikod.net, gnoack@google.com
Cc: linux-security-module@vger.kernel.org
Subject: [PATCH] landlock: shrink tsync works[] on partial allocation failure
Date: Tue, 23 Jun 2026 13:01:27 +0800	[thread overview]
Message-ID: <20260623050127.48247-1-flyingpeng@tencent.com> (raw)

When the per-slot kzalloc fails mid-loop in tsync_works_grow_by(), the
already-enlarged s->works array keeps uninitialized trailing entries.
Shrink the array back to its used size on the error path so no waste
is carried over: free it outright when nothing has been allocated yet,
otherwise try a shrinking krealloc_array() (keep the larger array if
the shrink fails, since tsync_works_release() honors s->capacity).

Signed-off-by: Peng Hao <flyingpeng@tencent.com>
---
 security/landlock/tsync.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/security/landlock/tsync.c b/security/landlock/tsync.c
index c5730bbd..356ce94b 100644
--- a/security/landlock/tsync.c
+++ b/security/landlock/tsync.c
@@ -272,9 +272,23 @@ static int tsync_works_grow_by(struct tsync_works *s, size_t n, gfp_t flags)
 		work = kzalloc_obj(*work, flags);
 		if (!work) {
 			/*
-			 * Leave the object in a consistent state,
-			 * but return an error.
+			 * Leave the object in a consistent state, but return
+			 * an error.  Shrink @s->works back to its used size to
+			 * avoid carrying uninitialized trailing entries.  A
+			 * shrinking krealloc_array() should normally succeed,
+			 * but if it does not we simply keep the larger array;
+			 * tsync_works_release() iterates only up to capacity.
 			 */
+			if (i == 0) {
+				kfree(s->works);
+				s->works = NULL;
+			} else {
+				works = krealloc_array(s->works, i,
+						       sizeof(s->works[0]),
+						       flags | __GFP_NOWARN);
+				if (works)
+					s->works = works;
+			}
 			s->capacity = i;
 			return -ENOMEM;
 		}
-- 
2.43.7


             reply	other threads:[~2026-06-23  5:01 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-23  5:01 Peng Hao [this message]
2026-06-23  9:45 ` [PATCH] landlock: shrink tsync works[] on partial allocation failure Günther Noack

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260623050127.48247-1-flyingpeng@tencent.com \
    --to=flyingpenghao@gmail.com \
    --cc=gnoack@google.com \
    --cc=linux-security-module@vger.kernel.org \
    --cc=mic@digikod.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox