Fix a wrong calculation of buffer size to make coap options
authorJihun Ha <jihun.ha@samsung.com>
Tue, 4 Apr 2017 02:32:32 +0000 (11:32 +0900)
committerDan Mihai <Daniel.Mihai@microsoft.com>
Thu, 6 Apr 2017 16:33:23 +0000 (16:33 +0000)
A 'buflen' variable produced by coap_split_query has a meaning of
remained buffer size after filling a set of coap query options to pBuf which
has CA_MAX_URI_LENGTH(512) size.
It means option data is filled upto pBuf + (CA_MAX_URI_LENGTH - buflen).
Thus, accessing pBuf can reach to the above location.

Change-Id: I1320531730800a57e78e4bc56552c51525903468
Signed-off-by: Jihun Ha <jihun.ha@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/18363
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Dan Mihai <Daniel.Mihai@microsoft.com>
resource/csdk/connectivity/src/caprotocolmessage.c [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index da0bbba..541f4e8
@@ -477,12 +477,14 @@ CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target
     {
         unsigned char uriBuffer[CA_MAX_URI_LENGTH] = { 0 };
         unsigned char *pBuf = uriBuffer;
-        size_t buflen = sizeof(uriBuffer);
-        int res = (target == COAP_OPTION_URI_PATH) ? coap_split_path(str, length, pBuf, &buflen) :
-                                                     coap_split_query(str, length, pBuf, &buflen);
+        size_t unusedBufferSize = sizeof(uriBuffer);
+        int res = (target == COAP_OPTION_URI_PATH) ? coap_split_path(str, length, pBuf, &unusedBufferSize) :
+                                                     coap_split_query(str, length, pBuf, &unusedBufferSize);
 
         if (res > 0)
         {
+            assert(unusedBufferSize < sizeof(uriBuffer));
+            size_t usedBufferSize = sizeof(uriBuffer) - unusedBufferSize;
             size_t prevIdx = 0;
             while (res--)
             {
@@ -496,11 +498,13 @@ CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target
                 }
 
                 size_t optSize = COAP_OPT_SIZE(pBuf);
-                if ((prevIdx + optSize) < buflen)
+                if (prevIdx + optSize > usedBufferSize)
                 {
-                    pBuf += optSize;
-                    prevIdx += optSize;
+                    assert(false);
+                    return CA_STATUS_INVALID_PARAM;
                 }
+                pBuf += optSize;
+                prevIdx += optSize;
             }
         }
         else