summaryrefslogtreecommitdiff
path: root/res/res_pjsip/presence_xml.c
diff options
context:
space:
mode:
authorJoshua Colp <jcolp@digium.com>2015-08-04 18:12:59 -0300
committerJoshua Colp <jcolp@digium.com>2015-08-06 05:20:47 -0500
commit4b6c657a820334ed3bef9daee48f21d6a9659f98 (patch)
tree217f64e86aee7eb0a798c18ecbf94956f90ff6c1 /res/res_pjsip/presence_xml.c
parent3d4db97253a059d38d3e37b34c44ee02448fbab9 (diff)
res_pjsip: Ensure sanitized XML is NULL terminated.
The ast_sip_sanitize_xml function is used to sanitize a string for placement into XML. This is done by examining an input string and then appending values to an output buffer. The function used by its implementation, strncat, has specific behavior that was not taken into account. If the size of the input string exceeded the available output buffer size it was possible for the sanitization function to write past the output buffer itself causing a crash. The crash would either occur because it was writing into memory it shouldn't be or because the resulting string was not NULL terminated. This change keeps count of how much remaining space is available in the output buffer for text and only allows strncat to use that amount. Since this was exposed by the res_pjsip_pidf_digium_body_supplement module attempting to send a large message the maximum allowed message size has also been increased in it. A unit test has also been added which confirms that the ast_sip_sanitize_xml function is providing NULL terminated output even when the input length exceeds the output buffer size. ASTERISK-25304 #close Change-Id: I743dd9809d3e13d722df1b0509dfe34621398302
Diffstat (limited to 'res/res_pjsip/presence_xml.c')
-rw-r--r--res/res_pjsip/presence_xml.c31
1 files changed, 20 insertions, 11 deletions
diff --git a/res/res_pjsip/presence_xml.c b/res/res_pjsip/presence_xml.c
index 12bfa078c..b98ea0237 100644
--- a/res/res_pjsip/presence_xml.c
+++ b/res/res_pjsip/presence_xml.c
@@ -30,45 +30,54 @@ void ast_sip_sanitize_xml(const char *input, char *output, size_t len)
{
char *copy = ast_strdupa(input);
char *break_point;
+ size_t remaining = len - 1;
output[0] = '\0';
- while ((break_point = strpbrk(copy, "<>\"&'\n\r"))) {
+ while ((break_point = strpbrk(copy, "<>\"&'\n\r")) && remaining) {
char to_escape = *break_point;
*break_point = '\0';
- strncat(output, copy, len);
+ strncat(output, copy, remaining);
+
+ /* The strncat function will write remaining+1 if the string length is
+ * equal to or greater than the size provided to it. We take this into
+ * account by subtracting 1, which ensures that the NULL byte is written
+ * inside of the provided buffer.
+ */
+ remaining = len - strlen(output) - 1;
switch (to_escape) {
case '<':
- strncat(output, "&lt;", len);
+ strncat(output, "&lt;", remaining);
break;
case '>':
- strncat(output, "&gt;", len);
+ strncat(output, "&gt;", remaining);
break;
case '"':
- strncat(output, "&quot;", len);
+ strncat(output, "&quot;", remaining);
break;
case '&':
- strncat(output, "&amp;", len);
+ strncat(output, "&amp;", remaining);
break;
case '\'':
- strncat(output, "&apos;", len);
+ strncat(output, "&apos;", remaining);
break;
case '\r':
- strncat(output, "&#13;", len);
+ strncat(output, "&#13;", remaining);
break;
case '\n':
- strncat(output, "&#10;", len);
+ strncat(output, "&#10;", remaining);
break;
};
copy = break_point + 1;
+ remaining = len - strlen(output) - 1;
}
/* Be sure to copy everything after the final bracket */
- if (*copy) {
- strncat(output, copy, len);
+ if (*copy && remaining) {
+ strncat(output, copy, remaining);
}
}