diff --git a/components/fs/fatfs/fatfs.c b/components/fs/fatfs/fatfs.c index 9aab47af2b3279084b9f33eb1498715c6ea9b628..48b82038340b0b33352ffa7118f7e4572e34d1be 100644 --- a/components/fs/fatfs/fatfs.c +++ b/components/fs/fatfs/fatfs.c @@ -295,6 +295,11 @@ int FatfsUmount(struct MountPoint *mp) } volId = GetPartIdByPartName(mp->mDev); + if ((volId < 0) || (volId >= MAX_PARTITION_NUM)) { + errno = EINVAL; + return (int)LOS_NOK; + } + /* umount is not allowed when a file or directory is opened. */ if (f_checkopenlock(volId) != FR_OK) { errno = EBUSY; @@ -531,16 +536,13 @@ int FatfsUnlink(struct MountPoint *mp, const char *path) FRESULT res; int ret; + (void)mp; + if (path == NULL) { errno = EFAULT; return (int)LOS_NOK; } - if (!mp->mWriteEnable) { - errno = EACCES; - return (int)LOS_NOK; - } - ret = FsChangeDrive(path); if (ret != (int)LOS_OK) { PRINT_ERR("FAT unlink ChangeDrive err 0x%x!\r\n", ret); @@ -564,6 +566,8 @@ int FatfsStat(struct MountPoint *mp, const char *path, struct stat *buf) FILINFO fileInfo = {0}; int ret; + (void)mp; + if ((path == NULL) || (buf == NULL)) { errno = EFAULT; return (int)LOS_NOK; @@ -625,16 +629,13 @@ int FatfsMkdir(struct MountPoint *mp, const char *path) FRESULT res; int ret; + (void)mp; + if (path == NULL) { errno = EFAULT; return (int)LOS_NOK; } - if (!mp->mWriteEnable) { - errno = EACCES; - return (int)LOS_NOK; - } - ret = FsChangeDrive(path); if (ret != (int)LOS_OK) { PRINT_ERR("FAT mkdir ChangeDrive err 0x%x!\r\n", ret); @@ -695,6 +696,7 @@ int FatfsReaddir(struct Dir *dir, struct dirent *dent) FRESULT res; FILINFO fileInfo = {0}; DIR *dp = NULL; + errno_t ret; if ((dir == NULL) || (dir->dData == NULL)) { errno = EBADF; @@ -710,8 +712,13 @@ int FatfsReaddir(struct Dir *dir, struct dirent *dent) return (int)LOS_NOK; } - (void)memcpy_s(dent->d_name, sizeof(dent->d_name), - fileInfo.fname, sizeof(dent->d_name)); + ret = memcpy_s(dent->d_name, sizeof(dent->d_name), + fileInfo.fname, sizeof(fileInfo.fname)); + if (ret != EOK) { + errno = EFAULT; + return LOS_NOK; + } + if (fileInfo.fattrib & AM_DIR) { dent->d_type = DT_DIR; } else { @@ -750,13 +757,10 @@ int FatfsRmdir(struct MountPoint *mp, const char *path) FRESULT res; int ret; - if ((path == NULL) || (mp == NULL)) { - errno = EFAULT; - return (int)LOS_NOK; - } + (void)mp; - if (!mp->mWriteEnable) { - errno = EACCES; + if (path == NULL) { + errno = EFAULT; return (int)LOS_NOK; } @@ -782,16 +786,13 @@ int FatfsRename(struct MountPoint *mp, const char *oldName, const char *newName) FRESULT res; int ret; + (void)mp; + if ((oldName == NULL) || (newName == NULL)) { errno = EFAULT; return (int)LOS_NOK; } - if (!mp->mWriteEnable) { - errno = EACCES; - return (int)LOS_NOK; - } - ret = FsChangeDrive(oldName); if (ret != (int)LOS_OK) { PRINT_ERR("FAT f_getfree ChangeDrive err 0x%x!\r\n", ret); @@ -907,13 +908,13 @@ int FatfsFdisk(const char *dev, int *partTbl, int arrayNum) int pdrv; FRESULT res; - if ((dev == NULL) || (partTbl == NULL)) { + if ((dev == NULL) || (partTbl == NULL) || (arrayNum < MAX_PARTITION_NUM)) { errno = EFAULT; return (int)LOS_NOK; } pdrv = GetDevIdByDevName(dev); - if (pdrv < 0) { + if ((pdrv < 0) || (pdrv >= MAX_PARTITION_NUM)) { errno = EFAULT; return (int)LOS_NOK; } diff --git a/components/fs/vfs/vfs_fs.c b/components/fs/vfs/vfs_fs.c index c97c052b33259ece8d18925415e3f5b4983c522c..7b49db58cafec782c8ad4fd43f9c36616ac5397a 100644 --- a/components/fs/vfs/vfs_fs.c +++ b/components/fs/vfs/vfs_fs.c @@ -425,17 +425,25 @@ static ssize_t VfsWrite(int fd, const void *buff, size_t bytes) return ret; } + if (!file->fMp->mWriteEnable) { + VFS_ERRNO_SET(EROFS); + goto OUT; + } + if ((file->fFlags & O_ACCMODE) == O_RDONLY) { VFS_ERRNO_SET(EACCES); - } else if ((file->fFops != NULL) && (file->fFops->write != NULL)) { - ret = file->fFops->write(file, buff, bytes); - } else { + goto OUT; + } + + if ((file->fFops == NULL) && (file->fFops->write == NULL)) { VFS_ERRNO_SET(ENOTSUP); + goto OUT; } + ret = file->fFops->write(file, buff, bytes); +OUT: /* else ret will be -1 */ VfsDetachFile(file); - return ret; } @@ -775,6 +783,12 @@ int unlink(const char *path) return MapToPosixRet(ret); } + if (!mp->mWriteEnable) { + VFS_ERRNO_SET(EROFS); + LOS_FsUnlock(); + return MapToPosixRet(ret); + } + ret = mp->mFs->fsFops->unlink(mp, pathInMp); LOS_FsUnlock(); @@ -830,6 +844,12 @@ int rename(const char *oldpath, const char *newpath) return MapToPosixRet(ret); } + if (!mpOld->mWriteEnable) { + VFS_ERRNO_SET(EROFS); + LOS_FsUnlock(); + return MapToPosixRet(ret); + } + if (mpOld->mFs->fsFops->rename != NULL) { ret = mpOld->mFs->fsFops->rename(mpOld, pathInMpOld, pathInMpNew); } else { @@ -1017,6 +1037,12 @@ int mkdir(const char *path, mode_t mode) return MapToPosixRet(ret); } + if (!mp->mWriteEnable) { + VFS_ERRNO_SET(EROFS); + LOS_FsUnlock(); + return MapToPosixRet(ret); + } + if (mp->mFs->fsFops->mkdir != NULL) { ret = mp->mFs->fsFops->mkdir(mp, pathInMp); } else { @@ -1051,6 +1077,12 @@ int rmdir(const char *path) return MapToPosixRet(ret); } + if (!mp->mWriteEnable) { + VFS_ERRNO_SET(EROFS); + LOS_FsUnlock(); + return MapToPosixRet(ret); + } + ret = mp->mFs->fsFops->rmdir(mp, pathInMp); LOS_FsUnlock();