Skip to content

Commit b1d026a

Browse files
committed
Fix buffer overflows when argv[0] or env variables are longer than PATH_MAX
1 parent 5e7217b commit b1d026a

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/runtime/runtime.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,10 +1482,18 @@ int main(int argc, char* argv[]) {
14821482
*/
14831483
if (getenv("TARGET_APPIMAGE") == NULL) {
14841484
strcpy(appimage_path, "/proc/self/exe");
1485-
strcpy(argv0_path, argv[0]);
1485+
char *res = memccpy(argv0_path, argv[0], '\0', sizeof(argv0_path));
1486+
if (res == NULL) {
1487+
fprintf(stderr, "Program name too big\n");
1488+
exit(EXIT_EXECERROR);
1489+
}
14861490
} else {
1487-
strcpy(appimage_path, getenv("TARGET_APPIMAGE"));
1488-
strcpy(argv0_path, getenv("TARGET_APPIMAGE"));
1491+
char *res1 = memccpy(appimage_path, getenv("TARGET_APPIMAGE"), '\0', sizeof(appimage_path));
1492+
char *res2 = memccpy(argv0_path, getenv("TARGET_APPIMAGE"), '\0', sizeof(argv0_path));
1493+
if (res1 == NULL || res2 == NULL) {
1494+
fprintf(stderr, "TARGET_APPIMAGE environment variable too big\n");
1495+
exit(EXIT_EXECERROR);
1496+
}
14891497
}
14901498

14911499
// temporary directories are required in a few places
@@ -1494,8 +1502,13 @@ int main(int argc, char* argv[]) {
14941502

14951503
{
14961504
const char* const TMPDIR = getenv("TMPDIR");
1497-
if (TMPDIR != NULL)
1498-
strcpy(temp_base, getenv("TMPDIR"));
1505+
if (TMPDIR != NULL) {
1506+
char *res = memccpy(temp_base, TMPDIR, '\0', sizeof(temp_base));
1507+
if (res == NULL) {
1508+
fprintf(stderr, "TMPDIR environemnt variable too big\n");
1509+
exit(EXIT_EXECERROR);
1510+
}
1511+
}
14991512
}
15001513

15011514
fs_offset = appimage_get_elf_size(appimage_path);

0 commit comments

Comments
 (0)