------------------------------------------------------------ revno: 14034 revision-id: squid3@treenet.co.nz-20160420063604-j0yzfwew8jtkunkj parent: squidadm@squid-cache.org-20160420061412-mx9inl21ujvoqqv9 committer: Amos Jeffries branch nick: 3.5 timestamp: Wed 2016-04-20 18:36:04 +1200 message: Fix several ESI element construction issues * Do not wrap active logic in assert(). * Fix localbuf array bounds checking. * Add Must() conditions to verify array writes will succeed ------------------------------------------------------------ # Bazaar merge directive format 2 (Bazaar 0.90) # revision_id: squid3@treenet.co.nz-20160420063604-j0yzfwew8jtkunkj # target_branch: http://bzr.squid-cache.org/bzr/squid3/3.5 # testament_sha1: cd7a062b8faefeb495d421b5945745bac49755bd # timestamp: 2016-04-20 06:51:05 +0000 # source_branch: http://bzr.squid-cache.org/bzr/squid3/3.5 # base_revision_id: squidadm@squid-cache.org-20160420061412-\ # mx9inl21ujvoqqv9 # # Begin patch === modified file 'src/esi/Esi.cc' --- src/esi/Esi.cc 2016-01-01 00:14:27 +0000 +++ src/esi/Esi.cc 2016-04-20 06:36:04 +0000 @@ -966,7 +966,7 @@ ESIElement::Pointer element; int specifiedattcount = attrCount * 2; char *position; - assert (ellen < sizeof (localbuf)); /* prevent unexpected overruns. */ + Must(ellen < sizeof(localbuf)); /* prevent unexpected overruns. */ debugs(86, 5, "ESIContext::Start: element '" << el << "' with " << specifiedattcount << " tags"); @@ -980,15 +980,17 @@ /* Spit out elements we aren't interested in */ localbuf[0] = '<'; localbuf[1] = '\0'; - assert (xstrncpy (&localbuf[1], el, sizeof(localbuf) - 2)); + xstrncpy(&localbuf[1], el, sizeof(localbuf) - 2); position = localbuf + strlen (localbuf); for (i = 0; i < specifiedattcount && attr[i]; i += 2) { + Must(static_cast(position - localbuf) < sizeof(localbuf) - 1); *position = ' '; ++position; /* TODO: handle thisNode gracefully */ - assert (xstrncpy (position, attr[i], sizeof(localbuf) + (position - localbuf))); + xstrncpy(position, attr[i], sizeof(localbuf) - (position - localbuf)); position += strlen (position); + Must(static_cast(position - localbuf) < sizeof(localbuf) - 2); *position = '='; ++position; *position = '\"'; @@ -997,18 +999,21 @@ char ch; while ((ch = *chPtr++) != '\0') { if (ch == '\"') { - assert( xstrncpy(position, """, sizeof(localbuf) + (position-localbuf)) ); + Must(static_cast(position - localbuf) < sizeof(localbuf) - 6); + xstrncpy(position, """, sizeof(localbuf) - (position-localbuf)); position += 6; } else { + Must(static_cast(position - localbuf) < sizeof(localbuf) - 1); *position = ch; ++position; } } - position += strlen (position); + Must(static_cast(position - localbuf) < sizeof(localbuf) - 1); *position = '\"'; ++position; } + Must(static_cast(position - localbuf) < sizeof(localbuf) - 2); *position = '>'; ++position; *position = '\0'; @@ -1094,11 +1099,11 @@ switch (ESIElement::IdentifyElement (el)) { case ESIElement::ESI_ELEMENT_NONE: - assert (ellen < sizeof (localbuf)); /* prevent unexpected overruns. */ + Must(ellen < sizeof(localbuf) - 3); /* prevent unexpected overruns. */ /* Add elements we aren't interested in */ localbuf[0] = '<'; localbuf[1] = '/'; - assert (xstrncpy (&localbuf[2], el, sizeof(localbuf) - 3)); + xstrncpy(&localbuf[2], el, sizeof(localbuf) - 3); position = localbuf + strlen (localbuf); *position = '>'; ++position;