Whamcloud - gitweb
LU-17794 lustre: replace 0-length arrays with flexible arrays (1/3) 74/55874/7
authorJian Yu <yujian@whamcloud.com>
Tue, 6 Aug 2024 10:26:45 +0000 (03:26 -0700)
committerOleg Drokin <green@whamcloud.com>
Fri, 23 Aug 2024 21:59:45 +0000 (21:59 +0000)
This patch replaces 0-length arrays with flexible arrays to
resolve the UBSAN array-index-out-of-bounds runtime warnings.

Most replacement of 0-length arrays with flexible arrays
requires no special handling. Simply removing the “0” in
the array declaration is sufficient.

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.
This was wrapped in Linux with the DECLARE_FLEX_ARRAY() macro.

Test-Parameters: optional mdtcount=4 mdscount=2 \
  clientdistro=ubuntu2404 testgroup=full-dne-part-1
Test-Parameters: optional mdtcount=4 mdscount=2 \
  clientdistro=ubuntu2404 testgroup=full-dne-part-2
Test-Parameters: optional mdtcount=4 mdscount=2 \
  clientdistro=ubuntu2404 testgroup=full-dne-part-3

Change-Id: I0e23a92382686678951f5d0dc56d91c895af7817
Signed-off-by: Jian Yu <yujian@whamcloud.com>
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/55874
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: Li Xi <lixi@ddn.com>
Reviewed-by: jsimmons <jsimmons@infradead.org>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
36 files changed:
libcfs/include/libcfs/libcfs_hash.h
lnet/include/lnet/lib-types.h
lnet/include/uapi/linux/lnet/libcfs_ioctl.h
lnet/include/uapi/linux/lnet/lnetst.h
lnet/klnds/o2iblnd/o2iblnd.h
lnet/klnds/socklnd/socklnd.h
lnet/lnet/lib-cpt.c
lnet/selftest/console.h
lnet/selftest/selftest.h
lnet/utils/lst.c
lustre/doc/llapi_heat_get.3
lustre/include/lprocfs_status.h
lustre/include/lustre/libiam.h
lustre/include/lustre/lustreapi.h
lustre/include/lustre_dlm.h
lustre/include/lustre_lmv.h
lustre/include/lustre_net.h
lustre/include/lustre_scrub.h
lustre/include/lustre_sec.h
lustre/include/uapi/linux/lustre/lustre_cfg.h
lustre/include/uapi/linux/lustre/lustre_idl.h
lustre/include/uapi/linux/lustre/lustre_user.h
lustre/lfsck/lfsck_internal.h
lustre/lmv/lmv_obd.c
lustre/lov/lov_cl_internal.h
lustre/mgs/mgs_llog.c
lustre/obdclass/kernelcomm.c
lustre/osd-ldiskfs/osd_handler.c
lustre/osd-ldiskfs/osd_iam.h
lustre/osd-ldiskfs/osd_iam_lvar.c
lustre/ptlrpc/gss/gss_krb5.h
lustre/ptlrpc/ptlrpcd.c
lustre/tests/check_fhandle_syscalls.c
lustre/utils/liblustreapi_chlg.c
lustre/utils/lsnapshot.c
lustre/utils/mount_utils.c

index 6fe31a6..5d5e998 100644 (file)
@@ -79,7 +79,7 @@ struct cfs_hash_bucket {
        __u32                   hsb_version;    /**< change version */
        unsigned int            hsb_index;      /**< index of bucket */
        int                     hsb_depmax;     /**< max depth on bucket */
-       long                    hsb_head[0];    /**< hash-head array */
+       long                    hsb_head[];     /**< hash-head array */
 };
 
 /**
index 74bf050..2eea235 100644 (file)
@@ -1712,7 +1712,7 @@ struct lnet_rtrbufpool {
 struct lnet_rtrbuf {
        struct list_head         rb_list;       /* chain on rbp_bufs */
        struct lnet_rtrbufpool  *rb_pool;       /* owning pool */
-       struct bio_vec           rb_kiov[0];    /* the buffer space */
+       struct bio_vec           rb_kiov[];     /* the buffer space */
 };
 
 #define LNET_PEER_HASHSIZE   503               /* prime! */
