[asterisk-commits] dvossel: branch group/aoc r251483 - in /team/group/aoc: include/asterisk/ mai...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Mar 9 14:31:33 CST 2010


Author: dvossel
Date: Tue Mar  9 14:31:30 2010
New Revision: 251483

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=251483
Log:
added some validity checks to the decode routine

Modified:
    team/group/aoc/include/asterisk/aoc.h
    team/group/aoc/main/aoc.c
    team/group/aoc/tests/test_aoc.c

Modified: team/group/aoc/include/asterisk/aoc.h
URL: http://svnview.digium.com/svn/asterisk/team/group/aoc/include/asterisk/aoc.h?view=diff&rev=251483&r1=251482&r2=251483
==============================================================================
--- team/group/aoc/include/asterisk/aoc.h (original)
+++ team/group/aoc/include/asterisk/aoc.h Tue Mar  9 14:31:30 2010
@@ -50,16 +50,16 @@
 
 enum ast_aoc_type {
 	AOC_REQUEST = 0,
+	/* AOC_S */  /* Reserved for AOC-S support */
 	AOC_D,
-	AOC_E,
-	/* AOC_S */  /* Reserved for AOC-S support */
+	AOC_E, /* aoc-e must remain the last item in this enum */
 };
 
 enum ast_aoc_charge_type {
 	AOC_CHARGE_NA,
 	AOC_CHARGE_FREE,
 	AOC_CHARGE_CURRENCY,
-	AOC_CHARGE_UNIT,
+	AOC_CHARGE_UNIT, /* make sure unit is always the last enum here */
 };
 
 enum ast_aoc_request {
@@ -108,20 +108,22 @@
 /* \brief decodes an encoded aoc payload.
  *
  * \param ast_aoc_encoded, the encoded payload to decode.
+ * \param total size of encoded payload
  *
  * \retval heap allocated ast_aoc_decoded object ptr on success
  * \retval NULL failure
  */
-struct ast_aoc_decoded *ast_aoc_decode(struct ast_aoc_encoded *encoded);
+struct ast_aoc_decoded *ast_aoc_decode(struct ast_aoc_encoded *encoded, size_t size);
 
 /* \brief encodes a decoded aoc structure so it can be passed on the wire
  *
- * \param ast_aoc_decoded structe to be encoded
- *
- * \retval heap allocated ast_aoc_encoded object ptr on success
- * \retval NULL failure
- */
-struct ast_aoc_encoded *ast_aoc_encode(struct ast_aoc_decoded *decoded);
+ * \param ast_aoc_decoded struct to be encoded
+ * \param ast_aoc_encoded struct hold encoded data
+ *
+ * \retval size of encoded data
+ * \retval 0 failure
+ */
+size_t ast_aoc_encode(struct ast_aoc_decoded *decoded, struct ast_aoc_encoded **encoded_out);
 
 /* \brief Sets the type of total for a AOC-D message
  *

Modified: team/group/aoc/main/aoc.c
URL: http://svnview.digium.com/svn/asterisk/team/group/aoc/main/aoc.c?view=diff&rev=251483&r1=251482&r2=251483
==============================================================================
--- team/group/aoc/main/aoc.c (original)
+++ team/group/aoc/main/aoc.c Tue Mar  9 14:31:30 2010
@@ -56,7 +56,7 @@
 
 #define AOC_ENCODE_VERSION 1
 
-#define AOC_CURRENCY_NAME_SIZE 10+1
+#define AOC_CURRENCY_NAME_SIZE (10+1)
 
 /* AOC Payload Header. Holds all the encoded AOC data to pass on the wire */
 struct ast_aoc_encoded {
@@ -126,6 +126,15 @@
 		const enum ast_aoc_request requests)
 {
 	struct ast_aoc_decoded *decoded = NULL;
+
+	/* verify input */
+	if (((unsigned int) charge_type > AOC_CHARGE_UNIT) ||
+		((unsigned int) msg_type > AOC_E) ||
+		((msg_type == AOC_REQUEST) && !requests)) {
+
+		ast_log(LOG_WARNING, "Failed to create ast_aoc_decoded object, invalid input\n");
+		return NULL;
+	}
 
 	if (!(decoded = ast_calloc(1, sizeof(struct ast_aoc_decoded)))) {
 		ast_log(LOG_WARNING, "Failed to create ast_aoc_decoded object \n");
@@ -213,9 +222,15 @@
 	return 0;
 }
 
-struct ast_aoc_decoded *ast_aoc_decode(struct ast_aoc_encoded *encoded)
+struct ast_aoc_decoded *ast_aoc_decode(struct ast_aoc_encoded *encoded, size_t size)
 {
 	struct ast_aoc_decoded *decoded = ast_calloc(1, sizeof(struct ast_aoc_decoded));
+
+	/* verify our encoded payload is actually large enough to hold all the ies */
+	if ((size - (sizeof(struct ast_aoc_encoded)) != ntohs(encoded->datalen))) {
+		ast_log(LOG_WARNING, "Corrupted aoc encoded object, can not decode\n");
+		return NULL;
+	}
 
 	if (!decoded) {
 		ast_log(LOG_WARNING, "Failed to create ast_aoc_decoded object \n");
@@ -258,7 +273,6 @@
 	}
 
 	/* decode information elements */
-	/* TODO, make the lengths actually match up */
 	aoc_parse_ie(decoded, encoded->data, ntohs(encoded->datalen));
 	return decoded;
 }
@@ -329,19 +343,26 @@
 	}
 }
 
