All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/4] squashfs: Fix integer overflow in sqfs_resolve_symlink()
@ 2024-07-12  8:23 Richard Weinberger
  2024-07-12  8:23 ` [PATCH 2/4] squashfs: Fix integer overflow in sqfs_inode_size() Richard Weinberger
                   ` (3 more replies)
  0 siblings, 4 replies; 11+ messages in thread
From: Richard Weinberger @ 2024-07-12  8:23 UTC (permalink / raw)
  To: u-boot
  Cc: jmcosta944, thomas.petazzoni, miquel.raynal, trini,
	upstream+uboot, Richard Weinberger

A carefully crafted squashfs filesystem can exhibit an inode size of 0xffffffff,
as a consequence malloc() will do a zero allocation.
Later in the function the inode size is again used for copying data.
So an attacker can overwrite memory.
Avoid the overflow by using the __builtin_add_overflow() helper.

Signed-off-by: Richard Weinberger <richard@nod.at>
---
 fs/squashfs/sqfs.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/fs/squashfs/sqfs.c b/fs/squashfs/sqfs.c
index 1430e671a5..16a07c0622 100644
--- a/fs/squashfs/sqfs.c
+++ b/fs/squashfs/sqfs.c
@@ -422,8 +422,10 @@ static char *sqfs_resolve_symlink(struct squashfs_symlink_inode *sym,
 	char *resolved, *target;
 	u32 sz;
 
-	sz = get_unaligned_le32(&sym->symlink_size);
-	target = malloc(sz + 1);
+	if (__builtin_add_overflow(get_unaligned_le32(&sym->symlink_size), 1, &sz))
+		return NULL;
+
+	target = malloc(sz);
 	if (!target)
 		return NULL;
 
@@ -431,9 +433,9 @@ static char *sqfs_resolve_symlink(struct squashfs_symlink_inode *sym,
 	 * There is no trailling null byte in the symlink's target path, so a
 	 * copy is made and a '\0' is added at its end.
 	 */
-	target[sz] = '\0';
+	target[sz - 1] = '\0';
 	/* Get target name (relative path) */
-	strncpy(target, sym->symlink, sz);
+	strncpy(target, sym->symlink, sz - 1);
 
 	/* Relative -> absolute path conversion */
 	resolved = sqfs_get_abs_path(base_path, target);
-- 
2.35.3


^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2024-07-17  8:45 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-12  8:23 [PATCH 1/4] squashfs: Fix integer overflow in sqfs_resolve_symlink() Richard Weinberger
2024-07-12  8:23 ` [PATCH 2/4] squashfs: Fix integer overflow in sqfs_inode_size() Richard Weinberger
2024-07-17  7:59   ` Miquel Raynal
2024-07-12  8:23 ` [PATCH 3/4] squashfs: Check sqfs_find_inode() return value Richard Weinberger
2024-07-17  8:00   ` Miquel Raynal
2024-07-12  8:23 ` [PATCH 4/4] squashfs: Fix stack overflow while symlink resolving Richard Weinberger
2024-07-17  8:06   ` Miquel Raynal
2024-07-17  8:16     ` Richard Weinberger
2024-07-17  8:26       ` Miquel Raynal
2024-07-17  8:45         ` Richard Weinberger
2024-07-17  7:59 ` [PATCH 1/4] squashfs: Fix integer overflow in sqfs_resolve_symlink() Miquel Raynal

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.