Skip to content

Commit 160df84

Browse files
yonghuahlijinxia
authored andcommitted
DM: fix buffer overflow risk issues in hugetlb.c
Add buffer boundaries to avoid overflow Signed-off-by: Yonghua Huang <yonghua.huang@intel.com>
1 parent cd07c2c commit 160df84

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

devicemodel/core/hugetlb.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ static int open_hugetlbfs(struct vmctx *ctx, int level)
119119
strncpy(path, hugetlb_priv[level].mount_path, MAX_PATH_LEN);
120120

121121
/* UUID will use 32 bytes */
122-
if (strlen(path) + 32 > MAX_PATH_LEN) {
122+
if (strnlen(path, MAX_PATH_LEN) + 32 > MAX_PATH_LEN) {
123123
perror("PATH overflow");
124124
return -ENOMEM;
125125
}
@@ -298,9 +298,10 @@ static int create_hugetlb_dirs(int level)
298298
return -EINVAL;
299299
}
300300

301-
strcpy(tmp_path, path);
301+
memset(tmp_path, '\0', MAX_PATH_LEN);
302+
strncpy(tmp_path, path, MAX_PATH_LEN - 1);
302303

303-
if (tmp_path[len - 1] != '/')
304+
if ((tmp_path[len - 1] != '/') && (strlen(tmp_path) < MAX_PATH_LEN - 1))
304305
strcat(tmp_path, "/");
305306

306307
len = strlen(tmp_path);

0 commit comments

Comments
 (0)