mirror of
https://github.com/openharmony/developtools_syscap_codec.git
synced 2026-08-27 02:11:19 -04:00
!686 merge refs/merge-requests/686/head into master
【轻量级 PR】添加statBuf.st_size范围校验 Created-by: doublelucky Commit-by: doublelucky Merged-by: openharmony_ci Description: 添加statBuf.st_size范围校验 See merge request: openharmony/developtools_syscap_codec!686
This commit is contained in:
+28
-9
@@ -37,14 +37,35 @@ void FreeContextBuffer(char *contextBuffer)
|
||||
}
|
||||
}
|
||||
|
||||
static int32_t ReadFileContent(FILE *fp, char *contextBuffer, long long fileSize, const char *path)
|
||||
{
|
||||
size_t retFread = fread(contextBuffer, (size_t)fileSize, 1, fp);
|
||||
if (retFread != 1) {
|
||||
PRINT_ERR("read file(%s) failed, errno = %d\n", path, errno);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int32_t CheckFileSize(const struct stat *statBuf)
|
||||
{
|
||||
if (statBuf->st_size <= 0) {
|
||||
PRINT_ERR("file size (%lld) out of range\n", (long long)statBuf->st_size);
|
||||
return -1;
|
||||
}
|
||||
if ((uint64_t)statBuf->st_size >= (uint64_t)(UINT32_MAX - 1)) {
|
||||
PRINT_ERR("file size (%lld) exceeds 32-bit limit\n", (long long)statBuf->st_size);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t GetFileContext(const char *inputFile, char **contextBufPtr, uint32_t *bufferLen)
|
||||
{
|
||||
int32_t ret;
|
||||
FILE *fp = NULL;
|
||||
struct stat statBuf;
|
||||
char *contextBuffer = NULL;
|
||||
char path[PATH_MAX + 1] = {0x00};
|
||||
|
||||
#ifdef _POSIX_
|
||||
if (strlen(inputFile) > PATH_MAX || strncpy_s(path, PATH_MAX, inputFile, strlen(inputFile)) != EOK) {
|
||||
PRINT_ERR("get path(%s) failed\n", inputFile);
|
||||
@@ -56,9 +77,7 @@ int32_t GetFileContext(const char *inputFile, char **contextBufPtr, uint32_t *bu
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
ret = stat(path, &statBuf);
|
||||
if (ret != 0) {
|
||||
if (stat(path, &statBuf) != 0) {
|
||||
PRINT_ERR("get file(%s) st_mode failed, errno = %d\n", path, errno);
|
||||
return -1;
|
||||
}
|
||||
@@ -66,6 +85,9 @@ int32_t GetFileContext(const char *inputFile, char **contextBufPtr, uint32_t *bu
|
||||
PRINT_ERR("don't have permission to read the file(%s)\n", path);
|
||||
return -1;
|
||||
}
|
||||
if (CheckFileSize(&statBuf) != 0) {
|
||||
return -1;
|
||||
}
|
||||
contextBuffer = (char *)malloc(statBuf.st_size + 1);
|
||||
if (contextBuffer == NULL) {
|
||||
PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", (int32_t)statBuf.st_size + 1, errno);
|
||||
@@ -77,16 +99,13 @@ int32_t GetFileContext(const char *inputFile, char **contextBufPtr, uint32_t *bu
|
||||
FreeContextBuffer(contextBuffer);
|
||||
return -1;
|
||||
}
|
||||
size_t retFread = fread(contextBuffer, statBuf.st_size, 1, fp);
|
||||
if (retFread != 1) {
|
||||
PRINT_ERR("read file(%s) failed, errno = %d\n", path, errno);
|
||||
if (ReadFileContent(fp, contextBuffer, statBuf.st_size, path) != 0) {
|
||||
FreeContextBuffer(contextBuffer);
|
||||
(void)fclose(fp);
|
||||
return -1;
|
||||
}
|
||||
contextBuffer[statBuf.st_size] = '\0';
|
||||
(void)fclose(fp);
|
||||
|
||||
*contextBufPtr = contextBuffer;
|
||||
*bufferLen = statBuf.st_size + 1;
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user