Blame SOURCES/unbound-1.7.3-security-hardening.patch

fdd92c
diff --git a/config.h.in b/config.h.in
fdd92c
index 04356f3..3b06bfa 100644
fdd92c
--- a/config.h.in
fdd92c
+++ b/config.h.in
fdd92c
@@ -666,6 +666,9 @@
fdd92c
 /* Shared data */
fdd92c
 #undef SHARE_DIR
fdd92c
 
fdd92c
+/* The size of `size_t', as computed by sizeof. */
fdd92c
+#undef SIZEOF_SIZE_T
fdd92c
+
fdd92c
 /* The size of `time_t', as computed by sizeof. */
fdd92c
 #undef SIZEOF_TIME_T
fdd92c
 
fdd92c
diff --git a/configure.ac b/configure.ac
fdd92c
index c5e0c7b..1bff4ed 100644
fdd92c
--- a/configure.ac
fdd92c
+++ b/configure.ac
fdd92c
@@ -371,6 +371,7 @@ AC_INCLUDES_DEFAULT
fdd92c
 # endif
fdd92c
 #endif
fdd92c
 ])
fdd92c
+AC_CHECK_SIZEOF(size_t)
fdd92c
 
fdd92c
 # add option to disable the evil rpath
fdd92c
 ACX_ARG_RPATH
fdd92c
diff --git a/contrib/create_unbound_ad_servers.sh b/contrib/create_unbound_ad_servers.sh
fdd92c
index d31f078..49fdbff 100644
fdd92c
--- a/contrib/create_unbound_ad_servers.sh
fdd92c
+++ b/contrib/create_unbound_ad_servers.sh
fdd92c
@@ -9,12 +9,13 @@
fdd92c
 # Variables
fdd92c
 dst_dir="/etc/opt/csw/unbound"
fdd92c
 work_dir="/tmp"
fdd92c
-list_addr="http://pgl.yoyo.org/adservers/serverlist.php?hostformat=nohtml&showintro=1&startdate%5Bday%5D=&startdate%5Bmonth%5D=&startdate%5Byear%5D="
fdd92c
+list_addr="https://pgl.yoyo.org/adservers/serverlist.php?hostformat=nohtml&showintro=1&startdate%5Bday%5D=&startdate%5Bmonth%5D=&startdate%5Byear%5D="
fdd92c
 
fdd92c
 # OS commands
fdd92c
 CAT=`which cat`
fdd92c
 ECHO=`which echo`
fdd92c
 WGET=`which wget`
fdd92c
+TR=`which tr`
fdd92c
 
fdd92c
 # Check Wget installed
fdd92c
 if [ ! -f $WGET ]; then
fdd92c
@@ -22,8 +23,10 @@ if [ ! -f $WGET ]; then
fdd92c
  exit 1
fdd92c
 fi
fdd92c
 
fdd92c
+# remove special characters with tr to protect unbound.conf
fdd92c
 $WGET -O $work_dir/yoyo_ad_servers "$list_addr" && \
fdd92c
 $CAT $work_dir/yoyo_ad_servers | \
fdd92c
+$TR -d '";$\\' | \
fdd92c
 while read line ; \
fdd92c
  do \
fdd92c
    $ECHO "local-zone: \"$line\" redirect" ;\
fdd92c
@@ -36,4 +39,4 @@ echo "Done."
fdd92c
 #  the unbound_ad_servers file:
fdd92c
 #
fdd92c
 #   include: $dst_dir/unbound_ad_servers
fdd92c
-#
fdd92c
\ No newline at end of file
fdd92c
+#
fdd92c
diff --git a/daemon/daemon.c b/daemon/daemon.c
fdd92c
index 6820e11..1b4f329 100644
fdd92c
--- a/daemon/daemon.c
fdd92c
+++ b/daemon/daemon.c
fdd92c
@@ -426,9 +426,7 @@ daemon_create_workers(struct daemon* daemon)
fdd92c
 	int* shufport;
fdd92c
 	log_assert(daemon && daemon->cfg);
fdd92c
 	if(!daemon->rand) {
fdd92c
-		unsigned int seed = (unsigned int)time(NULL) ^ 
fdd92c
-			(unsigned int)getpid() ^ 0x438;
fdd92c
-		daemon->rand = ub_initstate(seed, NULL);
fdd92c
+		daemon->rand = ub_initstate(NULL);
fdd92c
 		if(!daemon->rand)
fdd92c
 			fatal_exit("could not init random generator");
fdd92c
 		hash_set_raninit((uint32_t)ub_random(daemon->rand));
fdd92c
diff --git a/daemon/worker.c b/daemon/worker.c
fdd92c
index 3acecc1..8354010 100644
fdd92c
--- a/daemon/worker.c
fdd92c
+++ b/daemon/worker.c
fdd92c
@@ -1629,18 +1629,14 @@ worker_create(struct daemon* daemon, int id, int* ports, int n)
fdd92c
 		return NULL;
fdd92c
 	}
fdd92c
 	/* create random state here to avoid locking trouble in RAND_bytes */
