Whamcloud - gitweb
LU-17911 lustre: fix faked flexible arrays in getinfo_fid2path 54/55354/5
authorBruno Faccini <bfaccini@nvidia.com>
Fri, 7 Jun 2024 09:22:44 +0000 (11:22 +0200)
committerOleg Drokin <green@whamcloud.com>
Tue, 25 Jun 2024 03:24:17 +0000 (03:24 +0000)
faked (0-length) flexible arrays need some rework to comply with
new coding rules to stay Fortify feature compliant (see document
at https://people.kernel.org/kees/bounded-flexible-arrays-in-c).
With this particular getinfo_fid2path struct content, we ended-up
with generated code causing straight crash upon each call of
lmv_fid2path routine upon strlen() first call.

Signed-off-by: Bruno Faccini <bfaccini@nvidia.com>
Change-Id: Id6f594779ca0ae86f0c2842535abccbf4df688d3
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/55354
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: Aurelien Degremont <adegremont@nvidia.com>
Reviewed-by: James Simmons <jsimmons@infradead.org>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
lustre/include/uapi/linux/lustre/lustre_idl.h

index 6e2b6b6..0ad5db3 100644 (file)
@@ -116,6 +116,29 @@ extern "C" {
 /* #define DVS_PORTAL                  63 */
 /* reserved for Cray DVS - spitzcor@cray.com, roe@cray.com, n8851@cray.com */
 
+#ifndef DECLARE_FLEX_ARRAY
+#ifdef __cplusplus
+/* sizeof(struct{}) is 1 in C++, not 0, can't use C version of the macro. */
+#define DECLARE_FLEX_ARRAY(T, member) T member[0]
+#else
+/**
+ * DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union
+ *
+ * @TYPE: The type of each flexible array element
+ * @NAME: The name of the flexible array member
+ *
+ * In order to have a flexible array member in a union or alone in a
+ * struct, it needs to be wrapped in an anonymous struct with at least 1
+ * named member, but that member can be empty.
+ */
+#define DECLARE_FLEX_ARRAY(TYPE, NAME)        \
+       struct {                               \
+               struct { } __empty_ ## NAME;   \
+               TYPE NAME[];                   \
+       }
+#endif
+#endif /* DECLARE_FLEX_ARRAY */
+
 /**
  * Describes a range of sequence, lsr_start is included but lsr_end is
  * not in the range.
@@ -3423,8 +3446,8 @@ struct getinfo_fid2path {
        __u32           gf_linkno;
        __u32           gf_pathlen;
        union {
-               char            gf_path[0];
-               struct lu_fid   gf_root_fid[0];
+               DECLARE_FLEX_ARRAY(char, gf_path);
+               DECLARE_FLEX_ARRAY(struct lu_fid, gf_root_fid);
        } gf_u;
 } __attribute__((packed));