From b3ad44d4c1d3a265470a70da984cc3ceea7e0f5d Mon Sep 17 00:00:00 2001 From: Shuo A Liu Date: Tue, 25 Dec 2018 10:33:36 +0800 Subject: [PATCH] dm: use strnlen to replace strlen Tracked-On: #2133 Signed-off-by: Shuo A Liu Reviewed-by: Yonghua Huang --- devicemodel/core/hugetlb.c | 14 +++++++------- devicemodel/core/main.c | 4 ++-- devicemodel/core/smbiostbl.c | 4 ++-- devicemodel/core/sw_load_bzimage.c | 4 ++-- devicemodel/core/sw_load_common.c | 2 +- devicemodel/core/sw_load_ovmf.c | 2 +- devicemodel/core/sw_load_vsbl.c | 4 ++-- devicemodel/core/vmmapi.c | 2 +- devicemodel/hw/pci/virtio/virtio_net.c | 2 +- devicemodel/hw/pci/xhci.c | 3 ++- devicemodel/hw/platform/rpmb/rpmb_backend.c | 2 +- devicemodel/hw/platform/tpm/tpm.c | 4 ++-- devicemodel/hw/platform/tpm/tpm_emulator.c | 2 +- devicemodel/hw/platform/usb_mouse.c | 3 ++- 14 files changed, 27 insertions(+), 25 deletions(-) diff --git a/devicemodel/core/hugetlb.c b/devicemodel/core/hugetlb.c index 9c4787def..d39812b7b 100644 --- a/devicemodel/core/hugetlb.c +++ b/devicemodel/core/hugetlb.c @@ -157,7 +157,7 @@ static int open_hugetlbfs(struct vmctx *ctx, int level) UUID[12], UUID[13], UUID[14], UUID[15]); *(path + len) = '\0'; - strncat(path, uuid_str, strlen(uuid_str)); + strncat(path, uuid_str, strnlen(uuid_str, sizeof(uuid_str))); printf("open hugetlbfs file %s\n", path); @@ -347,19 +347,19 @@ static int create_hugetlb_dirs(int level) } path = hugetlb_priv[level].mount_path; - len = strlen(path); - if (len >= MAX_PATH_LEN || len == 0) { + len = strnlen(path, MAX_PATH_LEN); + if (len == MAX_PATH_LEN || len == 0) { perror("invalid path len"); return -EINVAL; } memset(tmp_path, '\0', MAX_PATH_LEN); - strncpy(tmp_path, path, MAX_PATH_LEN - 1); + strncpy(tmp_path, path, MAX_PATH_LEN); - if ((tmp_path[len - 1] != '/') && (strlen(tmp_path) < MAX_PATH_LEN - 1)) - strcat(tmp_path, "/"); + if ((tmp_path[len - 1] != '/') && (len < MAX_PATH_LEN - 1)) + tmp_path[len] = '/'; - len = strlen(tmp_path); + len = strnlen(tmp_path, MAX_PATH_LEN); for (i = 1; i < len; i++) { if (tmp_path[i] == '/') { tmp_path[i] = 0; diff --git a/devicemodel/core/main.c b/devicemodel/core/main.c index 40451be61..250301a7d 100644 --- a/devicemodel/core/main.c +++ b/devicemodel/core/main.c @@ -165,8 +165,8 @@ usage(int code) " --intr_monitor: enable interrupt storm monitor\n" " --vtpm2: Virtual TPM2 args: sock_path=$PATH_OF_SWTPM_SOCKET\n" "............its params: threshold/s,probe-period(s),delay_time(ms),delay_duration(ms)\n", - progname, (int)strlen(progname), "", (int)strlen(progname), "", - (int)strlen(progname), ""); + progname, (int)strnlen(progname, PATH_MAX), "", (int)strnlen(progname, PATH_MAX), "", + (int)strnlen(progname, PATH_MAX), ""); exit(code); } diff --git a/devicemodel/core/smbiostbl.c b/devicemodel/core/smbiostbl.c index f850ad282..4020c1529 100644 --- a/devicemodel/core/smbiostbl.c +++ b/devicemodel/core/smbiostbl.c @@ -544,7 +544,7 @@ smbios_generic_initializer(struct smbios_structure *template_entry, int len; string = template_strings[i]; - len = strlen(string) + 1; + len = strnlen(string, SMBIOS_MAX_LENGTH) + 1; memcpy(curaddr, string, len); curaddr += len; } @@ -597,7 +597,7 @@ smbios_type1_initializer(struct smbios_structure *template_entry, return -1; MD5_Init(&mdctx); - MD5_Update(&mdctx, vmname, strlen(vmname)); + MD5_Update(&mdctx, vmname, strnlen(vmname, PATH_MAX)); MD5_Update(&mdctx, hostname, sizeof(hostname)); MD5_Final(digest, &mdctx); diff --git a/devicemodel/core/sw_load_bzimage.c b/devicemodel/core/sw_load_bzimage.c index 93c33f154..f79e5da24 100644 --- a/devicemodel/core/sw_load_bzimage.c +++ b/devicemodel/core/sw_load_bzimage.c @@ -129,7 +129,7 @@ acrn_get_bzimage_setup_size(struct vmctx *ctx) int acrn_parse_kernel(char *arg) { - size_t len = strlen(arg); + size_t len = strnlen(arg, STR_LEN); if (len < STR_LEN) { strncpy(kernel_path, arg, len + 1); @@ -150,7 +150,7 @@ acrn_parse_kernel(char *arg) int acrn_parse_ramdisk(char *arg) { - size_t len = strlen(arg); + size_t len = strnlen(arg, STR_LEN); if (len < STR_LEN) { strncpy(ramdisk_path, arg, len + 1); diff --git a/devicemodel/core/sw_load_common.c b/devicemodel/core/sw_load_common.c index 66030d965..4779a3494 100644 --- a/devicemodel/core/sw_load_common.c +++ b/devicemodel/core/sw_load_common.c @@ -99,7 +99,7 @@ const struct e820_entry e820_default_entries[NUM_E820_ENTRIES] = { int acrn_parse_bootargs(char *arg) { - size_t len = strlen(arg); + size_t len = strnlen(arg, STR_LEN); if (len < STR_LEN) { strncpy(bootargs, arg, len + 1); diff --git a/devicemodel/core/sw_load_ovmf.c b/devicemodel/core/sw_load_ovmf.c index f91e7cec9..3916d9fce 100644 --- a/devicemodel/core/sw_load_ovmf.c +++ b/devicemodel/core/sw_load_ovmf.c @@ -70,7 +70,7 @@ int acrn_parse_ovmf(char *arg) { int error; - size_t len = strlen(arg); + size_t len = strnlen(arg, STR_LEN); if (len < STR_LEN) { strncpy(ovmf_path, arg, len + 1); diff --git a/devicemodel/core/sw_load_vsbl.c b/devicemodel/core/sw_load_vsbl.c index 583738b79..29963914f 100644 --- a/devicemodel/core/sw_load_vsbl.c +++ b/devicemodel/core/sw_load_vsbl.c @@ -126,7 +126,7 @@ int acrn_parse_guest_part_info(char *arg) { int error; - size_t len = strlen(arg); + size_t len = strnlen(arg, STR_LEN); if (len < STR_LEN) { strncpy(guest_part_info_path, arg, len + 1); @@ -195,7 +195,7 @@ int acrn_parse_vsbl(char *arg) { int error; - size_t len = strlen(arg); + size_t len = strnlen(arg, STR_LEN); if (len < STR_LEN) { strncpy(vsbl_path, arg, len + 1); diff --git a/devicemodel/core/vmmapi.c b/devicemodel/core/vmmapi.c index fdf9188db..00a0bca51 100644 --- a/devicemodel/core/vmmapi.c +++ b/devicemodel/core/vmmapi.c @@ -93,7 +93,7 @@ vm_create(const char *name, uint64_t req_buf) uuid_t vm_uuid; memset(&create_vm, 0, sizeof(struct acrn_create_vm)); - ctx = calloc(1, sizeof(struct vmctx) + strlen(name) + 1); + ctx = calloc(1, sizeof(struct vmctx) + strnlen(name, PATH_MAX) + 1); assert(ctx != NULL); assert(devfd == -1); diff --git a/devicemodel/hw/pci/virtio/virtio_net.c b/devicemodel/hw/pci/virtio/virtio_net.c index 6177b9793..9ba4b86a8 100644 --- a/devicemodel/hw/pci/virtio/virtio_net.c +++ b/devicemodel/hw/pci/virtio/virtio_net.c @@ -823,7 +823,7 @@ virtio_net_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts) dev->func, mac_seed); MD5_Init(&mdctx); - MD5_Update(&mdctx, nstr, strlen(nstr)); + MD5_Update(&mdctx, nstr, strnlen(nstr, sizeof(nstr))); MD5_Final(digest, &mdctx); net->config.mac[0] = 0x00; diff --git a/devicemodel/hw/pci/xhci.c b/devicemodel/hw/pci/xhci.c index 37bf570b1..34ed3084e 100644 --- a/devicemodel/hw/pci/xhci.c +++ b/devicemodel/hw/pci/xhci.c @@ -501,6 +501,7 @@ static int pci_xhci_parse_log_level(struct pci_xhci_vdev *xdev, char *opts); static int pci_xhci_parse_extcap(struct pci_xhci_vdev *xdev, char *opts); static int pci_xhci_convert_speed(int lspeed); +#define XHCI_OPT_MAX_LEN 32 static struct pci_xhci_option_elem xhci_option_table[] = { {"tablet", pci_xhci_parse_tablet}, {"log", pci_xhci_parse_log_level}, @@ -3985,7 +3986,7 @@ pci_xhci_parse_opts(struct pci_xhci_vdev *xdev, char *opts) if (!n || !f) continue; - if (!strncmp(t, n, strlen(n))) { + if (!strncmp(t, n, strnlen(n, XHCI_OPT_MAX_LEN))) { f(xdev, t); break; } diff --git a/devicemodel/hw/platform/rpmb/rpmb_backend.c b/devicemodel/hw/platform/rpmb/rpmb_backend.c index a4645d5c8..fe38104db 100644 --- a/devicemodel/hw/platform/rpmb/rpmb_backend.c +++ b/devicemodel/hw/platform/rpmb/rpmb_backend.c @@ -205,7 +205,7 @@ static int rpmb_check_frame(const char *cmd_str, int *err, return -1; } - len = strlen(cmd_str) + 1; + len = strnlen(cmd_str, sizeof(WRITE_DATA_STR)) + 1; if (len > sizeof(WRITE_DATA_STR)) len = sizeof(WRITE_DATA_STR); diff --git a/devicemodel/hw/platform/tpm/tpm.c b/devicemodel/hw/platform/tpm/tpm.c index b0baa0414..37a3f8e87 100644 --- a/devicemodel/hw/platform/tpm/tpm.c +++ b/devicemodel/hw/platform/tpm/tpm.c @@ -37,9 +37,9 @@ char *const token[] = { int acrn_parse_vtpm2(char *arg) { char *value; - size_t len = strlen(arg); + size_t len = strnlen(arg, STR_MAX_LEN); - if (len > STR_MAX_LEN) + if (len == STR_MAX_LEN) return -1; if (SOCK_PATH_OPT == getsubopt(&arg, token, &value)) { diff --git a/devicemodel/hw/platform/tpm/tpm_emulator.c b/devicemodel/hw/platform/tpm/tpm_emulator.c index da34ba5aa..640bea964 100644 --- a/devicemodel/hw/platform/tpm/tpm_emulator.c +++ b/devicemodel/hw/platform/tpm/tpm_emulator.c @@ -217,7 +217,7 @@ static int ctrl_chan_conn(const char *servername) return -1; } - if (strlen(servername) > (sizeof(servaddr.sun_path) -1)) { + if (strnlen(servername, sizeof(servaddr.sun_path)) == (sizeof(servaddr.sun_path))) { printf("%s error, length of servername is too long\n", __func__); return -1; } diff --git a/devicemodel/hw/platform/usb_mouse.c b/devicemodel/hw/platform/usb_mouse.c index 4a52a695e..e4fa05fd6 100644 --- a/devicemodel/hw/platform/usb_mouse.c +++ b/devicemodel/hw/platform/usb_mouse.c @@ -59,6 +59,7 @@ enum { UMSTR_MAX }; +#define UMOUSE_DESC_MAX_LEN 32 static const char *const umouse_desc_strings[] = { "\x04\x09", "ACRN-DM", @@ -437,7 +438,7 @@ umouse_request(void *scarg, struct usb_data_xfer *xfer) goto done; } - slen = 2 + strlen(str) * 2; + slen = 2 + strnlen(str, UMOUSE_DESC_MAX_LEN) * 2; udata[0] = slen; udata[1] = UDESC_STRING;