fdd92c
-	seed = (unsigned int)time(NULL) ^ (unsigned int)getpid() ^
fdd92c
-		(((unsigned int)worker->thread_num)<<17);
fdd92c
-		/* shift thread_num so it does not match out pid bits */
fdd92c
-	if(!(worker->rndstate = ub_initstate(seed, daemon->rand))) {
fdd92c
-		seed = 0;
fdd92c
+	if(!(worker->rndstate = ub_initstate(daemon->rand))) {
fdd92c
 		log_err("could not init random numbers.");
fdd92c
 		tube_delete(worker->cmd);
fdd92c
 		free(worker->ports);
fdd92c
 		free(worker);
fdd92c
 		return NULL;
fdd92c
 	}
fdd92c
-	seed = 0;
fdd92c
+	explicit_bzero(&seed, sizeof(seed));
fdd92c
 #ifdef USE_DNSTAP
fdd92c
 	if(daemon->cfg->dnstap) {
fdd92c
 		log_assert(daemon->dtenv != NULL);
fdd92c
diff --git a/dns64/dns64.c b/dns64/dns64.c
fdd92c
index 7889d72..300202c 100644
fdd92c
--- a/dns64/dns64.c
fdd92c
+++ b/dns64/dns64.c
fdd92c
@@ -782,6 +782,16 @@ dns64_inform_super(struct module_qstate* qstate, int id,
fdd92c
 	 * Signal that the sub-query is finished, no matter whether we are
fdd92c
 	 * successful or not. This lets the state machine terminate.
fdd92c
 	 */
fdd92c
+	if(!super->minfo[id]) {
fdd92c
+		super->minfo[id] = (enum dns64_qstate *)regional_alloc(super->region,
fdd92c
+			sizeof(*(super->minfo[id])));
fdd92c
+		if(!super->minfo[id]) {
fdd92c
+			log_err("out of memory");
fdd92c
+			super->return_rcode = LDNS_RCODE_SERVFAIL;
fdd92c
+			super->return_msg = NULL;
fdd92c
+			return;
fdd92c
+		}
fdd92c
+	}
fdd92c
	super->minfo[id] = (void*)DNS64_SUBQUERY_FINISHED;
fdd92c
 
fdd92c
 	/* If there is no successful answer, we're done. */
fdd92c
diff --git a/dnscrypt/dnscrypt.c b/dnscrypt/dnscrypt.c
fdd92c
index 3545d3d..7dd2ce5 100644
fdd92c
--- a/dnscrypt/dnscrypt.c
fdd92c
+++ b/dnscrypt/dnscrypt.c
fdd92c
@@ -732,6 +732,11 @@ dnsc_load_local_data(struct dnsc_env* dnscenv, struct config_file *cfg)
fdd92c
             );
fdd92c
             continue;
fdd92c
         }
fdd92c
+        if((unsigned)strlen(dnscenv->provider_name) >= (unsigned)0xffff0000) {
fdd92c
+		    /* guard against integer overflow in rrlen calculation */
fdd92c
+		    verbose(VERB_OPS, "cert #%" PRIu32 " is too long", serial);
fdd92c
+		    continue;
fdd92c
+        }
fdd92c
         rrlen = strlen(dnscenv->provider_name) +
fdd92c
                          strlen(ttl_class_type) +
fdd92c
                          4 * sizeof(struct SignedCert) + // worst case scenario
fdd92c
diff --git a/doc/Changelog b/doc/Changelog
fdd92c
index bb74461..4cb080e 100644
fdd92c
--- a/doc/Changelog
fdd92c
+++ b/doc/Changelog
fdd92c
@@ -1,3 +1,55 @@
fdd92c
+3 December 2019: Wouter
fdd92c
+	- Fix Assert Causing DoS in synth_cname(),
fdd92c
+	  reported by X41 D-Sec.
fdd92c
+	- Fix Assert Causing DoS in dname_pkt_copy(),
fdd92c
+	  reported by X41 D-Sec.
fdd92c
+	- Fix OOB Read in sldns_wire2str_dname_scan(),
fdd92c
+	  reported by X41 D-Sec.
fdd92c
+	- Fix Out of Bounds Write in sldns_str2wire_str_buf(),
fdd92c
+	  reported by X41 D-Sec.
fdd92c
+	- Fix Out of Bounds Write in sldns_b64_pton(),
fdd92c
+	  fixed by check in sldns_str2wire_int16_data_buf(),
fdd92c
+	  reported by X41 D-Sec.
fdd92c
+	- Fix Insufficient Handling of Compressed Names in dname_pkt_copy(),
fdd92c
+	  reported by X41 D-Sec.
fdd92c
+	- Fix Out of Bound Write Compressed Names in rdata_copy(),
fdd92c
+	  reported by X41 D-Sec.
fdd92c
+	- Fix Hang in sldns_wire2str_pkt_scan(),
fdd92c
+	  reported by X41 D-Sec.
fdd92c
+
fdd92c
+20 November 2019: Wouter
fdd92c
+	- Fix Out of Bounds Read in rrinternal_get_owner(),
fdd92c
+	  reported by X41 D-Sec.
fdd92c
+	- Fix Race Condition in autr_tp_create(),
fdd92c
+	  reported by X41 D-Sec.
fdd92c
+	- Fix Shared Memory World Writeable,
fdd92c
+	  reported by X41 D-Sec.
fdd92c
+	- Adjust unbound-control to make stats_shm a read only operation.
fdd92c
+	- Fix Weak Entropy Used For Nettle,
fdd92c
+	  reported by X41 D-Sec.
fdd92c
+	- Fix Randomness Error not Handled Properly,
fdd92c
+	  reported by X41 D-Sec.
fdd92c
+	- Fix Out-of-Bounds Read in dname_valid(),
fdd92c
+	  reported by X41 D-Sec.
fdd92c
+	- Fix Config Injection in create_unbound_ad_servers.sh,
fdd92c
+	  reported by X41 D-Sec.
fdd92c
+
fdd92c
+19 November 2019: Wouter
fdd92c
+	- Fix Integer Overflow in Regional Allocator,
fdd92c
+	  reported by X41 D-Sec.
fdd92c
+	- Fix Unchecked NULL Pointer in dns64_inform_super()
fdd92c
+	  and ipsecmod_new(), reported by X41 D-Sec.
fdd92c
+	- Fix Out-of-bounds Read in rr_comment_dnskey(),
fdd92c
+	  reported by X41 D-Sec.
fdd92c
+	- Fix Integer Overflows in Size Calculations,
fdd92c
+	  reported by X41 D-Sec.
fdd92c
+	- Fix Integer Overflow to Buffer Overflow in
fdd92c
+	  sldns_str2wire_dname_buf_origin(), reported by X41 D-Sec.
fdd92c
+	- Fix Out of Bounds Read in sldns_str2wire_dname(),
fdd92c
+	  reported by X41 D-Sec.
fdd92c
+	- Fix Out of Bounds Write in sldns_bget_token_par(),
fdd92c
+	  reported by X41 D-Sec.
fdd92c
+
fdd92c
 30 November 2018: Wouter
fdd92c
 	- log-tag-queryreply: yes in unbound.conf tags the log-queries and
fdd92c
 	  log-replies in the log file for easier log filter maintenance.
fdd92c
diff --git a/ipsecmod/ipsecmod.c b/ipsecmod/ipsecmod.c
fdd92c
index 3572f12..1422a62 100644
fdd92c
--- a/ipsecmod/ipsecmod.c
fdd92c
+++ b/ipsecmod/ipsecmod.c
fdd92c
@@ -103,11 +103,11 @@ ipsecmod_new(struct module_qstate* qstate, int id)
fdd92c
 {
fdd92c
 	struct ipsecmod_qstate* iq = (struct ipsecmod_qstate*)regional_alloc(
fdd92c
 		qstate->region, sizeof(struct ipsecmod_qstate));
fdd92c
-	memset(iq, 0, sizeof(*iq));
fdd92c
 	qstate->minfo[id] = iq;
fdd92c
 	if(!iq)
fdd92c
 		return 0;
fdd92c
 	/* Initialise it. */
fdd92c
+	memset(iq, 0, sizeof(*iq));
fdd92c
 	iq->enabled = qstate->env->cfg->ipsecmod_enabled;
fdd92c
 	iq->is_whitelisted = ipsecmod_domain_is_whitelisted(
fdd92c
 		(struct ipsecmod_env*)qstate->env->modinfo[id], qstate->qinfo.qname,
fdd92c
diff --git a/iterator/iter_scrub.c b/iterator/iter_scrub.c
fdd92c
index 8230d17..942c3d5 100644
fdd92c
--- a/iterator/iter_scrub.c
fdd92c
+++ b/iterator/iter_scrub.c
fdd92c
@@ -231,6 +231,10 @@ synth_cname(uint8_t* qname, size_t qnamelen, struct rrset_parse* dname_rrset,
fdd92c
 	size_t dtarglen;
fdd92c
 	if(!parse_get_cname_target(dname_rrset, &dtarg, &dtarglen, pkt))
fdd92c
 		return 0; 
fdd92c
+	if(qnamelen <= dname_rrset->dname_len)
fdd92c
+		return 0;
fdd92c
+	if(qnamelen == 0)
fdd92c
+		return 0;
fdd92c
 	log_assert(qnamelen > dname_rrset->dname_len);
fdd92c
 	/* DNAME from com. to net. with qname example.com. -> example.net. */
fdd92c
 	/* so: \3com\0 to \3net\0 and qname \7example\3com\0 */
fdd92c
diff --git a/libunbound/libunbound.c b/libunbound/libunbound.c
fdd92c
index 275e8d2..a8979c2 100644
fdd92c
--- a/libunbound/libunbound.c
fdd92c
+++ b/libunbound/libunbound.c
fdd92c
@@ -83,7 +83,6 @@
fdd92c
 static struct ub_ctx* ub_ctx_create_nopipe(void)
fdd92c
 {
fdd92c
 	struct ub_ctx* ctx;
fdd92c
-	unsigned int seed;
fdd92c
 #ifdef USE_WINSOCK
fdd92c
 	int r;
fdd92c
 	WSADATA wsa_data;
fdd92c
@@ -107,15 +106,12 @@ static struct ub_ctx* ub_ctx_create_nopipe(void)
fdd92c
 		return NULL;
fdd92c
 	}
fdd92c
 	alloc_init(&ctx->superalloc, NULL, 0);
fdd92c
-	seed = (unsigned int)time(NULL) ^ (unsigned int)getpid();
fdd92c
-	if(!(ctx->seed_rnd = ub_initstate(seed, NULL))) {
fdd92c
-		seed = 0;
fdd92c
+	if(!(ctx->seed_rnd = ub_initstate(NULL))) {
fdd92c
 		ub_randfree(ctx->seed_rnd);
fdd92c
 		free(ctx);
fdd92c
 		errno = ENOMEM;
fdd92c
 		return NULL;
fdd92c
 	}
fdd92c
-	seed = 0;
fdd92c
 	lock_basic_init(&ctx->qqpipe_lock);
fdd92c
 	lock_basic_init(&ctx->rrpipe_lock);
fdd92c
 	lock_basic_init(&ctx->cfglock);
fdd92c
diff --git a/libunbound/libworker.c b/libunbound/libworker.c
fdd92c
index 3dcaa78..07a08c6 100644
fdd92c
--- a/libunbound/libworker.c
fdd92c
+++ b/libunbound/libworker.c
fdd92c
@@ -122,7 +122,6 @@ libworker_delete_event(struct libworker* w)
fdd92c
 static struct libworker*
fdd92c
 libworker_setup(struct ub_ctx* ctx, int is_bg, struct ub_event_base* eb)
fdd92c
 {
fdd92c
-	unsigned int seed;
fdd92c
 	struct libworker* w = (struct libworker*)calloc(1, sizeof(*w));
fdd92c
 	struct config_file* cfg = ctx->env->cfg;
fdd92c
 	int* ports;
fdd92c
@@ -177,17 +176,13 @@ libworker_setup(struct ub_ctx* ctx, int is_bg, struct ub_event_base* eb)
fdd92c
 	}
fdd92c
 	w->env->worker = (struct worker*)w;
fdd92c
 	w->env->probe_timer = NULL;
fdd92c
-	seed = (unsigned int)time(NULL) ^ (unsigned int)getpid() ^
fdd92c
-		(((unsigned int)w->thread_num)<<17);
fdd92c
-	seed ^= (unsigned int)w->env->alloc->next_id;
fdd92c
 	if(!w->is_bg || w->is_bg_thread) {
fdd92c
 		lock_basic_lock(&ctx->cfglock);
fdd92c
 	}
fdd92c
-	if(!(w->env->rnd = ub_initstate(seed, ctx->seed_rnd))) {
fdd92c
+	if(!(w->env->rnd = ub_initstate(ctx->seed_rnd))) {
fdd92c
 		if(!w->is_bg || w->is_bg_thread) {
fdd92c
 			lock_basic_unlock(&ctx->cfglock);
fdd92c
 		}
fdd92c
-		seed = 0;
fdd92c
 		libworker_delete(w);
fdd92c
 		return NULL;
fdd92c
 	}
fdd92c
@@ -207,7 +202,6 @@ libworker_setup(struct ub_ctx* ctx, int is_bg, struct ub_event_base* eb)
fdd92c
 			hash_set_raninit((uint32_t)ub_random(w->env->rnd));
fdd92c
 		}
fdd92c
 	}
fdd92c
-	seed = 0;
fdd92c
 
fdd92c
 	if(eb)
fdd92c
 		w->base = comm_base_create_event(eb);
fdd92c
diff --git a/respip/respip.c b/respip/respip.c
fdd92c
index 2e9313f..7d2a588 100644
fdd92c
--- a/respip/respip.c
fdd92c
+++ b/respip/respip.c
fdd92c
@@ -475,10 +475,16 @@ copy_rrset(const struct ub_packed_rrset_key* key, struct regional* region)
fdd92c
 	if(!ck->rk.dname)
fdd92c
 		return NULL;
fdd92c
 
fdd92c
+	if((unsigned)data->count >= 0xffff00U)
fdd92c
+		return NULL; /* guard against integer overflow in dsize */
fdd92c
 	dsize = sizeof(struct packed_rrset_data) + data->count *
fdd92c
 		(sizeof(size_t)+sizeof(uint8_t*)+sizeof(time_t));
fdd92c
-	for(i=0; i<data->count; i++)
fdd92c
+	for(i=0; i<data->count; i++) {
fdd92c
+		if((unsigned)dsize >= 0x0fffffffU ||
fdd92c
+			(unsigned)data->rr_len[i] >= 0x0fffffffU)
fdd92c
+			return NULL; /* guard against integer overflow */
fdd92c
 		dsize += data->rr_len[i];
fdd92c
+	}
fdd92c
 	d = regional_alloc(region, dsize);
fdd92c
 	if(!d)
fdd92c
 		return NULL;
fdd92c
diff --git a/sldns/parse.c b/sldns/parse.c
fdd92c
index b62c405..b30264e 100644
fdd92c
--- a/sldns/parse.c
fdd92c
+++ b/sldns/parse.c
fdd92c
@@ -325,8 +325,14 @@ sldns_bget_token_par(sldns_buffer *b, char *token, const char *delim,
fdd92c
 		if (c == '\n' && p != 0) {
fdd92c
 			/* in parentheses */
fdd92c
 			/* do not write ' ' if we want to skip spaces */
fdd92c
-			if(!(skipw && (strchr(skipw, c)||strchr(skipw, ' '))))
fdd92c
+			if(!(skipw && (strchr(skipw, c)||strchr(skipw, ' ')))) {
fdd92c
+				/* check for space for the space character */
fdd92c
+				if (limit > 0 && (i >= limit || (size_t)(t-token) >= limit)) {
fdd92c
+					*t = '\0';
fdd92c
+					return -1;
fdd92c
+				}
fdd92c
 				*t++ = ' ';
fdd92c
+			}
fdd92c
 			lc = c;
fdd92c
 			continue;
fdd92c
 		}
fdd92c
diff --git a/sldns/str2wire.c b/sldns/str2wire.c
fdd92c
index 1a51bb6..414b7b8 100644
fdd92c
--- a/sldns/str2wire.c
fdd92c
+++ b/sldns/str2wire.c
fdd92c
@@ -150,6 +150,10 @@ int sldns_str2wire_dname_buf_origin(const char* str, uint8_t* buf, size_t* len,
fdd92c
 	if(s) return s;
fdd92c
 
fdd92c
 	if(rel && origin && dlen > 0) {
fdd92c
+		if((unsigned)dlen >= 0x00ffffffU ||
fdd92c
+			(unsigned)origin_len >= 0x00ffffffU)
fdd92c
+			/* guard against integer overflow in addition */
fdd92c
+			return RET_ERR(LDNS_WIREPARSE_ERR_GENERAL, *len);
fdd92c
 		if(dlen + origin_len - 1 > LDNS_MAX_DOMAINLEN)
fdd92c
 			return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW,
fdd92c
 				LDNS_MAX_DOMAINLEN);
fdd92c
@@ -168,7 +172,9 @@ uint8_t* sldns_str2wire_dname(const char* str, size_t* len)
fdd92c
 	uint8_t dname[LDNS_MAX_DOMAINLEN+1];
fdd92c
 	*len = sizeof(dname);
fdd92c
 	if(sldns_str2wire_dname_buf(str, dname, len) == 0) {
fdd92c
-		uint8_t* r = (uint8_t*)malloc(*len);
fdd92c
+		uint8_t* r;
fdd92c
+		if(*len > sizeof(dname)) return NULL;
fdd92c
+		r = (uint8_t*)malloc(*len);
fdd92c
 		if(r) return memcpy(r, dname, *len);
fdd92c
 	}
fdd92c
 	*len = 0;
fdd92c
@@ -187,6 +193,9 @@ rrinternal_get_owner(sldns_buffer* strbuf, uint8_t* rr, size_t* len,
fdd92c
 			sldns_buffer_position(strbuf));
fdd92c
 	}
fdd92c
 
fdd92c
+	if(token_len < 2) /* make sure there is space to read "@" or "" */
fdd92c
+		return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
fdd92c
+			sldns_buffer_position(strbuf));
fdd92c
 	if(strcmp(token, "@") == 0) {
fdd92c
 		uint8_t* tocopy;
fdd92c
 		if (origin) {
fdd92c
@@ -1094,7 +1103,7 @@ int sldns_str2wire_str_buf(const char* str, uint8_t* rd, size_t* len)
fdd92c
 	while(sldns_parse_char(&ch, &s)) {
fdd92c
 		if(sl >= 255)
fdd92c
 			return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR, s-str);
fdd92c
-		if(*len < sl+1)
fdd92c
+		if(*len < sl+2)
fdd92c
 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
fdd92c
 				s-str);
fdd92c
 		rd[++sl] = ch;
fdd92c
@@ -2095,6 +2104,8 @@ int sldns_str2wire_int16_data_buf(const char* str, uint8_t* rd, size_t* len)
fdd92c
 	char* s;
fdd92c
 	int n;
fdd92c
 	n = strtol(str, &s, 10);
fdd92c
+	if(n < 0) /* negative number not allowed */
fdd92c
+		return LDNS_WIREPARSE_ERR_SYNTAX;
fdd92c
 	if(*len < ((size_t)n)+2)
fdd92c
 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
fdd92c
 	if(n > 65535)
fdd92c
diff --git a/sldns/wire2str.c b/sldns/wire2str.c
fdd92c
index 832239f..a95c9b3 100644
fdd92c
--- a/sldns/wire2str.c
fdd92c
+++ b/sldns/wire2str.c
fdd92c
@@ -585,6 +585,7 @@ static int rr_comment_dnskey(char** s, size_t* slen, uint8_t* rr,
fdd92c
 	if(rrlen < dname_off + 10) return 0;
fdd92c
 	rdlen = sldns_read_uint16(rr+dname_off+8);
fdd92c
 	if(rrlen < dname_off + 10 + rdlen) return 0;
fdd92c
+	if(rdlen < 2) return 0;
fdd92c
 	rdata = rr + dname_off + 10;
fdd92c
 	flags = (int)sldns_read_uint16(rdata);
fdd92c
 	w += sldns_str_print(s, slen, " ;{");
fdd92c
@@ -781,7 +782,7 @@ int sldns_wire2str_dname_scan(uint8_t** d, size_t* dlen, char** s, size_t* slen,
fdd92c
 	/* spool labels onto the string, use compression if its there */
fdd92c
 	uint8_t* pos = *d;
fdd92c
 	unsigned i, counter=0;
fdd92c
-	const unsigned maxcompr = 1000; /* loop detection, max compr ptrs */
fdd92c
+	const unsigned maxcompr = 256; /* loop detection, max compr ptrs */
fdd92c
 	int in_buf = 1;
fdd92c
 	if(*dlen == 0) return sldns_str_print(s, slen, "ErrorMissingDname");
fdd92c
 	if(*pos == 0) {
fdd92c
@@ -789,7 +790,7 @@ int sldns_wire2str_dname_scan(uint8_t** d, size_t* dlen, char** s, size_t* slen,
fdd92c
 		(*dlen)--;
fdd92c
 		return sldns_str_print(s, slen, ".");
fdd92c
 	}
fdd92c
-	while(*pos) {
fdd92c
+	while((!pkt || pos < pkt+pktlen) && *pos) {
fdd92c
 		/* read label length */
fdd92c
 		uint8_t labellen = *pos++;
fdd92c
 		if(in_buf) { (*d)++; (*dlen)--; }
fdd92c
diff --git a/smallapp/unbound-control.c b/smallapp/unbound-control.c
fdd92c
index d165417..2884309 100644
fdd92c
--- a/smallapp/unbound-control.c
fdd92c
+++ b/smallapp/unbound-control.c
fdd92c
@@ -407,19 +407,19 @@ static void print_stats_shm(const char* cfgfile)
fdd92c
 	if(!config_read(cfg, cfgfile, NULL))
fdd92c
 		fatal_exit("could not read config file");
fdd92c
 	/* get shm segments */
fdd92c
-	id_ctl = shmget(cfg->shm_key, sizeof(int), SHM_R|SHM_W);
fdd92c
+	id_ctl = shmget(cfg->shm_key, sizeof(int), SHM_R);
fdd92c
 	if(id_ctl == -1) {
fdd92c
 		fatal_exit("shmget(%d): %s", cfg->shm_key, strerror(errno));
fdd92c
 	}
fdd92c
-	id_arr = shmget(cfg->shm_key+1, sizeof(int), SHM_R|SHM_W);
fdd92c
+	id_arr = shmget(cfg->shm_key+1, sizeof(int), SHM_R);
fdd92c
 	if(id_arr == -1) {
fdd92c
 		fatal_exit("shmget(%d): %s", cfg->shm_key+1, strerror(errno));
fdd92c
 	}
fdd92c
-	shm_stat = (struct ub_shm_stat_info*)shmat(id_ctl, NULL, 0);
fdd92c
+	shm_stat = (struct ub_shm_stat_info*)shmat(id_ctl, NULL, SHM_RDONLY);
fdd92c
 	if(shm_stat == (void*)-1) {
fdd92c
 		fatal_exit("shmat(%d): %s", id_ctl, strerror(errno));
fdd92c
 	}
fdd92c
-	stats = (struct ub_stats_info*)shmat(id_arr, NULL, 0);
fdd92c
+	stats = (struct ub_stats_info*)shmat(id_arr, NULL, SHM_RDONLY);
fdd92c
 	if(stats == (void*)-1) {
fdd92c
 		fatal_exit("shmat(%d): %s", id_arr, strerror(errno));
fdd92c
 	}
fdd92c
diff --git a/testcode/unitmain.c b/testcode/unitmain.c
fdd92c
index fecde80..96a6654 100644
fdd92c
--- a/testcode/unitmain.c
fdd92c
+++ b/testcode/unitmain.c
fdd92c
@@ -537,10 +537,8 @@ rnd_test(void)
fdd92c
 	struct ub_randstate* r;
fdd92c
 	int num = 1000, i;
fdd92c
 	long int a[1000];
fdd92c
-	unsigned int seed = (unsigned)time(NULL);
fdd92c
 	unit_show_feature("ub_random");
fdd92c
-	printf("ub_random seed is %u\n", seed);
fdd92c
-	unit_assert( (r = ub_initstate(seed, NULL)) );
fdd92c
+	unit_assert( (r = ub_initstate(NULL)) );
fdd92c
 	for(i=0; i
fdd92c
 		a[i] = ub_random(r);
fdd92c
 		unit_assert(a[i] >= 0);
fdd92c
diff --git a/util/data/dname.c b/util/data/dname.c
fdd92c
index b744f06..923be02 100644
fdd92c
--- a/util/data/dname.c
fdd92c
+++ b/util/data/dname.c
fdd92c
@@ -75,6 +75,8 @@ dname_valid(uint8_t* dname, size_t maxlen)
fdd92c
 {
fdd92c
 	size_t len = 0;
fdd92c
 	size_t labellen;
fdd92c
+	if(maxlen == 0)
fdd92c
+		return 0; /* too short, shortest is '0' root label */
fdd92c
 	labellen = *dname++;
fdd92c
 	while(labellen) {
fdd92c
 		if(labellen&0xc0)
fdd92c
@@ -345,11 +347,17 @@ dname_pkt_hash(sldns_buffer* pkt, uint8_t* dname, hashvalue_type h)
fdd92c
 void dname_pkt_copy(sldns_buffer* pkt, uint8_t* to, uint8_t* dname)
fdd92c
 {
fdd92c
 	/* copy over the dname and decompress it at the same time */
fdd92c
+	size_t comprcount = 0;
fdd92c
 	size_t len = 0;
fdd92c
 	uint8_t lablen;
fdd92c
 	lablen = *dname++;
fdd92c
 	while(lablen) {
fdd92c
 		if(LABEL_IS_PTR(lablen)) {
fdd92c
+			if(comprcount++ > MAX_COMPRESS_PTRS) {
fdd92c
+				/* too many compression pointers */
fdd92c
+				*to = 0; /* end the result prematurely */
fdd92c
+				return;
fdd92c
+			}
fdd92c
 			/* follow pointer */
fdd92c
             if((size_t)PTR_OFFSET(lablen, *dname)
fdd92c
                 >= sldns_buffer_limit(pkt))
fdd92c
@@ -358,6 +366,10 @@ void dname_pkt_copy(sldns_buffer* pkt, uint8_t* to, uint8_t* dname)
fdd92c
 			lablen = *dname++;
fdd92c
 			continue;
fdd92c
 		}
fdd92c
+		if(lablen > LDNS_MAX_LABELLEN) {
fdd92c
+			*to = 0; /* end the result prematurely */
fdd92c
+			return;
fdd92c
+		}
fdd92c
 		log_assert(lablen <= LDNS_MAX_LABELLEN);
fdd92c
 		len += (size_t)lablen+1;
fdd92c
 		if(len >= LDNS_MAX_DOMAINLEN) {
fdd92c
diff --git a/util/data/msgreply.c b/util/data/msgreply.c
fdd92c
index df2131c..dbae34d 100644
fdd92c
--- a/util/data/msgreply.c
fdd92c
+++ b/util/data/msgreply.c
fdd92c
@@ -238,10 +238,10 @@ rdata_copy(sldns_buffer* pkt, struct packed_rrset_data* data, uint8_t* to,
fdd92c
 				break;
fdd92c
 			}
fdd92c
 			if(len) {
fdd92c
+				log_assert(len <= pkt_len);
fdd92c
 				memmove(to, sldns_buffer_current(pkt), len);
fdd92c
 				to += len;
fdd92c
 				sldns_buffer_skip(pkt, (ssize_t)len);
fdd92c
-				log_assert(len <= pkt_len);
fdd92c
 				pkt_len -= len;
fdd92c
 			}
fdd92c
 			rdf++;
fdd92c
diff --git a/util/random.c b/util/random.c
fdd92c
index 8332960..9380502 100644
fdd92c
--- a/util/random.c
fdd92c
+++ b/util/random.c
fdd92c
@@ -86,8 +86,7 @@ ub_systemseed(unsigned int ATTR_UNUSED(seed))
fdd92c
 }
fdd92c
 
fdd92c
 struct ub_randstate* 
fdd92c
-ub_initstate(unsigned int ATTR_UNUSED(seed),
fdd92c
-	struct ub_randstate* ATTR_UNUSED(from))
fdd92c
+ub_initstate(struct ub_randstate* ATTR_UNUSED(from))
fdd92c
 {
fdd92c
 	struct ub_randstate* s = (struct ub_randstate*)malloc(1);
fdd92c
 	if(!s) {
fdd92c
@@ -123,8 +122,8 @@ void ub_systemseed(unsigned int ATTR_UNUSED(seed))
fdd92c
 {
fdd92c
 }
fdd92c
 
fdd92c
-struct ub_randstate* ub_initstate(unsigned int ATTR_UNUSED(seed), 
fdd92c
-	struct ub_randstate* ATTR_UNUSED(from))
fdd92c
+struct ub_randstate* 
fdd92c
+ub_initstate(struct ub_randstate* ATTR_UNUSED(from))
fdd92c
 {
fdd92c
 	struct ub_randstate* s = (struct ub_randstate*)calloc(1, sizeof(*s));
fdd92c
 	if(!s) {
fdd92c
@@ -140,7 +139,9 @@ long int ub_random(struct ub_randstate* ATTR_UNUSED(state))
fdd92c
 	/* random 31 bit value. */
fdd92c
 	SECStatus s = PK11_GenerateRandom((unsigned char*)&x, (int)sizeof(x));
fdd92c
 	if(s != SECSuccess) {
fdd92c
-		log_err("PK11_GenerateRandom error: %s",
fdd92c
+		/* unbound needs secure randomness for randomized
fdd92c
+		 * ID bits and port numbers in packets to upstream servers */
fdd92c
+		fatal_exit("PK11_GenerateRandom error: %s",
fdd92c
 			PORT_ErrorToString(PORT_GetError()));
fdd92c
 	}
fdd92c
 	return x & MAX_VALUE;
fdd92c
@@ -166,8 +167,7 @@ void ub_systemseed(unsigned int ATTR_UNUSED(seed))
fdd92c
 	log_err("Re-seeding not supported, generator untouched");
fdd92c
 }
fdd92c
 
fdd92c
-struct ub_randstate* ub_initstate(unsigned int seed,
fdd92c
-	struct ub_randstate* ATTR_UNUSED(from))
fdd92c
+struct ub_randstate* ub_initstate(struct ub_randstate* ATTR_UNUSED(from))
fdd92c
 {
fdd92c
 	struct ub_randstate* s = (struct ub_randstate*)calloc(1, sizeof(*s));
fdd92c
 	uint8_t buf[YARROW256_SEED_FILE_SIZE];
fdd92c
@@ -183,15 +183,10 @@ struct ub_randstate* ub_initstate(unsigned int seed,
fdd92c
 		yarrow256_seed(&s->ctx, YARROW256_SEED_FILE_SIZE, buf);
fdd92c
 		s->seeded = yarrow256_is_seeded(&s->ctx);
fdd92c
 	} else {
fdd92c
-		/* Stretch the uint32 input seed and feed it to Yarrow */
fdd92c
-		uint32_t v = seed;
fdd92c
-		size_t i;
fdd92c
-		for(i=0; i < (YARROW256_SEED_FILE_SIZE/sizeof(seed)); i++) {
fdd92c
-			memmove(buf+i*sizeof(seed), &v, sizeof(seed));
fdd92c
-			v = v*seed + (uint32_t)i;
fdd92c
-		}
fdd92c
-		yarrow256_seed(&s->ctx, YARROW256_SEED_FILE_SIZE, buf);
fdd92c
-		s->seeded = yarrow256_is_seeded(&s->ctx);
fdd92c
+		log_err("nettle random(yarrow) cannot initialize, "
fdd92c
+			"getentropy failed: %s", strerror(errno));
fdd92c
+		free(s);
fdd92c
+		return NULL;
fdd92c
 	}
fdd92c
 
fdd92c
 	return s;
fdd92c
diff --git a/util/random.h b/util/random.h
fdd92c
index a05a994..e75157d 100644
fdd92c
--- a/util/random.h
fdd92c
+++ b/util/random.h
fdd92c
@@ -57,15 +57,12 @@ void ub_systemseed(unsigned int seed);
fdd92c
 
fdd92c
 /**
fdd92c
  * Initialize a random generator state for use 
fdd92c
- * @param seed: seed value to create state contents.
fdd92c
- *	(ignored for arc4random).
fdd92c
  * @param from: if not NULL, the seed is taken from this random structure.
fdd92c
  * 	can be used to seed random states via a parent-random-state that
fdd92c
  * 	is itself seeded with entropy.
fdd92c
  * @return new state or NULL alloc failure.
fdd92c
  */
fdd92c
-struct ub_randstate* ub_initstate(unsigned int seed, 
fdd92c
-	struct ub_randstate* from);
fdd92c
+struct ub_randstate* ub_initstate(struct ub_randstate* from);
fdd92c
 
fdd92c
 /**
fdd92c
  * Generate next random number from the state passed along.
fdd92c
diff --git a/util/regional.c b/util/regional.c
fdd92c
index 899a54e..5be09eb 100644
fdd92c
--- a/util/regional.c
fdd92c
+++ b/util/regional.c
fdd92c
@@ -120,8 +120,18 @@ regional_destroy(struct regional *r)
fdd92c
 void *
fdd92c
 regional_alloc(struct regional *r, size_t size)
fdd92c
 {
fdd92c
-	size_t a = ALIGN_UP(size, ALIGNMENT);
fdd92c
+	size_t a;
fdd92c
 	void *s;
fdd92c
+	if(
fdd92c
+#if SIZEOF_SIZE_T == 8
fdd92c
+		(unsigned long long)size >= 0xffffffffffffff00ULL
fdd92c
+#else
fdd92c
+		(unsigned)size >= (unsigned)0xffffff00UL
fdd92c
+#endif
fdd92c
+		)
fdd92c
+		return NULL; /* protect against integer overflow in
fdd92c
+			malloc and ALIGN_UP */
fdd92c
+	a = ALIGN_UP(size, ALIGNMENT);
fdd92c
 	/* large objects */
fdd92c
 	if(a > REGIONAL_LARGE_OBJECT_SIZE) {
fdd92c
 		s = malloc(ALIGNMENT + size);
fdd92c
diff --git a/util/shm_side/shm_main.c b/util/shm_side/shm_main.c
fdd92c
index a783c09..69bee4d 100644
fdd92c
--- a/util/shm_side/shm_main.c
fdd92c
+++ b/util/shm_side/shm_main.c
fdd92c
@@ -121,7 +121,7 @@ int shm_main_init(struct daemon* daemon)
fdd92c
 		shmctl(daemon->shm_info->id_arr, IPC_RMID, NULL);
fdd92c
 
fdd92c
 	/* SHM: Create the segment */
fdd92c
-	daemon->shm_info->id_ctl = shmget(daemon->shm_info->key, sizeof(struct ub_shm_stat_info), IPC_CREAT | 0666);
fdd92c
+	daemon->shm_info->id_ctl = shmget(daemon->shm_info->key, sizeof(struct ub_shm_stat_info), IPC_CREAT | 0644);
fdd92c
 
fdd92c
 	if (daemon->shm_info->id_ctl < 0)
fdd92c
 	{
fdd92c
@@ -134,7 +134,7 @@ int shm_main_init(struct daemon* daemon)
fdd92c
 		return 0;
fdd92c
 	}
fdd92c
 
fdd92c
-	daemon->shm_info->id_arr = shmget(daemon->shm_info->key + 1, shm_size, IPC_CREAT | 0666);
fdd92c
+	daemon->shm_info->id_arr = shmget(daemon->shm_info->key + 1, shm_size, IPC_CREAT | 0644);
fdd92c
 
fdd92c
 	if (daemon->shm_info->id_arr < 0)
fdd92c
 	{
fdd92c
diff --git a/validator/autotrust.c b/validator/autotrust.c
fdd92c
index 7bc5577..e19bd7b 100644
fdd92c
--- a/validator/autotrust.c
fdd92c
+++ b/validator/autotrust.c
fdd92c
@@ -370,10 +370,10 @@ autr_tp_create(struct val_anchors* anchors, uint8_t* own, size_t own_len,
fdd92c
 		free(tp);
fdd92c
 		return NULL;
fdd92c
 	}
fdd92c
-	lock_basic_unlock(&anchors->lock);
fdd92c
 	lock_basic_init(&tp->lock);
fdd92c
 	lock_protect(&tp->lock, tp, sizeof(*tp));
fdd92c
 	lock_protect(&tp->lock, tp->autr, sizeof(*tp->autr));
fdd92c
+	lock_basic_unlock(&anchors->lock);
fdd92c
 	return tp;
fdd92c
 }
fdd92c