sim-se: Fix crash in chdirFunc() on nonexistent directory

This commit fixes a crash in the syscall emulation of the chdir(2)
syscall, implemented by chdirFunc() in src/sim/syscall_emul.cc,
when passed a nonexistent directory. The buggy code did not check
the return value of realpath().

This patch adds code to check the return value of realpath(), and
if it is NULL (i.e., there was an error with the requested directory
to change to), propagates the error in `errno` to the application.

GitHub issue: https://github.com/gem5/gem5/issues/276

Change-Id: I8a576f60fe3687f320d0cfc28e9d3a6b477d7054
This commit is contained in:
Nicholas Mosier
2023-09-07 03:18:58 +00:00
parent e80cde0713
commit 6cdaa2c16a

View File

@@ -959,7 +959,9 @@ chdirFunc(SyscallDesc *desc, ThreadContext *tc, VPtr<> pathname)
tgt_cwd = path;
} else {
char buf[PATH_MAX];
tgt_cwd = realpath((p->tgtCwd + "/" + path).c_str(), buf);
if (!realpath((p->tgtCwd + "/" + path).c_str(), buf))
return -errno;
tgt_cwd = buf;
}
std::string host_cwd = p->checkPathRedirect(tgt_cwd);