Blame lib/checkbufferoverrun.h

Packit 2035a7
/*
Packit 2035a7
 * Cppcheck - A tool for static C/C++ code analysis
Packit 2035a7
 * Copyright (C) 2007-2018 Cppcheck team.
Packit 2035a7
 *
Packit 2035a7
 * This program is free software: you can redistribute it and/or modify
Packit 2035a7
 * it under the terms of the GNU General Public License as published by
Packit 2035a7
 * the Free Software Foundation, either version 3 of the License, or
Packit 2035a7
 * (at your option) any later version.
Packit 2035a7
 *
Packit 2035a7
 * This program is distributed in the hope that it will be useful,
Packit 2035a7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 2035a7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 2035a7
 * GNU General Public License for more details.
Packit 2035a7
 *
Packit 2035a7
 * You should have received a copy of the GNU General Public License
Packit 2035a7
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 2035a7
 */
Packit 2035a7
Packit 2035a7
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
#ifndef checkbufferoverrunH
Packit 2035a7
#define checkbufferoverrunH
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
Packit 2035a7
#include "check.h"
Packit 2035a7
#include "config.h"
Packit 2035a7
#include "errorlogger.h"
Packit 2035a7
#include "mathlib.h"
Packit 2035a7
#include "tokenize.h"
Packit 2035a7
Packit 2035a7
#include <cstddef>
Packit 2035a7
#include <list>
Packit 2035a7
#include <map>
Packit 2035a7
#include <string>
Packit 2035a7
#include <vector>
Packit 2035a7
Packit 2035a7
class Settings;
Packit 2035a7
class SymbolDatabase;
Packit 2035a7
class Token;
Packit 2035a7
namespace ValueFlow {
Packit 2035a7
    class Value;
Packit 2035a7
}  // namespace ValueFlow
Packit 2035a7
namespace tinyxml2 {
Packit 2035a7
    class XMLElement;
Packit 2035a7
}  // namespace tinyxml2
Packit 2035a7
Packit 2035a7
// CWE ids used
Packit 2035a7
static const struct CWE CWE119(119U); // Improper Restriction of Operations within the Bounds of a Memory Buffer
Packit 2035a7
Packit 2035a7
class Variable;
Packit 2035a7
Packit 2035a7
/// @addtogroup Checks
Packit 2035a7
/// @{
Packit 2035a7
Packit 2035a7
/**
Packit 2035a7
 * @brief buffer overruns and array index out of bounds
Packit 2035a7
 *
Packit 2035a7
 * Buffer overrun and array index out of bounds are pretty much the same.
Packit 2035a7
 * But I generally use 'array index' if the code contains []. And the given
Packit 2035a7
 * index is out of bounds.
Packit 2035a7
 * I generally use 'buffer overrun' if you for example call a strcpy or
Packit 2035a7
 * other function and pass a buffer and reads or writes too much data.
Packit 2035a7
 */
Packit 2035a7
class CPPCHECKLIB CheckBufferOverrun : public Check {
Packit 2035a7
public:
Packit 2035a7
Packit 2035a7
    /** This constructor is used when registering the CheckClass */
Packit 2035a7
    CheckBufferOverrun() : Check(myName()), symbolDatabase(nullptr) {
Packit 2035a7
    }
Packit 2035a7
Packit 2035a7
    /** This constructor is used when running checks. */
Packit 2035a7
    CheckBufferOverrun(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
Packit 2035a7
        : Check(myName(), tokenizer, settings, errorLogger)
Packit 2035a7
        , symbolDatabase(tokenizer?tokenizer->getSymbolDatabase():nullptr) {
Packit 2035a7
    }
Packit 2035a7
Packit 2035a7
    void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
Packit 2035a7
        CheckBufferOverrun checkBufferOverrun(tokenizer, settings, errorLogger);
Packit 2035a7
        checkBufferOverrun.checkGlobalAndLocalVariable();
Packit 2035a7
        if (tokenizer && tokenizer->isMaxTime())
Packit 2035a7
            return;
Packit 2035a7
        checkBufferOverrun.checkStructVariable();
Packit 2035a7
        checkBufferOverrun.checkBufferAllocatedWithStrlen();
Packit 2035a7
        checkBufferOverrun.checkInsecureCmdLineArgs();
Packit 2035a7
        checkBufferOverrun.arrayIndexThenCheck();
Packit 2035a7
        checkBufferOverrun.negativeArraySize();
Packit 2035a7
    }
Packit 2035a7
Packit 2035a7
    void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
Packit 2035a7
        CheckBufferOverrun checkBufferOverrun(tokenizer, settings, errorLogger);
Packit 2035a7
        checkBufferOverrun.bufferOverrun();
Packit 2035a7
        checkBufferOverrun.checkStringArgument();
Packit 2035a7
    }
