Как решить проблему запятнанного скалярного укрытия, введенную «операцией замены байтов» в c - PullRequest
0 голосов
/ 25 января 2019

Я работаю в C - Исправления предупреждений Coverity. Новое в предупреждениях TAINTED_SCALAR. У меня есть этот код ниже, в котором я получаю предупреждение скалярное скаляр,

VOID func1 (UINT1 *p_u1RadiusReceivedPacket,
            UINT1 *p_u1Secret,
            UINT1 a_u1Concatenated[],
            INT4 *i4_Length)
{
    INT4                i4_seclen = 0;
    UINT2               u2_pktlen = 0;
    UINT1    a_u1RequestAuth[LEN_REQ_AUTH_AUTH] = {};
    INT4                        index = 0;

    if (p_u1RadiusReceivedPacket == NULL)
    {
        return;
    }
#if 0 // this part of code, when included, comes under dead code saying the if condition inside this for loop will never be stru and hence will never get hit. Hence commented out this code using "#if 0". When this code was added, the abouve NULL check is removed.
    for (index = 0; index < (PKT_LEN+2); index++)
    {
        if(!(p_u1RadiusReceivedPacket + index))
        {
            return;
        }
    }
#endif

    MEMSET (a_u1Concatenated, 0, LEN_RX_PKT + LEN_SECRET);

    MEMCPY (&u2_pktlen, p_u1RadiusReceivedPacket + PKT_LEN, 2);

    /* Validate the packet length to avoid  buffer overflow, denial of service,
     * memory corruption, or other security vulnerability. */
    if ((u2_pktlen < 20) || (u2_pktlen > 4096))
    {
        return;
    }

    u2_pktlen = OSIX_NTOHS (u2_pktlen); // this part of code says , "Performing a byte swapping operation on {0} implies that it came from an external source, and is therefore tainted.". Also says, "Assigning: {0} = {1}. Both are now tainted.}{{code{u2_pktlen}}}{{code{(UINT2)(((u2_pktlen &amp; 0xff00) &gt;&gt; 8) | ((u2_pktlen &amp; 0xff) &lt;&lt; 8))}}}

    MEMCPY (a_u1Concatenated, p_u1RadiusReceivedPacket, u2_pktlen); // This part of code says, "Passing tainted variable {0} to a tainted sink.}{{code{(size_t)u2_pktlen}}}" ==> eventSetCaption is "Tainted data flows to untainted sink".
    MEMCPY (a_u1RequestAuth, p_u1RadiusReceivedPacket + PKT_REQA,
            LEN_REQ_AUTH_AUTH);
    MEMCPY (a_u1Concatenated + PKT_REQA,
            a_u1RequestAuth, LEN_REQ_AUTH_AUTH);
    i4_seclen = STRLEN ((char *) p_u1Secret);
    MEMCPY (a_u1Concatenated + u2_pktlen, p_u1Secret, i4_seclen);
    *i4_Length = u2_pktlen + i4_seclen;
}

Я включил цикл for, чтобы проверить, не является ли полученный radiusPkt значением NULL, пока не будет получена необходимая длина для копирования значения поля «pktLen». Это не сработало, вместо этого выдало еще одно предупреждение, т.е. мертвый код. Об этом рассказано в коде.

Ожидаемые предупреждения TAINTED_SCALAR равны 0. Но из-за этой проблемы получено предупреждение TAINTED_SCALAR как 1.

Может кто-нибудь помочь мне избавиться от этого предупреждения TAINTED_SCALAR?

1 Ответ

0 голосов
/ 25 января 2019

Приведенные ниже изменения кода устраняют проблему и дают ожидаемый результат.

VOID func1 (UINT1 *p_u1RadiusReceivedPacket,
            UINT1 *p_u1Secret,
            UINT1 a_u1Concatenated[],
            INT4 *i4_Length)
{
    INT4                i4_seclen = 0;
    UINT2               u2_pktlen = 0;
    UINT1    a_u1RequestAuth[LEN_REQ_AUTH_AUTH] = {};
    INT4                        index = 0;

    if (p_u1RadiusReceivedPacket == NULL)
    {
        return;
    }

    MEMSET (a_u1Concatenated, 0, LEN_RX_PKT + LEN_SECRET);

    MEMCPY (&u2_pktlen, p_u1RadiusReceivedPacket + PKT_LEN, 2);

    u2_pktlen = OSIX_NTOHS (u2_pktlen);

    /* Validate the packet length to avoid buffer overflow, denial of service,
     * memory corruption, or other security vulnerability. */
    if ((u2_pktlen < 20) || (u2_pktlen > 4096))
    {
        return;
    }

    MEMCPY (a_u1Concatenated, p_u1RadiusReceivedPacket, u2_pktlen);
    MEMCPY (a_u1RequestAuth, p_u1RadiusReceivedPacket + PKT_REQA,
            LEN_REQ_AUTH_AUTH);
    MEMCPY (a_u1Concatenated + PKT_REQA,
            a_u1RequestAuth, LEN_REQ_AUTH_AUTH);
    i4_seclen = STRLEN ((char *) p_u1Secret);
    MEMCPY (a_u1Concatenated + u2_pktlen, p_u1Secret, i4_seclen);
    *i4_Length = u2_pktlen + i4_seclen;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...