Skip to contentSkip to conversation

bcachefs: Refactor bkey_i to use a flexible array

PATCH In review Kees Cook 3 participants 3 messages Oct 16, 2023 – Oct 17, 2023

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH v2 1/1 bcachefs: Refactor bkey_i to use a flexible array

The memcpy() in bch2_bkey_append_ptr() is operating on an embedded fake flexible array. Instead, make it explicit, and convert the memcpy to target the flexible array instead. Fixes the W=1 warning seen for -Wstringop-overflow:

In file included from include/linux/string.h:254,
from include/linux/bitmap.h:11,
from include/linux/cpumask.h:12,
from include/linux/smp.h:13,
from include/linux/lockdep.h:14,
from include/linux/radix-tree.h:14,
from include/linux/backing-dev-defs.h:6,
from fs/bcachefs/bcachefs.h:182:
fs/bcachefs/extents.c: In function 'bch2_bkey_append_ptr':
include/linux/fortify-string.h:57:33: warning: writing 8 bytes into a region of size 0 [-Wstringop-overflow=]

      57 | #define __underlying_memcpy     __builtin_memcpy
         |                                 ^
   include/linux/fortify-string.h:648:9: note: in expansion of macro '__underlying_memcpy'
     648 |         __underlying_##op(p, q, __fortify_size);                        \
         |         ^~~~~~~~~~~~~
   include/linux/fortify-string.h:693:26: note: in expansion of macro '__fortify_memcpy_chk'
     693 | #define memcpy(p, q, s)  __fortify_memcpy_chk(p, q, s,                  \
         |                          ^~~~~~~~~~~~~~~~~~~~
   fs/bcachefs/extents.c:235:17: note: in expansion of macro 'memcpy'
     235 |                 memcpy((void *) &k->v + bkey_val_bytes(&k->k),
         |                 ^~~~~~
   fs/bcachefs/bcachefs_format.h:287:33: note: destination object 'v' of size 0
     287 |                 struct bch_val  v;
         |                                 ^

---
v2 - Change flex array name to "v_bytes" (bfoster)
v1 - https://lore.kernel.org/r/[email protected]

---
 fs/bcachefs/bcachefs_format.h | 5 ++++-
 fs/bcachefs/extents.h         | 2 +-
 2 files changed, 5 insertions(+), 2 deletions(-)
fs/bcachefs/bcachefs_format.h +4 -1
index f0d130440baa..cb1af3799b59 100644
@@ -300,7 +300,10 @@ struct bkey_i {
 	__u64			_data[0];
 
 	struct bkey	k;
-	struct bch_val	v;
+	union {
+		struct bch_val	v;
+		DECLARE_FLEX_ARRAY(__u8, v_bytes);
+	};
 };
 
 #define KEY(_inode, _offset, _size)					\
fs/bcachefs/extents.h +1 -1
index 7ee8d031bb6c..896fcfca4f21 100644
@@ -642,7 +642,7 @@ static inline void bch2_bkey_append_ptr(struct bkey_i *k, struct bch_extent_ptr
 
 		ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
 
-		memcpy((void *) &k->v + bkey_val_bytes(&k->k),
+		memcpy(&k->v_bytes[bkey_val_bytes(&k->k)],
 		       &ptr,
 		       sizeof(ptr));
 		k->k.u64s++;
Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 30 lines · Kees Cook

The memcpy() in bch2_bkey_append_ptr() is operating on an embedded fake flexible array. Instead, make it explicit, and convert the memcpy to target the flexible array instead. Fixes the W=1 warning seen for -Wstringop-overflow:

In file included from include/linux/string.h:254,
from include/linux/bitmap.h:11,
from include/linux/cpumask.h:12,
from include/linux/smp.h:13,
from include/linux/lockdep.h:14,
from include/linux/radix-tree.h:14,
from include/linux/backing-dev-defs.h:6,
from fs/bcachefs/bcachefs.h:182:
fs/bcachefs/extents.c: In function 'bch2_bkey_append_ptr':
include/linux/fortify-string.h:57:33: warning: writing 8 bytes into a region of size 0 [-Wstringop-overflow=]
57 | #define __underlying_memcpy __builtin_memcpy
| ^
include/linux/fortify-string.h:648:9: note: in expansion of macro '__underlying_memcpy'
648 | __underlying_##op(p, q, __fortify_size); \
| ^~~~~~~~~~~~~
include/linux/fortify-string.h:693:26: note: in expansion of macro '__fortify_memcpy_chk'
693 | #define memcpy(p, q, s) __fortify_memcpy_chk(p, q, s, \
| ^~~~~~~~~~~~~~~~~~~~
fs/bcachefs/extents.c:235:17: note: in expansion of macro 'memcpy'
235 | memcpy((void *) &k->v + bkey_val_bytes(&k->k),
| ^~~~~~
fs/bcachefs/bcachefs_format.h:287:33: note: destination object 'v' of size 0
287 | struct bch_val v;
| ^

Quoted 6 lines

Yes. This looks good.

Thanks!
--
Gustavo

Quoted 37 lines

---
v2 - Change flex array name to "v_bytes" (bfoster)
v1 - https://lore.kernel.org/r/[email protected]

---
  fs/bcachefs/bcachefs_format.h | 5 ++++-
  fs/bcachefs/extents.h         | 2 +-
  2 files changed, 5 insertions(+), 2 deletions(-)
fs/bcachefs/bcachefs_format.h +4 -1
index f0d130440baa..cb1af3799b59 100644
@@ -300,7 +300,10 @@ struct bkey_i {
  	__u64			_data[0];
 
  	struct bkey	k;
-	struct bch_val	v;
+	union {
+		struct bch_val	v;
+		DECLARE_FLEX_ARRAY(__u8, v_bytes);
+	};
  };
 
  #define KEY(_inode, _offset, _size)					\
fs/bcachefs/extents.h +1 -1
index 7ee8d031bb6c..896fcfca4f21 100644
@@ -642,7 +642,7 @@ static inline void bch2_bkey_append_ptr(struct bkey_i *k, struct bch_extent_ptr
 
  		ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
 
-		memcpy((void *) &k->v + bkey_val_bytes(&k->k),
+		memcpy(&k->v_bytes[bkey_val_bytes(&k->k)],
  		       &ptr,
  		       sizeof(ptr));
  		k->k.u64s++;
Reviewed-by on v2 1/1
#2 lore raw
In reply to Kees Cook
Quoted 36 lines · Kees Cook

The memcpy() in bch2_bkey_append_ptr() is operating on an embedded fake flexible array. Instead, make it explicit, and convert the memcpy to target the flexible array instead. Fixes the W=1 warning seen for -Wstringop-overflow:

In file included from include/linux/string.h:254,
from include/linux/bitmap.h:11, from include/linux/cpumask.h:12, from include/linux/smp.h:13, from include/linux/lockdep.h:14, from include/linux/radix-tree.h:14, from include/linux/backing-dev-defs.h:6, from fs/bcachefs/bcachefs.h:182:
fs/bcachefs/extents.c: In function 'bch2_bkey_append_ptr':

   include/linux/fortify-string.h:57:33: warning: writing 8 bytes into a region of size 0 [-Wstringop-overflow=]
      57 | #define __underlying_memcpy     __builtin_memcpy
         |                                 ^
   include/linux/fortify-string.h:648:9: note: in expansion of macro '__underlying_memcpy'
     648 |         __underlying_##op(p, q, __fortify_size);                        \
         |         ^~~~~~~~~~~~~
   include/linux/fortify-string.h:693:26: note: in expansion of macro '__fortify_memcpy_chk'
     693 | #define memcpy(p, q, s)  __fortify_memcpy_chk(p, q, s,                  \
         |                          ^~~~~~~~~~~~~~~~~~~~
   fs/bcachefs/extents.c:235:17: note: in expansion of macro 'memcpy'
     235 |                 memcpy((void *) &k->v + bkey_val_bytes(&k->k),
         |                 ^~~~~~
   fs/bcachefs/bcachefs_format.h:287:33: note: destination object 'v' of size 0
     287 |                 struct bch_val  v;
         |                                 ^
Quoted 4 lines

---
v2 - Change flex array name to "v_bytes" (bfoster)
v1 - https://lore.kernel.org/r/[email protected]
---

Quoted 36 lines
 fs/bcachefs/bcachefs_format.h | 5 ++++-
 fs/bcachefs/extents.h         | 2 +-
 2 files changed, 5 insertions(+), 2 deletions(-)
fs/bcachefs/bcachefs_format.h +4 -1
index f0d130440baa..cb1af3799b59 100644
@@ -300,7 +300,10 @@ struct bkey_i {
 	__u64			_data[0];
 
 	struct bkey	k;
-	struct bch_val	v;
+	union {
+		struct bch_val	v;
+		DECLARE_FLEX_ARRAY(__u8, v_bytes);
+	};
 };
 
 #define KEY(_inode, _offset, _size)					\
fs/bcachefs/extents.h +1 -1
index 7ee8d031bb6c..896fcfca4f21 100644
@@ -642,7 +642,7 @@ static inline void bch2_bkey_append_ptr(struct bkey_i *k, struct bch_extent_ptr
 
 		ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
 
-		memcpy((void *) &k->v + bkey_val_bytes(&k->k),
+		memcpy(&k->v_bytes[bkey_val_bytes(&k->k)],
 		       &ptr,
 		       sizeof(ptr));
 		k->k.u64s++;
Signature · 1 line
2.34.1