Skip to content

Commit

Permalink
pm: fix issue that system crash when passed invalid relpath value
Browse files Browse the repository at this point in the history
Signed-off-by: wanggang26 <[email protected]>
  • Loading branch information
wanggang26 committed Sep 11, 2023
1 parent 6367f24 commit fbe7827
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions drivers/power/pm/pm_procfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_path_validate(FAR const char *relpath);

/****************************************************************************
* Public Data
****************************************************************************/
Expand Down Expand Up @@ -159,6 +161,23 @@ static FAR const char *g_pm_state[PM_COUNT] =
* Private Functions
****************************************************************************/

static int pm_path_validate(FAR const char *relpath)
{
int index = -1;

for (int i = 0; i < nitems(g_pm_files); i++)
{
if (strncmp(relpath, g_pm_files[i].name,
strlen(g_pm_files[i].name)) == 0)
{
index = i;
break;
}
}

return index;
}

/****************************************************************************
* Name: pm_open
****************************************************************************/
Expand Down Expand Up @@ -191,6 +210,11 @@ static int pm_open(FAR struct file *filep, FAR const char *relpath,
}

relpath += strlen("pm/");
if (pm_path_validate(relpath) < 0)
{
return -ENOENT;
}

for (i = 0; i < nitems(g_pm_files); i++)
{
if (strncmp(relpath, g_pm_files[i].name,
Expand Down Expand Up @@ -555,6 +579,12 @@ static int pm_stat(FAR const char *relpath, FAR struct stat *buf)
}
else
{
relpath += strlen("pm/");
if (pm_path_validate(relpath) < 0)
{
return -ENOENT;
}

buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
}

Expand Down

0 comments on commit fbe7827

Please sign in to comment.