mailsend icon indicating copy to clipboard operation
mailsend copied to clipboard

A potential Buffer Overflow bug found in mutils.c

Open x14ngch3n opened this issue 1 year ago • 2 comments

Hi, I'm currently trying to use the static analysis tool Infer to find uncatched API-misuse bugs in OpenWrt packages, and I find a potential Buffer Overflow in your project, version 1.19.

The bug located in mutils.c. Firstly, the program read the environment variable PATH to p in line 1234. That variable p is later been segmented by ':', and finally used as the parameter of sprintf(), as shown in the following code:

path=getenv("PATH");
if (path == NULL)
    return(-1);

p=path;
found=0;
while (*p != '\0' && found == 0)
{
    len=0;
    while (*p != ':' && *p != '\0')
    {
        len++;
        p++;
    }
    s=(char) *p;
    *p='\0';
    (void) sprintf(szbuf,"%s/%s",p-len,name);
    *p=s;
    if (*p)
        p++;

    if (access(szbuf,X_OK) == 0)
        found=1;
}

As the length of buffer szbuf is 8192 and the maximum length of env variable is 32768, it remains possible to cause a Buffer Overflow when p contains no ':' .I also attached the analysis trace given by Infer FYI:

"trace": [
  {
    "file": "libs/libmutils/mutils.c",
    "line": 1234,
    "col": 10,
    "feature": [ "Input", "getenv" ]
  },
  {
    "file": "libs/libmutils/mutils.c",
    "line": 1250,
    "col": 16,
    "feature": [
      "BufferOverflow",
      "sprintf",
      [ "BinOp", "-", [ "Var" ], [ "Var" ] ]
    ]
  }
],

x14ngch3n avatar Aug 21 '23 08:08 x14ngch3n

This function is not used in mailsend but should be fixed neverthless.

muquit avatar Aug 21 '23 19:08 muquit

Thank you for your reply

x14ngch3n avatar Aug 22 '23 17:08 x14ngch3n