Skip to content

Potential buffer overflow #3201

@BsAtHome

Description

@BsAtHome

There is a possible buffer overflow by using strncat(), even though it only appends one character at a time. The target buffer is a static on-stack array (initialized with zeros). It should be a NUL-terminated string at all times. However, if the buffer is full and the NUL-terminator is positioned as last character in the array, then a call to strncat, appending one character, will overwrite the string termination and exhaust the buffer space.

Other places have already be replaced by rtapi "safe" api calls. The following diff fixes the problem and makes use of that api. It may not be as fast, but it is secure.

diff --git a/src/emc/rs274ngc/interp_remap.cc b/src/emc/rs274ngc/interp_remap.cc
index 3e4069e0e8..08a36deecc 100644
--- a/src/emc/rs274ngc/interp_remap.cc
+++ b/src/emc/rs274ngc/interp_remap.cc
@@ -327,8 +327,8 @@ int Interp::add_parameters(setup_pointer settings,
     }
     while (*s) {
        errored = true;
-       char c  = toupper(*s);
-       strncat(tail,&c,1);
+       char c[2]  = { (char)toupper(*s), 0 };
+       rtapi_strxcat(tail, c);
        if (*(s+1)) rtapi_strxcat(tail,",");
        s++;
     }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions