Blame SOURCES/0017-MS-cert-template-validate-argument.patch

20746d
From 481fb8581fdf891b768eeb0bc88855c27689722b Mon Sep 17 00:00:00 2001
20746d
From: Fraser Tweedale <ftweedal@redhat.com>
20746d
Date: Fri, 18 Aug 2017 16:17:49 +1000
20746d
Subject: [PATCH] MS cert template: validate argument
20746d
20746d
Update the server to validate the MS V2 certificate template option
20746d
argument when adding or updating a request.
20746d
20746d
Fixes: https://pagure.io/certmonger/issue/78
20746d
---
20746d
 src/Makefile.am |  4 +++-
20746d
 src/certext.c   | 13 +++++++++++++
20746d
 src/certext.h   |  5 +++++
20746d
 src/tdbush.c    | 25 +++++++++++++++++++++++--
20746d
 4 files changed, 44 insertions(+), 3 deletions(-)
20746d
20746d
diff --git a/src/Makefile.am b/src/Makefile.am
20746d
index 479903c..213bfa9 100644
20746d
--- a/src/Makefile.am
20746d
+++ b/src/Makefile.am
20746d
@@ -186,7 +186,7 @@ tdbusm_check_SOURCES = tdbusm-check.c tm.c tm.h
20746d
 tdbusm_check_LDADD = libcm.a $(CERTMONGER_LIBS) $(POPT_LIBS)
20746d
 serial_check_LDADD = libcm.a $(CERTMONGER_LIBS) $(LTLIBICONV)
20746d
 nl_check_LDADD = libcm.a $(CERTMONGER_LIBS)
20746d
-submit_x_CFLAGS = $(AM_CFLAGS) -DCM_SUBMIT_X_MAIN
20746d
+submit_x_CFLAGS = $(AM_CFLAGS) $(NSS_CFLAGS) -DCM_SUBMIT_X_MAIN
20746d
 submit_x_SOURCES = submit-x.c submit-x.h submit-u.c submit-u.h log.c log.h \
20746d
 	tm.c tm.h
20746d
 submit_x_LDADD = $(XMLRPC_LIBS) $(KRB5_LIBS) $(TALLOC_LIBS) \
20746d
@@ -205,12 +205,14 @@ pkglibexec_PROGRAMS += local-submit
20746d
 pkglibexec_PROGRAMS += scep-submit
20746d
 endif
20746d
 noinst_PROGRAMS += submit-h submit-d
20746d
+ipa_submit_CFLAGS = $(AM_CFLAGS) $(NSS_CFLAGS)
20746d
 ipa_submit_SOURCES = ipa.c srvloc.c srvloc.h store.h store-gen.c \
20746d
 		submit-x.c submit-x.h submit-u.c submit-u.h \
20746d
 		submit-e.h util.c util.h log.c log.h tm.c tm.h
20746d
 ipa_submit_LDADD = $(XMLRPC_LIBS) $(LDAP_LIBS) $(KRB5_LIBS) $(TALLOC_LIBS) \
20746d
 		   $(GMP_LIBS) $(IDN_LIBS) $(OPENSSL_LIBS) $(UUID_LIBS) \
20746d
 		   $(RESOLV_LIBS) $(LTLIBICONV) $(POPT_LIBS)
20746d
+certmaster_submit_CFLAGS = $(AM_CFLAGS) $(NSS_CFLAGS)
20746d
 certmaster_submit_SOURCES = certmaster.c submit-x.c submit-x.h \
20746d
 		submit-e.h submit-u.c submit-u.h util.c util.h log.c log.h \
20746d
 		tm.c tm.h
20746d
diff --git a/src/certext.c b/src/certext.c
20746d
index 5f8a743..587496f 100644
20746d
--- a/src/certext.c
20746d
+++ b/src/certext.c
20746d
@@ -1663,6 +1663,19 @@ cm_certext_build_certificate_template(
20746d
 	return SECITEM_ArenaDupItem(arena, &encoded);
20746d
 }
20746d
 
20746d
+/* Validate a V2 template spec */
20746d
+PRBool cm_ms_template_valid(char *template_spec) {
20746d
+	PLArenaPool *arena = PORT_NewArena(sizeof(double));
20746d
+	if (arena == NULL)
20746d
+		return PR_FALSE;
20746d
+	SECItem *result =
20746d
+		cm_certext_build_certificate_template(arena, template_spec);
20746d
+	PORT_FreeArena(arena, PR_FALSE);
20746d
+	// *result has been freed, but we don't read it;
20746d
+	// we only need to know whether the parse succeeded
20746d
+	return result != NULL;
20746d
+}
20746d
+
20746d
 /* Build a Netscape certtype extension value. */
