Skip to contentSkip to conversation

ubifs: widen compr_type bitfields and guard uncompiled compressors

PATCH In review Ian Rogers 2 participants 2 messages Nov 19 – Nov 20

Subsystems (2)
lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

PATCH 1/1 ubifs: widen compr_type bitfields and guard uncompiled compressors

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.")
---
 fs/ubifs/compress.c | 26 ++++++++++++++++++++------
 fs/ubifs/super.c    | 11 ++++++-----
 fs/ubifs/ubifs.h    |  6 +++---
 3 files changed, 29 insertions(+), 14 deletions(-)
fs/ubifs/compress.c +20 -6
index 059a02691edd..c5c21d30d846 100644
@@ -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);
 }
 
fs/ubifs/super.c +6 -5
index 38972786817e..cca1b459d978 100644
@@ -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
fs/ubifs/ubifs.h +3 -3
index b85b45a0564b..f2511f866c38 100644
@@ -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;
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
reviewer Tested-by Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · 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.

Quoted 5 lines

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.

Quoted 14 lines

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.

Quoted 8 lines
Fixes 1e51764a3c2a ("UBIFS: add new flash file system")
Fixes eeabb9866e4c ("ubifs: Add support for zstd compression.")
---
  fs/ubifs/compress.c | 26 ++++++++++++++++++++------
  fs/ubifs/super.c    | 11 ++++++-----
  fs/ubifs/ubifs.h    |  6 +++---
  3 files changed, 29 insertions(+), 14 deletions(-)
Quoted 122 lines
fs/ubifs/compress.c +20 -6
index 059a02691edd..c5c21d30d846 100644
@@ -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);
  }
 
fs/ubifs/super.c +6 -5
index 38972786817e..cca1b459d978 100644
@@ -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
fs/ubifs/ubifs.h +3 -3
index b85b45a0564b..f2511f866c38 100644
@@ -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;