Packit 2035a7
Packit 2035a7
    /** @brief %Check for buffer overruns (single pass, use ast and valueflow) */
Packit 2035a7
    void bufferOverrun();
Packit 2035a7
Packit 2035a7
    /** @brief Using array index before bounds check */
Packit 2035a7
    void arrayIndexThenCheck();
Packit 2035a7
Packit 2035a7
    /** @brief negative size for array */
Packit 2035a7
    void negativeArraySize();
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * @brief Get minimum length of format string result
Packit 2035a7
     * @param input_string format string
Packit 2035a7
     * @param parameters given parameters to sprintf
Packit 2035a7
     * @return minimum length of resulting string
Packit 2035a7
     */
Packit 2035a7
    static MathLib::biguint countSprintfLength(const std::string &input_string, const std::list<const Token*> &parameters);
Packit 2035a7
Packit 2035a7
    /** Check for buffer overruns - locate struct variables and check them with the .._CheckScope function */
Packit 2035a7
    void checkStructVariable();
Packit 2035a7
Packit 2035a7
    /** Check for buffer overruns - locate global variables and local function variables and check them with the checkScope function */
Packit 2035a7
    void checkGlobalAndLocalVariable();
Packit 2035a7
Packit 2035a7
    /** Check for buffer overruns due to allocating strlen(src) bytes instead of (strlen(src)+1) bytes before copying a string */
Packit 2035a7
    void checkBufferAllocatedWithStrlen();
Packit 2035a7
Packit 2035a7
    /** Check string argument buffer overruns */
Packit 2035a7
    void checkStringArgument();
Packit 2035a7
Packit 2035a7
    /** Check for buffer overruns due to copying command-line args to fixed-sized buffers without bounds checking */
Packit 2035a7
    void checkInsecureCmdLineArgs();
Packit 2035a7
Packit 2035a7
    /** Information about N-dimensional array */
Packit 2035a7
    class CPPCHECKLIB ArrayInfo {
Packit 2035a7
    private:
Packit 2035a7
        /** number of elements of array */
Packit 2035a7
        std::vector<MathLib::bigint> _num;
Packit 2035a7
Packit 2035a7
        /** full name of variable as pattern */
Packit 2035a7
        std::string _varname;
Packit 2035a7
Packit 2035a7
        /** size of each element in array */
Packit 2035a7
        MathLib::bigint _element_size;
Packit 2035a7
Packit 2035a7
        /** declaration id */
Packit 2035a7
        unsigned int _declarationId;
Packit 2035a7
Packit 2035a7
    public:
Packit 2035a7
        ArrayInfo();
Packit 2035a7
        ArrayInfo(const Variable *var, const SymbolDatabase *symbolDatabase, const unsigned int forcedeclid = 0);
Packit 2035a7
Packit 2035a7
        /**
Packit 2035a7
         * Create array info with specified data
Packit 2035a7
         * The intention is that this is only a temporary solution.. all
Packit 2035a7
         * checking should be based on ArrayInfo from the start and then
Packit 2035a7
         * this will not be needed as the declare can be used instead.
Packit 2035a7
         */
Packit 2035a7
        ArrayInfo(unsigned int id, const std::string &name, MathLib::bigint size1, MathLib::bigint n);
Packit 2035a7
Packit 2035a7
        /** Create a copy ArrayInfo where the number of elements have been limited by a value */
Packit 2035a7
        ArrayInfo limit(MathLib::bigint value) const;
Packit 2035a7
Packit 2035a7
        /** array sizes */
Packit 2035a7
        const std::vector<MathLib::bigint> &num() const {
Packit 2035a7
            return _num;
Packit 2035a7
        }
Packit 2035a7
Packit 2035a7
        /** array size */
Packit 2035a7
        MathLib::bigint num(std::size_t index) const {
Packit 2035a7
            return _num[index];
Packit 2035a7
        }
Packit 2035a7
        void num(std::size_t index, MathLib::bigint number) {
Packit 2035a7
            _num[index] = number;
Packit 2035a7
        }
Packit 2035a7
Packit 2035a7
        /** size of each element */
Packit 2035a7
        MathLib::bigint element_size() const {
Packit 2035a7
            return _element_size;
Packit 2035a7
        }
Packit 2035a7
Packit 2035a7
        /** Variable name */
Packit 2035a7
        unsigned int declarationId() const {
Packit 2035a7
            return _declarationId;
Packit 2035a7
        }
Packit 2035a7
        void declarationId(unsigned int id) {
Packit 2035a7
            _declarationId = id;
Packit 2035a7
        }
Packit 2035a7
Packit 2035a7
        /** Variable name */
Packit 2035a7
        const std::string &varname() const {
Packit 2035a7
            return _varname;
Packit 2035a7
        }
Packit 2035a7
        void varname(const std::string &name) {
Packit 2035a7
            _varname = name;
Packit 2035a7
        }
Packit 2035a7
Packit 2035a7
        MathLib::bigint numberOfElements() const;
Packit 2035a7
        MathLib::bigint totalIndex(const std::vector<ValueFlow::Value> &indexes) const;
Packit 2035a7
    };
