From 79b34a83f811f5f1dc40a077bd6d5f65194c9bbe Mon Sep 17 00:00:00 2001 From: James Simmons Date: Sat, 29 Apr 2023 11:39:56 -0400 Subject: [PATCH 1/1] LU-9325 obd: replace simple_strtoul() Replace the use of simple_strtoul() in filename_is_volatile() with sscanf(). This change also strengthens the checking of the format of the volatile file's name. Remove the use of simple_strtoul() from target_name2index() as well. Change-Id: I4bf15af00d6cecdd46eba3cca5df0fbeb473d9ce Signed-off-by: James Simmons Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/49910 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Andreas Dilger Reviewed-by: Arshad Hussain Reviewed-by: Oleg Drokin --- lustre/include/obd.h | 13 ++++++------- lustre/obdclass/obd_mount.c | 39 ++++++++++++++++++++++++++++----------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/lustre/include/obd.h b/lustre/include/obd.h index 1961ddb..efda050 100644 --- a/lustre/include/obd.h +++ b/lustre/include/obd.h @@ -1364,8 +1364,8 @@ static inline const char *lu_dev_name(const struct lu_device *lu_dev) static inline bool filename_is_volatile(const char *name, size_t namelen, int *idx) { - const char *start; - char *end; + const char *start; + int rnd, fd, rc; if (strncmp(name, LUSTRE_VOLATILE_HDR, LUSTRE_VOLATILE_HDR_LEN) != 0) return false; @@ -1387,12 +1387,11 @@ static inline bool filename_is_volatile(const char *name, size_t namelen, } /* we have an idx, read it */ start = name + LUSTRE_VOLATILE_HDR_LEN + 1; - *idx = simple_strtoul(start, &end, 16); - /* error cases: - * no digit, no trailing :, negative value + rc = sscanf(start, "%4x:%4x:fd=%2d", idx, &rnd, &fd); + /* error cases: no digit or negative value + * rc will never be larger then 3 */ - if (((*idx == 0) && (end == start)) || - (*end != ':') || (*idx < 0)) + if (rc <= 0 || *idx < 0) goto bad_format; return true; diff --git a/lustre/obdclass/obd_mount.c b/lustre/obdclass/obd_mount.c index e692180..e96ca5f 100644 --- a/lustre/obdclass/obd_mount.c +++ b/lustre/obdclass/obd_mount.c @@ -870,16 +870,16 @@ EXPORT_SYMBOL(server_name_is_ost); * Get the index from the target name MDTXXXX/OSTXXXX * rc = server type, or rc < 0 on error **/ -int target_name2index(const char *tgtname, __u32 *idx, const char **endptr) +int target_name2index(const char *tgtname, u32 *idx, const char **endptr) { const char *dash = tgtname; - unsigned long index; - int rc; + int type, len, rc; + u16 index; if (strncmp(dash, "MDT", 3) == 0) - rc = LDD_F_SV_TYPE_MDT; + type = LDD_F_SV_TYPE_MDT; else if (strncmp(dash, "OST", 3) == 0) - rc = LDD_F_SV_TYPE_OST; + type = LDD_F_SV_TYPE_OST; else return -EINVAL; @@ -888,17 +888,34 @@ int target_name2index(const char *tgtname, __u32 *idx, const char **endptr) if (strncmp(dash, "all", 3) == 0) { if (endptr != NULL) *endptr = dash + 3; - return rc | LDD_F_SV_ALL; + return type | LDD_F_SV_ALL; } - index = simple_strtoul(dash, (char **)endptr, 16); - if (idx != NULL) + len = strspn(dash, "0123456789ABCDEFabcdef"); + if (len > 4) + return -ERANGE; + + if (strlen(dash) != len) { + char num[5]; + + num[4] = '\0'; + memcpy(num, dash, sizeof(num) - 1); + rc = kstrtou16(num, 16, &index); + if (rc < 0) + return rc; + } else { + rc = kstrtou16(dash, 16, &index); + if (rc < 0) + return rc; + } + + if (idx) *idx = index; - if (index > 0xffff) - return -ERANGE; + if (endptr) + *endptr = dash + len; - return rc; + return type; } EXPORT_SYMBOL(target_name2index); -- 1.8.3.1