From 990ec14cef514ad18caecd92dae1958e66cb2790 Mon Sep 17 00:00:00 2001 From: wangchen <253227059@qq.com> Date: Tue, 17 Jan 2023 12:52:55 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=96=87=E4=BB=B6=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E4=B8=AD=E6=A3=80=E8=A7=86=E6=84=8F=E8=A7=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 方案描述: 1, 只读mount点的处理修改到vfs侧, 同时删除之前在fat里对应的处理 2 , p字符匹配产生的数组下标判断增加 3, fatfs memcpy参数问题修改 Close #I690WH Signed-off-by: wangchen --- components/fs/fatfs/fatfs.c | 51 +++++++++++++++++++------------------ components/fs/vfs/vfs_fs.c | 40 ++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 29 deletions(-) diff --git a/components/fs/fatfs/fatfs.c b/components/fs/fatfs/fatfs.c index 9aab47af..48b82038 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 c97c052b..7b49db58 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(); -- Gitee