c - 相当简单的 C 代码中的安全漏洞

标签 c security buffer-overflow

我正在为我的最后一次考试学习(是的!),并且遇到了一个我很难弄清楚的问题。这是一个旧的考试问题,您应该在其中找到至少两个可以在读取 ppm 图像文件的函数中被利用的漏洞。我能确定的唯一问题是,如果 cols 和/或 rows 被赋予了意外的值,太大(导致整数溢出)或负值,这会导致 img->raster 的大小不正确,从而开启了基于堆的可能性缓冲区溢出攻击。

据我所知,未经检查的 malloc 不应该被利用。

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 ", &version) < 1) || (version != 6)) {
        return NULL;
    }
    /* Read the image dimensions and color depth from the file */
    if (fscanf(fp, " %d %d %d ", &cols, &rows, &maxval) < 3) {
        return NULL;
    }
    /* Calculate some sizes */
    pixBytes = (maxval > 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->rows = rows;
    img->cols = cols; 
    img->depth = (maxval > 255) ? 2 : 1;
    img->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->raster;
    /* Iterate over the rows in the file */
    while (rows--) {
        /* Iterate over the columns in the file */
        cols = img->cols;
        while (cols--) {
            /* Try to read a single pixel from the file */
            if (fread(p, pixBytes, 1, fp) < 1) {
                /* If the read fails, free memory and return */
                free(img->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;
}

原文(最后一题):http://www.ida.liu.se/~TDDC90/exam/old/TDDC90%20TEN1%202009-12-22.pdf

感谢您的帮助。

最佳答案

创建一个大文件,以便读取 rowcols 都是负数。 rasterBytes = pixBytes * rows * cols正数 所以一切都会好起来直到 p = img->raster;。但是此时你有两个无限循环,程序可能会覆盖堆。

另一种攻击是设置rowcols,使它们具有不同的符号。您可以选择任一值为 -1,而另一个值足够大以读取您想要的数据。分配

  img->raster = (void*)malloc(rasterBytes);

将失败,导致 img->raster 指向 NULL。这意味着

 fread(p, pixBytes, 1, fp) < 1

会尝试将文件内容读入内核内存。如果这段代码在内核模式下执行,取决于系统(比如不使用内存段的旧 unix),那么您将用文件的内容覆盖内核内存的内容。不使用内存段的内核不依赖于段错误,而是依赖于页面错误(没有分配任何实际页面的虚拟地址)。问题在于虚拟内存设计使得第一个实际页面直接分配给内核页面。即内核虚拟地址 0x0 对应于 0x0 处的实际内存并且完全有效(在内核内)。

编辑:在这两种情况下,攻击者的目标是将输入文件的内容(完全在他的控制之下)注入(inject)他不应该访问的内存区域到,同时不能修改函数 read_ppm()

关于c - 相当简单的 C 代码中的安全漏洞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13991018/

相关文章:

c - 在函数中分配结构体数组

我可以在VS2013中正式使用free()吗?

c - 如何查看子进程的/proc/信息?

php - 在 Web 服务器中存储文件的安全方法?

c - 缓冲区溢出的 C 代码问题

c++ - memcpy 标记为缓冲区溢出

c - 函数头说明

java - 有效的 Java 项目 76 : Serialization & Security - How exactly hacker gets references to the internal Date fields of immutable Period object?

sql-server - 跨数据库边界的 SQL Server 外键 - 强制执行技术

c - 导致 StrCmp 返回 0 的非 Null 终止值?