From b8efaf4e135379d3192897f6ffd5bfb3ea842b61 Mon Sep 17 00:00:00 2001 From: evgeny Date: Mon, 26 Oct 2026 00:15:47 +0100 Subject: [PATCH] Check service scalars and proxy ports before storing configuration --- src/config_parser.c | 87 ++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 45 deletions(-) diff --git a/src/config_parser.c b/src/config_parser.c index d3f93833f..ef77f6482 100644 --- a/src/config_parser.c +++ b/src/config_parser.c @@ -456,40 +456,34 @@ static int parse_control(const char *key, const char *value, struct global_confi static int parse_tcp_proxy_client(const char *key, const char *value, struct global_config *global, const char *filename, int line_num) { if (strcmp(key, "enabled") == 0) { - global->tcp_proxy_client_enabled = strcasecmp(value, "yes") == 0 || strcasecmp(value, "1") == 0 || strcasecmp(value, "true") == 0; - return 0; + int enabled = parse_boolean(value); if (enabled < 0) return -1; + global->tcp_proxy_client_enabled = enabled; return 0; } if (strcmp(key, "tun_name") == 0) { - strncpy(global->tcp_proxy_client_tun_name, value, sizeof(global->tcp_proxy_client_tun_name) - 1); - return 0; + return assign_string(global->tcp_proxy_client_tun_name, sizeof(global->tcp_proxy_client_tun_name), value); } if (strcmp(key, "tun_ip") == 0) { - strncpy(global->tcp_proxy_client_tun_ip, value, sizeof(global->tcp_proxy_client_tun_ip) - 1); - return 0; + return assign_string(global->tcp_proxy_client_tun_ip, sizeof(global->tcp_proxy_client_tun_ip), value); } if (strcmp(key, "mtu") == 0) { - global->tcp_proxy_client_mtu = atoi(value); - return 0; + return parse_integer(value, 0, UINT16_MAX, &global->tcp_proxy_client_mtu); } if (strcmp(key, "via_node") == 0) { - global->tcp_proxy_client_via_node_id = strtoull(value, NULL, 16); - return 0; + return parse_unsigned(value, 16, UINT64_MAX, &global->tcp_proxy_client_via_node_id); } if (strcmp(key, "socks_enabled") == 0) { - global->tcp_proxy_client_socks_enabled = strcasecmp(value, "yes") == 0 || strcasecmp(value, "1") == 0 || strcasecmp(value, "true") == 0; - return 0; + int enabled = parse_boolean(value); if (enabled < 0) return -1; + global->tcp_proxy_client_socks_enabled = enabled; return 0; } if (strcmp(key, "socks_addr") == 0) { - strncpy(global->tcp_proxy_client_socks_addr, value, sizeof(global->tcp_proxy_client_socks_addr) - 1); - return 0; + return assign_string(global->tcp_proxy_client_socks_addr, sizeof(global->tcp_proxy_client_socks_addr), value); } if (strcmp(key, "http_proxy_enabled") == 0) { - global->tcp_proxy_client_http_proxy_enabled = strcasecmp(value, "yes") == 0 || strcasecmp(value, "1") == 0 || strcasecmp(value, "true") == 0; - return 0; + int enabled = parse_boolean(value); if (enabled < 0) return -1; + global->tcp_proxy_client_http_proxy_enabled = enabled; return 0; } if (strcmp(key, "http_proxy_addr") == 0) { - strncpy(global->tcp_proxy_client_http_proxy_addr, value, sizeof(global->tcp_proxy_client_http_proxy_addr) - 1); - return 0; + return assign_string(global->tcp_proxy_client_http_proxy_addr, sizeof(global->tcp_proxy_client_http_proxy_addr), value); } if (strcmp(key, "forward") == 0) { if (global->tcp_proxy_client_mapping_count >= MAX_TCP_PROXY_CLIENT_MAPPINGS) { @@ -497,8 +491,7 @@ static int parse_tcp_proxy_client(const char *key, const char *value, struct glo return -1; } char buf[256]; - strncpy(buf, value, sizeof(buf) - 1); - buf[sizeof(buf) - 1] = '\0'; + if (assign_string(buf, sizeof(buf), value)) return -1; config_ini_trim(buf); char* local_port_str = strtok(buf, " "); @@ -508,8 +501,10 @@ static int parse_tcp_proxy_client(const char *key, const char *value, struct glo DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Invalid tcp_proxy_client forward format: %s (expected: port -> ip:port)", value); return -1; } - int local_port = atoi(local_port_str); - if (local_port <= 0 || local_port > 65535) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Invalid tcp_proxy_client forward local port: %s", local_port_str); return -1; } + uint64_t local_port; + if (parse_unsigned(local_port_str, 10, UINT16_MAX, &local_port) || !local_port) { + DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "invalid tcp_proxy_client forward local port"); return -1; + } char* remote_ip_str = strtok(remote_str, ":"); char* remote_port_str = strtok(NULL, ""); @@ -517,8 +512,11 @@ static int parse_tcp_proxy_client(const char *key, const char *value, struct glo DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Invalid tcp_proxy_client forward remote: %s (expected ip:port)", remote_str); return -1; } - int remote_port = atoi(remote_port_str); - if (remote_port <= 0 || remote_port > 65535) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Invalid tcp_proxy_client forward remote port: %s", remote_port_str); return -1; } + uint64_t remote_port; + struct in_addr address; + if (parse_unsigned(remote_port_str, 10, UINT16_MAX, &remote_port) || !remote_port || inet_pton(AF_INET, remote_ip_str, &address) != 1) { + DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "invalid tcp_proxy_client forward remote IPv4/port"); return -1; + } int idx = global->tcp_proxy_client_mapping_count; global->tcp_proxy_client_mappings[idx].local_port = (uint16_t)local_port; @@ -660,12 +658,10 @@ static int parse_server(const char *key, const char *value, struct CFG_SERVER *s return parse_address_and_port(value, &srv->ip); } if (strcmp(key, "so_mark") == 0) { - srv->so_mark = atoi(value); - return 0; + return parse_integer(value, INT_MIN, INT_MAX, &srv->so_mark); } if (strcmp(key, "fib") == 0) { - srv->fib = atoi(value); - return 0; + return parse_integer(value, -1, INT_MAX, &srv->fib); } if (strcmp(key, "netif") == 0) { srv->netif_index = get_netif_index(value); @@ -686,12 +682,11 @@ static int parse_server(const char *key, const char *value, struct CFG_SERVER *s return 0; } if (strcmp(key, "mtu") == 0) { - srv->mtu = atoi(value); - return 0; + return parse_integer(value, 0, UINT16_MAX, &srv->mtu); } if (strcmp(key, "only_local") == 0) { - srv->only_local = atoi(value) ? 1 : 0; - return 0; + int enabled = parse_boolean(value); if (enabled < 0) return -1; + srv->only_local = (uint8_t)enabled; return 0; } if (strcmp(key, "transport") == 0) { if (strcmp(value, "tcp") == 0) srv->transport = 1; @@ -700,12 +695,12 @@ static int parse_server(const char *key, const char *value, struct CFG_SERVER *s return 0; } if (strcmp(key, "reality") == 0) { - srv->reality_enabled = strcasecmp(value, "yes") == 0 || strcasecmp(value, "1") == 0 || strcasecmp(value, "true") == 0; - return 0; + int enabled = parse_boolean(value); if (enabled < 0) return -1; + srv->reality_enabled = (uint8_t)enabled; return 0; } if (strcmp(key, "socks") == 0) { - srv->socks_enabled = strcasecmp(value, "yes") == 0 || strcasecmp(value, "1") == 0 || strcasecmp(value, "true") == 0; - return 0; + int enabled = parse_boolean(value); if (enabled < 0) return -1; + srv->socks_enabled = (uint8_t)enabled; return 0; } DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Unknown server option '%s'. Valid: addr, so_mark, fib, netif, type, mtu, only_local, transport, reality, socks", filename, line_num, key); return -1; @@ -744,13 +739,12 @@ static int parse_client(const char *key, const char *value, struct CFG_CLIENT *c } if (strcmp(key, "keepalive") == 0) { - cli->keepalive = atoi(value); - return 0; + return parse_integer(value, 0, INT_MAX, &cli->keepalive); } if (strcmp(key, "reality") == 0) { - cli->reality_enabled = strcasecmp(value, "yes") == 0 || strcasecmp(value, "1") == 0 || strcasecmp(value, "true") == 0; - return 0; + int enabled = parse_boolean(value); if (enabled < 0) return -1; + cli->reality_enabled = (uint8_t)enabled; return 0; } if (strcmp(key, "server_name") == 0) { return assign_string(cli->reality.server_name, sizeof(cli->reality.server_name), value); } if (strcmp(key, "short_id") == 0) { @@ -858,7 +852,10 @@ static int parse_network(const char *key, const char *value, struct CFG_NETWORK } static int parse_reality(const char *key, const char *value, struct reality_config *rc, const char *filename, int line_num) { - if (strcmp(key, "enabled") == 0) { rc->enabled = atoi(value) ? 1 : 0; return 0; } + if (strcmp(key, "enabled") == 0) { + int enabled = parse_boolean(value); if (enabled < 0) return -1; + rc->enabled = enabled; return 0; + } if (strcmp(key, "server_name") == 0) { return assign_string(rc->server_name, sizeof(rc->server_name), value); } if (strcmp(key, "dest") == 0) { return assign_string(rc->dest, sizeof(rc->dest), value); } if (strcmp(key, "short_id") == 0) { @@ -897,8 +894,8 @@ static int parse_reality(const char *key, const char *value, struct reality_conf if (strcmp(key, "version") == 0) { return parse_version(value, rc->version); } - if (strcmp(key, "time_window") == 0) { rc->time_window_sec = atoi(value); return 0; } - if (strcmp(key, "relay_idle_timeout") == 0) { rc->relay_idle_timeout_sec = atoi(value); return 0; } + if (strcmp(key, "time_window") == 0) return parse_integer(value, 0, 3600, &rc->time_window_sec); + if (strcmp(key, "relay_idle_timeout") == 0) return parse_integer(value, 0, INT_MAX / 10000, &rc->relay_idle_timeout_sec); if (strcmp(key, "fingerprint") == 0) { if (strcmp(value, "openssl") == 0) { rc->fingerprint = REALITY_FP_OPENSSL; return 0; } DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: unknown reality fingerprint '%s' (valid: openssl)", filename, line_num, value); @@ -911,8 +908,8 @@ static int parse_reality(const char *key, const char *value, struct reality_conf // Разбор [socks] секции: прокси для исходящих ETCP-линков. static int parse_socks(const char *key, const char *value, struct global_config *global, const char *filename, int line_num) { if (strcmp(key, "enabled") == 0) { - global->socks_enabled = strcasecmp(value, "yes") == 0 || strcasecmp(value, "1") == 0 || strcasecmp(value, "true") == 0; - return 0; + int enabled = parse_boolean(value); if (enabled < 0) return -1; + global->socks_enabled = enabled; return 0; } if (strcmp(key, "addr") == 0) { const char *colon = strrchr(value, ':');