@@ -1799,7 +1799,7 @@ struct lnet_portal {
        /* # active entries for this portal */
        int                     ptl_mt_nmaps;
        /* array of active entries' cpu-partition-id */
-       int                     ptl_mt_maps[0];
+       int                     ptl_mt_maps[];
 };
 
 #define LNET_LH_HASH_BITS      12
index 0179e3e..10c97c2 100644 (file)
@@ -57,7 +57,7 @@ struct libcfs_ioctl_data {
        __u32 ioc_plen2; /* buffers in userspace */
        void __user *ioc_pbuf2;
 
-       char ioc_bulk[0];
+       char ioc_bulk[];
 };
 
 #define IOCTL_LIBCFS_TYPE                long
index 0e8a105..5bee8b0 100644 (file)
@@ -126,7 +126,7 @@ struct lstcon_rpc_ent {
        struct lst_sid          rpe_sid;                /* peer's session id */
        int                     rpe_fwk_errno;          /* framework errno */
        int                     rpe_priv[4];            /* private data */
-       char                    rpe_payload[0];         /* private reply payload */
+       char                    rpe_payload[];          /* private reply payload */
 };
 
 struct lstcon_trans_stat {
index 3c99cf9..92c95ea 100644 (file)
@@ -240,8 +240,8 @@ struct kib_hca_dev {
 #define IBLND_POOL_RETRY        1
 
 struct kib_pages {
-        int                     ibp_npages;             /* # pages */
-        struct page            *ibp_pages[0];           /* page array */
+       int                     ibp_npages;             /* # pages */
+       struct page            *ibp_pages[];            /* page array */
 };
 
 struct kib_pool;
index 76544fa..0e3894d 100644 (file)
@@ -273,7 +273,7 @@ struct ksock_tx {                   /* transmit packet */
        int             tx_desc_size;   /* size of this descriptor */
        enum lnet_msg_hstatus tx_hstatus; /* health status of tx */
        struct kvec     tx_hdr;         /* virt hdr */
-       struct bio_vec  tx_payload[0];  /* paged payload */
+       struct bio_vec  tx_payload[];   /* paged payload */
 };
 
 #define KSOCK_NOOP_TX_SIZE  ((int)offsetof(struct ksock_tx, tx_payload[0]))
index a922b37..6d026c7 100644 (file)
@@ -1114,7 +1114,7 @@ struct cfs_var_array {
        unsigned int            va_count;       /* # of buffers */
        unsigned int            va_size;        /* size of each var */
        struct cfs_cpt_table    *va_cptab;      /* cpu partition table */
-       void                    *va_ptrs[0];    /* buffer addresses */
+       void                    *va_ptrs[];     /* buffer addresses */
 };
 
 /* free per-cpu data, see more detail in cfs_percpt_free */
index 6cde58f..158d682 100644 (file)
@@ -52,7 +52,7 @@ struct lstcon_group {
 
        struct list_head        grp_trans_list; /* transaction list */
        struct list_head        grp_ndl_list;   /* nodes list */
-       struct list_head        grp_ndl_hash[0];/* hash table for nodes */
+       struct list_head        grp_ndl_hash[]; /* hash table for nodes */
 };
 
 #define LST_BATCH_IDLE          0xB0            /* idle batch */
@@ -112,7 +112,7 @@ struct lstcon_test {
        struct lstcon_group     *tes_src_grp;   /* group run the test */
        struct lstcon_group     *tes_dst_grp;   /* target group */
        int                     tes_paramlen;   /* test parameter length */
-       char                    tes_param[0];   /* test parameter */
+       char                    tes_param[];    /* test parameter */
 };
 
 #define LST_GLOBAL_HASHSIZE     503   /* global nodes hash table size */
index af42128..05788cc 100644 (file)
@@ -217,7 +217,7 @@ struct srpc_bulk {
        int                     bk_sink; /* sink/source */
        int                     bk_alloc; /* # allocated iov */
        int                     bk_niov; /* # iov in bk_iovs */
-       struct bio_vec          bk_iovs[0];
+       struct bio_vec          bk_iovs[];
 };
 
 /* message buffer descriptor */
index c767730..71a6d7c 100644 (file)
@@ -52,7 +52,7 @@ static struct lstcon_trans_stat       trans_stat;
 typedef struct list_string {
        struct list_string *lstr_next;
        int                 lstr_sz;
-       char                lstr_str[0];
+       char                lstr_str[];
 } lstr_t;
 
 #ifndef offsetof
index 840d639..7b0cda7 100644 (file)
@@ -25,7 +25,7 @@ data structure, which contains the following fields:
 struct lu_heat {
        __u32 lh_heat_count;
        __u32 lh_padding1;
-       __u64 lh_heat[0];
+       __u64 lh_heat[];
 };
 .fi
 The function
index 572564a..e3a678f 100644 (file)
@@ -174,7 +174,7 @@ struct lprocfs_counter {
 };
 
 struct lprocfs_percpu {
-       struct lprocfs_counter lp_cntr[0];
+       DECLARE_FLEX_ARRAY(struct lprocfs_counter, lp_cntr);
 };
 
 enum lprocfs_stats_lock_ops {
@@ -211,7 +211,7 @@ struct lprocfs_stats {
 
        /* has ls_num of counter headers */
        struct lprocfs_counter_header   *ls_cnt_header;
-       struct lprocfs_percpu           *ls_percpu[0];
+       struct lprocfs_percpu           *ls_percpu[];
 };
 
 #define OPC_RANGE(seg) (seg ## _LAST_OPC - seg ## _FIRST_OPC)
index 39b6224..5b9cf2b 100644 (file)
@@ -70,7 +70,7 @@ struct iam_lfix_root {
        struct dx_countlimit limit;
        u_int32_t idle_blocks;
        u_int8_t  ilr_paddingdd2[12];
-       unsigned char entries[0];
+       unsigned char entries[];
 } __attribute__((packed));
 
 struct iam_leaf_head {
@@ -90,13 +90,13 @@ struct iam_idle_head {
        __le16 iih_magic;
        __le16 iih_count; /* how many idle blocks in this head */
        __le32 iih_next; /* next head for idle blocks */
-       __le32 iih_blks[0];
+       __le32 iih_blks[];
 } __attribute__((packed));
 
 struct iam_index_head {
        struct dx_countlimit limit;
        u_int8_t  paddingdd[16];
-       unsigned char entries[0];
+       unsigned char entries[];
 } __attribute__((packed));
 
 struct lvar_root {
@@ -112,7 +112,7 @@ struct lvar_root {
 struct lvar_leaf_entry {
        u_int32_t vle_hash;
        u_int16_t vle_keysize;
-       u_int8_t  vle_key[0];
+       u_int8_t  vle_key[];
 } __attribute__((packed));
 
 struct osd_inode_id {
index 13c4061..a155950 100644 (file)
@@ -153,7 +153,7 @@ struct llapi_stripe_param {
        bool                    lsp_is_create;
        __u8                    lsp_max_inherit;
        __u8                    lsp_max_inherit_rr;
-       __u32                   lsp_osts[0];
+       __u32                   lsp_osts[];
 };
 
 #define lsp_tgts       lsp_osts
index 3214c6f..2a44221 100644 (file)
@@ -1089,7 +1089,7 @@ struct lustre_handle_array {
        unsigned int            ha_count;
        /* ha_map is used as bit flag to indicate handle is remote or local */
        DECLARE_BITMAP(ha_map, LMV_MAX_STRIPE_COUNT);
-       struct lustre_handle    ha_handles[0];
+       struct lustre_handle    ha_handles[];
 };
 
 /**
index a323dd7..a571301 100644 (file)
@@ -51,7 +51,7 @@ struct lmv_stripe_md {
        __u32   lsm_md_migrate_offset;
        __u32   lsm_md_migrate_hash;
        char    lsm_md_pool_name[LOV_MAXPOOLNAME + 1];
-       struct lmv_oinfo lsm_md_oinfo[0];
+       struct lmv_oinfo lsm_md_oinfo[];
 };
 
 struct lmv_stripe_object {
index a196c05..e70efb1 100644 (file)
@@ -1673,7 +1673,7 @@ struct ptlrpc_service {
        /**
         * partition data for ptlrpc service
         */
-       struct ptlrpc_service_part      *srv_parts[0];
+       struct ptlrpc_service_part      *srv_parts[];
 };
 
 /**
index 001c50e..d8230ca 100644 (file)
@@ -227,7 +227,7 @@ struct lustre_index_restore_unit {
        struct lu_fid           liru_cfid;
        __u64                   liru_clid;
        int                     liru_len;
-       char                    liru_name[0];
+       char                    liru_name[];
 };
 
 void scrub_file_init(struct lustre_scrub *scrub, guid_t uuid);
index 7e44b26..0039317 100644 (file)
@@ -916,7 +916,7 @@ struct ptlrpc_user_desc {
        __u32           pud_fsgid;
        __u32           pud_cap;
        __u32           pud_ngroups;
-       __u32           pud_groups[0];
+       __u32           pud_groups[];
 };
 
 /*
@@ -947,7 +947,7 @@ struct ptlrpc_bulk_sec_desc {
        __u8            bsd_svc;        /* SPTLRPC_BULK_SVC_XXXX */
        __u8            bsd_flags;      /* flags */
        __u32           bsd_nob;        /* nob of bulk data */
-       __u8            bsd_data[0];    /* policy-specific token */
+       __u8            bsd_data[];     /* policy-specific token */
 };
 
 extern struct dentry *sptlrpc_debugfs_dir;
index 2dcb57a..4a50600 100644 (file)
@@ -160,7 +160,7 @@ struct lustre_cfg {
        __u32 lcfg_nal;         /* not used any more */
 
        __u32 lcfg_bufcount;
-       __u32 lcfg_buflens[0];
+       __u32 lcfg_buflens[];
 };
 
 struct lcfg_type_data {
index d963e4e..b68389d 100644 (file)
@@ -155,7 +155,7 @@ struct lu_seq_range {
 struct lu_seq_range_array {
        __u32 lsra_count;
        __u32 lsra_padding;
-       struct lu_seq_range lsra_lsr[0];
+       struct lu_seq_range lsra_lsr[];
 };
 
 #define LU_SEQ_RANGE_MDT       0x0
@@ -644,7 +644,7 @@ struct lustre_msg_v2 {
         * message buffers are packed after padded lm_buflens[] array,
         * padded to a multiple of 8 bytes each to align contents.
         */
-       __u32 lm_buflens[0];
+       __u32 lm_buflens[];
 };
 
 /* The returned result of the SUB request in a batch request */
@@ -2345,7 +2345,7 @@ struct lmv_mds_md_v1 {
        __u32 lmv_padding2;
        __u64 lmv_padding3;
        char lmv_pool_name[LOV_MAXPOOLNAME + 1];        /* pool name */
-       struct lu_fid lmv_stripe_fids[0];       /* FIDs for each stripe */
+       struct lu_fid lmv_stripe_fids[];        /* FIDs for each stripe */
 };
 
 /* stripe count before directory split */
@@ -2742,8 +2742,8 @@ struct mgs_nidtbl_entry {
        __u8            mne_nid_size;   /* size of each NID, by bytes */
        __u8            mne_nid_count;  /* # of NIDs in buffer */
        union {
-               lnet_nid_t nids[0];     /* variable size buffer for NIDs. */
-               struct lnet_nid nidlist[0];
+               DECLARE_FLEX_ARRAY(lnet_nid_t, nids); /* variable size buffer for NIDs. */
+               DECLARE_FLEX_ARRAY(struct lnet_nid, nidlist);
        } u;
 };
 
@@ -3456,7 +3456,7 @@ struct getparent {
        struct lu_fid   gp_fid;         /**< parent FID */
        __u32           gp_linkno;      /**< hardlink number */
        __u32           gp_name_size;   /**< size of the name field */
-       char            gp_name[0];     /**< zero-terminated link name */
+       char            gp_name[];      /**< zero-terminated link name */
 } __attribute__((packed));
 
 enum layout_intent_opc {
@@ -3649,7 +3649,7 @@ struct batch_update_request {
         * it can locate the next request message via the function
         * @batch_update_reqmsg_next() in lustre/include/obj_update.h
         */
-       struct lustre_msg       burq_reqmsg[0];
+       struct lustre_msg       burq_reqmsg[];
 };
 
 #define BUT_HEADER_MAGIC       0xBADF0001
@@ -3674,7 +3674,7 @@ struct but_update_header {
        /* Unused padding field. */
        __u32   buh_padding;
        /* Inline buffer used when the RPC request can be packed inline. */
-       __u32   buh_inline_data[0];
+       __u32   buh_inline_data[];
 };
 
 struct but_update_buffer {
@@ -3696,7 +3696,7 @@ struct batch_update_reply {
         * It can locate the next reply message buffer via the function
         * @batch_update_repmsg_next() in lustre/include/obj_update.h
         */
-       struct lustre_msg       burp_repmsg[0];
+       struct lustre_msg       burp_repmsg[];
 };
 
 /**
index 15be589..0f1262a 100644 (file)
@@ -1507,7 +1507,7 @@ struct identity_downcall_data {
        __u32                            idd_nperms;
        __u32                            idd_ngroups;
        struct perm_downcall_data idd_perms[N_PERMS_MAX];
-       __u32                            idd_groups[0];
+       __u32                            idd_groups[];
 };
 
 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
@@ -1517,7 +1517,7 @@ struct sepol_downcall_data_old {
        __u32           sdd_magic;
        __s64           sdd_sepol_mtime;
        __u16           sdd_sepol_len;
-       char            sdd_sepol[0];
+       char            sdd_sepol[];
 };
 #endif
 
@@ -1527,7 +1527,7 @@ struct sepol_downcall_data {
        __u16           sdd_sepol_len;
        __u16           sdd_padding1;
        __s64           sdd_sepol_mtime;
-       char            sdd_sepol[0];
+       char            sdd_sepol[];
 };
 
 #ifdef NEED_QUOTA_DEFS
@@ -2918,7 +2918,7 @@ enum obd_heat_type {
 struct lu_heat {
        __u32 lh_count;
        __u32 lh_flags;
-       __u64 lh_heat[0];
+       __u64 lh_heat[];
 };
 
 enum lu_pcc_type {
@@ -3061,7 +3061,7 @@ struct fid_array {
        /* make header's size equal lu_fid */
        __u32 fa_padding0;
        __u64 fa_padding1;
-       struct lu_fid fa_fids[0];
+       struct lu_fid fa_fids[];
 };
 #define OBD_MAX_FIDS_IN_ARRAY  4096
 
index bb82522..c674d16 100644 (file)
@@ -352,7 +352,7 @@ struct lfsck_layout {
        u64     ll_reserved_2[7];
 
        /* OST target bitmap to record OSTs that contain non-verified OST-obj */
-       __u8    ll_ost_bitmap[0];
+       __u8    ll_ost_bitmap[];
 };
 
 struct lfsck_assistant_object {
index 008ddbd..99b7360 100644 (file)
@@ -2921,7 +2921,7 @@ struct lmv_dir_ctxt {
        struct md_readdir_info  *ldc_mrinfo;
        __u64                    ldc_hash;
        int                      ldc_count;
-       struct stripe_dirent     ldc_stripes[0];
+       struct stripe_dirent     ldc_stripes[];
 };
 
 static inline void stripe_dirent_unload(struct stripe_dirent *stripe)
index 9bccabc..33bb43d 100644 (file)
@@ -423,7 +423,7 @@ struct lov_lock {
        /* Number of sub-locks in this lock */
        int                     lls_nr;
        /* sublock array */
-       struct lov_lock_sub     lls_sub[0];
+       struct lov_lock_sub     lls_sub[];
 };
 
 /* Bottom half. */
index 9dfb114..6c7be7a 100644 (file)
@@ -4986,7 +4986,7 @@ struct mgs_lcfg_fork_data {
        struct llog_handle      *mlfd_llh;
        const char              *mlfd_oldname;
        const char              *mlfd_newname;
-       char                     mlfd_data[0];
+       char                     mlfd_data[];
 };
 
 static bool contain_valid_fsname(char *buf, const char *fsname,
index c08c85c..64cb167 100644 (file)
@@ -367,7 +367,7 @@ struct kkuc_reg {
        struct obd_uuid  kr_uuid;
        int              kr_uid;
        struct file     *kr_fp;
-       char             kr_data[0];
+       char             kr_data[];
 };
 
 static struct list_head kkuc_groups[KUC_GRP_MAX + 1];
index 1c0cca0..78d5167 100644 (file)
@@ -1545,7 +1545,7 @@ struct osd_xattr_entry {
        size_t                  oxe_namelen;
        bool                    oxe_exist;
        struct rcu_head         oxe_rcu;
-       char                    oxe_buf[0];
+       char                    oxe_buf[];
 };
 
 static int osd_oxc_get(struct osd_object *obj, const char *name,
index 92505d8..d620f97 100644 (file)
@@ -430,7 +430,7 @@ struct iam_idle_head {
        __le16 iih_magic;
        __le16 iih_count; /* how many idle blocks in this head */
        __le32 iih_next; /* next head for idle blocks */
-       __le32 iih_blks[0];
+       __le32 iih_blks[];
 };
 
 /*
index f3d03cd..77a9343 100644 (file)
@@ -46,7 +46,7 @@ typedef u32 lvar_hash_t;
 struct lvar_leaf_entry {
        __le32 vle_hash;
        __le16 vle_keysize;
-       u8 vle_key[0];
+       u8 vle_key[];
 };
 
 #define PDIFF(ptr0, ptr1) (((char *)(ptr0)) - ((char *)(ptr1)))
index 6111604..6590a62 100644 (file)
 #define FLAG_ACCEPTOR_SUBKEY            0x04
 
 struct krb5_header {
-        __u16           kh_tok_id;      /* token id */
-        __u8            kh_flags;       /* acceptor flags */
-        __u8            kh_filler;      /* 0xff */
-        __u16           kh_ec;          /* extra count */
-        __u16           kh_rrc;         /* right rotation count */
-        __u64           kh_seq;         /* sequence number */
-        __u8            kh_cksum[0];    /* checksum */
+       __u16           kh_tok_id;      /* token id */
+       __u8            kh_flags;       /* acceptor flags */
+       __u8            kh_filler;      /* 0xff */
+       __u16           kh_ec;          /* extra count */
+       __u16           kh_rrc;         /* right rotation count */
+       __u64           kh_seq;         /* sequence number */
+       __u8            kh_cksum[];     /* checksum */
 };
 
 struct krb5_ctx {
index e452152..78e3f6a 100644 (file)
@@ -71,7 +71,7 @@ struct ptlrpcd {
        int                     pd_cursor;
        int                     pd_nthreads;
        int                     pd_groupsize;
-       struct ptlrpcd_ctl      pd_threads[0];
+       struct ptlrpcd_ctl      pd_threads[];
 };
 
 /*
index 2fdd124..b8642c9 100644 (file)
@@ -64,7 +64,7 @@ struct file_handle {
        __u32 handle_bytes;
        int handle_type;
        /* file identifier */
-       unsigned char f_handle[0];
+       unsigned char f_handle[];
 };
 
 #if defined(_ASM_X86_UNISTD_64_H)
index 31b291c..ce1727e 100644 (file)
@@ -77,7 +77,7 @@ struct changelog_private {
        /* Current position in buffer */
        char                            *clp_buf_pos;
        /* Read buffer with records read from system */
-       char                             clp_buf[0];
+       char                             clp_buf[];
 };
 
 /**
index 7dacf66..2d93176 100644 (file)
@@ -2169,7 +2169,7 @@ static int snapshot_list_all(struct snapshot_instance *si)
        struct snapshot_target *st = si->si_mdt0;
        struct list_sub_item {
                struct list_head lsi_list;
-               char lsi_ssname[0];
+               char lsi_ssname[];
        };
 
        struct list_head list_sub_items;
index cbe2c5f..1c41f35 100644 (file)
@@ -1063,7 +1063,7 @@ out_bad_mnt_str:
 #ifdef HAVE_SERVER_SUPPORT
 struct lustre_cfg_entry {
        struct list_head lce_list;
-       char             lce_name[0];
+       char             lce_name[];
 };
 
 static struct lustre_cfg_entry *lustre_cfg_entry_init(const char *name)