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

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