Blame SOURCES/unbound-1.7.3-ipsec-hook.patch

45f416
diff --git a/ipsecmod/ipsecmod.c b/ipsecmod/ipsecmod.c
45f416
index c8400c6..9e916d6 100644
45f416
--- a/ipsecmod/ipsecmod.c
45f416
+++ b/ipsecmod/ipsecmod.c
45f416
@@ -162,6 +162,71 @@ generate_request(struct module_qstate* qstate, int id, uint8_t* name,
45f416
 }
45f416
 
45f416
 /**
45f416
+ * Check if the string passed is a valid domain name with safe characters to
45f416
+ * pass to a shell.
45f416
+ * This will only allow:
45f416
+ *  - digits
45f416
+ *  - alphas
45f416
+ *  - hyphen (not at the start)
45f416
+ *  - dot (not at the start, or the only character)
45f416
+ *  - underscore
45f416
+ * @param s: pointer to the string.
45f416
+ * @param slen: string's length.
45f416
+ * @return true if s only contains safe characters; false otherwise.
45f416
+ */
45f416
+static int
45f416
+domainname_has_safe_characters(char* s, size_t slen) {
45f416
+	size_t i;
45f416
+	for(i = 0; i < slen; i++) {
45f416
+		if(s[i] == '\0') return 1;
45f416
+		if((s[i] == '-' && i != 0)
45f416
+			|| (s[i] == '.' && (i != 0 || s[1] == '\0'))
45f416
+			|| (s[i] == '_') || (s[i] >= '0' && s[i] <= '9')
45f416
+			|| (s[i] >= 'A' && s[i] <= 'Z')
45f416
+			|| (s[i] >= 'a' && s[i] <= 'z')) {
45f416
+			continue;
45f416
+		}
45f416
+		return 0;
45f416
+	}
45f416
+	return 1;
45f416
+}
45f416
+
45f416
+/**
45f416
+ * Check if the stringified IPSECKEY RDATA contains safe characters to pass to
45f416
+ * a shell.
45f416
+ * This is only relevant for checking the gateway when the gateway type is 3
45f416
+ * (domainname).
45f416
+ * @param s: pointer to the string.
45f416
+ * @param slen: string's length.
45f416
+ * @return true if s contains only safe characters; false otherwise.
45f416
+ */
45f416
+static int
45f416
+ipseckey_has_safe_characters(char* s, size_t slen) {
45f416
+	int precedence, gateway_type, algorithm;
45f416
+	char* gateway;
45f416
+	gateway = (char*)calloc(slen, sizeof(char));
45f416
+	if(!gateway) {
45f416
+		log_err("ipsecmod: out of memory when calling the hook");
45f416
+		return 0;
45f416
+	}
45f416
+	if(sscanf(s, "%d %d %d %s ",
45f416
+			&precedence, &gateway_type, &algorithm, gateway) != 4) {
45f416
+		free(gateway);
45f416
+		return 0;
45f416
+	}
45f416
+	if(gateway_type != 3) {
45f416
+		free(gateway);
45f416
+		return 1;
45f416
+	}
45f416
+	if(domainname_has_safe_characters(gateway, slen)) {
45f416
+		free(gateway);
45f416
+		return 1;
45f416
+	}
45f416
+	free(gateway);
45f416
+	return 0;
45f416
+}
45f416
+
45f416
+/**
45f416
  *  Prepare the data and call the hook.
45f416
  *
45f416
  *  @param qstate: query state.
45f416
@@ -175,7 +240,7 @@ call_hook(struct module_qstate* qstate, struct ipsecmod_qstate* iq,
45f416
 {
45f416
 	size_t slen, tempdata_len, tempstring_len, i;
45f416
 	char str[65535], *s, *tempstring;
45f416
-	int w;
45f416
+	int w = 0, w_temp, qtype;
45f416
 	struct ub_packed_rrset_key* rrset_key;
45f416
 	struct packed_rrset_data* rrset_data;
45f416
 	uint8_t *tempdata;
45f416
@@ -192,9 +257,9 @@ call_hook(struct module_qstate* qstate, struct ipsecmod_qstate* iq,
45f416
 	memset(s, 0, slen);
45f416
 
45f416
 	/* Copy the hook into the buffer. */
45f416
-	sldns_str_print(&s, &slen, "%s", qstate->env->cfg->ipsecmod_hook);
45f416
+	w += sldns_str_print(&s, &slen, "%s", qstate->env->cfg->ipsecmod_hook);
45f416
 	/* Put space into the buffer. */
45f416
-	sldns_str_print(&s, &slen, " ");
45f416
+	w += sldns_str_print(&s, &slen, " ");
45f416
 	/* Copy the qname into the buffer. */
45f416
 	tempstring = sldns_wire2str_dname(qstate->qinfo.qname,
45f416
 		qstate->qinfo.qname_len);
45f416
@@ -202,68 +267,96 @@ call_hook(struct module_qstate* qstate, struct ipsecmod_qstate* iq,
45f416
 		log_err("ipsecmod: out of memory when calling the hook");
45f416
 		return 0;
45f416
 	}
45f416
-	sldns_str_print(&s, &slen, "\"%s\"", tempstring);
45f416
+	if(!domainname_has_safe_characters(tempstring, strlen(tempstring))) {
45f416
+		log_err("ipsecmod: qname has unsafe characters");
45f416
+		free(tempstring);
45f416
+		return 0;
45f416
+	}
45f416
+	w += sldns_str_print(&s, &slen, "\"%s\"", tempstring);
45f416
 	free(tempstring);
45f416
 	/* Put space into the buffer. */
45f416
-	sldns_str_print(&s, &slen, " ");
45f416
+	w += sldns_str_print(&s, &slen, " ");
45f416
 	/* Copy the IPSECKEY TTL into the buffer. */
45f416
 	rrset_data = (struct packed_rrset_data*)iq->ipseckey_rrset->entry.data;
45f416
-	sldns_str_print(&s, &slen, "\"%ld\"", (long)rrset_data->ttl);
45f416
+	w += sldns_str_print(&s, &slen, "\"%ld\"", (long)rrset_data->ttl);
45f416
 	/* Put space into the buffer. */
45f416
-	sldns_str_print(&s, &slen, " ");
45f416
-	/* Copy the A/AAAA record(s) into the buffer. Start and end this section
45f416
-	 * with a double quote. */
45f416
+	w += sldns_str_print(&s, &slen, " ");
45f416
 	rrset_key = reply_find_answer_rrset(&qstate->return_msg->qinfo,
45f416
 		qstate->return_msg->rep);
45f416
+	/* Double check that the records are indeed A/AAAA.
45f416
+	 * This should never happen as this function is only executed for A/AAAA
45f416
+	 * queries but make sure we don't pass anything other than A/AAAA to the
45f416
+	 * shell. */
45f416
+	qtype = ntohs(rrset_key->rk.type);
45f416
+	if(qtype != LDNS_RR_TYPE_AAAA && qtype != LDNS_RR_TYPE_A) {
45f416
+		log_err("ipsecmod: Answer is not of A or AAAA type");
45f416
+		return 0;
45f416
+	}
45f416
 	rrset_data = (struct packed_rrset_data*)rrset_key->entry.data;
45f416
-	sldns_str_print(&s, &slen, "\"");
45f416
+	/* Copy the A/AAAA record(s) into the buffer. Start and end this section
45f416
+	 * with a double quote. */
45f416
+	w += sldns_str_print(&s, &slen, "\"");
45f416
 	for(i=0; i<rrset_data->count; i++) {
45f416
 		if(i > 0) {
45f416
 			/* Put space into the buffer. */
45f416
-			sldns_str_print(&s, &slen, " ");
45f416
+			w += sldns_str_print(&s, &slen, " ");
45f416
 		}
45f416
 		/* Ignore the first two bytes, they are the rr_data len. */
45f416
-		w = sldns_wire2str_rdata_buf(rrset_data->rr_data[i] + 2,
45f416
+		w_temp = sldns_wire2str_rdata_buf(rrset_data->rr_data[i] + 2,
45f416
 			rrset_data->rr_len[i] - 2, s, slen, qstate->qinfo.qtype);
45f416
-		if(w < 0) {
45f416
+		if(w_temp < 0) {
45f416
 			/* Error in printout. */
45f416
-			return -1;
45f416
-		} else if((size_t)w >= slen) {
45f416
+			log_err("ipsecmod: Error in printing IP address");
45f416
+			return 0;
45f416
+		} else if((size_t)w_temp >= slen) {
45f416
 			s = NULL; /* We do not want str to point outside of buffer. */
45f416
 			slen = 0;
45f416
-			return -1;
45f416
+			log_err("ipsecmod: shell command too long");
45f416
+			return 0;
45f416
 		} else {
45f416
-			s += w;
45f416
-			slen -= w;
45f416
+			s += w_temp;
45f416
+			slen -= w_temp;
45f416
+			w += w_temp;
45f416
 		}
45f416
 	}
45f416
-	sldns_str_print(&s, &slen, "\"");
45f416
+	w += sldns_str_print(&s, &slen, "\"");
45f416
 	/* Put space into the buffer. */
45f416
-	sldns_str_print(&s, &slen, " ");
45f416
+	w += sldns_str_print(&s, &slen, " ");
45f416
 	/* Copy the IPSECKEY record(s) into the buffer. Start and end this section
45f416
 	 * with a double quote. */
45f416
-	sldns_str_print(&s, &slen, "\"");
45f416
+	w += sldns_str_print(&s, &slen, "\"");
45f416
 	rrset_data = (struct packed_rrset_data*)iq->ipseckey_rrset->entry.data;
45f416
 	for(i=0; i<rrset_data->count; i++) {
45f416
 		if(i > 0) {
45f416
 			/* Put space into the buffer. */
45f416
-			sldns_str_print(&s, &slen, " ");
45f416
+			w += sldns_str_print(&s, &slen, " ");
45f416
 		}
45f416
 		/* Ignore the first two bytes, they are the rr_data len. */
45f416
 		tempdata = rrset_data->rr_data[i] + 2;
45f416
 		tempdata_len = rrset_data->rr_len[i] - 2;
45f416
 		/* Save the buffer pointers. */
45f416
 		tempstring = s; tempstring_len = slen;
45f416
-		w = sldns_wire2str_ipseckey_scan(&tempdata, &tempdata_len, &s, &slen,
45f416
-			NULL, 0);
45f416
+		w_temp = sldns_wire2str_ipseckey_scan(&tempdata, &tempdata_len, &s,
45f416
+			&slen, NULL, 0);
45f416
 		/* There was an error when parsing the IPSECKEY; reset the buffer
45f416
 		 * pointers to their previous values. */
45f416
-		if(w == -1){
45f416
+		if(w_temp == -1) {
45f416
 			s = tempstring; slen = tempstring_len;
45f416
+		} else if(w_temp > 0) {
45f416
+			if(!ipseckey_has_safe_characters(
45f416
+					tempstring, tempstring_len - slen)) {
45f416
+				log_err("ipsecmod: ipseckey has unsafe characters");
45f416
+				return 0;
45f416
+			}
45f416
+			w += w_temp;
45f416
 		}
45f416
 	}
45f416
-	sldns_str_print(&s, &slen, "\"");
45f416
-	verbose(VERB_ALGO, "ipsecmod: hook command: '%s'", str);
45f416
+	w += sldns_str_print(&s, &slen, "\"");
45f416
+	if(w >= (int)sizeof(str)) {
45f416
+		log_err("ipsecmod: shell command too long");
45f416
+		return 0;
45f416
+	}
45f416
+	verbose(VERB_ALGO, "ipsecmod: shell command: '%s'", str);
45f416
 	/* ipsecmod-hook should return 0 on success. */
45f416
 	if(system(str) != 0)
45f416
 		return 0;