From 5750fedb9125af7c8d4ec5ef41d06ae72b728244 Mon Sep 17 00:00:00 2001 From: jake Date: Wed, 30 Aug 2023 05:06:57 +0000 Subject: [PATCH 1/2] !46 Check empty pointer before referenced * Fix empty pointer and overflow --- src/api.c | 8 ++++++++ src/conf.c | 3 +++ src/invoke/exec.c | 2 ++ src/types/types.c | 23 +++++++++++++++++++++++ src/utils.c | 2 +- src/version/version.c | 5 +++++ 6 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/api.c b/src/api.c index 13a4ec8..460223f 100644 --- a/src/api.c +++ b/src/api.c @@ -844,6 +844,10 @@ int cni_conf_from_file(const char *filename, struct cni_network_conf **config, c ERROR("Empty err"); return -1; } + if (config == NULL) { + ERROR("Empty config"); + return -1; + } ret = conf_from_file(filename, &netconf, err); if (ret != 0) { ERROR("Parse conf file: %s failed: %s", filename, *err != NULL ? *err : ""); @@ -932,6 +936,10 @@ int cni_conflist_from_file(const char *filename, struct cni_network_list_conf ** ERROR("Empty err"); return -1; } + if (list == NULL) { + ERROR("Empty list"); + return -1; + } ret = conflist_from_file(filename, &tmp_cni_net_conf_list, err); if (ret != 0) { return ret; diff --git a/src/conf.c b/src/conf.c index d1ff3d9..a3214b3 100644 --- a/src/conf.c +++ b/src/conf.c @@ -495,6 +495,7 @@ int load_conf(const char *dir, const char *name, struct network_config **conf, c *err = clibcni_util_strdup_s("Out of memory"); } ERROR("no net configurations found in %s", dir); + ret = -1; goto free_out; } @@ -538,6 +539,7 @@ static int generate_new_conflist(const cni_net_conf_list *list, struct network_c *err = clibcni_util_strdup_s("Out of memory"); } ERROR("Generate conf list json failed: %s", jerr); + ret = -1; goto free_out; } free(jerr); @@ -551,6 +553,7 @@ static int generate_new_conflist(const cni_net_conf_list *list, struct network_c *err = clibcni_util_strdup_s("Out of memory"); } ERROR("Parse conf list from json failed: %s", jerr); + ret = -1; goto free_out; } ret = 0; diff --git a/src/invoke/exec.c b/src/invoke/exec.c index becba55..16d53ea 100644 --- a/src/invoke/exec.c +++ b/src/invoke/exec.c @@ -72,6 +72,7 @@ static int do_parse_exec_stdout_str(int exec_ret, const char *cni_net_conf_json, goto out; } if (clibcni_is_null_or_empty(stdout_str)) { + ret = -1; ERROR("Get empty stdout message"); goto out; } @@ -140,6 +141,7 @@ int exec_plugin_without_result(const char *plugin_path, const char *cni_net_conf envs = as_env(cniargs); if (envs == NULL) { *err = clibcni_util_strdup_s("As env failed"); + ret = -1; goto out; } } diff --git a/src/types/types.c b/src/types/types.c index a9a04e7..24e3f1b 100644 --- a/src/types/types.c +++ b/src/types/types.c @@ -453,6 +453,12 @@ static int get_ipv6_mask(const struct ipnet *value, size_t iplen, uint8_t **mask (void)memcpy(*mask, (value->ip_mask + IPV4_TO_V6_EMPTY_PREFIX_BYTES), IPV4LEN); return IPV4LEN; } else { + *mask = clibcni_util_smart_calloc_s(IPV6LEN, sizeof(uint8_t)); + if (*mask == NULL) { + *err = clibcni_util_strdup_s("Out of memory"); + ERROR("Out of memory"); + return 0; + } (void)memcpy(*mask, value->ip_mask, IPV6LEN); return IPV6LEN; } @@ -551,6 +557,10 @@ char *ipnet_to_string(const struct ipnet *value, char **err) int nret = 0; size_t res_len = 0; + if (value == NULL || err == NULL) { + ERROR("Invalid arg"); + return NULL; + } iplen = try_to_ipv4(value, &ip, err); if (iplen == 0) { goto free_out; @@ -686,6 +696,14 @@ int parse_ip_from_str(const char *addr, uint8_t **ips, size_t *len, char **err) ERROR("Empty address"); return -1; } + if (err == NULL) { + ERROR("Empty err"); + return -1; + } + if (ips == NULL || len == NULL) { + ERROR("Invalid argument"); + return -1; + } nret = inet_pton(AF_INET, addr, &ipv4); if (nret < 0) { nret = asprintf(err, "ipv4 inet_pton %s", strerror(errno)); @@ -754,6 +772,11 @@ int parse_cidr(const char *cidr_str, struct ipnet **ipnet_val, char **err) return -1; } + if (ipnet_val == NULL || err == NULL) { + ERROR("Invalid argument"); + return -1; + } + work_cidr = clibcni_util_strdup_s(cidr_str); result = clibcni_util_common_calloc_s(sizeof(struct ipnet)); diff --git a/src/utils.c b/src/utils.c index 4308b62..8efa330 100644 --- a/src/utils.c +++ b/src/utils.c @@ -417,7 +417,7 @@ int clibcni_util_safe_uint(const char *numstr, unsigned int *converted) char *err_str = NULL; unsigned long long ull = 0; - if (converted == NULL) { + if (numstr == NULL || converted == NULL) { return -1; } errno = 0; diff --git a/src/version/version.c b/src/version/version.c index 058e30f..00aa149 100644 --- a/src/version/version.c +++ b/src/version/version.c @@ -75,6 +75,11 @@ struct plugin_info *plugin_supports(const char * const *supported_versions, size size_t size = 0; bool invalid_arg = (supported_versions == NULL || len < 1); + if (errmsg == NULL) { + ERROR("Empty errmsg"); + return NULL; + } + if (invalid_arg) { *errmsg = clibcni_util_strdup_s("Invalid version argument"); return NULL; -- 2.33.0