mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 04:55:44 +00:00
test(handoff): a held exit with no free fd, with and without the parent's pidfd
This commit is contained in:
1 parent
e8a998bab1
commit
6d0442ef4c
1 file changed
+82
-6
@@ -11,6 +11,7 @@ import sys
|
||||
import tempfile
|
||||
import termios
|
||||
import threading
|
||||
import time
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
@@ -1241,6 +1242,31 @@ int main(void) {
|
||||
}
|
||||
"""
|
||||
|
||||
# A worker's program that says whether it has the hook's pidfd of its parent (kept at 900) and exits
|
||||
# with 9 at once. Built with NOFDS, it first takes every free descriptor (/proc then cannot be read).
|
||||
EXIT9 = r"""
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/resource.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void) {
|
||||
char target[64];
|
||||
ssize_t n = readlink("/proc/self/fd/900", target, sizeof(target) - 1);
|
||||
target[n > 0 ? n : 0] = 0;
|
||||
printf("pidfd\t%d\n", strcmp(target, "anon_inode:[pidfd]") == 0);
|
||||
#ifdef NOFDS
|
||||
struct rlimit lim = {512, 512}; // below the hook's kept fds, which stay open
|
||||
setrlimit(RLIMIT_NOFILE, &lim);
|
||||
while (open("/dev/null", O_RDONLY) >= 0) {}
|
||||
printf("nofds\t%d\n", open("/proc/self/stat", O_RDONLY) < 0);
|
||||
#endif
|
||||
fflush(stdout);
|
||||
return 9;
|
||||
}
|
||||
"""
|
||||
|
||||
# An unlisted program that execs the next one, as env and shell wrappers do
|
||||
WRAP = r"""
|
||||
#include <unistd.h>
|
||||
@@ -2001,31 +2027,81 @@ class NativeHookHandoffTest(unittest.TestCase):
|
||||
self.assertEqual("6", result.get("other"), out.stdout)
|
||||
self.assertEqual("-1", result.get("again"), out.stdout)
|
||||
|
||||
def start_worker_exit(self, name, parent, state):
|
||||
def start_worker_exit(self, name, parent, state, nofds=False, child_start=1 << 62):
|
||||
# A worker's program that exits with 9 at once, its entry (slot 0) given parent and state, as
|
||||
# the parent's check of a gone worker leaves it while it holds it (TAKEN). Returns the run and
|
||||
# the table.
|
||||
# the parent's check of a gone worker leaves it while it holds it (TAKEN). With nofds it first
|
||||
# takes every free descriptor, so that /proc cannot be read when it exits. A child_start before
|
||||
# the parent's start leaves it without the parent's pidfd. Returns the run and the table.
|
||||
tmp = Path(self.tmp.name)
|
||||
probe = tmp / name
|
||||
(tmp / (name + ".c")).write_text("int main(void) { return 9; }\n")
|
||||
(tmp / (name + ".c")).write_text(("#define NOFDS 1\n" if nofds else "") + EXIT9)
|
||||
subprocess.run([self.cc, "-O1", "-o", str(probe), str(tmp / (name + ".c"))], check=True)
|
||||
table = os.memfd_create("ddh-test-table", 0)
|
||||
self.addCleanup(os.close, table)
|
||||
os.ftruncate(table, 64 + 1024 * 64)
|
||||
os.pwrite(table, struct.pack("<i", 0x6464686F), 0)
|
||||
os.pwrite(table, struct.pack("<ii", parent, state), 64 + 8)
|
||||
os.pwrite(table, struct.pack("<q", 1 << 62), 64 + 40)
|
||||
os.pwrite(table, struct.pack("<q", child_start), 64 + 40)
|
||||
env = {"LD_PRELOAD": str(self.hook), "DDNATV_ROOT": str(self.root), "DDH_TABLE": "%d:0:%d:0" % (table, parent)}
|
||||
settings = " ".join("-E %s=%s" % item for item in env.items()) if self.prefix else \
|
||||
" ".join("%s=%s" % item for item in env.items())
|
||||
worker = "-E DDH_WORKER_PID=$$" if self.prefix else "DDH_WORKER_PID=$$"
|
||||
runner = " ".join(self.prefix) + " " + settings + " " + worker if self.prefix else "env " + settings + " " + worker
|
||||
run = subprocess.Popen(["sh", "-c", "exec %s %s" % (runner, probe)], pass_fds=(table,),
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True)
|
||||
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True)
|
||||
self.addCleanup(run.wait)
|
||||
self.addCleanup(lambda: run.poll() is None and run.kill())
|
||||
return run, table
|
||||
|
||||
@staticmethod
|
||||
def printed(run):
|
||||
return dict(line.split("\t", 1) for line in run.stdout.read().splitlines() if "\t" in line)
|
||||
|
||||
def test_with_no_free_fd_a_pidfd_ends_a_held_exit_once_its_holder_has_exited(self):
|
||||
forker = subprocess.Popen(["sleep", "1"])
|
||||
self.addCleanup(forker.wait) # a zombie once it exits: kill() alone would call it alive
|
||||
start = time.monotonic()
|
||||
run, table = self.start_worker_exit("takenpidfd", forker.pid, 4, nofds=True)
|
||||
run.wait(timeout=60)
|
||||
took = time.monotonic() - start
|
||||
result = self.printed(run)
|
||||
self.assertEqual("1", result.get("pidfd"), result)
|
||||
self.assertEqual("1", result.get("nofds"), result)
|
||||
# well inside the bounded wait (5000 looks) that kill() alone would have taken
|
||||
self.assertLess(took, 4, result)
|
||||
self.assertEqual(4, struct.unpack("<i", os.pread(table, 4, 64 + 12))[0])
|
||||
|
||||
def test_with_no_free_fd_and_no_pidfd_a_held_exit_waits_a_bounded_time(self):
|
||||
forker = subprocess.Popen(["sleep", "120"])
|
||||
self.addCleanup(forker.wait)
|
||||
self.addCleanup(forker.kill)
|
||||
run, table = self.start_worker_exit("takenbounded", forker.pid, 4, nofds=True, child_start=0)
|
||||
with self.assertRaises(subprocess.TimeoutExpired):
|
||||
run.wait(timeout=2) # the holder lives, as kill() tells
|
||||
self.assertEqual(9, run.wait(timeout=90)) # then the bound: it gives up waiting
|
||||
result = self.printed(run)
|
||||
self.assertEqual("0", result.get("pidfd"), result)
|
||||
self.assertEqual("1", result.get("nofds"), result)
|
||||
state, status = struct.unpack("<ii", os.pread(table, 8, 64 + 12))
|
||||
self.assertEqual(4, state) # nothing written into the entry its holder still has
|
||||
self.assertEqual(0, status)
|
||||
|
||||
def test_with_no_free_fd_a_pidfd_keeps_a_held_exit_waiting_for_a_live_holder(self):
|
||||
forker = subprocess.Popen(["sleep", "120"])
|
||||
self.addCleanup(forker.wait)
|
||||
self.addCleanup(forker.kill)
|
||||
run, table = self.start_worker_exit("takenlivepidfd", forker.pid, 4, nofds=True)
|
||||
with self.assertRaises(subprocess.TimeoutExpired):
|
||||
run.wait(timeout=15) # past the bound kill() alone gives (5000 looks)
|
||||
os.pwrite(table, struct.pack("<i", 1), 64 + 12) # given back, HANDED
|
||||
self.assertEqual(9, run.wait(timeout=30))
|
||||
result = self.printed(run)
|
||||
self.assertEqual("1", result.get("pidfd"), result)
|
||||
self.assertEqual("1", result.get("nofds"), result)
|
||||
state, status = struct.unpack("<ii", os.pread(table, 8, 64 + 12))
|
||||
self.assertEqual(2, state) # EXITED
|
||||
self.assertEqual(9 << 8, status)
|
||||
|
||||
def test_an_exit_waits_while_a_live_parent_holds_the_entry_and_reports_once_given_back(self):
|
||||
forker = subprocess.Popen(["sleep", "60"])
|
||||
self.addCleanup(forker.wait)
|
||||
|
||||
Reference in new issue
Block a user