Skip to contentSkip to conversation

Fix integer overflow in libiniparser.c

PATCH In review Anton Moryakov 2 participants 2 messages Dec 13, 2024 – Dec 16, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix integer overflow in libiniparser.c

Report of the static analyzer:
Possible integer underflow: left operand is tainted.
An integer underflow may occur due to arithmetic operation (subtraction) between values
{ [-2147483648, 2147483647] } and '1', where the first value comes from the expression '(int)strlen(line)'

Corrections explained:
Modified logic toskip empty lines immediately, reducing unnecessary processing.- Ensured safety by validating len >= 0 before accessing line[len] to avoid out-of-bounds access.

Triggers found by static analyzer Svace.

---
 lib/libiniparser.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/libiniparser.c +1 -1
index a6ddcc7..2facbde 100644
@@ -524,7 +524,7 @@ dictionary * iniparser_load(const char * ininame)
         lineno++ ;
         len = (int)strlen(line)-1;
         /* Safety check against buffer overflows */
-        if (line[len]!='\n') {
+        if (len < 0 || line[len]!='\n') {
             fprintf(stderr,
                     "iniparser: input line too long in %s (%d)\n",
                     ininame,
Signature · 6 lines
2.30.2


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
#1 lore raw

Hei hei,

Am Fri, Dec 13, 2024 at 04:31:56PM +0300 schrieb Anton Moryakov:

Quoted 21 lines

Report of the static analyzer:
Possible integer underflow: left operand is tainted.
An integer underflow may occur due to arithmetic operation (subtraction) between values
{ [-2147483648, 2147483647] } and '1', where the first value comes from the expression '(int)strlen(line)'

Corrections explained:
Modified logic toskip empty lines immediately, reducing unnecessary processing.- Ensured safety by validating len >= 0 before accessing line[len] to avoid out-of-bounds access.

Triggers found by static analyzer Svace.

---
 lib/libiniparser.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/libiniparser.c +0 -0
index a6ddcc7..2facbde 100644

lib/libiniparser.c dictionary * iniparser_load(const char * ininame) · line 527

Quoted 5 lines
lib/libiniparser.c +1 -1
@@ -524,7 +524,7 @@ dictionary * iniparser_load(const char * ininame)
         lineno++ ;
         len = (int)strlen(line)-1;
         /* Safety check against buffer overflows */
-        if (line[len]!='\n') {
+        if (len < 0 || line[len]!='\n') {

This has been fixed years ago in upstream iniparser, while mtd-utils still uses an ancient copy of that library. Maybe someone should step up and modernize that before doing fixes here. The current development of libiniparser takes place here (since May 2024):

https://gitlab.com/iniparser/iniparser

Greets
Alex

Quoted 10 lines
lib/libiniparser.c +0 -0
             fprintf(stderr,
                     "iniparser: input line too long in %s (%d)\n",
                     ininame,
Signature · 6 lines
2.30.2


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/