MiSTer-devel / Linux-Kernel_MiSTer

Other
13 stars 19 forks source link

Segmentation fault in exFAT #11

Closed mrsonicblue closed 3 years ago

mrsonicblue commented 3 years ago

A segmentation fault occurs when writing to a file in /media/fat using mmap. This is in code that worked before the recent kernel upgrade and which still works elsewhere on the filesystem. Since exFAT was ported in, I'm hoping you could review the issue.

Below is a sample application which forces the segmentation fault. The specific line it crashes on is memset.

#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    if(argc < 2)
    {
        printf("File path not mentioned\n");
        exit(0);
    }

    const char *filepath = argv[1];
    int fd = open(filepath, O_RDWR | O_CREAT);
    if (fd < 0)
    {
        printf("Could not open file\n");
        exit(1);
    }

    if (ftruncate(fd, 8192) != 0)
    {
        printf("Could not resize file\n");
        exit(1);
    }

    char *ptr = mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    if (ptr == MAP_FAILED)
    {
        printf("Mapping Failed\n");
        return 1;
    }

    memset(ptr, 0, 1);

    if (munmap(ptr, 8192) != 0)
    {
        printf("UnMapping Failed\n");
        return 1;
    }

    close(fd);

    return 0;
}

Works: ./sample /test.txt Works: ./sample /tmp/test.txt Crashes: ./sample /media/fat/test.txt

sorgelig commented 3 years ago

looks like serious problem... exception has no mention of any exFAT source files.. Not sure if i will be able to fix it.

sorgelig commented 3 years ago

try this build: kernel_mmap_exfat.zip since i did other changes, existing kernel modules won't work, so you need unpack modules to your linux.img according path in modules.tar.gz

mrsonicblue commented 3 years ago

try this build: kernel_mmap_exfat.zip since i did other changes, existing kernel modules won't work, so you need unpack modules to your linux.img according path in modules.tar.gz

I don't know what you did, but it seems to have completely fixed the problem!

Thank you for looking into this. Let me know if you need any additional testing.

sorgelig commented 3 years ago

Great! Test this version as well. Modules are same as above, just replace zImage_dtb: kernel_mmap_exfat2.zip I did cleanup from all those version control #if #else #endif because it's very hard to support. Some files have it in almost every other line.

dkseschneider commented 3 years ago

Great! Test this version as well. Modules are same as above, just replace zImage_dtb: kernel_mmap_exfat2.zip I did cleanup from all those version control #if #else #endif because it's very hard to support. Some files have it in almost every other line.

Gave it a try. Still works! Agreed: code written for a bunch of platforms can be a nightmare to read.

mrsonicblue commented 3 years ago

Confirmed that this is fixed on the latest release