diff --git a/tools/nativeguest/hook/fileops.c b/tools/nativeguest/hook/fileops.c index 840812fe69..56daaad9dd 100644 --- a/tools/nativeguest/hook/fileops.c +++ b/tools/nativeguest/hook/fileops.c @@ -422,6 +422,7 @@ static ssize_t copy_readlink_result(char *buf, size_t bufsiz, const char *out, s if (ret <= 0 || out == buf) return ret; size_t n = (size_t)ret < bufsiz ? (size_t)ret : bufsiz; memcpy(buf, out, n); + if (n < bufsiz) buf[n] = '\0'; return (ssize_t)n; } diff --git a/tools/nativeguest/hook/setup.c b/tools/nativeguest/hook/setup.c index f44604f9c8..ff0f5de2ac 100644 --- a/tools/nativeguest/hook/setup.c +++ b/tools/nativeguest/hook/setup.c @@ -305,6 +305,7 @@ static ssize_t copy_guest_exe(const char *real_exe, char *buf, size_t bufsiz) { size_t len = strlen(out_path); size_t copy_len = (len < bufsiz) ? len : bufsiz; memcpy(buf, out_path, copy_len); + if (copy_len < bufsiz) buf[copy_len] = '\0'; return (ssize_t)copy_len; } diff --git a/tools/tests/test_native_hook_proc_exe.py b/tools/tests/test_native_hook_proc_exe.py index 479c78109d..3e1182c9bf 100644 --- a/tools/tests/test_native_hook_proc_exe.py +++ b/tools/tests/test_native_hook_proc_exe.py @@ -19,6 +19,13 @@ int main(int argc, char **argv) { for (int i = 1; i < argc; i++) { char buf[4096]; ssize_t n; + if (strncmp(argv[i], "fill:", 5) == 0 || strncmp(argv[i], "short:", 6) == 0) { + size_t size = argv[i][0] == 'f' ? sizeof(buf) : 256; + memset(buf, '/', sizeof(buf)); + n = readlink(strchr(argv[i], ':') + 1, buf, size); + printf("%s\t%s\n", argv[i], n > 0 && (size_t)n < size && buf[n] == 0 ? "ends" : "runs on"); + continue; + } if (strncmp(argv[i], "at:", 3) == 0) { int dfd = open(argv[i] + 3, O_RDONLY | O_DIRECTORY); n = dfd >= 0 ? readlinkat(dfd, "exe", buf, sizeof(buf) - 1) : -1; @@ -57,6 +64,7 @@ class NativeHookProcExeTest(unittest.TestCase): (cls.root / "usr/bin").mkdir(parents=True) cls.kwin = cls.root / "usr/bin/kwin_wayland" shutil.copy(tmp / "sleeper", cls.kwin) + (cls.root / "compositor").symlink_to("usr/bin/kwin_wayland") cls.runtime = tmp / "runtime" cls.runtime.mkdir() cls.bound = cls.runtime / "chrome" @@ -151,6 +159,14 @@ class NativeHookProcExeTest(unittest.TestCase): link = f"/proc/{pid}/exe" self.assertEqual("/usr/bin/kwin_wayland", self.readlink(link)[link]) + def test_the_own_program_answer_ends_where_the_name_ends(self): + key = "fill:/proc/self/exe" + self.assertEqual("ends", self.readlink(key)[key]) + + def test_a_link_read_into_a_short_buffer_ends_where_its_target_ends(self): + key = "short:/compositor" + self.assertEqual("ends", self.readlink(key)[key]) + def test_a_loader_without_a_known_program_keeps_the_kernel_link(self): pid = self.start(loader=True) link = f"/proc/{pid}/exe"