From bf6a303883bde40cf96b960c8574cddd89e71701 Mon Sep 17 00:00:00 2001
From: Jon Shallow <supjps-libcoap@jpshallow.com>
Date: Thu, 25 Jan 2024 18:03:17 +0000
Subject: [PATCH] coap_oscore.c: Fix parsing OSCORE configuration information

A vulnerability was found in obgm libcoap 4.3.4. It has been rated as critical.
Affected by this issue is the function get_split_entry of the file src/coap_oscore.c of the component Configuration File Handler.
The manipulation leads to stack-based buffer overflow.

CVE: CVE-2024-0962

Upstream-Status: Backport [https://github.com/obgm/libcoap/pull/1311]

Signed-off-by: alperak <alperyasinak1@gmail.com>
---
 src/coap_oscore.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/coap_oscore.c b/src/coap_oscore.c
index 83f785c92..e0fb22947 100644
--- a/src/coap_oscore.c
+++ b/src/coap_oscore.c
@@ -1678,11 +1678,12 @@ get_split_entry(const char **start,
                 oscore_value_t *value) {
   const char *begin = *start;
   const char *end;
+  const char *kend;
   const char *split;
   size_t i;

 retry:
-  end = memchr(begin, '\n', size);
+  kend = end = memchr(begin, '\n', size);
   if (end == NULL)
     return 0;

@@ -1693,7 +1694,7 @@ get_split_entry(const char **start,

   if (begin[0] == '#' || (end - begin) == 0) {
     /* Skip comment / blank line */
-    size -= end - begin + 1;
+    size -= kend - begin + 1;
     begin = *start;
     goto retry;
   }
