* [PATCH] ubifs: widen compr_type bitfields and guard uncompiled compressors
@ 2026-11-19 17:04 Ian Rogers
2026-11-20 6:14 ` Zhihao Cheng
0 siblings, 1 reply; 2+ messages in thread
From: Ian Rogers @ 2026-11-19 17:04 UTC (permalink / raw)
To: Richard Weinberger
Cc: Zhihao Cheng, linux-mtd, linux-kernel, llvm, Elkin Cruz, Ian Rogers
Commit eeabb9866e4c ("ubifs: Add support for zstd compression.") added
UBIFS_COMPR_ZSTD (3), raising UBIFS_COMPR_TYPES_CNT to 4. However,
compr_type in struct ubifs_inode and struct ubifs_mount_opts, as well as
default_compr in struct ubifs_info and struct ubifs_fs_context, remained
2-bit unsigned bitfields (0..3). Because ubifs_read_superblock() and
ubifs_iget() store 16-bit on-flash __le16 compressor IDs into those 2-bit
bitfields before validate_sb() and validate_inode() check
'>= UBIFS_COMPR_TYPES_CNT', out-of-range on-flash compressor IDs silently
wrap modulo 4 and render both validation checks dead code.
In addition, when an inode uses a valid compressor that is disabled in
Kconfig (!compr->capi_name, compr->cc == NULL), validate_inode() only
logs a warning and allows the inode to be opened, but
ubifs_compress_common() lacks ubifs_decompress_common()'s !compr->capi_name
check and passes NULL compr->cc into crypto_acomp_compress() on write.
Widen all four compression-type bitfields from 2 bits to 16 bits to match
the __le16 on-flash fields (fitting within existing 32-bit bitfield groups
without increasing any structure size), update the BUILD_BUG_ON() in
ubifs_init(), and fall back to uncompressed data with a warning in
ubifs_compress_common() if compr_type is invalid or not compiled in.
Found via an x86_64 LLVM=1 (Clang 21.1.8) allyesconfig audit with
-Wtautological-value-range-compare enabled; fix and changelog drafted with
an AI coding assistant. Verified with a standalone C bitfield/layout test
and build-tested across fs/ubifs/ under LLVM=1 W=1 and
-Wtautological-value-range-compare; not runtime-tested on a mounted UBIFS
volume.
Fixes: 1e51764a3c2a ("UBIFS: add new flash file system")
Fixes: eeabb9866e4c ("ubifs: Add support for zstd compression.")
Signed-off-by: Ian Rogers <irogers@google.com>
---
fs/ubifs/compress.c | 26 ++++++++++++++++++++------
fs/ubifs/super.c | 11 ++++++-----
fs/ubifs/ubifs.h | 6 +++---
3 files changed, 29 insertions(+), 14 deletions(-)
diff --git a/fs/ubifs/compress.c b/fs/ubifs/compress.c
index 059a02691edd..c5c21d30d846 100644
--- a/fs/ubifs/compress.c
+++ b/fs/ubifs/compress.c
@@ -73,11 +73,12 @@ static struct ubifs_compressor zstd_compr = {
/* All UBIFS compressors */
struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
-static void ubifs_compress_common(int *compr_type, union ubifs_in_ptr in_ptr,
- size_t in_offset, int in_len, bool in_folio,
- void *out_buf, int *out_len)
+static void ubifs_compress_common(const struct ubifs_info *c, int *compr_type,
+ union ubifs_in_ptr in_ptr, size_t in_offset,
+ int in_len, bool in_folio, void *out_buf,
+ int *out_len)
{
- struct ubifs_compressor *compr = ubifs_compressors[*compr_type];
+ struct ubifs_compressor *compr;
int dlen = *out_len;
int err;
@@ -88,6 +89,19 @@ static void ubifs_compress_common(int *compr_type, union ubifs_in_ptr in_ptr,
if (in_len < UBIFS_MIN_COMPR_LEN)
goto no_compr;
+ if (unlikely(*compr_type < 0 || *compr_type >= UBIFS_COMPR_TYPES_CNT)) {
+ ubifs_warn(c, "invalid compression type %d, leave data uncompressed",
+ *compr_type);
+ goto no_compr;
+ }
+
+ compr = ubifs_compressors[*compr_type];
+ if (unlikely(!compr->capi_name)) {
+ ubifs_warn(c, "%s compression is not compiled in, leave data uncompressed",
+ compr->name);
+ goto no_compr;
+ }
+
dlen = min(dlen, in_len - UBIFS_MIN_COMPRESS_DIFF);
do {
@@ -154,7 +168,7 @@ void ubifs_compress(const struct ubifs_info *c, const void *in_buf,
{
union ubifs_in_ptr in_ptr = { .buf = in_buf };
- ubifs_compress_common(compr_type, in_ptr, 0, in_len, false,
+ ubifs_compress_common(c, compr_type, in_ptr, 0, in_len, false,
out_buf, out_len);
}
@@ -184,7 +198,7 @@ void ubifs_compress_folio(const struct ubifs_info *c, struct folio *in_folio,
{
union ubifs_in_ptr in_ptr = { .folio = in_folio };
- ubifs_compress_common(compr_type, in_ptr, in_offset, in_len, true,
+ ubifs_compress_common(c, compr_type, in_ptr, in_offset, in_len, true,
out_buf, out_len);
}
diff --git a/fs/ubifs/super.c b/fs/ubifs/super.c
index 38972786817e..cca1b459d978 100644
--- a/fs/ubifs/super.c
+++ b/fs/ubifs/super.c
@@ -1019,7 +1019,7 @@ struct ubifs_fs_context {
char *auth_hash_name;
unsigned int no_chk_data_crc:1;
unsigned int bulk_read:1;
- unsigned int default_compr:2;
+ unsigned int default_compr:16;
unsigned int assert_action:2;
};
@@ -2424,12 +2424,13 @@ static int __init ubifs_init(void)
BUILD_BUG_ON(UBIFS_REF_NODE_SZ != 64);
/*
- * We use 2 bit wide bit-fields to store compression type, which should
- * be amended if more compressors are added. The bit-fields are:
+ * We use 16 bit wide bit-fields to store compression type, matching
+ * the 16-bit on-flash fields. The bit-fields are:
* @compr_type in 'struct ubifs_inode', @default_compr in
- * 'struct ubifs_info' and @compr_type in 'struct ubifs_mount_opts'.
+ * 'struct ubifs_info' and 'struct ubifs_fs_context', and @compr_type
+ * in 'struct ubifs_mount_opts'.
*/
- BUILD_BUG_ON(UBIFS_COMPR_TYPES_CNT > 4);
+ BUILD_BUG_ON(UBIFS_COMPR_TYPES_CNT > (1 << 16));
/*
* We require that PAGE_SIZE is greater-than-or-equal-to
diff --git a/fs/ubifs/ubifs.h b/fs/ubifs/ubifs.h
index b85b45a0564b..f2511f866c38 100644
--- a/fs/ubifs/ubifs.h
+++ b/fs/ubifs/ubifs.h
@@ -406,7 +406,7 @@ struct ubifs_inode {
unsigned int dirty:1;
unsigned int xattr:1;
unsigned int bulk_read:1;
- unsigned int compr_type:2;
+ unsigned int compr_type:16;
struct mutex ui_mutex;
struct rw_semaphore xattr_sem;
spinlock_t ui_lock;
@@ -948,7 +948,7 @@ struct ubifs_mount_opts {
unsigned int bulk_read:2;
unsigned int chk_data_crc:2;
unsigned int override_compr:1;
- unsigned int compr_type:2;
+ unsigned int compr_type:16;
};
/**
@@ -1311,7 +1311,7 @@ struct ubifs_info {
unsigned int encrypted:1;
unsigned int no_chk_data_crc:1;
unsigned int bulk_read:1;
- unsigned int default_compr:2;
+ unsigned int default_compr:16;
unsigned int rw_incompat:1;
unsigned int assert_action:2;
unsigned int authenticated:1;
--
2.56.1.708.g77570a6a1d-goog
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH] ubifs: widen compr_type bitfields and guard uncompiled compressors
2026-11-19 17:04 [PATCH] ubifs: widen compr_type bitfields and guard uncompiled compressors Ian Rogers
@ 2026-11-20 6:14 ` Zhihao Cheng
0 siblings, 0 replies; 2+ messages in thread
From: Zhihao Cheng @ 2026-11-20 6:14 UTC (permalink / raw)
To: Ian Rogers, Richard Weinberger; +Cc: linux-mtd, linux-kernel, llvm, Elkin Cruz
在 2026/11/20 1:04, Ian Rogers 写道:
> Commit eeabb9866e4c ("ubifs: Add support for zstd compression.") added
> UBIFS_COMPR_ZSTD (3), raising UBIFS_COMPR_TYPES_CNT to 4. However,
> compr_type in struct ubifs_inode and struct ubifs_mount_opts, as well as
> default_compr in struct ubifs_info and struct ubifs_fs_context, remained
> 2-bit unsigned bitfields (0..3). Because ubifs_read_superblock() and
> ubifs_iget() store 16-bit on-flash __le16 compressor IDs into those 2-bit
> bitfields before validate_sb() and validate_inode() check
> '>= UBIFS_COMPR_TYPES_CNT', out-of-range on-flash compressor IDs silently
> wrap modulo 4 and render both validation checks dead code.
>
> In addition, when an inode uses a valid compressor that is disabled in
> Kconfig (!compr->capi_name, compr->cc == NULL), validate_inode() only
> logs a warning and allows the inode to be opened, but
> ubifs_compress_common() lacks ubifs_decompress_common()'s !compr->capi_name
> check and passes NULL compr->cc into crypto_acomp_compress() on write.
Nice catch.
>
> Widen all four compression-type bitfields from 2 bits to 16 bits to match
> the __le16 on-flash fields (fitting within existing 32-bit bitfield groups
> without increasing any structure size), update the BUILD_BUG_ON() in
> ubifs_init(), and fall back to uncompressed data with a warning in
> ubifs_compress_common() if compr_type is invalid or not compiled in.
>
> Found via an x86_64 LLVM=1 (Clang 21.1.8) allyesconfig audit with
> -Wtautological-value-range-compare enabled; fix and changelog drafted with
> an AI coding assistant. Verified with a standalone C bitfield/layout test
> and build-tested across fs/ubifs/ under LLVM=1 W=1 and
> -Wtautological-value-range-compare; not runtime-tested on a mounted UBIFS
> volume.
>
> Fixes: 1e51764a3c2a ("UBIFS: add new flash file system")
> Fixes: eeabb9866e4c ("ubifs: Add support for zstd compression.")
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
> fs/ubifs/compress.c | 26 ++++++++++++++++++++------
> fs/ubifs/super.c | 11 ++++++-----
> fs/ubifs/ubifs.h | 6 +++---
> 3 files changed, 29 insertions(+), 14 deletions(-)
Tested-by: Zhihao Cheng <chengzhihao1@huawei.com>
Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com>
>
> diff --git a/fs/ubifs/compress.c b/fs/ubifs/compress.c
> index 059a02691edd..c5c21d30d846 100644
> --- a/fs/ubifs/compress.c
> +++ b/fs/ubifs/compress.c
> @@ -73,11 +73,12 @@ static struct ubifs_compressor zstd_compr = {
> /* All UBIFS compressors */
> struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
>
> -static void ubifs_compress_common(int *compr_type, union ubifs_in_ptr in_ptr,
> - size_t in_offset, int in_len, bool in_folio,
> - void *out_buf, int *out_len)
> +static void ubifs_compress_common(const struct ubifs_info *c, int *compr_type,
> + union ubifs_in_ptr in_ptr, size_t in_offset,
> + int in_len, bool in_folio, void *out_buf,
> + int *out_len)
> {
> - struct ubifs_compressor *compr = ubifs_compressors[*compr_type];
> + struct ubifs_compressor *compr;
> int dlen = *out_len;
> int err;
>
> @@ -88,6 +89,19 @@ static void ubifs_compress_common(int *compr_type, union ubifs_in_ptr in_ptr,
> if (in_len < UBIFS_MIN_COMPR_LEN)
> goto no_compr;
>
> + if (unlikely(*compr_type < 0 || *compr_type >= UBIFS_COMPR_TYPES_CNT)) {
> + ubifs_warn(c, "invalid compression type %d, leave data uncompressed",
> + *compr_type);
> + goto no_compr;
> + }
> +
> + compr = ubifs_compressors[*compr_type];
> + if (unlikely(!compr->capi_name)) {
> + ubifs_warn(c, "%s compression is not compiled in, leave data uncompressed",
> + compr->name);
> + goto no_compr;
> + }
> +
> dlen = min(dlen, in_len - UBIFS_MIN_COMPRESS_DIFF);
>
> do {
> @@ -154,7 +168,7 @@ void ubifs_compress(const struct ubifs_info *c, const void *in_buf,
> {
> union ubifs_in_ptr in_ptr = { .buf = in_buf };
>
> - ubifs_compress_common(compr_type, in_ptr, 0, in_len, false,
> + ubifs_compress_common(c, compr_type, in_ptr, 0, in_len, false,
> out_buf, out_len);
> }
>
> @@ -184,7 +198,7 @@ void ubifs_compress_folio(const struct ubifs_info *c, struct folio *in_folio,
> {
> union ubifs_in_ptr in_ptr = { .folio = in_folio };
>
> - ubifs_compress_common(compr_type, in_ptr, in_offset, in_len, true,
> + ubifs_compress_common(c, compr_type, in_ptr, in_offset, in_len, true,
> out_buf, out_len);
> }
>
> diff --git a/fs/ubifs/super.c b/fs/ubifs/super.c
> index 38972786817e..cca1b459d978 100644
> --- a/fs/ubifs/super.c
> +++ b/fs/ubifs/super.c
> @@ -1019,7 +1019,7 @@ struct ubifs_fs_context {
> char *auth_hash_name;
> unsigned int no_chk_data_crc:1;
> unsigned int bulk_read:1;
> - unsigned int default_compr:2;
> + unsigned int default_compr:16;
> unsigned int assert_action:2;
> };
>
> @@ -2424,12 +2424,13 @@ static int __init ubifs_init(void)
> BUILD_BUG_ON(UBIFS_REF_NODE_SZ != 64);
>
> /*
> - * We use 2 bit wide bit-fields to store compression type, which should
> - * be amended if more compressors are added. The bit-fields are:
> + * We use 16 bit wide bit-fields to store compression type, matching
> + * the 16-bit on-flash fields. The bit-fields are:
> * @compr_type in 'struct ubifs_inode', @default_compr in
> - * 'struct ubifs_info' and @compr_type in 'struct ubifs_mount_opts'.
> + * 'struct ubifs_info' and 'struct ubifs_fs_context', and @compr_type
> + * in 'struct ubifs_mount_opts'.
> */
> - BUILD_BUG_ON(UBIFS_COMPR_TYPES_CNT > 4);
> + BUILD_BUG_ON(UBIFS_COMPR_TYPES_CNT > (1 << 16));
>
> /*
> * We require that PAGE_SIZE is greater-than-or-equal-to
> diff --git a/fs/ubifs/ubifs.h b/fs/ubifs/ubifs.h
> index b85b45a0564b..f2511f866c38 100644
> --- a/fs/ubifs/ubifs.h
> +++ b/fs/ubifs/ubifs.h
> @@ -406,7 +406,7 @@ struct ubifs_inode {
> unsigned int dirty:1;
> unsigned int xattr:1;
> unsigned int bulk_read:1;
> - unsigned int compr_type:2;
> + unsigned int compr_type:16;
> struct mutex ui_mutex;
> struct rw_semaphore xattr_sem;
> spinlock_t ui_lock;
> @@ -948,7 +948,7 @@ struct ubifs_mount_opts {
> unsigned int bulk_read:2;
> unsigned int chk_data_crc:2;
> unsigned int override_compr:1;
> - unsigned int compr_type:2;
> + unsigned int compr_type:16;
> };
>
> /**
> @@ -1311,7 +1311,7 @@ struct ubifs_info {
> unsigned int encrypted:1;
> unsigned int no_chk_data_crc:1;
> unsigned int bulk_read:1;
> - unsigned int default_compr:2;
> + unsigned int default_compr:16;
> unsigned int rw_incompat:1;
> unsigned int assert_action:2;
> unsigned int authenticated:1;
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-20 6:14 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 17:04 [PATCH] ubifs: widen compr_type bitfields and guard uncompiled compressors Ian Rogers
2026-11-20 6:14 ` Zhihao Cheng
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®