Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File System Service - Illegal Memory Access #34

Open
htcrane opened this issue Nov 16, 2023 · 2 comments
Open

File System Service - Illegal Memory Access #34

htcrane opened this issue Nov 16, 2023 · 2 comments

Comments

@htcrane
Copy link

htcrane commented Nov 16, 2023

TL;DR: FATFS_stat & FATFS_readdir of the file system interface make illegal memory accesses to a character array never allocated.

When the File System Service is configured to use FatFS, the SYS_FS_FileStat interface attempts an illegal memory access that causes a bus fault (specifically an imprecise bus error). This occurs due to char* lfname of the SYS_FS_FSTAT struct is not used according to documentation, but the sys_fs_fat_interface.c dereferences this uninitialized pointer and assigns a value to a random location. The offending code snippet is provided below. I would recommend setting the pointer to NULL instead fileStat->lfname = NULL as an easy fix.

int FATFS_stat (
    const char* path,   /* Pointer to the file path */
    uintptr_t fileInfo  /* Pointer to file information to return */
)
{
    FRESULT res;
    FILINFO *finfo = (FILINFO *)fileInfo;

    res = f_stat((const TCHAR *)path, finfo);

    if (finfo != NULL)
    {
        SYS_FS_FSTAT *fileStat = (SYS_FS_FSTAT *)fileInfo;

        if ((res == FR_OK) && (fileStat->lfname != NULL))
        {
            /* Use fileStat->fname instead */
            fileStat->lfname[0] = '\0';
        }
    }

    return ((int)res);
}

I see the FATFS_readdir() function in the interface makes similar illegal access as well.

For replication, one should test with calling on a directory or file that already exists. I am using a SAME53 that had this code generated using the latest MCC.

@htcrane
Copy link
Author

htcrane commented Nov 17, 2023

@vishalnxt, just tagging to notify.

@jigneshmoradiya1
Copy link
Contributor

@htcrane , Thanks for reporting this issue. This issue will be fixed in the upcoming core v3.14.0 release.

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

No branches or pull requests

2 participants