From 6cdaa2c16add86a749df75199ac2a4a556e49ef9 Mon Sep 17 00:00:00 2001 From: Nicholas Mosier Date: Thu, 7 Sep 2023 03:18:58 +0000 Subject: [PATCH] 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 --- src/sim/syscall_emul.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/sim/syscall_emul.cc b/src/sim/syscall_emul.cc index c212d242fb..9794a4835e 100644 --- a/src/sim/syscall_emul.cc +++ b/src/sim/syscall_emul.cc @@ -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);