fix potential read past end of buffer in getnameinfo host name lookup
[musl] / src / network / getnameinfo.c
1 #include <netdb.h>
2 #include <limits.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8 #include <arpa/inet.h>
9 #include <net/if.h>
10 #include <ctype.h>
11 #include <resolv.h>
12 #include "lookup.h"
13 #include "stdio_impl.h"
14
15 #define PTR_MAX (64 + sizeof ".in-addr.arpa")
16 #define RR_PTR 12
17
18 static char *itoa(char *p, unsigned x) {
19         p += 3*sizeof(int);
20         *--p = 0;
21         do {
22                 *--p = '0' + x % 10;
23                 x /= 10;
24         } while (x);
25         return p;
26 }
27
28 static void mkptr4(char *s, const unsigned char *ip)
29 {
30         sprintf(s, "%d.%d.%d.%d.in-addr.arpa",
31                 ip[3], ip[2], ip[1], ip[0]);
32 }
33
34 static void mkptr6(char *s, const unsigned char *ip)
35 {
36         static const char xdigits[] = "0123456789abcdef";
37         int i;
38         for (i=15; i>=0; i--) {
39                 *s++ = xdigits[ip[i]&15]; *s++ = '.';
40                 *s++ = xdigits[ip[i]>>4]; *s++ = '.';
41         }
42         strcpy(s, "ip6.arpa");
43 }
44
45 static void reverse_hosts(char *buf, const unsigned char *a, unsigned scopeid, int family)
46 {
47         char line[512], *p, *z;
48         unsigned char _buf[1032], atmp[16];
49         struct address iplit;
50         FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
51         if (!f) return;
52         if (family == AF_INET) {
53                 memcpy(atmp+12, a, 4);
54                 memcpy(atmp, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
55                 a = atmp;
56         }
57         while (fgets(line, sizeof line, f)) {
58                 if ((p=strchr(line, '#'))) *p++='\n', *p=0;
59
60                 for (p=line; *p && !isspace(*p); p++);
61                 if (!*p) continue;
62                 *p++ = 0;
63                 if (__lookup_ipliteral(&iplit, line, AF_UNSPEC)<=0)
64                         continue;
65
66                 if (iplit.family == AF_INET) {
67                         memcpy(iplit.addr+12, iplit.addr, 4);
68                         memcpy(iplit.addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
69                         iplit.scopeid = 0;
70                 }
71
72                 if (memcmp(a, iplit.addr, 16) || iplit.scopeid != scopeid)
73                         continue;
74                         
75                 for (; *p && isspace(*p); p++);
76                 for (z=p; *z && !isspace(*z); z++);
77                 *z = 0;
78                 if (z-p < 256) {
79                         memcpy(buf, p, z-p+1);
80                         break;
81                 }
82         }
83         __fclose_ca(f);
84 }
85
86 static void reverse_services(char *buf, int port, int dgram)
87 {
88         unsigned long svport;
89         char line[128], *p, *z;
90         unsigned char _buf[1032];
91         FILE _f, *f = __fopen_rb_ca("/etc/services", &_f, _buf, sizeof _buf);
92         if (!f) return;
93         while (fgets(line, sizeof line, f)) {
94                 if ((p=strchr(line, '#'))) *p++='\n', *p=0;
95
96                 for (p=line; *p && !isspace(*p); p++);
97                 if (!*p) continue;
98                 *p++ = 0;
99                 svport = strtoul(p, &z, 10);
100
101                 if (svport != port || z==p) continue;
102                 if (dgram && strncmp(z, "/udp", 4)) continue;
103                 if (!dgram && strncmp(z, "/tcp", 4)) continue;
104                 if (p-line > 32) continue;
105
106                 memcpy(buf, line, p-line);
107                 break;
108         }
109         __fclose_ca(f);
110 }
111
112 static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet, int plen)
113 {
114         if (rr != RR_PTR) return 0;
115         if (__dn_expand(packet, (const unsigned char *)packet + plen,
116             data, c, 256) <= 0)
117                 *(char *)c = 0;
118         return 0;
119         
120 }
121
122 int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl,
123         char *restrict node, socklen_t nodelen,
124         char *restrict serv, socklen_t servlen,
125         int flags)
126 {
127         char ptr[PTR_MAX];
128         char buf[256], num[3*sizeof(int)+1];
129         int af = sa->sa_family;
130         unsigned char *a;
131         unsigned scopeid;
132
133         switch (af) {
134         case AF_INET:
135                 a = (void *)&((struct sockaddr_in *)sa)->sin_addr;
136                 if (sl < sizeof(struct sockaddr_in)) return EAI_FAMILY;
137                 mkptr4(ptr, a);
138                 scopeid = 0;
139                 break;
140         case AF_INET6:
141                 a = (void *)&((struct sockaddr_in6 *)sa)->sin6_addr;
142                 if (sl < sizeof(struct sockaddr_in6)) return EAI_FAMILY;
143                 if (memcmp(a, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12))
144                         mkptr6(ptr, a);
145                 else
146                         mkptr4(ptr, a+12);
147                 scopeid = ((struct sockaddr_in6 *)sa)->sin6_scope_id;
148                 break;
149         default:
150                 return EAI_FAMILY;
151         }
152
153         if (node && nodelen) {
154                 buf[0] = 0;
155                 if (!(flags & NI_NUMERICHOST)) {
156                         reverse_hosts(buf, a, scopeid, af);
157                 }
158                 if (!*buf && !(flags & NI_NUMERICHOST)) {
159                         unsigned char query[18+PTR_MAX], reply[512];
160                         int qlen = __res_mkquery(0, ptr, 1, RR_PTR,
161                                 0, 0, 0, query, sizeof query);
162                         query[3] = 0; /* don't need AD flag */
163                         int rlen = __res_send(query, qlen, reply, sizeof reply);
164                         buf[0] = 0;
165                         if (rlen > 0)
166                                 __dns_parse(reply, rlen, dns_parse_callback, buf);
167                 }
168                 if (!*buf) {
169                         if (flags & NI_NAMEREQD) return EAI_NONAME;
170                         inet_ntop(af, a, buf, sizeof buf);
171                         if (scopeid) {
172                                 char *p = 0, tmp[IF_NAMESIZE+1];
173                                 if (!(flags & NI_NUMERICSCOPE) &&
174                                     (IN6_IS_ADDR_LINKLOCAL(a) ||
175                                      IN6_IS_ADDR_MC_LINKLOCAL(a)))
176                                         p = if_indextoname(scopeid, tmp+1);
177                                 if (!p)
178                                         p = itoa(num, scopeid);
179                                 *--p = '%';
180                                 strcat(buf, p);
181                         }
182                 }
183                 if (strlen(buf) >= nodelen) return EAI_OVERFLOW;
184                 strcpy(node, buf);
185         }
186
187         if (serv && servlen) {
188                 char *p = buf;
189                 int port = ntohs(((struct sockaddr_in *)sa)->sin_port);
190                 buf[0] = 0;
191                 if (!(flags & NI_NUMERICSERV))
192                         reverse_services(buf, port, flags & NI_DGRAM);
193                 if (!*p)
194                         p = itoa(num, port);
195                 if (strlen(p) >= servlen)
196                         return EAI_OVERFLOW;
197                 strcpy(serv, p);
198         }
199
200         return 0;
201 }