vfs: check userland buffers before reading them.
[haiku.git] / src / kits / network / netresolv / nameser / ns_parse.c
bloba0527a363d02c4ddde0d35b90a06519b7f7335b1
1 /* $NetBSD: ns_parse.c,v 1.9 2012/03/13 21:13:39 christos Exp $ */
3 /*
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/cdefs.h>
21 #ifndef lint
22 #ifdef notdef
23 static const char rcsid[] = "Id: ns_parse.c,v 1.10 2009/01/23 19:59:16 each Exp";
24 #else
25 //__RCSID("$NetBSD: ns_parse.c,v 1.9 2012/03/13 21:13:39 christos Exp $");
26 #endif
27 #endif
29 /* Import. */
31 #include "port_before.h"
33 #include <sys/types.h>
35 #include <netinet/in.h>
36 #include <arpa/nameser.h>
38 #include <assert.h>
39 #include <errno.h>
40 #include <resolv.h>
41 #include <string.h>
43 #include "port_after.h"
45 /* Forward. */
47 static void setsection(ns_msg *msg, ns_sect sect);
49 /* Macros. */
51 #if !defined(SOLARIS2) || defined(__COVERITY__)
52 #define RETERR(err) do { errno = (err); return (-1); } while (/*NOTREACHED*//*CONSTCOND*/0)
53 #else
54 #define RETERR(err) \
55 do { errno = (err); if (errno == errno) return (-1); } while (0)
56 #endif
58 #define PARSE_FMT_PRESO 0 /* Parse using presentation-format names */
59 #define PARSE_FMT_WIRE 1 /* Parse using network-format names */
61 /* Public. */
63 /* These need to be in the same order as the nres.h:ns_flag enum. */
64 struct _ns_flagdata _ns_flagdata[16] = {
65 { 0x8000, 15 }, /*%< qr. */
66 { 0x7800, 11 }, /*%< opcode. */
67 { 0x0400, 10 }, /*%< aa. */
68 { 0x0200, 9 }, /*%< tc. */
69 { 0x0100, 8 }, /*%< rd. */
70 { 0x0080, 7 }, /*%< ra. */
71 { 0x0040, 6 }, /*%< z. */
72 { 0x0020, 5 }, /*%< ad. */
73 { 0x0010, 4 }, /*%< cd. */
74 { 0x000f, 0 }, /*%< rcode. */
75 { 0x0000, 0 }, /*%< expansion (1/6). */
76 { 0x0000, 0 }, /*%< expansion (2/6). */
77 { 0x0000, 0 }, /*%< expansion (3/6). */
78 { 0x0000, 0 }, /*%< expansion (4/6). */
79 { 0x0000, 0 }, /*%< expansion (5/6). */
80 { 0x0000, 0 }, /*%< expansion (6/6). */
83 int ns_msg_getflag(ns_msg handle, int flag) {
84 return((u_int32_t)((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift);
87 int
88 ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
89 const u_char *optr = ptr;
91 for (; count > 0; count--) {
92 int b, rdlength;
94 b = dn_skipname(ptr, eom);
95 if (b < 0)
96 RETERR(EMSGSIZE);
97 ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
98 if (section != ns_s_qd) {
99 if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
100 RETERR(EMSGSIZE);
101 ptr += NS_INT32SZ/*TTL*/;
102 NS_GET16(rdlength, ptr);
103 ptr += rdlength/*RData*/;
106 if (ptr > eom)
107 RETERR(EMSGSIZE);
108 assert(INT_MIN <= (ptr - optr) && (ptr - optr) <= INT_MAX);
109 return (int)(ptr - optr);
113 ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
114 const u_char *eom = msg + msglen;
115 int i;
117 handle->_msg = msg;
118 handle->_eom = eom;
119 if (msg + NS_INT16SZ > eom)
120 RETERR(EMSGSIZE);
121 NS_GET16(handle->_id, msg);
122 if (msg + NS_INT16SZ > eom)
123 RETERR(EMSGSIZE);
124 NS_GET16(handle->_flags, msg);
125 for (i = 0; i < ns_s_max; i++) {
126 if (msg + NS_INT16SZ > eom)
127 RETERR(EMSGSIZE);
128 NS_GET16(handle->_counts[i], msg);
130 for (i = 0; i < ns_s_max; i++)
131 if (handle->_counts[i] == 0)
132 handle->_sections[i] = NULL;
133 else {
134 int b = ns_skiprr(msg, eom, (ns_sect)i,
135 handle->_counts[i]);
137 if (b < 0)
138 return (-1);
139 handle->_sections[i] = msg;
140 msg += b;
142 if (msg != eom)
143 RETERR(EMSGSIZE);
144 setsection(handle, ns_s_max);
145 return (0);
149 ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
150 int b;
151 int tmp;
153 /* Make section right. */
154 tmp = section;
155 if (tmp < 0 || section >= ns_s_max)
156 RETERR(ENODEV);
157 if (section != handle->_sect)
158 setsection(handle, section);
160 /* Make rrnum right. */
161 if (rrnum == -1)
162 rrnum = handle->_rrnum;
163 if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
164 RETERR(ENODEV);
165 if (rrnum < handle->_rrnum)
166 setsection(handle, section);
167 if (rrnum > handle->_rrnum) {
168 b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
169 rrnum - handle->_rrnum);
171 if (b < 0)
172 return (-1);
173 handle->_msg_ptr += b;
174 handle->_rrnum = rrnum;
177 /* Do the parse. */
178 b = dn_expand(handle->_msg, handle->_eom,
179 handle->_msg_ptr, rr->name, NS_MAXDNAME);
180 if (b < 0)
181 return (-1);
182 handle->_msg_ptr += b;
183 if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
184 RETERR(EMSGSIZE);
185 NS_GET16(rr->type, handle->_msg_ptr);
186 NS_GET16(rr->rr_class, handle->_msg_ptr);
187 if (section == ns_s_qd) {
188 rr->ttl = 0;
189 rr->rdlength = 0;
190 rr->rdata = NULL;
191 } else {
192 if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
193 RETERR(EMSGSIZE);
194 NS_GET32(rr->ttl, handle->_msg_ptr);
195 NS_GET16(rr->rdlength, handle->_msg_ptr);
196 if (handle->_msg_ptr + rr->rdlength > handle->_eom)
197 RETERR(EMSGSIZE);
198 rr->rdata = handle->_msg_ptr;
199 handle->_msg_ptr += rr->rdlength;
201 if (++handle->_rrnum > handle->_counts[(int)section])
202 setsection(handle, (ns_sect)((int)section + 1));
204 /* All done. */
205 return (0);
209 * This is identical to the above but uses network-format (uncompressed) names.
212 ns_parserr2(ns_msg *handle, ns_sect section, int rrnum, ns_rr2 *rr) {
213 int b;
214 int tmp;
216 /* Make section right. */
217 tmp = section;
218 if (tmp < 0 || section >= ns_s_max)
219 RETERR(ENODEV);
220 if (section != handle->_sect)
221 setsection(handle, section);
223 /* Make rrnum right. */
224 if (rrnum == -1)
225 rrnum = handle->_rrnum;
226 if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
227 RETERR(ENODEV);
228 if (rrnum < handle->_rrnum)
229 setsection(handle, section);
230 if (rrnum > handle->_rrnum) {
231 b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
232 rrnum - handle->_rrnum);
234 if (b < 0)
235 return (-1);
236 handle->_msg_ptr += b;
237 handle->_rrnum = rrnum;
240 /* Do the parse. */
241 b = ns_name_unpack2(handle->_msg, handle->_eom, handle->_msg_ptr,
242 rr->nname, NS_MAXNNAME, &rr->nnamel);
243 if (b < 0)
244 return (-1);
245 handle->_msg_ptr += b;
246 if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
247 RETERR(EMSGSIZE);
248 NS_GET16(rr->type, handle->_msg_ptr);
249 NS_GET16(rr->rr_class, handle->_msg_ptr);
250 if (section == ns_s_qd) {
251 rr->ttl = 0;
252 rr->rdlength = 0;
253 rr->rdata = NULL;
254 } else {
255 if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
256 RETERR(EMSGSIZE);
257 NS_GET32(rr->ttl, handle->_msg_ptr);
258 NS_GET16(rr->rdlength, handle->_msg_ptr);
259 if (handle->_msg_ptr + rr->rdlength > handle->_eom)
260 RETERR(EMSGSIZE);
261 rr->rdata = handle->_msg_ptr;
262 handle->_msg_ptr += rr->rdlength;
264 if (++handle->_rrnum > handle->_counts[(int)section])
265 setsection(handle, (ns_sect)((int)section + 1));
267 /* All done. */
268 return (0);
271 /* Private. */
273 static void
274 setsection(ns_msg *msg, ns_sect sect) {
275 msg->_sect = sect;
276 if (sect == ns_s_max) {
277 msg->_rrnum = -1;
278 msg->_msg_ptr = NULL;
279 } else {
280 msg->_rrnum = 0;
281 msg->_msg_ptr = msg->_sections[(int)sect];
285 /*! \file */