toolscript-mcp 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -1458
- package/dist/host/broker.js +887 -169
- package/dist/host/main.js +21673 -20181
- package/package.json +1 -1
- package/runtime/child-guard.cs +132 -0
package/package.json
CHANGED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// The child guard of Windows (opt-in with TOOLSCRIPT_CHILD_GUARD=1). toolscript-mcp compiles this file one time with the
|
|
2
|
+
// csc.exe of .NET Framework, keeps the program under ~/.toolscript-mcp/guard (named by a hash of this text) and starts it
|
|
3
|
+
// once for each host or broker: child-guard.exe <pid of the host>
|
|
4
|
+
//
|
|
5
|
+
// It makes one Job Object (KILL_ON_JOB_CLOSE, breakaway allowed). For each line `guard <pid>` on its standard input it
|
|
6
|
+
// puts that process in the job, but only a process that the host started: its parent is the host and it is not older than
|
|
7
|
+
// the host. Then it puts in the job every descendant of the processes that it holds (a `cmd /c` wrapper can start its
|
|
8
|
+
// server before the assign), by parent pid, until a pass finds none. Windows never updates the parent pid of a process
|
|
9
|
+
// and reuses pids within seconds, so a descendant counts only when it is not older than its parent, and a member whose
|
|
10
|
+
// process has exited is dropped at once, with its handle. A process that a member starts is a member. When the host is
|
|
11
|
+
// gone (its process handle is signalled, or the standard input ends) this program exits, the last handle of the job
|
|
12
|
+
// closes, and the kernel ends every process in it. It is not in its own job. It reads and writes only pids and the
|
|
13
|
+
// words ready and guarded: no command, argument or environment value ever reaches it.
|
|
14
|
+
|
|
15
|
+
using System;
|
|
16
|
+
using System.Collections.Generic;
|
|
17
|
+
using System.IO;
|
|
18
|
+
using System.Runtime.InteropServices;
|
|
19
|
+
using System.Threading;
|
|
20
|
+
|
|
21
|
+
public static class ChildGuard {
|
|
22
|
+
[StructLayout(LayoutKind.Sequential)] struct BASIC { public long a, b; public uint LimitFlags; public UIntPtr c, d; public uint e; public UIntPtr f; public uint g, h; }
|
|
23
|
+
[StructLayout(LayoutKind.Sequential)] struct IOC { public ulong a, b, c, d, e, f; }
|
|
24
|
+
[StructLayout(LayoutKind.Sequential)] struct EXT { public BASIC Basic; public IOC Io; public UIntPtr p1, p2, p3, p4; }
|
|
25
|
+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
|
26
|
+
struct PE { public uint size, usage, pid; public IntPtr heap; public uint modid, threads, ppid; public int pri; public uint flags;
|
|
27
|
+
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string exe; }
|
|
28
|
+
[DllImport("kernel32.dll", SetLastError = true)] static extern IntPtr CreateJobObjectW(IntPtr a, IntPtr n);
|
|
29
|
+
[DllImport("kernel32.dll", SetLastError = true)] static extern bool SetInformationJobObject(IntPtr j, int c, ref EXT i, uint l);
|
|
30
|
+
[DllImport("kernel32.dll", SetLastError = true)] static extern bool AssignProcessToJobObject(IntPtr j, IntPtr p);
|
|
31
|
+
[DllImport("kernel32.dll")] static extern IntPtr OpenProcess(uint access, bool inherit, int pid);
|
|
32
|
+
[DllImport("kernel32.dll")] static extern uint WaitForSingleObject(IntPtr h, uint ms);
|
|
33
|
+
[DllImport("kernel32.dll")] static extern bool CloseHandle(IntPtr h);
|
|
34
|
+
[DllImport("kernel32.dll")] static extern bool GetProcessTimes(IntPtr h, out long created, out long exited, out long kernel, out long user);
|
|
35
|
+
[DllImport("kernel32.dll")] static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint pid);
|
|
36
|
+
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)] static extern bool Process32FirstW(IntPtr s, ref PE e);
|
|
37
|
+
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)] static extern bool Process32NextW(IntPtr s, ref PE e);
|
|
38
|
+
|
|
39
|
+
// PROCESS_TERMINATE | PROCESS_SET_QUOTA | PROCESS_QUERY_LIMITED_INFORMATION | SYNCHRONIZE
|
|
40
|
+
const uint ACCESS = 0x1 | 0x100 | 0x1000 | 0x100000;
|
|
41
|
+
|
|
42
|
+
class Member { public IntPtr Handle; public long Created; }
|
|
43
|
+
|
|
44
|
+
static IntPtr job;
|
|
45
|
+
static int hostPid;
|
|
46
|
+
static long hostCreated;
|
|
47
|
+
static readonly Dictionary<int, Member> members = new Dictionary<int, Member>();
|
|
48
|
+
|
|
49
|
+
// The processes of the system now: pid and parent pid.
|
|
50
|
+
static List<int[]> Snapshot() {
|
|
51
|
+
List<int[]> rows = new List<int[]>();
|
|
52
|
+
IntPtr snap = CreateToolhelp32Snapshot(2, 0);
|
|
53
|
+
PE e = new PE();
|
|
54
|
+
e.size = (uint)Marshal.SizeOf(typeof(PE));
|
|
55
|
+
if (Process32FirstW(snap, ref e)) { do { rows.Add(new int[] { (int)e.pid, (int)e.ppid }); } while (Process32NextW(snap, ref e)); }
|
|
56
|
+
CloseHandle(snap);
|
|
57
|
+
return rows;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Puts the process in the job, if it is not older than `notBefore` (the creation time of its parent). It keeps the handle.
|
|
61
|
+
static bool Adopt(int pid, long notBefore) {
|
|
62
|
+
IntPtr h = OpenProcess(ACCESS, false, pid);
|
|
63
|
+
if (h == IntPtr.Zero) return false;
|
|
64
|
+
long created, exited, kernel, user;
|
|
65
|
+
if (!GetProcessTimes(h, out created, out exited, out kernel, out user) || created < notBefore || WaitForSingleObject(h, 0) == 0 || !AssignProcessToJobObject(job, h)) {
|
|
66
|
+
CloseHandle(h);
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
Member m = new Member();
|
|
70
|
+
m.Handle = h;
|
|
71
|
+
m.Created = created;
|
|
72
|
+
members[pid] = m;
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// A member whose process has exited is dropped, so its pid can never match a later process that reuses it.
|
|
77
|
+
static void Prune() {
|
|
78
|
+
List<int> gone = new List<int>();
|
|
79
|
+
foreach (KeyValuePair<int, Member> pair in members) if (WaitForSingleObject(pair.Value.Handle, 0) == 0) gone.Add(pair.Key);
|
|
80
|
+
foreach (int pid in gone) { CloseHandle(members[pid].Handle); members.Remove(pid); }
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// The descendants of the members, by parent pid, each not older than its parent, until a pass finds none.
|
|
84
|
+
static int Sweep() {
|
|
85
|
+
int found = 0;
|
|
86
|
+
for (int pass = 0; pass < 8; pass++) {
|
|
87
|
+
Prune();
|
|
88
|
+
int before = found;
|
|
89
|
+
foreach (int[] r in Snapshot()) {
|
|
90
|
+
Member parent;
|
|
91
|
+
if (members.ContainsKey(r[0]) || !members.TryGetValue(r[1], out parent)) continue;
|
|
92
|
+
if (Adopt(r[0], parent.Created)) found++;
|
|
93
|
+
}
|
|
94
|
+
if (found == before) break;
|
|
95
|
+
}
|
|
96
|
+
return found;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// `guard <pid>`: only a process that the host started, which is its child by parent pid and not older than the host.
|
|
100
|
+
static bool Guard(int pid) {
|
|
101
|
+
foreach (int[] r in Snapshot()) if (r[0] == pid) return r[1] == hostPid && Adopt(pid, hostCreated);
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
public static int Main(string[] args) {
|
|
106
|
+
if (args.Length != 1 || !int.TryParse(args[0], out hostPid)) return 2;
|
|
107
|
+
job = CreateJobObjectW(IntPtr.Zero, IntPtr.Zero);
|
|
108
|
+
EXT limits = new EXT();
|
|
109
|
+
limits.Basic.LimitFlags = 0x2000 | 0x800; // KILL_ON_JOB_CLOSE, breakaway allowed
|
|
110
|
+
if (job == IntPtr.Zero || !SetInformationJobObject(job, 9, ref limits, (uint)Marshal.SizeOf(typeof(EXT)))) return 3;
|
|
111
|
+
IntPtr host = OpenProcess(0x100000 | 0x1000, false, hostPid);
|
|
112
|
+
long exited, kernel, user;
|
|
113
|
+
if (host == IntPtr.Zero || !GetProcessTimes(host, out hostCreated, out exited, out kernel, out user)) return 4;
|
|
114
|
+
Thread watch = new Thread(() => { WaitForSingleObject(host, 0xFFFFFFFF); Environment.Exit(0); });
|
|
115
|
+
watch.IsBackground = true;
|
|
116
|
+
watch.Start();
|
|
117
|
+
TextWriter output = Console.Out;
|
|
118
|
+
output.WriteLine("ready");
|
|
119
|
+
output.Flush();
|
|
120
|
+
string line;
|
|
121
|
+
while ((line = Console.In.ReadLine()) != null) {
|
|
122
|
+
int pid;
|
|
123
|
+
if (!line.StartsWith("guard ") || !int.TryParse(line.Substring(6), out pid) || pid <= 0) continue;
|
|
124
|
+
Prune();
|
|
125
|
+
bool ok = members.ContainsKey(pid) || Guard(pid);
|
|
126
|
+
int more = Sweep();
|
|
127
|
+
output.WriteLine("guarded:" + pid + ":" + ok + ":" + more);
|
|
128
|
+
output.Flush();
|
|
129
|
+
}
|
|
130
|
+
return 0; // the host closed our standard input: the job closes with this process
|
|
131
|
+
}
|
|
132
|
+
}
|