Note that there are some explanatory texts on larger screens.

plurals
  1. POSecurity vulnerabilities in fairly simple c code
    primarykey
    data
    text
    <p>I'm studying for my last ever exam (yey!), and have run into a problem I have a hard time figuring out. It's an old exam question where you are supposed to find at least two vulnerabilities that can be exploited in a function which reads a ppm image file. The only issue I can identify is if cols and/or rows are given unexpected values, either too large (causing integer overflow) or negative, which leads to img->raster having an incorrect size, opening up the possibility of a heap-based buffer-overflow attack.</p> <p>As far as I can reason, the unchecked malloc should not be exploitable.</p> <pre><code>struct image *read_ppm(FILE *fp) { int version; int rows, cols, maxval; int pixBytes=0, rowBytes=0, rasterBytes; uint8_t *p; struct image *img; /* Read the magic number from the file */ if ((fscanf(fp, " P%d ", &amp;version) &lt; 1) || (version != 6)) { return NULL; } /* Read the image dimensions and color depth from the file */ if (fscanf(fp, " %d %d %d ", &amp;cols, &amp;rows, &amp;maxval) &lt; 3) { return NULL; } /* Calculate some sizes */ pixBytes = (maxval &gt; 255) ? 6 : 3; // Bytes per pixel rowBytes = pixBytes * cols; // Bytes per row rasterBytes = rowBytes * rows; // Bytes for the whole image /* Allocate the image structure and initialize its fields */ img = malloc(sizeof(*img)); if (img == NULL) return NULL; img-&gt;rows = rows; img-&gt;cols = cols; img-&gt;depth = (maxval &gt; 255) ? 2 : 1; img-&gt;raster = (void*)malloc(rasterBytes); /* Get a pointer to the first pixel in the raster data. */ /* It is to this pointer that all image data will be written. */ p = img-&gt;raster; /* Iterate over the rows in the file */ while (rows--) { /* Iterate over the columns in the file */ cols = img-&gt;cols; while (cols--) { /* Try to read a single pixel from the file */ if (fread(p, pixBytes, 1, fp) &lt; 1) { /* If the read fails, free memory and return */ free(img-&gt;raster); free(img); return NULL; } /* Advance the pointer to the next location to which we should read a single pixel. */ p += pixBytes; } } /* Return the image */ return img; } </code></pre> <p>Original (the last question): <a href="http://www.ida.liu.se/~TDDC90/exam/old/TDDC90%20TEN1%202009-12-22.pdf" rel="noreferrer">http://www.ida.liu.se/~TDDC90/exam/old/TDDC90%20TEN1%202009-12-22.pdf</a></p> <p>Thanks for any help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload