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

Adding a strstr() check #1486

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/os/shared/src/osapi-filesys.c
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ int32 OS_TranslatePath(const char *VirtualPath, char *LocalPath)
OS_object_token_t token;
int32 return_code;
const char * name_ptr;
char * result;
OS_filesys_internal_record_t *filesys;
size_t SysMountPointLen;
size_t VirtPathLen;
Expand Down Expand Up @@ -702,6 +703,15 @@ int32 OS_TranslatePath(const char *VirtualPath, char *LocalPath)
return OS_FS_ERR_PATH_INVALID;
}

/*
** Preventing backing out of the virtual mount point
*/
result = strstr(VirtualPath, "..");
if (result)
{
return OS_FS_ERR_PATH_INVALID;
}

/* Get a reference lock, as a filesystem check could take some time. */
return_code = OS_ObjectIdGetBySearch(OS_LOCK_MODE_GLOBAL, LOCAL_OBJID_TYPE, OS_FileSys_FindVirtMountPoint,
(void *)VirtualPath, &token);
Expand Down
5 changes: 5 additions & 0 deletions src/unit-test-coverage/shared/src/coveragetest-filesys.c
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,11 @@ void Test_OS_TranslatePath(void)
actual = OS_TranslatePath("invalid/", LocalBuffer);
UtAssert_True(actual == expected, "OS_TranslatePath() (%ld) == OS_FS_ERR_PATH_INVALID", (long)actual);

/* Invalid has '..' */
expected = OS_FS_ERR_PATH_INVALID;
actual = OS_TranslatePath("/cf/../test", LocalBuffer);
UtAssert_True(actual == expected, "OS_TranslatePath() (%ld) == OS_FS_ERR_PATH_INVALID", (long)actual);

UT_SetDefaultReturnValue(UT_KEY(OS_ObjectIdGetBySearch), OS_ERR_NAME_NOT_FOUND);
actual = OS_TranslatePath("/cf/test", LocalBuffer);
UtAssert_True(actual == expected, "OS_TranslatePath() (%ld) == OS_FS_ERR_PATH_INVALID", (long)actual);
Expand Down
1 change: 1 addition & 0 deletions src/unit-test-coverage/ut-stubs/inc/OCS_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ extern int OCS_strncmp(const char *s1, const char *s2, size_t n);
extern char * OCS_strncpy(char *dest, const char *src, size_t n);
extern char * OCS_strchr(const char *s, int c);
extern char * OCS_strrchr(const char *s, int c);
extern char * OCS_strstr(const char *haystack, const char *needle);
extern char * OCS_strcat(char *dest, const char *src);
extern char * OCS_strncat(char *dest, const char *src, size_t n);
extern char * OCS_strerror(int errnum);
Expand Down
1 change: 1 addition & 0 deletions src/unit-test-coverage/ut-stubs/override_inc/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#define strncpy OCS_strncpy
#define strchr OCS_strchr
#define strrchr OCS_strrchr
#define strstr OCS_strstr
#define strcat OCS_strcat
#define strncat OCS_strncat
#define strerror OCS_strerror
Expand Down
19 changes: 19 additions & 0 deletions src/unit-test-coverage/ut-stubs/src/libc-string-stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,25 @@ char *OCS_strrchr(const char *s, int c)
return (char *)&s[Status - 1];
}

char *OCS_strstr(const char *haystack, const char *needle)
{
int32 Status;

Status = UT_DEFAULT_IMPL(OCS_strstr);

if (Status == 0)
{
/* "nominal" response */
return strstr(haystack, needle);
}
if (Status < 0)
{
return (char *)0;
}

return (char *)&haystack[Status - 1];
}

size_t OCS_strlen(const char *s)
{
int32 Status;
Expand Down
Loading