From d51198f972f18ef99e887294c8c1de521ca66cf1 Mon Sep 17 00:00:00 2001 From: liubo Date: Thu, 26 May 2022 15:26:30 +0800 Subject: [PATCH 29/33] etmem: fix problem of abnormal task value 1. When adding a task, if the task value contains abnormal characters or the name is too long, an error should be returned. 2. remove useless function. Signed-off-by: liubo --- etmem/inc/etmemd_inc/etmemd_common.h | 4 ++-- etmem/src/etmemd_src/etmemd_common.c | 39 ++++++++---------------------------- etmem/src/etmemd_src/etmemd_task.c | 7 +++++++ 3 files changed, 17 insertions(+), 33 deletions(-) diff --git a/etmem/inc/etmemd_inc/etmemd_common.h b/etmem/inc/etmemd_inc/etmemd_common.h index f3808b3..795e600 100644 --- a/etmem/inc/etmemd_inc/etmemd_common.h +++ b/etmem/inc/etmemd_inc/etmemd_common.h @@ -45,6 +45,7 @@ /* in some system the max length of pid may be larger than 5, so we use 10 herr */ #define PID_STR_MAX_LEN 10 +#define NAME_STR_MAX_LEN 15 #define SWAP_THRESHOLD_MAX_LEN 10 #define PIPE_FD_LEN 2 @@ -76,7 +77,6 @@ void etmemd_safe_free(void **ptr); FILE *etmemd_get_proc_file(const char *pid, const char *file, const char *mode); int etmemd_send_ioctl_cmd(FILE *fp, struct ioctl_para *request); -int get_keyword_and_value(const char *str, char *key, char *val); unsigned long get_pagesize(void); int get_mem_from_proc_file(const char *pid, const char *file_name, unsigned long *data, const char *cmpstr); @@ -85,5 +85,5 @@ int dprintf_all(int fd, const char *format, ...); int get_swap_threshold_inKB(const char *string, unsigned long *value); int file_permission_check(const char *file_path, mode_t mode); int file_size_check(const char *file_path, off_t size); - +int check_str_valid(const char *str); #endif diff --git a/etmem/src/etmemd_src/etmemd_common.c b/etmem/src/etmemd_src/etmemd_common.c index a12a43f..649f472 100644 --- a/etmem/src/etmemd_src/etmemd_common.c +++ b/etmem/src/etmemd_src/etmemd_common.c @@ -357,54 +357,31 @@ static bool check_result_str_valid(const char *result) return true; } -static unsigned long skip_colon_to_parse(const char *str, unsigned long idx) -{ - size_t str_len = strlen(str); - - while (idx < str_len) { - if (!is_valid_char_for_value(" :\t", str[idx])) { - break; - } - idx += 1; - } - - return idx; -} - -/* - * the caller should make sure that str is not NULL, and the content - * of str must not contain ' ' or '\t' at the beginning and the end - * */ -int get_keyword_and_value(const char *str, char *key, char *val) +int check_str_valid(const char *str) { unsigned long idx = 0; unsigned long end_idx; size_t val_len; + char val[KEY_VALUE_MAX_LEN] = {0}; - get_valid_conf_str(str, idx, key, "%_", &end_idx); - if (!check_result_str_valid(key)) { - etmemd_log(ETMEMD_LOG_ERR, "get keyword of %s fail\n", str); - return -1; - } - if (!is_valid_char_for_value(" :\t", str[end_idx])) { - etmemd_log(ETMEMD_LOG_ERR, "%s contains invalid symbol in keyword\n", str); + if (str == NULL || strlen(str) == 0 || strlen(str) > NAME_STR_MAX_LEN) { + etmemd_log(ETMEMD_LOG_ERR, "input str: %s is invalid.\n", str); return -1; } - etmemd_log(ETMEMD_LOG_DEBUG, "parse config get key: %s\n", key); - /* skip the string contains ' ', '\t' and ':' between key and value */ - idx = skip_colon_to_parse(str, end_idx); - val_len = strlen(str) - idx; + val_len = strlen(str); get_valid_conf_str(str, idx, val, ".%/_-", &end_idx); if (!check_result_str_valid(val)) { - etmemd_log(ETMEMD_LOG_ERR, "get value of %s fail\n", str); + etmemd_log(ETMEMD_LOG_ERR, "get value of %s fail, contain invalid symbol\n", str); return -1; } + if (strlen(val) != val_len) { etmemd_log(ETMEMD_LOG_ERR, "%s contains invalid symbol in value\n", str); return -1; } + etmemd_log(ETMEMD_LOG_DEBUG, "parse config get value: %s\n", val); return 0; diff --git a/etmem/src/etmemd_src/etmemd_task.c b/etmem/src/etmemd_src/etmemd_task.c index 649d48e..dfe911f 100644 --- a/etmem/src/etmemd_src/etmemd_task.c +++ b/etmem/src/etmemd_src/etmemd_task.c @@ -440,6 +440,13 @@ static int fill_task_value(void *obj, void *val) { struct task *tk = (struct task *)obj; char *value = (char *)val; + + if (check_str_valid(value) != 0) { + etmemd_log(ETMEMD_LOG_ERR, "invalid task value, please check.\n"); + free(val); + return -1; + } + tk->value = value; return 0; } -- 1.8.3.1