udhcp: we call DNS name encoder with NULL, this can SEGV. added a check
[oweals/busybox.git] / networking / udhcp / domain_codec.c
1 /* vi: set sw=4 ts=4: */
2
3 /* RFC1035 domain compression routines (C) 2007 Gabriel Somlo <somlo at cmu.edu>
4  *
5  * Loosely based on the isc-dhcpd implementation by dhankins@isc.org
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 #include "common.h"
11
12 #define NS_MAXDNAME  1025       /* max domain name length */
13 #define NS_MAXCDNAME  255       /* max compressed domain name length */
14 #define NS_MAXLABEL    63       /* max label length */
15 #define NS_MAXDNSRCH    6       /* max domains in search path */
16 #define NS_CMPRSFLGS 0xc0       /* name compression pointer flag */
17
18
19 /* expand a RFC1035-compressed list of domain names "cstr", of length "clen";
20  * returns a newly allocated string containing the space-separated domains,
21  * prefixed with the contents of string pre, or NULL if an error occurs.
22  */
23 char* FAST_FUNC dname_dec(const uint8_t *cstr, int clen, const char *pre)
24 {
25         char *ret = ret; /* for compiler */
26         char *dst = NULL;
27
28         /* We make two passes over the cstr string. First, we compute
29          * how long the resulting string would be. Then we allocate a
30          * new buffer of the required length, and fill it in with the
31          * expanded content. The advantage of this approach is not
32          * having to deal with requiring callers to supply their own
33          * buffer, then having to check if it's sufficiently large, etc.
34          */
35         while (1) {
36                 /* note: "return NULL" below are leak-safe since
37                  * dst isn't yet allocated */
38                 const uint8_t *c;
39                 unsigned crtpos, retpos, depth, len;
40
41                 crtpos = retpos = depth = len = 0;
42                 while (crtpos < clen) {
43                         c = cstr + crtpos;
44
45                         if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
46                                 /* pointer */
47                                 if (crtpos + 2 > clen) /* no offset to jump to? abort */
48                                         return NULL;
49                                 if (retpos == 0) /* toplevel? save return spot */
50                                         retpos = crtpos + 2;
51                                 depth++;
52                                 crtpos = ((c[0] & 0x3f) << 8) | c[1]; /* jump */
53                         } else if (*c) {
54                                 /* label */
55                                 if (crtpos + *c + 1 > clen) /* label too long? abort */
56                                         return NULL;
57                                 if (dst)
58                                         memcpy(dst + len, c + 1, *c);
59                                 len += *c + 1;
60                                 crtpos += *c + 1;
61                                 if (dst)
62                                         dst[len - 1] = '.';
63                         } else {
64                                 /* NUL: end of current domain name */
65                                 if (retpos == 0) {
66                                         /* toplevel? keep going */
67                                         crtpos++;
68                                 } else {
69                                         /* return to toplevel saved spot */
70                                         crtpos = retpos;
71                                         retpos = depth = 0;
72                                 }
73                                 if (dst)
74                                         dst[len - 1] = ' ';
75                         }
76
77                         if (depth > NS_MAXDNSRCH /* too many jumps? abort, it's a loop */
78                          || len > NS_MAXDNAME * NS_MAXDNSRCH /* result too long? abort */
79                         ) {
80                                 return NULL;
81                         }
82                 }
83
84                 if (!len) /* expanded string has 0 length? abort */
85                         return NULL;
86
87                 if (!dst) { /* first pass? */
88                         /* allocate dst buffer and copy pre */
89                         unsigned plen = strlen(pre);
90                         ret = dst = xmalloc(plen + len);
91                         memcpy(dst, pre, plen);
92                         dst += plen;
93                 } else {
94                         dst[len - 1] = '\0';
95                         break;
96                 }
97         }
98
99         return ret;
100 }
101
102 /* Convert a domain name (src) from human-readable "foo.blah.com" format into
103  * RFC1035 encoding "\003foo\004blah\003com\000". Return allocated string, or
104  * NULL if an error occurs.
105  */
106 static uint8_t *convert_dname(const char *src)
107 {
108         uint8_t c, *res, *lenptr, *dst;
109         int len;
110
111         res = xmalloc(strlen(src) + 2);
112         dst = lenptr = res;
113         dst++;
114
115         for (;;) {
116                 c = (uint8_t)*src++;
117                 if (c == '.' || c == '\0') {  /* end of label */
118                         len = dst - lenptr - 1;
119                         /* label too long, too short, or two '.'s in a row? abort */
120                         if (len > NS_MAXLABEL || len == 0 || (c == '.' && *src == '.')) {
121                                 free(res);
122                                 return NULL;
123                         }
124                         *lenptr = len;
125                         if (c == '\0' || *src == '\0')  /* "" or ".": end of src */
126                                 break;
127                         lenptr = dst++;
128                         continue;
129                 }
130                 if (c >= 'A' && c <= 'Z')  /* uppercase? convert to lower */
131                         c += ('a' - 'A');
132                 *dst++ = c;
133         }
134
135         if (dst - res >= NS_MAXCDNAME) {  /* dname too long? abort */
136                 free(res);
137                 return NULL;
138         }
139
140         *dst = 0;
141         return res;
142 }
143
144 /* returns the offset within cstr at which dname can be found, or -1
145  */
146 static int find_offset(const uint8_t *cstr, int clen, const uint8_t *dname)
147 {
148         const uint8_t *c, *d;
149         int off;
150
151         /* find all labels in cstr */
152         off = 0;
153         while (off < clen) {
154                 c = cstr + off;
155
156                 if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) {  /* pointer, skip */
157                         off += 2;
158                         continue;
159                 }
160                 if (*c) {  /* label, try matching dname */
161                         d = dname;
162                         while (1) {
163                                 unsigned len1 = *c + 1;
164                                 if (memcmp(c, d, len1) != 0)
165                                         break;
166                                 if (len1 == 1)  /* at terminating NUL - match, return offset */
167                                         return off;
168                                 d += len1;
169                                 c += len1;
170                                 if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS)  /* pointer, jump */
171                                         c = cstr + (((c[0] & 0x3f) << 8) | c[1]);
172                         }
173                         off += cstr[off] + 1;
174                         continue;
175                 }
176                 /* NUL, skip */
177                 off++;
178         }
179
180         return -1;
181 }
182
183 /* computes string to be appended to cstr so that src would be added to
184  * the compression (best case, it's a 2-byte pointer to some offset within
185  * cstr; worst case, it's all of src, converted to <4>host<3>com<0> format).
186  * The computed string is returned directly; its length is returned via retlen;
187  * NULL and 0, respectively, are returned if an error occurs.
188  */
189 uint8_t* FAST_FUNC dname_enc(const uint8_t *cstr, int clen, const char *src, int *retlen)
190 {
191         uint8_t *d, *dname;
192         int off;
193
194         dname = convert_dname(src);
195         if (dname == NULL) {
196                 *retlen = 0;
197                 return NULL;
198         }
199
200         d = dname;
201         while (*d) {
202                 if (cstr) {
203                         off = find_offset(cstr, clen, d);
204                         if (off >= 0) { /* found a match, add pointer and return */
205                                 *d++ = NS_CMPRSFLGS | (off >> 8);
206                                 *d = off;
207                                 break;
208                         }
209                 }
210                 d += *d + 1;
211         }
212
213         *retlen = d - dname + 1;
214         return dname;
215 }