Enforce transport names being C identifiers.

Introduce string_is_C_identifier() and use it to enforce transport
names according to the 180 spec.
This commit is contained in:
George Kadianakis 2011-09-11 23:34:11 +02:00
parent 3136107421
commit c6811c57cb
4 changed files with 48 additions and 0 deletions

View File

@ -719,6 +719,34 @@ find_str_at_start_of_line(const char *haystack, const char *needle)
return NULL;
}
/** Returns true if <b>string</b> could be a C identifier.
A C identifier must begin with a letter or an underscore and the
rest of its characters can be letters, numbers or underscores. No
length limit is imposed. */
int
string_is_C_identifier(const char *string)
{
size_t iter;
size_t length = strlen(string);
if (!length)
return 0;
for (iter = 0; iter < length ; iter++) {
if (iter == 0) {
if (!(TOR_ISALPHA(string[iter]) ||
string[iter] == '_'))
return 0;
} else {
if (!(TOR_ISALPHA(string[iter]) ||
TOR_ISDIGIT(string[iter]) ||
string[iter] == '_'))
return 0;
}
}
return 1;
}
/** Return true iff the 'len' bytes at 'mem' are all zero. */
int
tor_mem_is_zero(const char *mem, size_t len)

View File

@ -203,6 +203,8 @@ const char *find_whitespace(const char *s) ATTR_PURE;
const char *find_whitespace_eos(const char *s, const char *eos) ATTR_PURE;
const char *find_str_at_start_of_line(const char *haystack, const char *needle)
ATTR_PURE;
int string_is_C_identifier(const char *string);
int tor_mem_is_zero(const char *mem, size_t len) ATTR_PURE;
int tor_digest_is_zero(const char *digest) ATTR_PURE;
int tor_digest256_is_zero(const char *digest) ATTR_PURE;

View File

@ -4724,6 +4724,10 @@ parse_client_transport_line(const char *line, int validate_only)
}
name = smartlist_get(items, 0);
if (!string_is_C_identifier(name)) {
log_warn(LD_CONFIG, "Transport name is not a C identifier (%s).", name);
goto err;
}
/* field2 is either a SOCKS version or "exec" */
field2 = smartlist_get(items, 1);
@ -4826,6 +4830,10 @@ parse_server_transport_line(const char *line, int validate_only)
}
name = smartlist_get(items, 0);
if (!string_is_C_identifier(name)) {
log_warn(LD_CONFIG, "Transport name is not a C identifier (%s).", name);
goto err;
}
type = smartlist_get(items, 1);

View File

@ -686,6 +686,11 @@ parse_smethod_line(const char *line, managed_proxy_t *mp)
tor_assert(!strcmp(smartlist_get(items,0),PROTO_SMETHOD));
method_name = smartlist_get(items,1);
if (!string_is_C_identifier(method_name)) {
log_warn(LD_CONFIG, "Transport name is not a C identifier (%s).",
method_name);
goto err;
}
addrport = smartlist_get(items, 2);
if (tor_addr_port_parse(addrport, &addr, &port)<0) {
@ -754,6 +759,11 @@ parse_cmethod_line(const char *line, managed_proxy_t *mp)
tor_assert(!strcmp(smartlist_get(items,0),PROTO_CMETHOD));
method_name = smartlist_get(items,1);
if (!string_is_C_identifier(method_name)) {
log_warn(LD_CONFIG, "Transport name is not a C identifier (%s).",
method_name);
goto err;
}
socks_ver_str = smartlist_get(items,2);