-struct ast_aoc_encoded *ast_aoc_encode(struct ast_aoc_decoded *decoded)
-{
-	struct ast_aoc_encoded *encoded = NULL;
+size_t ast_aoc_encode(struct ast_aoc_decoded *decoded, struct ast_aoc_encoded **encoded_out)
+{
 	struct aoc_ie_data ied;
+	struct ast_aoc_encoded *encoded;
+	unsigned int size = 0;
+
+	if (!decoded || !encoded_out) {
+		return 0;
+	}
 
 	/* create information element buffer before allocating the payload,
 	 * by doing this the exact size of the payload + the id data can be
 	 * allocated all at once. */
 	aoc_create_ie_data(decoded, &ied);
 
-	if (!(encoded = ast_calloc(1, (sizeof(struct ast_aoc_encoded) + ied.pos)))) {
+	size = sizeof(struct ast_aoc_encoded) + ied.pos;
+
+	if (!(encoded = ast_calloc(1, size))) {
 		ast_log(LOG_WARNING, "Failed to create ast_aoc_encoded object during decode routine. \n");
-		return NULL;
+		return 0;
 	}
 
 	/* -- Set ie data buffer */
@@ -399,8 +420,10 @@
 	/* --- Set Version Number --- */
 	encoded->version = AOC_ENCODE_VERSION;
 
-
-	return encoded;
+	/* set the output param */
+	*encoded_out = encoded;
+
+	return size;
 }
 
 enum ast_aoc_type ast_aoc_get_msg_type(struct ast_aoc_decoded *decoded)
@@ -538,12 +561,14 @@
 {
 	struct ast_aoc_decoded *new = NULL;
 	struct ast_aoc_encoded *encoded = NULL;
+	unsigned int size;
 	int res = 0;
-	if (!(encoded = ast_aoc_encode(decoded))) {
+
+	if (!(size = ast_aoc_encode(decoded, &encoded))) {
 		return -1;
 	}
 
-	if (!(new = ast_aoc_decode(encoded))) {
+	if (!(new = ast_aoc_decode(encoded, size))) {
 		ast_free(encoded);
 		return -1;
 	}

Modified: team/group/aoc/tests/test_aoc.c
URL: http://svnview.digium.com/svn/asterisk/team/group/aoc/tests/test_aoc.c?view=diff&rev=251483&r1=251482&r2=251483
==============================================================================
--- team/group/aoc/tests/test_aoc.c (original)
+++ team/group/aoc/tests/test_aoc.c Tue Mar  9 14:31:30 2010
@@ -189,13 +189,13 @@
 	/* ---- Test 3 ---- create AOC-Request. test all possible combinations */
 	{
 		int request[7] = { /* all possible request combinations */
-			0,
 			AOC_REQUEST_S,
 			AOC_REQUEST_D,
 			AOC_REQUEST_E,
 			(AOC_REQUEST_S | AOC_REQUEST_D),
 			(AOC_REQUEST_S | AOC_REQUEST_E),
 			(AOC_REQUEST_D | AOC_REQUEST_E),
+			(AOC_REQUEST_D | AOC_REQUEST_E | AOC_REQUEST_S)
 		};
 		int i;
 
@@ -219,6 +219,21 @@
 			decoded = ast_aoc_destroy_decoded(decoded);
 		}
 	}
+
+	/* ---- Test 4 ----  Make stuff blow up */
+	if ((decoded = ast_aoc_create(AOC_D, 1234567, 0))) {
+
+		ast_test_status_update(test, "Test 4: aoc-d creation with no valid charge type should fail\n");
+		res = AST_TEST_FAIL;
+		goto cleanup_aoc_test;
+	}
+	if ((decoded = ast_aoc_create(AOC_REQUEST, 0, 0))) {
+
+		ast_test_status_update(test, "Test 4: aoc-d creation with no valid charge type should fail\n");
+		res = AST_TEST_FAIL;
+		goto cleanup_aoc_test;
+	}
+
 cleanup_aoc_test:
 
 	decoded = ast_aoc_destroy_decoded(decoded);
@@ -237,4 +252,4 @@
 	return AST_MODULE_LOAD_SUCCESS;
 }
 
-AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Skeleton (sample) Test");
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "AOC unit tests");




More information about the asterisk-commits mailing list