Packit 2035a7
Packit 2035a7
    /** Check for buffer overruns (based on ArrayInfo) */
Packit 2035a7
    void checkScope(const Token *tok, const ArrayInfo &arrayInfo);
Packit 2035a7
    void checkScope(const Token *tok, std::map<unsigned int, ArrayInfo> arrayInfos);
Packit 2035a7
    void checkScope_inner(const Token *tok, const ArrayInfo &arrayInfo);
Packit 2035a7
Packit 2035a7
    /** Check for buffer overruns */
Packit 2035a7
    void checkScope(const Token *tok, const std::vector<const std::string*> &varname, const ArrayInfo &arrayInfo);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * Helper function for checkFunctionCall - check a function parameter
Packit 2035a7
     * \param ftok token for the function name
Packit 2035a7
     * \param paramIndex on what parameter is the array used
Packit 2035a7
     * \param arrayInfo the array information
Packit 2035a7
     * \param callstack call stack. This is used to prevent recursion and to provide better error messages. Pass a empty list from checkScope etc.
Packit 2035a7
     */
Packit 2035a7
    void checkFunctionParameter(const Token &ftok, const unsigned int paramIndex, const ArrayInfo &arrayInfo, const std::list<const Token *>& callstack);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * Helper function that checks if the array is used and if so calls the checkFunctionCall
Packit 2035a7
     * @param tok token that matches "%name% ("
Packit 2035a7
     * @param arrayInfo the array information
Packit 2035a7
     * \param callstack call stack. This is used to prevent recursion and to provide better error messages. Pass a empty list from checkScope etc.
Packit 2035a7
     */
Packit 2035a7
    void checkFunctionCall(const Token *tok, const ArrayInfo &arrayInfo, std::list<const Token *> callstack);
Packit 2035a7
Packit 2035a7
    void arrayIndexOutOfBoundsError(const Token *tok, const ArrayInfo &arrayInfo, const std::vector<MathLib::bigint> &index);
Packit 2035a7
    void arrayIndexOutOfBoundsError(const Token *tok, const ArrayInfo &arrayInfo, const std::vector<ValueFlow::Value> &index);
Packit 2035a7
Packit 2035a7
    /* data for multifile checking */
Packit 2035a7
    class MyFileInfo : public Check::FileInfo {
Packit 2035a7
    public:
Packit 2035a7
        std::string toString() const;
Packit 2035a7
Packit 2035a7
        struct ArrayUsage {
Packit 2035a7
            MathLib::bigint   index;
Packit 2035a7
            std::string       fileName;
Packit 2035a7
            unsigned int      linenr;
Packit 2035a7
        };
Packit 2035a7
Packit 2035a7
        /* key:arrayName */
Packit 2035a7
        std::map<std::string, ArrayUsage> arrayUsage;
Packit 2035a7
Packit 2035a7
        /* key:arrayName, data:arraySize */
Packit 2035a7
        std::map<std::string, MathLib::bigint>  arraySize;
Packit 2035a7
    };
Packit 2035a7
Packit 2035a7
    /** @brief Parse current TU and extract file info */
Packit 2035a7
    Check::FileInfo *getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const;
Packit 2035a7
Packit 2035a7
    Check::FileInfo * loadFileInfoFromXml(const tinyxml2::XMLElement *xmlElement) const;
Packit 2035a7
Packit 2035a7
    /** @brief Analyse all file infos for all TU */
Packit 2035a7
    bool analyseWholeProgram(const std::list<Check::FileInfo*> &fileInfo, const Settings& settings, ErrorLogger &errorLogger);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * Calculates sizeof value for given type.
Packit 2035a7
     * @param type Token which will contain e.g. "int", "*", or string.
Packit 2035a7
     * @return sizeof for given type, or 0 if it can't be calculated.
Packit 2035a7
     */
Packit 2035a7
    unsigned int sizeOfType(const Token *type) const;
Packit 2035a7
Packit 2035a7
private:
Packit 2035a7
    const SymbolDatabase *symbolDatabase;
Packit 2035a7
Packit 2035a7
    static bool isArrayOfStruct(const Token* tok, int &position);
Packit 2035a7
    void arrayIndexOutOfBoundsError(const std::list<const Token *> &callstack, const ArrayInfo &arrayInfo, const std::vector<MathLib::bigint> &index);
Packit 2035a7
    void bufferOverrunError(const Token *tok, const std::string &varnames = emptyString);
