diff --git a/tools/nativeguest/hook/privileged.c b/tools/nativeguest/hook/privileged.c index 14ef7c6654..0ca6f97880 100644 --- a/tools/nativeguest/hook/privileged.c +++ b/tools/nativeguest/hook/privileged.c @@ -235,8 +235,16 @@ static int sandbox_only_limit(int resource, const struct rlimit *rlim) { return 0; } +// The kernel checks RLIMIT_RTTIME on a thread that is realtime or has a CPU-time timer armed, and a +// limit of 0 is reached at once: SIGXCPU at the soft limit, SIGKILL at the hard one. SDL sets the +// RTTimeUSecMax it reads from org.freedesktop.portal.Realtime before it asks for a realtime thread, +// and xdg-desktop-portal reports 0 when it reaches no RealtimeKit, as in a session with no system bus. +static int fatal_limit(int resource, const struct rlimit *rlim) { + return rlim && resource == RLIMIT_RTTIME && rlim->rlim_cur == 0; +} + int setrlimit(__rlimit_resource_t resource, const struct rlimit *rlim) { - if (sandbox_only_limit((int)resource, rlim)) return 0; + if (sandbox_only_limit((int)resource, rlim) || fatal_limit((int)resource, rlim)) return 0; static int (*orig_setrlimit)(__rlimit_resource_t, const struct rlimit *) = NULL; #if defined(__USE_FILE_OFFSET64) && !defined(__LP64__) // With 64-bit file offsets on a 32-bit system this function is setrlimit64 diff --git a/tools/tests/test_native_hook_rttime_limit.py b/tools/tests/test_native_hook_rttime_limit.py new file mode 100644 index 0000000000..5cfe76b155 --- /dev/null +++ b/tools/tests/test_native_hook_rttime_limit.py @@ -0,0 +1,81 @@ +import os +from pathlib import Path +import subprocess +import tempfile +import unittest + +from tools.tests.test_usr2_preload import runner + +HOOK_BUILD = Path(__file__).resolve().parents[1] / "nativeguest/build.sh" + +PROBE = r""" +#include +#include +#include + +static void show(const char *name, int ret) { + struct rlimit now; + if (getrlimit(RLIMIT_RTTIME, &now) != 0) exit(2); + printf("%s\t%d\t%lld\t%lld\n", name, ret, (long long)now.rlim_cur, (long long)now.rlim_max); +} + +int main(void) { + struct rlimit zero = {0, 0}, soft_zero = {0, 200000}, usable = {100000, 200000}; + show("start", 0); + show("zero", setrlimit(RLIMIT_RTTIME, &zero)); + show("soft_zero", setrlimit(RLIMIT_RTTIME, &soft_zero)); + show("usable", setrlimit(RLIMIT_RTTIME, &usable)); + return 0; +} +""" + + +class NativeHookRttimeLimitTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.cc, cls.prefix = runner() + if cls.cc is None: + raise unittest.SkipTest("no aarch64 compiler and runner") + cls.tmp = tempfile.TemporaryDirectory(dir="/tmp") + tmp = Path(cls.tmp.name) + cls.hook = tmp / "hook.so" + subprocess.run(["bash", str(HOOK_BUILD), str(cls.hook), cls.cc], check=True, capture_output=True) + (tmp / "probe.c").write_text(PROBE) + cls.probe = tmp / "probe" + subprocess.run([cls.cc, "-O1", "-o", str(cls.probe), str(tmp / "probe.c")], check=True) + cls.root = tmp / "root" + cls.root.mkdir() + + @classmethod + def tearDownClass(cls): + cls.tmp.cleanup() + + def limits(self): + hook_env = {"DDNATV_ROOT": str(self.root), "LD_PRELOAD": str(self.hook)} + env = {k: v for k, v in os.environ.items() if k not in ("LD_PRELOAD", "DDNATV_ROOT")} + if self.prefix: + cmd = list(self.prefix) + [arg for key, value in hook_env.items() for arg in ("-E", f"{key}={value}")] + else: + cmd = [] + env.update(hook_env) + out = subprocess.run(cmd + [str(self.probe)], env=env, capture_output=True, text=True, timeout=120) + self.assertEqual(0, out.returncode, out.stdout + out.stderr) + return {name: (int(ret), int(cur), int(hard)) + for name, ret, cur, hard in (line.split("\t") for line in out.stdout.splitlines())} + + def test_a_realtime_limit_of_zero_is_reported_set_and_not_applied(self): + limits = self.limits() + self.assertEqual(0, limits["zero"][0]) + self.assertEqual(limits["start"][1:], limits["zero"][1:]) + + def test_a_soft_realtime_limit_of_zero_is_not_applied(self): + limits = self.limits() + self.assertEqual(0, limits["soft_zero"][0]) + self.assertEqual(limits["start"][1:], limits["soft_zero"][1:]) + + def test_a_usable_realtime_limit_is_applied(self): + self.assertEqual((0, 100000, 200000), self.limits()["usable"]) + + +if __name__ == "__main__": + unittest.main()