20746d
 static SECItem *
20746d
 cm_certext_build_ns_certtype(struct cm_store_entry *entry,
20746d
diff --git a/src/certext.h b/src/certext.h
20746d
index 530ece4..5e95835 100644
20746d
--- a/src/certext.h
20746d
+++ b/src/certext.h
20746d
@@ -15,6 +15,8 @@
20746d
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20746d
  */
20746d
 
20746d
+#include <prtypes.h>
20746d
+
20746d
 #ifndef cmcertext_h
20746d
 #define cmcertext_h
20746d
 
20746d
@@ -25,4 +27,7 @@ void cm_certext_build_csr_extensions(struct cm_store_entry *entry,
20746d
 				     struct NSSInitContextStr *ctx,
20746d
 				     unsigned char **encoded, size_t *length);
20746d
 
20746d
+/* Validate a V2 template spec */
20746d
+PRBool cm_ms_template_valid(char *template_spec);
20746d
+
20746d
 #endif
20746d
diff --git a/src/tdbush.c b/src/tdbush.c
20746d
index 9e2a372..04fe57e 100644
20746d
--- a/src/tdbush.c
20746d
+++ b/src/tdbush.c
20746d
@@ -32,6 +32,7 @@
20746d
 
20746d
 #include "log.h"
20746d
 #include "cm.h"
20746d
+#include "certext.h"
20746d
 #include "prefs.h"
20746d
 #include "store.h"
20746d
 #include "store-int.h"
20746d
@@ -1572,7 +1573,18 @@ base_add_request(DBusConnection *conn, DBusMessage *msg,
20746d
 					  CM_DBUS_PROP_TEMPLATE_MS_CERTIFICATE_TEMPLATE,
20746d
 					  cm_tdbusm_dict_s);
20746d
 	if (param != NULL) {
20746d
-		// TODO check validity
20746d
+		if (param->value.s != NULL
20746d
+		    && strlen(param->value.s) > 0
20746d
+		    && !cm_ms_template_valid(param->value.s)) {
20746d
+			cm_log(1, "Invalid V2 certificate template specifier: %s", param->value.s);
20746d
+			ret = send_internal_base_bad_arg_error(
20746d
+				conn, msg,
20746d
+				_("Invalid V2 certificate template specifier: %s"),
20746d
+				param->value.s,
20746d
+				CM_DBUS_PROP_TEMPLATE_MS_CERTIFICATE_TEMPLATE);
20746d
+			talloc_free(parent);
20746d
+			return ret;
20746d
+		}
20746d
 		new_entry->cm_template_certificate_template = maybe_strdup(new_entry,
20746d
 									   param->value.s);
20746d
 	}
20746d
@@ -3330,8 +3342,17 @@ request_modify(DBusConnection *conn, DBusMessage *msg,
20746d
 			} else
20746d
 			if ((param->value_type == cm_tdbusm_dict_s) &&
20746d
 			    (strcasecmp(param->key, CM_DBUS_PROP_TEMPLATE_MS_CERTIFICATE_TEMPLATE) == 0)) {
20746d
+				if (param->value.s != NULL
20746d
+				    && strlen(param->value.s) > 0
20746d
+				    && !cm_ms_template_valid(param->value.s)) {
20746d
+					cm_log(1, "Invalid V2 certificate template specifier: %s", param->value.s);
20746d
+					return send_internal_base_bad_arg_error(
20746d
+						conn, msg,
20746d
+						_("Invalid V2 certificate template specifier: %s"),
20746d
+						param->value.s,
20746d
+						CM_DBUS_PROP_TEMPLATE_MS_CERTIFICATE_TEMPLATE);
20746d
+				}
20746d
 				talloc_free(entry->cm_template_certificate_template);
20746d
-				// TODO check validity
20746d
 				entry->cm_template_certificate_template =
20746d
 					maybe_strdup(entry, param->value.s);
20746d
 				if (n_propname + 2 < sizeof(propname) / sizeof(propname[0])) {
20746d
-- 
20746d
2.14.4
20746d