From 40459f708da779d9accb19f64eb7bb59780bd53e Mon Sep 17 00:00:00 2001 From: Shaun Tancheff Date: Wed, 8 Feb 2023 20:02:00 -0800 Subject: [PATCH] LU-16118 build: Workaround __write_overflow_field errors MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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); Lustre-change: https://review.whamcloud.com/48364 Lustre-commit: a3a51806ef361f55421a1bc07f64c78730ae50d5 Test-Parameters: trivial HPE-bug-id: LUS-11194 Signed-off-by: Shaun Tancheff Change-Id: Iacd1ab03d1b90ce62b5d7b65e1cd518a5f7981f2 Reviewed-by: Petros Koutoupis Reviewed-by: jsimmons Reviewed-by: Andreas Dilger Reviewed-by: Neil Brown Reviewed-on: https://review.whamcloud.com/c/ex/lustre-release/+/49876 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Yang Sheng --- libcfs/include/libcfs/linux/linux-misc.h | 11 +++++++++ lustre/include/cl_object.h | 9 ++------ lustre/include/uapi/linux/lustre/lustre_idl.h | 32 +++++++++++++++----------- lustre/include/uapi/linux/lustre/lustre_user.h | 2 +- lustre/obdclass/md_attrs.c | 3 +-- 5 files changed, 34 insertions(+), 23 deletions(-) diff --git a/libcfs/include/libcfs/linux/linux-misc.h b/libcfs/include/libcfs/linux/linux-misc.h index 81782f2..e7b5f41 100644 --- a/libcfs/include/libcfs/linux/linux-misc.h +++ b/libcfs/include/libcfs/linux/linux-misc.h @@ -153,6 +153,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) { diff --git a/lustre/include/cl_object.h b/lustre/include/cl_object.h index 4393749..bd13526 100644 --- a/lustre/include/cl_object.h +++ b/lustre/include/cl_object.h @@ -99,6 +99,7 @@ #include #include #include +#include #include struct obd_info; @@ -2504,13 +2505,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 */ diff --git a/lustre/include/uapi/linux/lustre/lustre_idl.h b/lustre/include/uapi/linux/lustre/lustre_idl.h index fb980ed..c569741 100644 --- a/lustre/include/uapi/linux/lustre/lustre_idl.h +++ b/lustre/include/uapi/linux/lustre/lustre_idl.h @@ -1541,23 +1541,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) diff --git a/lustre/include/uapi/linux/lustre/lustre_user.h b/lustre/include/uapi/linux/lustre/lustre_user.h index 39ad948..7e5807f 100644 --- a/lustre/include/uapi/linux/lustre/lustre_user.h +++ b/lustre/include/uapi/linux/lustre/lustre_user.h @@ -1482,7 +1482,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 */ diff --git a/lustre/obdclass/md_attrs.c b/lustre/obdclass/md_attrs.c index fe8fca5..b0a68a0 100644 --- a/lustre/obdclass/md_attrs.c +++ b/lustre/obdclass/md_attrs.c @@ -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); -- 1.8.3.1