Whamcloud - gitweb
LU-16118 build: Workaround __write_overflow_field errors 64/48364/23
authorShaun Tancheff <shaun.tancheff@hpe.com>
Sun, 22 Jan 2023 17:43:29 +0000 (11:43 -0600)
committerOleg Drokin <green@whamcloud.com>
Wed, 8 Feb 2023 06:26:57 +0000 (06:26 +0000)
Linux commit v5.17-rc3-1-gf68f2ff91512
   fortify: Detect struct member overflows in memcpy() at compile-time

memcpy and memset of collections of struct members
will trigger:

error: call to ‘__write_overflow_field’ declared with attribute
   warning: detected write beyond size of field (1st parameter);
   maybe use struct_group()?
   [-Werror] __write_overflow_field(p_size_field, size);

Test-Parameters: trivial
HPE-bug-id: LUS-11194
Signed-off-by: Shaun Tancheff <shaun.tancheff@hpe.com>
Change-Id: Iacd1ab03d1b90ce62b5d7b65e1cd518a5f7981f2
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/48364
Reviewed-by: Petros Koutoupis <petros.koutoupis@hpe.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
Reviewed-by: jsimmons <jsimmons@infradead.org>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: Neil Brown <neilb@suse.de>
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
libcfs/include/libcfs/linux/linux-misc.h
lnet/include/uapi/linux/lnet/lnet-types.h
lustre/include/cl_object.h
lustre/include/uapi/linux/lustre/lustre_idl.h
lustre/include/uapi/linux/lustre/lustre_user.h
lustre/obdclass/md_attrs.c

index 5e21896..841db69 100644 (file)
@@ -154,6 +154,17 @@ void cfs_arch_init(void);
 #define task_is_running(task)          (task->state == TASK_RUNNING)
 #endif
 
+#ifndef memset_startat
+/** from linux 5.19 include/linux/string.h: */
+#define memset_startat(obj, v, member)                                 \
+({                                                                     \
+       u8 *__ptr = (u8 *)(obj);                                        \
+       typeof(v) __val = (v);                                          \
+       memset(__ptr + offsetof(typeof(*(obj)), member), __val,         \
+              sizeof(*(obj)) - offsetof(typeof(*(obj)), member));      \
+})
+#endif /* memset_startat() */
+
 #ifdef HAVE_KALLSYMS_LOOKUP_NAME
 static inline void *cfs_kallsyms_lookup_name(const char *name)
 {
index c8a61ab..f515173 100644 (file)
@@ -145,8 +145,7 @@ static inline __u32 LNET_NID_NET(const struct lnet_nid *nid)
 static inline void lnet_nid4_to_nid(lnet_nid_t nid4, struct lnet_nid *nid)
 {
        if (nid4 == LNET_NID_ANY) {
-               /* equal to setting to LNET_ANY_NID */
-               memset(nid, 0xff, sizeof(*nid));
+               *nid = LNET_ANY_NID;
                return;
        }
 
index 5449cef..4e58394 100644 (file)
@@ -98,6 +98,7 @@
 #include <linux/spinlock.h>
 #include <linux/wait.h>
 #include <linux/pagevec.h>
+#include <libcfs/linux/linux-misc.h>
 #include <lustre_dlm.h>
 
 struct obd_info;
@@ -2410,13 +2411,7 @@ struct cl_io *cl_io_top(struct cl_io *io);
 void cl_io_print(const struct lu_env *env, void *cookie,
                  lu_printer_t printer, const struct cl_io *io);
 
-#define CL_IO_SLICE_CLEAN(foo_io, base)                                        \
-do {                                                                   \
-       typeof(foo_io) __foo_io = (foo_io);                             \
-                                                                       \
-       memset(&__foo_io->base, 0,                                      \
-              sizeof(*__foo_io) - offsetof(typeof(*__foo_io), base));  \
-} while (0)
+#define CL_IO_SLICE_CLEAN(obj, base) memset_startat(obj, 0, base)
 
 /** @} cl_io */
 
index 3e05502..d8c432e 100644 (file)
@@ -1504,23 +1504,29 @@ struct obd_quotactl {
        __u32                   qc_stat;
        struct obd_dqinfo       qc_dqinfo;
        struct obd_dqblk        qc_dqblk;
-       char                    qc_poolname[0];
+       char                    qc_poolname[];
 };
 
 #define Q_COPY(out, in, member) (out)->member = (in)->member
 
-#define __QCTL_COPY(out, in, need_pname)               \
-do {                                                   \
-       Q_COPY(out, in, qc_cmd);                        \
-       Q_COPY(out, in, qc_type);                       \
-       Q_COPY(out, in, qc_id);                         \
-       Q_COPY(out, in, qc_stat);                       \
-       Q_COPY(out, in, qc_dqinfo);                     \
-       Q_COPY(out, in, qc_dqblk);                      \
-       if (need_pname && LUSTRE_Q_CMD_IS_POOL(in->qc_cmd))             \
-               memcpy(out->qc_poolname,                \
-                      in->qc_poolname,                 \
-                      LOV_MAXPOOLNAME + 1);            \
+/* NOTE:
+ * - in and out maybe a type of struct if_quotactl or struct obd_quotactl
+ * - in and out need not be of the same type.
+ */
+#define __QCTL_COPY(out, in, need_pname)                               \
+do {                                                                   \
+       Q_COPY(out, in, qc_cmd);                                        \
+       Q_COPY(out, in, qc_type);                                       \
+       Q_COPY(out, in, qc_id);                                         \
+       Q_COPY(out, in, qc_stat);                                       \
+       Q_COPY(out, in, qc_dqinfo);                                     \
+       Q_COPY(out, in, qc_dqblk);                                      \
+       if (need_pname && LUSTRE_Q_CMD_IS_POOL(in->qc_cmd)) {           \
+               size_t len = strnlen(in->qc_poolname, LOV_MAXPOOLNAME); \
+                                                                       \
+               memcpy(out->qc_poolname, in->qc_poolname, len);         \
+               out->qc_poolname[len] = '\0';                           \
+       }                                                               \
 } while (0)
 
 #define QCTL_COPY(out, in) __QCTL_COPY(out, in, true)
index 391b8c9..b78ea2b 100644 (file)
@@ -1500,7 +1500,7 @@ struct if_quotactl {
        struct obd_dqblk        qc_dqblk;
        char                    obd_type[16];
        struct obd_uuid         obd_uuid;
-       char                    qc_poolname[0];
+       char                    qc_poolname[];
 };
 
 /* swap layout flags */
index fe8fca5..b0a68a0 100644 (file)
@@ -73,8 +73,7 @@ void lustre_loa_init(struct lustre_ost_attrs *loa, const struct lu_fid *fid,
 {
        BUILD_BUG_ON(sizeof(*loa) != LMA_OLD_SIZE);
 
-       memset(&loa->loa_parent_fid, 0,
-              sizeof(*loa) - offsetof(typeof(*loa), loa_parent_fid));
+       memset_startat(loa, 0, loa_parent_fid);
        lustre_lma_init(&loa->loa_lma, fid, compat, incompat);
 }
 EXPORT_SYMBOL(lustre_loa_init);