Skip to content

Buffer bounds #22

Closed
Closed

Description

@luben

I understand that the project is in experimental stage and I am not expecting to be bugs-free. So here is one bug.

Sometimes the destination buffer bounds are not checked properly (when compressing/decompressing) and overflows happens that could lead to a lot of nasty thing. Here is a code that demonstrates it:

#include <stdio.h>
#include <stdlib.h>
#include <zstd.h>

int main(int argc, char **argv ) {
    char *raw = (char *)malloc(1000);
    char compressed[20];
    char decompressed[900];
    size_t i, ccode, dcode;

    // fill it with ones
    for (i=0; i<1000; i++) raw[i] = 1;

    ccode = ZSTD_compress(compressed, 20, raw, 1000);
    printf("Compression code %i\n", ccode);
    dcode = ZSTD_decompress(decompressed, 900, compressed, ccode);
    printf("Decompression code %i\n", dcode);
    return 0;
}

Regards

Activity

  1. Cyan4973 commented on Feb 20, 2015

    @Cyan4973
    Contributor

    Hi Luben

    Yes that's correct. Zstd is not yet protected against buffer overflow, hence not ready for production usage.

    The first error surprised me : ZSTD_compress() is supposed to be protected against overflow, because it requires maxDstSize >= ZSTD_compressBound(rawSize).
    But looking at the code, it seems this protection was removed during one of the refactoring exercise. Not a huge deal, since this protection is supposed to be temporary, but as long as the definitive solution is not avaiable, it should stay around, so I'll update the code to re-instate that condition.

    ZSTD_decompress(), on the other hand, is more difficult to protect. So it will require a bit more time to get there.

    Best Regards

  2. Cyan4973 commented on Jul 7, 2015

    @Cyan4973
    Contributor

    Latest version in "dev" branch seems to correctly prevent buffer overflow during compression.
    On the other hand, decompression side still needs to be done.

    For the record, here is my slightly modified version of your very useful test program :

    #include <stdio.h>
    #include <stdlib.h>
    #include "zstd.h"
    
    #define SAMPLE_SIZE 1000
    #define CBUFF_SIZE (ZSTD_compressBound(SAMPLE_SIZE)) 
    #define DBUFF_SIZE (SAMPLE_SIZE-100)
    int main(int argc, char **argv ) {
        char *raw = (char *)malloc(SAMPLE_SIZE);
        char compressed[CBUFF_SIZE];
        char decompressed[DBUFF_SIZE];
        size_t i, ccode, dcode;
    
        // fill it with ones
        for (i=0; i<1000; i++) raw[i] = 1;
    
        ccode = ZSTD_compress(compressed, 20, raw, SAMPLE_SIZE);
        printf("Max 20 : Compression code %zu\n", ccode);   /* will correctly return an error code */
        ccode = ZSTD_compress(compressed, CBUFF_SIZE, raw, SAMPLE_SIZE);
        printf("Max CBUFF_SIZE : Compression code %zu\n", ccode);
        dcode = ZSTD_decompress(decompressed, DBUFF_SIZE, compressed, ccode);   /* will smash the stack */
        printf("Decompression code %zu\n", dcode);
        return 0;
    }
    
  3. Cyan4973 commented on Jul 7, 2015

    @Cyan4973
    Contributor

    By the way, the decoding side of this issue seems technically the same as #11. Maybe we should fold both issues into one.

  4. Cyan4973 commented on Jul 8, 2015

    @Cyan4973
    Contributor

    Merged this issue into #11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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