Packit 2035a7
    void bufferOverrunError(const std::list<const Token *> &callstack, const std::string &varnames = emptyString);
Packit 2035a7
    void strncatUsageError(const Token *tok);
Packit 2035a7
    void negativeMemoryAllocationSizeError(const Token *tok); // provide a negative value to memory allocation function
Packit 2035a7
    void negativeArraySizeError(const Token *tok);
Packit 2035a7
    void outOfBoundsError(const Token *tok, const std::string &what, const bool show_size_info, const MathLib::bigint &supplied_size, const MathLib::bigint &actual_size);
Packit 2035a7
    void sizeArgumentAsCharError(const Token *tok);
Packit 2035a7
    void terminateStrncpyError(const Token *tok, const std::string &varname);
Packit 2035a7
    void bufferNotZeroTerminatedError(const Token *tok, const std::string &varname, const std::string &function);
Packit 2035a7
    void negativeIndexError(const Token *tok, MathLib::bigint index);
Packit 2035a7
    void negativeIndexError(const Token *tok, const ValueFlow::Value &index);
Packit 2035a7
    void cmdLineArgsError(const Token *tok);
Packit 2035a7
    void pointerOutOfBoundsError(const Token *tok, const Token *index=nullptr, const MathLib::bigint indexvalue=0);
Packit 2035a7
    void arrayIndexThenCheckError(const Token *tok, const std::string &indexName);
Packit 2035a7
    void possibleBufferOverrunError(const Token *tok, const std::string &src, const std::string &dst, bool cat);
Packit 2035a7
    void argumentSizeError(const Token *tok, const std::string &functionName, const std::string &varname);
Packit 2035a7
Packit 2035a7
    void valueFlowCheckArrayIndex(const Token * const tok, const ArrayInfo &arrayInfo);
Packit 2035a7
Packit 2035a7
public:
Packit 2035a7
    void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
Packit 2035a7
        CheckBufferOverrun c(nullptr, settings, errorLogger);
Packit 2035a7
        const std::vector<MathLib::bigint> indexes(2, 1);
Packit 2035a7
        c.arrayIndexOutOfBoundsError(nullptr, ArrayInfo(0, "array", 1, 2), indexes);
Packit 2035a7
        c.bufferOverrunError(nullptr, std::string("buffer"));
Packit 2035a7
        c.strncatUsageError(nullptr);
Packit 2035a7
        c.outOfBoundsError(nullptr, "index", true, 2, 1);
Packit 2035a7
        c.sizeArgumentAsCharError(nullptr);
Packit 2035a7
        c.terminateStrncpyError(nullptr, "buffer");
Packit 2035a7
        c.bufferNotZeroTerminatedError(nullptr, "buffer", "strncpy");
Packit 2035a7
        c.negativeIndexError(nullptr, -1);
Packit 2035a7
        c.cmdLineArgsError(nullptr);
Packit 2035a7
        c.pointerOutOfBoundsError(nullptr, nullptr, 0);
Packit 2035a7
        c.arrayIndexThenCheckError(nullptr, "index");
Packit 2035a7
        c.possibleBufferOverrunError(nullptr, "source", "destination", false);
Packit 2035a7
        c.argumentSizeError(nullptr, "function", "array");
Packit 2035a7
        c.negativeMemoryAllocationSizeError(nullptr);
Packit 2035a7
        c.negativeArraySizeError(nullptr);
Packit 2035a7
        c.reportError(nullptr, Severity::warning, "arrayIndexOutOfBoundsCond", "Array 'x[10]' accessed at index 20, which is out of bounds. Otherwise condition 'y==20' is redundant.", CWE119, false);
Packit 2035a7
    }
Packit 2035a7
private:
Packit 2035a7
Packit 2035a7
    static std::string myName() {
Packit 2035a7
        return "Bounds checking";
Packit 2035a7
    }
Packit 2035a7
Packit 2035a7
    std::string classInfo() const {
Packit 2035a7
        return "Out of bounds checking:\n"
Packit 2035a7
               "- Array index out of bounds detection by value flow analysis\n"
Packit 2035a7
               "- Dangerous usage of strncat()\n"
Packit 2035a7
               "- char constant passed as size to function like memset()\n"
Packit 2035a7
               "- strncpy() leaving string unterminated\n"
Packit 2035a7
               "- Accessing array with negative index\n"
Packit 2035a7
               "- Unsafe usage of main(argv, argc) arguments\n"
Packit 2035a7
               "- Accessing array with index variable before checking its value\n"
Packit 2035a7
               "- Check for large enough arrays being passed to functions\n"
Packit 2035a7
               "- Allocating memory with a negative size\n";
Packit 2035a7
    }
Packit 2035a7
};
Packit 2035a7
/// @}
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
#endif // checkbufferoverrunH