other-licenses/android/ns_parse.c

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /*	$NetBSD: ns_parse.c,v 1.2 2004/05/20 20:35:05 christos Exp $	*/
     3 /*
     4  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
     5  * Copyright (c) 1996,1999 by Internet Software Consortium.
     6  *
     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.
    10  *
    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.
    18  */
    20 /*
    21  * This version of this file is derived from Android 2.3 "Gingerbread",
    22  * which contains uncredited changes by Android/Google developers.  It has
    23  * been modified in 2011 for use in the Android build of Mozilla Firefox by
    24  * Mozilla contributors (including Michael Edwards <m.k.edwards@gmail.com>,
    25  * and Steve Workman <sjhworkman@gmail.com>).
    26  * These changes are offered under the same license as the original NetBSD
    27  * file, whose copyright and license are unchanged above.
    28  */
    30 #define ANDROID_CHANGES 1
    31 #define MOZILLA_NECKO_EXCLUDE_CODE 1
    33 #include <sys/cdefs.h>
    34 #ifndef lint
    35 #ifdef notdef
    36 static const char rcsid[] = "Id: ns_parse.c,v 1.3.2.1.4.1 2004/03/09 08:33:44 marka Exp";
    37 #else
    38 __RCSID("$NetBSD: ns_parse.c,v 1.2 2004/05/20 20:35:05 christos Exp $");
    39 #endif
    40 #endif
    42 /* Import. */
    44 #include <sys/types.h>
    46 #include <netinet/in.h>
    47 #include "arpa_nameser.h"
    49 #include <errno.h>
    50 #ifdef ANDROID_CHANGES
    51 #include "resolv_private.h"
    52 #else
    53 #include <resolv.h>
    54 #endif
    55 #include <string.h>
    57 /* Forward. */
    59 static void	setsection(ns_msg *msg, ns_sect sect);
    61 /* Macros. */
    63 #define RETERR(err) do { errno = (err); return (-1); } while (/*NOTREACHED*//*CONSTCOND*/0)
    65 /* Public. */
    67 /* These need to be in the same order as the nres.h:ns_flag enum. */
    68 const struct _ns_flagdata _ns_flagdata[16] = {
    69 	{ 0x8000, 15 },		/* qr. */
    70 	{ 0x7800, 11 },		/* opcode. */
    71 	{ 0x0400, 10 },		/* aa. */
    72 	{ 0x0200, 9 },		/* tc. */
    73 	{ 0x0100, 8 },		/* rd. */
    74 	{ 0x0080, 7 },		/* ra. */
    75 	{ 0x0040, 6 },		/* z. */
    76 	{ 0x0020, 5 },		/* ad. */
    77 	{ 0x0010, 4 },		/* cd. */
    78 	{ 0x000f, 0 },		/* rcode. */
    79 	{ 0x0000, 0 },		/* expansion (1/6). */
    80 	{ 0x0000, 0 },		/* expansion (2/6). */
    81 	{ 0x0000, 0 },		/* expansion (3/6). */
    82 	{ 0x0000, 0 },		/* expansion (4/6). */
    83 	{ 0x0000, 0 },		/* expansion (5/6). */
    84 	{ 0x0000, 0 },		/* expansion (6/6). */
    85 };
    87 int ns_msg_getflag(ns_msg handle, int flag) {
    88 	return((u_int32_t)((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift);
    89 }
    91 int
    92 ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
    93 	const u_char *optr = ptr;
    95 	for (; count > 0; count--) {
    96 		int b, rdlength;
    98 		b = dn_skipname(ptr, eom);
    99 		if (b < 0)
   100 			RETERR(EMSGSIZE);
   101 		ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
   102 		if (section != ns_s_qd) {
   103 			if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
   104 				RETERR(EMSGSIZE);
   105 			ptr += NS_INT32SZ/*TTL*/;
   106 			NS_GET16(rdlength, ptr);
   107 			ptr += rdlength/*RData*/;
   108 		}
   109 	}
   110 	if (ptr > eom)
   111 		RETERR(EMSGSIZE);
   112 	return (ptr - optr);
   113 }
   115 int
   116 ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
   117 	const u_char *eom = msg + msglen;
   118 	int i;
   120 	memset(handle, 0x5e, sizeof *handle);
   121 	handle->_msg = msg;
   122 	handle->_eom = eom;
   123 	if (msg + NS_INT16SZ > eom)
   124 		RETERR(EMSGSIZE);
   125 	NS_GET16(handle->_id, msg);
   126 	if (msg + NS_INT16SZ > eom)
   127 		RETERR(EMSGSIZE);
   128 	NS_GET16(handle->_flags, msg);
   129 	for (i = 0; i < ns_s_max; i++) {
   130 		if (msg + NS_INT16SZ > eom)
   131 			RETERR(EMSGSIZE);
   132 		NS_GET16(handle->_counts[i], msg);
   133 	}
   134 	for (i = 0; i < ns_s_max; i++)
   135 		if (handle->_counts[i] == 0)
   136 			handle->_sections[i] = NULL;
   137 		else {
   138 			int b = ns_skiprr(msg, eom, (ns_sect)i,
   139 					  handle->_counts[i]);
   141 			if (b < 0)
   142 				return (-1);
   143 			handle->_sections[i] = msg;
   144 			msg += b;
   145 		}
   146 	if (msg != eom)
   147 		RETERR(EMSGSIZE);
   148 	setsection(handle, ns_s_max);
   149 	return (0);
   150 }
   152 int
   153 ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
   154 	int b;
   156 	/* Make section right. */
   157 	if ((unsigned)section >= (unsigned)ns_s_max)
   158 		RETERR(ENODEV);
   159 	if (section != handle->_sect)
   160 		setsection(handle, section);
   162 	/* Make rrnum right. */
   163 	if (rrnum == -1)
   164 		rrnum = handle->_rrnum;
   165 	if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
   166 		RETERR(ENODEV);
   167 	if (rrnum < handle->_rrnum)
   168 		setsection(handle, section);
   169 	if (rrnum > handle->_rrnum) {
   170 		b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
   171 			      rrnum - handle->_rrnum);
   173 		if (b < 0)
   174 			return (-1);
   175 		handle->_msg_ptr += b;
   176 		handle->_rrnum = rrnum;
   177 	}
   179 	/* Do the parse. */
   180 	b = dn_expand(handle->_msg, handle->_eom,
   181 		      handle->_msg_ptr, rr->name, NS_MAXDNAME);
   182 	if (b < 0)
   183 		return (-1);
   184 	handle->_msg_ptr += b;
   185 	if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
   186 		RETERR(EMSGSIZE);
   187 	NS_GET16(rr->type, handle->_msg_ptr);
   188 	NS_GET16(rr->rr_class, handle->_msg_ptr);
   189 	if (section == ns_s_qd) {
   190 		rr->ttl = 0;
   191 		rr->rdlength = 0;
   192 		rr->rdata = NULL;
   193 	} else {
   194 		if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
   195 			RETERR(EMSGSIZE);
   196 		NS_GET32(rr->ttl, handle->_msg_ptr);
   197 		NS_GET16(rr->rdlength, handle->_msg_ptr);
   198 		if (handle->_msg_ptr + rr->rdlength > handle->_eom)
   199 			RETERR(EMSGSIZE);
   200 		rr->rdata = handle->_msg_ptr;
   201 		handle->_msg_ptr += rr->rdlength;
   202 	}
   203 	if (++handle->_rrnum > handle->_counts[(int)section])
   204 		setsection(handle, (ns_sect)((int)section + 1));
   206 	/* All done. */
   207 	return (0);
   208 }
   210 /* Private. */
   212 static void
   213 setsection(ns_msg *msg, ns_sect sect) {
   214 	msg->_sect = sect;
   215 	if (sect == ns_s_max) {
   216 		msg->_rrnum = -1;
   217 		msg->_msg_ptr = NULL;
   218 	} else {
   219 		msg->_rrnum = 0;
   220 		msg->_msg_ptr = msg->_sections[(int)sect];
   221 	}
   222 }

mercurial