Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dns/ns/resource: send REFUSED if name contains special characters (big fix 2/4) #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,6 @@ hsk_cache_key_set(hsk_cache_key_t *ck, const char *name, uint16_t type) {
if (!hsk_dns_name_verify(name))
return false;

if (hsk_dns_name_dirty(name))
return false;

int labels = hsk_dns_label_count(name);
bool ref = false;

Expand Down
36 changes: 19 additions & 17 deletions src/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -725,9 +725,6 @@ hsk_dns_rr_set_name(hsk_dns_rr_t *rr, const char *name) {
if (!hsk_dns_name_verify(name))
return false;

if (hsk_dns_name_dirty(name))
return false;

if (hsk_dns_name_is_fqdn(name))
strcpy(rr->name, name);
else
Expand Down Expand Up @@ -2404,26 +2401,31 @@ hsk_dns_name_alloc(

bool
hsk_dns_name_dirty(const char *name) {
char *s = (char *)name;
int len = strlen(name);
if (len > HSK_DNS_MAX_LABEL)
return true;

while (*s) {
uint8_t c = (uint8_t)*s;
for (int i = 0; i < len; i++) {
uint8_t c = name[i];

if (c >= 0x41 && c <= 0x5a)
c ^= 0x20;

switch (c) {
case 0x28 /*(*/:
case 0x29 /*)*/:
case 0x3b /*;*/:
case 0x20 /* */:
case 0x40 /*@*/:
case 0x22 /*"*/:
case 0x5c /*\\*/:
return true;
case 0x5f: /* _ */
case 0x2d: /* - */
if (i == 0 || i == len - 1) {
return true;
} else {
continue;
}
}

if (c < 0x20 || c > 0x7e)
if (c < 0x30 ||
(c > 0x39 && c < 0x61) ||
c > 0x7a) {
return true;

s += 1;
}
}

return false;
Expand Down
30 changes: 26 additions & 4 deletions src/ns.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ hsk_ns_onrecv(
const struct sockaddr *addr,
uint32_t flags
) {
uint8_t *wire = NULL;
size_t wire_len = 0;
hsk_dns_msg_t *msg = NULL;

hsk_dns_req_t *req = hsk_dns_req_create(data, data_len, addr);

if (!req) {
Expand All @@ -306,10 +310,6 @@ hsk_ns_onrecv(

hsk_dns_req_print(req, "ns: ");

uint8_t *wire = NULL;
size_t wire_len = 0;
hsk_dns_msg_t *msg = NULL;

// Hit cache first.
msg = hsk_cache_get(&ns->cache, req);

Expand Down Expand Up @@ -434,6 +434,28 @@ hsk_ns_onrecv(
goto done;
}

// Send REFUSED if name is dirty
// (contains escaped byte codes or special characters)
if (hsk_dns_name_dirty(req->tld)) {
msg = hsk_resource_to_refused();

if (!msg) {
hsk_ns_log(ns, "failed creating refused\n");
goto fail;
}

if (!hsk_dns_msg_finalize(&msg, req, ns->ec, ns->key, &wire, &wire_len)) {
hsk_ns_log(ns, "could not reply\n");
goto done;
}

hsk_ns_log(ns, "refusing query for msg (%u): %u\n", req->id, wire_len);

hsk_ns_send(ns, wire, wire_len, addr, true);

goto done;
}

// Requesting a lookup.
if (req->labels > 0) {
// Check blacklist.
Expand Down
13 changes: 0 additions & 13 deletions src/req.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,9 @@ hsk_dns_req_create(
// Grab the first question.
hsk_dns_qs_t *qs = msg->qd.items[0];

#if 0
if (qs->class != HSK_DNS_IN)
goto fail;

// Don't allow dirty names.
if (hsk_dns_name_dirty(qs->name))
goto fail;
#endif

// Check for a TLD.
hsk_dns_label_get(qs->name, -1, req->tld);

// Don't allow dirty TLDs.
if (hsk_dns_name_dirty(req->tld))
goto fail;

// Lowercase.
hsk_to_lower(req->tld);

Expand Down
12 changes: 12 additions & 0 deletions src/resource.c
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,18 @@ hsk_resource_to_servfail(void) {
return msg;
}

hsk_dns_msg_t *
hsk_resource_to_refused(void) {
hsk_dns_msg_t *msg = hsk_dns_msg_alloc();

if (!msg)
return NULL;

msg->code = HSK_DNS_REFUSED;

return msg;
}

hsk_dns_msg_t *
hsk_resource_to_notimp(void) {
hsk_dns_msg_t *msg = hsk_dns_msg_alloc();
Expand Down
3 changes: 3 additions & 0 deletions src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ hsk_resource_to_nx(void);
hsk_dns_msg_t *
hsk_resource_to_servfail(void);

hsk_dns_msg_t *
hsk_resource_to_refused(void);

hsk_dns_msg_t *
hsk_resource_to_notimp(void);

Expand Down
23 changes: 23 additions & 0 deletions test/hnsd-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,34 @@ test_pointer_to_ip() {
assert(family6 == HSK_DNS_AAAA);
}

void
test_name_dirty() {
printf("test_name_dirty\n");

assert(!hsk_dns_name_dirty("hello"));
assert(!hsk_dns_name_dirty("HELLO"));
assert(!hsk_dns_name_dirty("heLLo"));
assert(!hsk_dns_name_dirty("HeLl0"));
assert(!hsk_dns_name_dirty("hel-lo"));
assert(!hsk_dns_name_dirty("1"));
assert(!hsk_dns_name_dirty("000_000"));
assert(!hsk_dns_name_dirty("this-domain-name-has-sixty-three-octets-taking-max-label-length"));
assert(hsk_dns_name_dirty("hel!lo"));
assert(hsk_dns_name_dirty("-hello"));
assert(hsk_dns_name_dirty("hello_"));
assert(hsk_dns_name_dirty("1@1"));
assert(hsk_dns_name_dirty("x\\000y"));
assert(hsk_dns_name_dirty("H&ELLO"));
assert(hsk_dns_name_dirty("3 3"));
assert(hsk_dns_name_dirty("this-domain-name-has-sixtyfour-octets-exceeding-max-label-length"));
}

int
main() {
printf("Testing hnsd...\n");
test_base32();
test_pointer_to_ip();
test_name_dirty();

printf("ok\n");

Expand Down