• ranshid's avatar
    Avoid using unsafe C functions (#10932) · eacca729
    ranshid authored
    replace use of:
    sprintf --> snprintf
    strcpy/strncpy  --> redis_strlcpy
    strcat/strncat  --> redis_strlcat
    
    **why are we making this change?**
    Much of the code uses some unsafe variants or deprecated buffer handling
    functions.
    While most cases are probably not presenting any issue on the known path
    programming errors and unterminated strings might lead to potential
    buffer overflows which are not covered by tests.
    
    **As part of this PR we change**
    1. added implementation for redis_strlcpy and redis_strlcat based on the strl implementation: https://linux.die.net/man/3/strl
    2. change all occurrences of use of sprintf with use of snprintf
    3. change occurrences of use of  strcpy/strncpy with redis_strlcpy
    4. change occurrences of use of strcat/strncat with redis_strlcat
    5. change the behavior of ll2string/ull2string/ld2string so that it will always place null
      termination ('\0') on the output buffer in the first index. this was done in order to make
      the use of these functions more safe in cases were the user will not check the output
      returned by them (for example in rdbRemoveTempFile)
    6. we added a compiler directive to issue a deprecation error in case a use of
      sprintf/strcpy/strcat is found during compilation which will result in error during compile time.
      However keep in mind that since the deprecation attribute is not supported on all compilers,
      this is expected to fail during push workflows.
    
    
    **NOTE:** while this is only an initial milestone. We might also consider
    using the *_s implementation provided by the C11 Extensions (however not
    yet widly supported). I would also suggest to start
    looking at static code analyzers to track unsafe use cases.
    For example LLVM clang checker supports security.insecureAPI.DeprecatedOrUnsafeBufferHandling
    which can help locate unsafe function usage.
    https://clang.llvm.org/docs/analyzer/checkers.html#security-insecureapi-deprecatedorunsafebufferhandling-c
    The main reason not to onboard it at this stage is that the alternative
    excepted by clang is to use the C11 extensions which are not always
    supported by stdlib.
    eacca729
util.c 35.2 KB