Skip to content

Commit 362f91d

Browse files
committed
ares_expand_name() should escape more characters
RFC1035 5.1 specifies some reserved characters and escaping sequences that are allowed to be specified. Expand the list of reserved characters and also escape non-printable characters using the \DDD format as specified in the RFC. Bug Reported By: philipp.jeitner@sit.fraunhofer.de Fix By: Brad House (@bradh352)
1 parent 809d5e8 commit 362f91d

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

src/lib/ares_expand_name.c

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,26 @@
3232
static int name_length(const unsigned char *encoded, const unsigned char *abuf,
3333
int alen);
3434

35+
/* Reserved characters for names that need to be escaped */
36+
static int is_reservedch(int ch)
37+
{
38+
switch (ch) {
39+
case '"':
40+
case '.':
41+
case ';':
42+
case '\\':
43+
case '(':
44+
case ')':
45+
case '@':
46+
case '$':
47+
return 1;
48+
default:
49+
break;
50+
}
51+
52+
return 0;
53+
}
54+
3555
/* Expand an RFC1035-encoded domain name given by encoded. The
3656
* containing message is given by abuf and alen. The result given by
3757
* *s, which is set to a NUL-terminated allocated buffer. *enclen is
@@ -111,9 +131,18 @@ int ares_expand_name(const unsigned char *encoded, const unsigned char *abuf,
111131
p++;
112132
while (len--)
113133
{
114-
if (*p == '.' || *p == '\\')
134+
if (!isprint(*p)) {
135+
/* Output as \DDD for consistency with RFC1035 5.1 */
136+
*q++ = '\\';
137+
*q++ = '0' + *p / 100;
138+
*q++ = '0' + (*p % 100) / 10;
139+
*q++ = '0' + (*p % 10);
140+
} else if (is_reservedch(*p)) {
115141
*q++ = '\\';
116-
*q++ = *p;
142+
*q++ = *p;
143+
} else {
144+
*q++ = *p;
145+
}
117146
p++;
118147
}
119148
*q++ = '.';
@@ -171,7 +200,13 @@ static int name_length(const unsigned char *encoded, const unsigned char *abuf,
171200
encoded++;
172201
while (offset--)
173202
{
174-
n += (*encoded == '.' || *encoded == '\\') ? 2 : 1;
203+
if (!isprint(*encoded)) {
204+
n += 4;
205+
} else if (is_reservedch(*encoded)) {
206+
n += 2;
207+
} else {
208+
n += 1;
209+
}
175210
encoded++;
176211
}
177212
n++;

0 commit comments

Comments
 (0)