pm: fix issue that system crash when passed invalid relpath value

Signed-off-by: wanggang26 <wanggang26@xiaomi.com>
This commit is contained in:
wanggang26 2023-09-11 12:47:04 +08:00 committed by Xiang Xiao
parent b3973496cd
commit dcadd87919

View file

@ -114,6 +114,8 @@ static int pm_rewinddir(FAR struct fs_dirent_s *dir);
static int pm_stat(FAR const char *relpath, FAR struct stat *buf);
static int pm_get_file_index(FAR const char *relpath);
/****************************************************************************
* Public Data
****************************************************************************/
@ -159,6 +161,26 @@ static FAR const char *g_pm_state[PM_COUNT] =
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: pm_get_file_index
****************************************************************************/
static int pm_get_file_index(FAR const char *relpath)
{
int i;
for (i = 0; i < nitems(g_pm_files); i++)
{
if (strncmp(relpath, g_pm_files[i].name,
strlen(g_pm_files[i].name)) == 0)
{
return i;
}
}
return -1;
}
/****************************************************************************
* Name: pm_open
****************************************************************************/
@ -181,6 +203,13 @@ static int pm_open(FAR struct file *filep, FAR const char *relpath,
return -EACCES;
}
relpath += strlen("pm/");
i = pm_get_file_index(relpath);
if (i < 0)
{
return -ENOENT;
}
/* Allocate a container to hold the file attributes */
pmfile = kmm_zalloc(sizeof(struct pm_file_s));
@ -190,17 +219,7 @@ static int pm_open(FAR struct file *filep, FAR const char *relpath,
return -ENOMEM;
}
relpath += strlen("pm/");
for (i = 0; i < nitems(g_pm_files); i++)
{
if (strncmp(relpath, g_pm_files[i].name,
strlen(g_pm_files[i].name)) == 0)
{
pmfile->read = g_pm_files[i].read;
break;
}
}
pmfile->read = g_pm_files[i].read;
pmfile->domain = atoi(relpath + strlen(g_pm_files[i].name));
DEBUGASSERT(pmfile->read);
@ -555,6 +574,12 @@ static int pm_stat(FAR const char *relpath, FAR struct stat *buf)
}
else
{
relpath += strlen("pm/");
if (pm_get_file_index(relpath) < 0)
{
return -ENOENT;
}
buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
}