mlai-code 0.0.0-stage → 0.3.0
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 +12 -2
- package/bin/mlai.mjs +3 -0
- package/package.json +18 -3
- package/src/browser-setup.mjs +77 -0
- package/src/cli-ext.mjs +146 -0
- package/src/cli.mjs +534 -0
- package/src/cloud.mjs +29 -0
- package/src/completion.mjs +159 -0
- package/src/credentials.mjs +54 -0
- package/src/deploy.mjs +134 -0
- package/src/distribution.mjs +373 -0
- package/src/engine-key.mjs +4 -0
- package/src/engine.mjs +167 -0
- package/src/headless.mjs +196 -0
- package/src/home.mjs +22 -0
- package/src/host.mjs +123 -0
- package/src/local-models.mjs +64 -0
- package/src/modes.mjs +38 -0
- package/src/netbox.mjs +97 -0
- package/src/netjail.py +59 -0
- package/src/offline.mjs +98 -0
- package/src/parity.mjs +86 -0
- package/src/project.mjs +68 -0
- package/src/relay.mjs +140 -0
- package/src/render.mjs +73 -0
- package/src/restricted.mjs +47 -0
- package/src/review.mjs +70 -0
- package/src/runner.mjs +127 -0
- package/src/runtime.mjs +75 -0
- package/src/sandbox.mjs +135 -0
- package/src/sdk.mjs +11 -0
- package/src/server.mjs +110 -0
- package/src/session.mjs +290 -0
- package/src/settings.mjs +135 -0
- package/src/shared-data.mjs +3 -0
- package/src/tui/acp.mjs +145 -0
- package/src/tui/agents-cli.mjs +114 -0
- package/src/tui/agents-local.mjs +65 -0
- package/src/tui/ansi.mjs +123 -0
- package/src/tui/app.mjs +710 -0
- package/src/tui/approval.mjs +88 -0
- package/src/tui/bg.mjs +172 -0
- package/src/tui/bgterm.mjs +79 -0
- package/src/tui/chat.mjs +230 -0
- package/src/tui/clipboard.mjs +74 -0
- package/src/tui/cloud-cli.mjs +57 -0
- package/src/tui/cloud-run.mjs +123 -0
- package/src/tui/cmd-agents.mjs +130 -0
- package/src/tui/cmd-auto-review.mjs +94 -0
- package/src/tui/cmd-cloud.mjs +17 -0
- package/src/tui/cmd-config.mjs +104 -0
- package/src/tui/cmd-core.mjs +160 -0
- package/src/tui/cmd-distribution.mjs +15 -0
- package/src/tui/cmd-ecosystem.mjs +169 -0
- package/src/tui/cmd-extensibility.mjs +333 -0
- package/src/tui/cmd-remote.mjs +18 -0
- package/src/tui/cmd-review.mjs +58 -0
- package/src/tui/cmd-session-extra.mjs +152 -0
- package/src/tui/cmd-tui.mjs +200 -0
- package/src/tui/commands.mjs +160 -0
- package/src/tui/custom-commands.mjs +98 -0
- package/src/tui/diff.mjs +60 -0
- package/src/tui/editor.mjs +190 -0
- package/src/tui/ext-state.mjs +16 -0
- package/src/tui/extensions-local.mjs +140 -0
- package/src/tui/files.mjs +72 -0
- package/src/tui/handoff.mjs +110 -0
- package/src/tui/import-setups.mjs +151 -0
- package/src/tui/init.mjs +104 -0
- package/src/tui/keymap.mjs +70 -0
- package/src/tui/keys.mjs +101 -0
- package/src/tui/loop.mjs +75 -0
- package/src/tui/markdown.mjs +118 -0
- package/src/tui/mcp-config.mjs +225 -0
- package/src/tui/mcp-oauth.mjs +162 -0
- package/src/tui/permissions.mjs +189 -0
- package/src/tui/plain.mjs +32 -0
- package/src/tui/plugins-local.mjs +103 -0
- package/src/tui/remote.mjs +125 -0
- package/src/tui/review-cli.mjs +332 -0
- package/src/tui/review-github.mjs +59 -0
- package/src/tui/screen.mjs +60 -0
- package/src/tui/session-ops.mjs +104 -0
- package/src/tui/skills-local.mjs +92 -0
- package/src/tui/snapshots.mjs +87 -0
- package/src/tui/statusline.mjs +98 -0
- package/src/tui/transcript.mjs +116 -0
- package/src/two-words.mjs +36 -0
- package/src/wakeups.mjs +53 -0
- package/src/winenv.mjs +94 -0
- package/src/winsandbox.py +351 -0
- package/src/workspace-api.mjs +621 -0
- package/src/worktree.mjs +128 -0
- package/vendor/client-sdk/controller.cjs +115 -0
- package/vendor/client-sdk/files.cjs +38 -0
- package/vendor/client-sdk/index.cjs +133 -0
- package/vendor/client-sdk/login.cjs +52 -0
- package/vendor/client-sdk/updates.cjs +41 -0
- package/vendor/cloud-cli/mlai.mjs +394 -0
- package/worker/architecture_parse.cjs +23 -0
- package/worker/architecture_worker.py +394 -0
- package/worker/ask_first.py +96 -0
- package/worker/attachment_reader.py +66 -0
- package/worker/background_jobs.py +111 -0
- package/worker/brief_questions.py +56 -0
- package/worker/browser_local.py +49 -0
- package/worker/browser_pane.py +89 -0
- package/worker/browser_tool.py +101 -0
- package/worker/build_planner.py +187 -0
- package/worker/chat_fork.py +277 -0
- package/worker/checkpoints.py +133 -0
- package/worker/cloud_pty.py +351 -0
- package/worker/command_policy.py +39 -0
- package/worker/command_safety.py +717 -0
- package/worker/computer_card.sh +23 -0
- package/worker/computer_setup.sh +86 -0
- package/worker/computer_use.py +221 -0
- package/worker/connector_search.py +43 -0
- package/worker/context_manager.py +60 -0
- package/worker/conversation.py +144 -0
- package/worker/deploy_pack.py +116 -0
- package/worker/design_director.py +240 -0
- package/worker/dev_jobs.py +166 -0
- package/worker/edit_guard.py +99 -0
- package/worker/env_setup.py +129 -0
- package/worker/evidence.py +109 -0
- package/worker/fast_search.py +24 -0
- package/worker/git_sync.py +333 -0
- package/worker/goals.py +156 -0
- package/worker/handover.py +98 -0
- package/worker/harness_memory.py +194 -0
- package/worker/image_generation.py +173 -0
- package/worker/intelligence.py +149 -0
- package/worker/intent_model.py +78 -0
- package/worker/intent_weights.json +1 -0
- package/worker/ios_simulator.py +87 -0
- package/worker/kimi_bridge.py +997 -0
- package/worker/kimi_compact.py +93 -0
- package/worker/kimi_gates.py +304 -0
- package/worker/kimi_hook.py +617 -0
- package/worker/lifecycle.py +95 -0
- package/worker/lsp.py +77 -0
- package/worker/mcp_local.py +293 -0
- package/worker/memory.py +99 -0
- package/worker/mlai_auto_review.py +177 -0
- package/worker/mlai_hooks.py +966 -0
- package/worker/mlai_mcp.py +441 -0
- package/worker/mlai_permissions.py +283 -0
- package/worker/model_stream.py +75 -0
- package/worker/nested_repos.py +76 -0
- package/worker/offline_guard.py +17 -0
- package/worker/preview.py +806 -0
- package/worker/proc_watch.py +124 -0
- package/worker/project_context.py +72 -0
- package/worker/project_rules.py +99 -0
- package/worker/quality.py +180 -0
- package/worker/reply_language.py +56 -0
- package/worker/repo_hygiene.py +132 -0
- package/worker/repo_memory.py +101 -0
- package/worker/repo_policy.py +269 -0
- package/worker/roles.json +3173 -0
- package/worker/roles.py +303 -0
- package/worker/runner.py +1795 -0
- package/worker/runtime_control.py +205 -0
- package/worker/secret_scan.py +168 -0
- package/worker/session_hub.py +80 -0
- package/worker/skill_library.py +129 -0
- package/worker/skill_pack.py +533 -0
- package/worker/subagents.py +91 -0
- package/worker/task_controller.py +139 -0
- package/worker/task_skills.py +159 -0
- package/worker/team_checkpoint.py +33 -0
- package/worker/token_count.py +32 -0
- package/worker/tool_catalog.py +38 -0
- package/worker/upload_storage.py +531 -0
- package/worker/verification.py +725 -0
- package/worker/vision.py +84 -0
- package/worker/wakeups.py +63 -0
- package/worker/web_tools.py +203 -0
- package/worker/winproc.py +135 -0
- package/worker/work_ledger.py +117 -0
- package/worker/worktrees.py +79 -0
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
"""ML AI Code sandbox for Windows (no administrator setup).
|
|
2
|
+
|
|
3
|
+
Runs a command under a write-restricted copy of the current user's token. Every write must pass a second check
|
|
4
|
+
against a short list of SIDs: Everyone, this logon session, and a machine-local ML AI Code sandbox SID. The sandbox
|
|
5
|
+
SID is granted write access only to the allowed folders (the project, the session state, a private temp folder and
|
|
6
|
+
package caches), so the command and everything it starts can read the machine as usual but write only there.
|
|
7
|
+
Administrator rights and privileges are removed from the token. A job object stops the whole tree if this launcher ends.
|
|
8
|
+
|
|
9
|
+
Limits, stated plainly: reads are not restricted, and network access stays on.
|
|
10
|
+
|
|
11
|
+
usage: python winsandbox.py --sid S-1-... --write DIR [--write DIR ...] -- program [args...]
|
|
12
|
+
python winsandbox.py --grant-only --sid S-1-... --write DIR ...
|
|
13
|
+
"""
|
|
14
|
+
import ctypes, json, os, subprocess, sys
|
|
15
|
+
from ctypes import wintypes
|
|
16
|
+
|
|
17
|
+
k32 = ctypes.WinDLL('kernel32', use_last_error=True)
|
|
18
|
+
adv = ctypes.WinDLL('advapi32', use_last_error=True)
|
|
19
|
+
|
|
20
|
+
TOKEN_QUERY, TOKEN_DUPLICATE, TOKEN_ASSIGN_PRIMARY, TOKEN_ADJUST_DEFAULT = 0x8, 0x2, 0x1, 0x80
|
|
21
|
+
TOKEN_ADJUST_SESSIONID, TOKEN_ADJUST_PRIVILEGES = 0x100, 0x20
|
|
22
|
+
DISABLE_MAX_PRIVILEGE, LUA_TOKEN, WRITE_RESTRICTED = 0x1, 0x4, 0x8
|
|
23
|
+
TokenGroups, TokenDefaultDacl = 2, 6
|
|
24
|
+
SE_GROUP_LOGON_ID = 0xC0000000
|
|
25
|
+
GENERIC_ALL = 0x10000000
|
|
26
|
+
ACL_REVISION = 2
|
|
27
|
+
CREATE_SUSPENDED, CREATE_UNICODE_ENVIRONMENT, CREATE_BREAKAWAY_FROM_JOB = 0x4, 0x400, 0x01000000
|
|
28
|
+
STARTF_USESTDHANDLES = 0x100
|
|
29
|
+
INFINITE = 0xFFFFFFFF
|
|
30
|
+
JobObjectExtendedLimitInformation = 9
|
|
31
|
+
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = 0x2000
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class SID_AND_ATTRIBUTES(ctypes.Structure):
|
|
35
|
+
_fields_ = [('Sid', ctypes.c_void_p), ('Attributes', wintypes.DWORD)]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class TOKEN_DEFAULT_DACL(ctypes.Structure):
|
|
39
|
+
_fields_ = [('DefaultDacl', ctypes.c_void_p)]
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class STARTUPINFOW(ctypes.Structure):
|
|
43
|
+
_fields_ = [('cb', wintypes.DWORD), ('lpReserved', wintypes.LPWSTR), ('lpDesktop', wintypes.LPWSTR), ('lpTitle', wintypes.LPWSTR),
|
|
44
|
+
('dwX', wintypes.DWORD), ('dwY', wintypes.DWORD), ('dwXSize', wintypes.DWORD), ('dwYSize', wintypes.DWORD),
|
|
45
|
+
('dwXCountChars', wintypes.DWORD), ('dwYCountChars', wintypes.DWORD), ('dwFillAttribute', wintypes.DWORD),
|
|
46
|
+
('dwFlags', wintypes.DWORD), ('wShowWindow', wintypes.WORD), ('cbReserved2', wintypes.WORD), ('lpReserved2', ctypes.c_void_p),
|
|
47
|
+
('hStdInput', wintypes.HANDLE), ('hStdOutput', wintypes.HANDLE), ('hStdError', wintypes.HANDLE)]
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class PROCESS_INFORMATION(ctypes.Structure):
|
|
51
|
+
_fields_ = [('hProcess', wintypes.HANDLE), ('hThread', wintypes.HANDLE), ('dwProcessId', wintypes.DWORD), ('dwThreadId', wintypes.DWORD)]
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class IO_COUNTERS(ctypes.Structure):
|
|
55
|
+
_fields_ = [(n, ctypes.c_ulonglong) for n in ('r', 'w', 'o', 'rb', 'wb', 'ob')]
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class JOBOBJECT_BASIC_LIMIT_INFORMATION(ctypes.Structure):
|
|
59
|
+
_fields_ = [('PerProcessUserTimeLimit', ctypes.c_longlong), ('PerJobUserTimeLimit', ctypes.c_longlong), ('LimitFlags', wintypes.DWORD),
|
|
60
|
+
('MinimumWorkingSetSize', ctypes.c_size_t), ('MaximumWorkingSetSize', ctypes.c_size_t), ('ActiveProcessLimit', wintypes.DWORD),
|
|
61
|
+
('Affinity', ctypes.c_size_t), ('PriorityClass', wintypes.DWORD), ('SchedulingClass', wintypes.DWORD)]
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class JOBOBJECT_EXTENDED_LIMIT_INFORMATION(ctypes.Structure):
|
|
65
|
+
_fields_ = [('BasicLimitInformation', JOBOBJECT_BASIC_LIMIT_INFORMATION), ('IoInfo', IO_COUNTERS), ('ProcessMemoryLimit', ctypes.c_size_t),
|
|
66
|
+
('JobMemoryLimit', ctypes.c_size_t), ('PeakProcessMemoryUsed', ctypes.c_size_t), ('PeakJobMemoryUsed', ctypes.c_size_t)]
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
for fn, res in (('GetCurrentProcess', wintypes.HANDLE), ('CreateJobObjectW', wintypes.HANDLE), ('GetStdHandle', wintypes.HANDLE)):
|
|
70
|
+
getattr(k32, fn).restype = res
|
|
71
|
+
adv.OpenProcessToken.argtypes = [wintypes.HANDLE, wintypes.DWORD, ctypes.POINTER(wintypes.HANDLE)]
|
|
72
|
+
adv.GetTokenInformation.argtypes = [wintypes.HANDLE, ctypes.c_int, ctypes.c_void_p, wintypes.DWORD, ctypes.POINTER(wintypes.DWORD)]
|
|
73
|
+
adv.SetTokenInformation.argtypes = [wintypes.HANDLE, ctypes.c_int, ctypes.c_void_p, wintypes.DWORD]
|
|
74
|
+
adv.ConvertStringSidToSidW.argtypes = [wintypes.LPCWSTR, ctypes.POINTER(ctypes.c_void_p)]
|
|
75
|
+
adv.CreateRestrictedToken.argtypes = [wintypes.HANDLE, wintypes.DWORD, wintypes.DWORD, ctypes.c_void_p, wintypes.DWORD, ctypes.c_void_p,
|
|
76
|
+
wintypes.DWORD, ctypes.c_void_p, ctypes.POINTER(wintypes.HANDLE)]
|
|
77
|
+
adv.GetLengthSid.argtypes = [ctypes.c_void_p]
|
|
78
|
+
adv.InitializeAcl.argtypes = [ctypes.c_void_p, wintypes.DWORD, wintypes.DWORD]
|
|
79
|
+
adv.AddAccessAllowedAce.argtypes = [ctypes.c_void_p, wintypes.DWORD, wintypes.DWORD, ctypes.c_void_p]
|
|
80
|
+
adv.CreateProcessAsUserW.argtypes = [wintypes.HANDLE, wintypes.LPCWSTR, wintypes.LPWSTR, ctypes.c_void_p, ctypes.c_void_p, wintypes.BOOL,
|
|
81
|
+
wintypes.DWORD, ctypes.c_void_p, wintypes.LPCWSTR, ctypes.POINTER(STARTUPINFOW), ctypes.POINTER(PROCESS_INFORMATION)]
|
|
82
|
+
k32.SetInformationJobObject.argtypes = [wintypes.HANDLE, ctypes.c_int, ctypes.c_void_p, wintypes.DWORD]
|
|
83
|
+
k32.AssignProcessToJobObject.argtypes = [wintypes.HANDLE, wintypes.HANDLE]
|
|
84
|
+
k32.ResumeThread.argtypes = [wintypes.HANDLE]
|
|
85
|
+
k32.WaitForSingleObject.argtypes = [wintypes.HANDLE, wintypes.DWORD]
|
|
86
|
+
k32.GetExitCodeProcess.argtypes = [wintypes.HANDLE, ctypes.POINTER(wintypes.DWORD)]
|
|
87
|
+
k32.SetHandleInformation.argtypes = [wintypes.HANDLE, wintypes.DWORD, wintypes.DWORD]
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def fail(what):
|
|
91
|
+
raise OSError(ctypes.get_last_error(), what + ' failed: ' + ctypes.FormatError(ctypes.get_last_error()).strip())
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def sid(text):
|
|
95
|
+
p = ctypes.c_void_p()
|
|
96
|
+
if not adv.ConvertStringSidToSidW(text, ctypes.byref(p)): fail('ConvertStringSidToSid ' + text)
|
|
97
|
+
return p
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def logon_sid(token):
|
|
101
|
+
n = wintypes.DWORD()
|
|
102
|
+
adv.GetTokenInformation(token, TokenGroups, None, 0, ctypes.byref(n))
|
|
103
|
+
buf = ctypes.create_string_buffer(n.value)
|
|
104
|
+
if not adv.GetTokenInformation(token, TokenGroups, buf, n, ctypes.byref(n)): fail('GetTokenInformation')
|
|
105
|
+
count = ctypes.cast(buf, ctypes.POINTER(wintypes.DWORD))[0]
|
|
106
|
+
offset = ctypes.sizeof(ctypes.c_void_p) # DWORD GroupCount, padded to pointer alignment
|
|
107
|
+
groups = (SID_AND_ATTRIBUTES * count).from_buffer(buf, offset)
|
|
108
|
+
for g in groups:
|
|
109
|
+
if g.Attributes & SE_GROUP_LOGON_ID == SE_GROUP_LOGON_ID: return ctypes.c_void_p(g.Sid), buf
|
|
110
|
+
return None, buf
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class TRUSTEE_W(ctypes.Structure):
|
|
114
|
+
_fields_ = [('pMultipleTrustee', ctypes.c_void_p), ('MultipleTrusteeOperation', ctypes.c_int), ('TrusteeForm', ctypes.c_int),
|
|
115
|
+
('TrusteeType', ctypes.c_int), ('ptstrName', ctypes.c_void_p)]
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
class EXPLICIT_ACCESS_W(ctypes.Structure):
|
|
119
|
+
_fields_ = [('grfAccessPermissions', wintypes.DWORD), ('grfAccessMode', ctypes.c_int), ('grfInheritance', wintypes.DWORD), ('Trustee', TRUSTEE_W)]
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
SE_FILE_OBJECT, DACL_SECURITY_INFORMATION = 1, 4
|
|
123
|
+
GRANT_ACCESS, TRUSTEE_IS_SID = 1, 0
|
|
124
|
+
SUB_CONTAINERS_AND_OBJECTS_INHERIT = 3
|
|
125
|
+
FILE_MODIFY = 0x1301BF # read, write, execute and delete: the "Modify" permission
|
|
126
|
+
adv.GetNamedSecurityInfoW.argtypes = [wintypes.LPCWSTR, ctypes.c_int, wintypes.DWORD, ctypes.c_void_p, ctypes.c_void_p,
|
|
127
|
+
ctypes.POINTER(ctypes.c_void_p), ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p)]
|
|
128
|
+
adv.SetEntriesInAclW.argtypes = [wintypes.ULONG, ctypes.POINTER(EXPLICIT_ACCESS_W), ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p)]
|
|
129
|
+
adv.SetNamedSecurityInfoW.argtypes = [wintypes.LPWSTR, ctypes.c_int, wintypes.DWORD, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p]
|
|
130
|
+
k32.LocalFree.argtypes = [ctypes.c_void_p]
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def allow_modify(folder, sandbox_sid):
|
|
134
|
+
"""Adds an inheritable Modify entry for the sandbox SID; Windows applies it to everything already inside the folder."""
|
|
135
|
+
s = sid(sandbox_sid)
|
|
136
|
+
old, sd, new = ctypes.c_void_p(), ctypes.c_void_p(), ctypes.c_void_p()
|
|
137
|
+
err = adv.GetNamedSecurityInfoW(folder, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, None, None, ctypes.byref(old), None, ctypes.byref(sd))
|
|
138
|
+
if err: raise OSError(err, 'Could not read permissions of ' + folder + ': ' + ctypes.FormatError(err).strip())
|
|
139
|
+
try:
|
|
140
|
+
ea = EXPLICIT_ACCESS_W(FILE_MODIFY, GRANT_ACCESS, SUB_CONTAINERS_AND_OBJECTS_INHERIT, TRUSTEE_W(None, 0, TRUSTEE_IS_SID, 0, s.value))
|
|
141
|
+
err = adv.SetEntriesInAclW(1, ctypes.byref(ea), old, ctypes.byref(new))
|
|
142
|
+
if err: raise OSError(err, 'Could not prepare permissions for ' + folder + ': ' + ctypes.FormatError(err).strip())
|
|
143
|
+
err = adv.SetNamedSecurityInfoW(folder, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, None, None, new, None)
|
|
144
|
+
if err: raise OSError(err, 'Could not allow writes to ' + folder + ': ' + ctypes.FormatError(err).strip())
|
|
145
|
+
finally:
|
|
146
|
+
if new: k32.LocalFree(new)
|
|
147
|
+
if sd: k32.LocalFree(sd)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
adv.ConvertSecurityDescriptorToStringSecurityDescriptorW.argtypes = [ctypes.c_void_p, wintypes.DWORD, wintypes.DWORD,
|
|
151
|
+
ctypes.POINTER(wintypes.LPWSTR), ctypes.c_void_p]
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def has_grant(folder, sandbox_sid):
|
|
155
|
+
"""True when the folder already carries the inheritable entry for the sandbox SID (a single read, no tree walk)."""
|
|
156
|
+
old, sd, text = ctypes.c_void_p(), ctypes.c_void_p(), wintypes.LPWSTR()
|
|
157
|
+
if adv.GetNamedSecurityInfoW(folder, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, None, None, ctypes.byref(old), None, ctypes.byref(sd)): return False
|
|
158
|
+
try:
|
|
159
|
+
if not adv.ConvertSecurityDescriptorToStringSecurityDescriptorW(sd, 1, DACL_SECURITY_INFORMATION, ctypes.byref(text), None): return False
|
|
160
|
+
sddl = text.value or ''
|
|
161
|
+
k32.LocalFree(ctypes.cast(text, ctypes.c_void_p))
|
|
162
|
+
return any(ace.startswith('A;OICI') and ace.endswith(';' + sandbox_sid) for ace in sddl.replace('(', ')').split(')'))
|
|
163
|
+
finally:
|
|
164
|
+
if sd: k32.LocalFree(sd)
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def grant(dirs, sandbox_sid):
|
|
168
|
+
"""Lets the sandbox SID modify each folder and everything inside it. The first grant walks the folder once."""
|
|
169
|
+
for d in dirs:
|
|
170
|
+
if os.path.isdir(d) and not has_grant(d, sandbox_sid): allow_modify(d, sandbox_sid)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
TokenUser = 1
|
|
174
|
+
adv.ConvertSidToStringSidW.argtypes = [ctypes.c_void_p, ctypes.POINTER(wintypes.LPWSTR)]
|
|
175
|
+
adv.GetTokenInformation.argtypes = [wintypes.HANDLE, ctypes.c_int, ctypes.c_void_p, wintypes.DWORD, ctypes.POINTER(wintypes.DWORD)]
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def user_sid(token=None):
|
|
179
|
+
"""String SID of the signed-in user (the token user, which the restricted copy keeps)."""
|
|
180
|
+
if token is None:
|
|
181
|
+
token = wintypes.HANDLE()
|
|
182
|
+
if not adv.OpenProcessToken(k32.GetCurrentProcess(), TOKEN_QUERY, ctypes.byref(token)): fail('OpenProcessToken')
|
|
183
|
+
n = wintypes.DWORD()
|
|
184
|
+
adv.GetTokenInformation(token, TokenUser, None, 0, ctypes.byref(n))
|
|
185
|
+
buf = ctypes.create_string_buffer(n.value)
|
|
186
|
+
if not adv.GetTokenInformation(token, TokenUser, buf, n, ctypes.byref(n)): fail('GetTokenInformation')
|
|
187
|
+
text = wintypes.LPWSTR()
|
|
188
|
+
if not adv.ConvertSidToStringSidW(ctypes.cast(buf, ctypes.POINTER(ctypes.c_void_p))[0], ctypes.byref(text)): fail('ConvertSidToStringSid')
|
|
189
|
+
value = text.value; k32.LocalFree(ctypes.cast(text, ctypes.c_void_p))
|
|
190
|
+
return value
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
# Git Bash (MSYS2, a Cygwin fork) keeps one shared-memory section per user, named after the user's SID, in an object
|
|
194
|
+
# directory such as \Sessions\1\BaseNamedObjects\msys-2.0S5-<key>. The first Git Bash of the user creates it for the
|
|
195
|
+
# user only, and a write-restricted process may open it for writing only if the sandbox SID is also allowed. The
|
|
196
|
+
# launcher (unrestricted, and the owner) adds that entry. Nothing else in the session is changed.
|
|
197
|
+
class UNICODE_STRING(ctypes.Structure):
|
|
198
|
+
_fields_ = [('Length', wintypes.USHORT), ('MaximumLength', wintypes.USHORT), ('Buffer', ctypes.c_void_p)]
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
class OBJECT_ATTRIBUTES(ctypes.Structure):
|
|
202
|
+
_fields_ = [('Length', wintypes.ULONG), ('RootDirectory', wintypes.HANDLE), ('ObjectName', ctypes.POINTER(UNICODE_STRING)),
|
|
203
|
+
('Attributes', wintypes.ULONG), ('SecurityDescriptor', ctypes.c_void_p), ('SecurityQualityOfService', ctypes.c_void_p)]
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
class OBJECT_DIRECTORY_INFORMATION(ctypes.Structure):
|
|
207
|
+
_fields_ = [('Name', UNICODE_STRING), ('TypeName', UNICODE_STRING)]
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
nt = ctypes.WinDLL('ntdll')
|
|
211
|
+
DIRECTORY_QUERY, DIRECTORY_TRAVERSE = 0x1, 0x2
|
|
212
|
+
READ_CONTROL, WRITE_DAC = 0x00020000, 0x00040000
|
|
213
|
+
SE_KERNEL_OBJECT = 6
|
|
214
|
+
KERNEL_OPEN = {'Section': 'NtOpenSection', 'Mutant': 'NtOpenMutant', 'Event': 'NtOpenEvent', 'Semaphore': 'NtOpenSemaphore'}
|
|
215
|
+
KERNEL_ACCESS = {'Section': 0xF001F, 'Mutant': 0x1F0001, 'Event': 0x1F0003, 'Semaphore': 0x1F0003} # full access per type
|
|
216
|
+
adv.GetSecurityInfo.argtypes = [wintypes.HANDLE, ctypes.c_int, wintypes.DWORD, ctypes.c_void_p, ctypes.c_void_p,
|
|
217
|
+
ctypes.POINTER(ctypes.c_void_p), ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p)]
|
|
218
|
+
adv.SetSecurityInfo.argtypes = [wintypes.HANDLE, ctypes.c_int, wintypes.DWORD, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p]
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def nt_open(func, name, access, root=None):
|
|
222
|
+
text = ctypes.create_unicode_buffer(name)
|
|
223
|
+
us = UNICODE_STRING(len(name) * 2, len(name) * 2 + 2, ctypes.cast(text, ctypes.c_void_p))
|
|
224
|
+
oa = OBJECT_ATTRIBUTES(ctypes.sizeof(OBJECT_ATTRIBUTES), root, ctypes.pointer(us), 0x40, None, None) # OBJ_CASE_INSENSITIVE
|
|
225
|
+
h = wintypes.HANDLE()
|
|
226
|
+
status = getattr(nt, func)(ctypes.byref(h), access, ctypes.byref(oa))
|
|
227
|
+
return h if status == 0 else None
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def list_directory(h):
|
|
231
|
+
out, context, buf = [], wintypes.ULONG(0), ctypes.create_string_buffer(4096)
|
|
232
|
+
restart = True
|
|
233
|
+
while True:
|
|
234
|
+
n = wintypes.ULONG()
|
|
235
|
+
if nt.NtQueryDirectoryObject(h, buf, len(buf), True, restart, ctypes.byref(context), ctypes.byref(n)) != 0: return out
|
|
236
|
+
restart = False
|
|
237
|
+
info = OBJECT_DIRECTORY_INFORMATION.from_buffer(buf)
|
|
238
|
+
out.append((ctypes.wstring_at(info.Name.Buffer, info.Name.Length // 2), ctypes.wstring_at(info.TypeName.Buffer, info.TypeName.Length // 2)))
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def allow_kernel_object(h, kind, sandbox_sid):
|
|
242
|
+
old, sd, new = ctypes.c_void_p(), ctypes.c_void_p(), ctypes.c_void_p()
|
|
243
|
+
if adv.GetSecurityInfo(h, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION, None, None, ctypes.byref(old), None, ctypes.byref(sd)): return False
|
|
244
|
+
try:
|
|
245
|
+
s = sid(sandbox_sid)
|
|
246
|
+
ea = EXPLICIT_ACCESS_W(KERNEL_ACCESS[kind], GRANT_ACCESS, 0, TRUSTEE_W(None, 0, TRUSTEE_IS_SID, 0, s.value))
|
|
247
|
+
if adv.SetEntriesInAclW(1, ctypes.byref(ea), old, ctypes.byref(new)): return False
|
|
248
|
+
return adv.SetSecurityInfo(h, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION, None, None, new, None) == 0
|
|
249
|
+
finally:
|
|
250
|
+
if new: k32.LocalFree(new)
|
|
251
|
+
if sd: k32.LocalFree(sd)
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def share_git_bash_objects(sandbox_sid):
|
|
255
|
+
"""Lets sandboxed Git Bash processes use the user's existing MSYS2/Cygwin per-user objects. Returns how many were shared."""
|
|
256
|
+
me, shared = user_sid(), 0
|
|
257
|
+
session = wintypes.DWORD()
|
|
258
|
+
k32.ProcessIdToSessionId(k32.GetCurrentProcessId(), ctypes.byref(session))
|
|
259
|
+
roots = ['\\BaseNamedObjects'] + (['\\Sessions\\%d\\BaseNamedObjects' % session.value] if session.value else [])
|
|
260
|
+
for root in roots:
|
|
261
|
+
top = nt_open('NtOpenDirectoryObject', root, DIRECTORY_QUERY | DIRECTORY_TRAVERSE)
|
|
262
|
+
if not top: continue
|
|
263
|
+
try:
|
|
264
|
+
for name, kind in list_directory(top):
|
|
265
|
+
if kind != 'Directory' or not (name.lower().startswith('msys-') or name.lower().startswith('cygwin1')): continue
|
|
266
|
+
sub = nt_open('NtOpenDirectoryObject', name, DIRECTORY_QUERY | DIRECTORY_TRAVERSE, top)
|
|
267
|
+
if not sub: continue
|
|
268
|
+
try:
|
|
269
|
+
for obj, objkind in list_directory(sub):
|
|
270
|
+
if objkind not in KERNEL_OPEN or me.lower() not in obj.lower(): continue
|
|
271
|
+
h = nt_open(KERNEL_OPEN[objkind], obj, READ_CONTROL | WRITE_DAC, sub)
|
|
272
|
+
if h:
|
|
273
|
+
shared += allow_kernel_object(h, objkind, sandbox_sid)
|
|
274
|
+
k32.CloseHandle(h)
|
|
275
|
+
finally:
|
|
276
|
+
k32.CloseHandle(sub)
|
|
277
|
+
finally:
|
|
278
|
+
k32.CloseHandle(top)
|
|
279
|
+
return shared
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def restricted_token(sandbox_sid):
|
|
283
|
+
base = wintypes.HANDLE()
|
|
284
|
+
access = TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_ASSIGN_PRIMARY | TOKEN_ADJUST_DEFAULT | TOKEN_ADJUST_SESSIONID | TOKEN_ADJUST_PRIVILEGES
|
|
285
|
+
if not adv.OpenProcessToken(k32.GetCurrentProcess(), access, ctypes.byref(base)): fail('OpenProcessToken')
|
|
286
|
+
logon, keep = logon_sid(base)
|
|
287
|
+
sids = [sid('S-1-1-0'), sid(sandbox_sid)] + ([logon] if logon else [])
|
|
288
|
+
arr = (SID_AND_ATTRIBUTES * len(sids))(*[SID_AND_ATTRIBUTES(s.value, 0) for s in sids])
|
|
289
|
+
token = wintypes.HANDLE()
|
|
290
|
+
if not adv.CreateRestrictedToken(base, DISABLE_MAX_PRIVILEGE | LUA_TOKEN | WRITE_RESTRICTED, 0, None, 0, None, len(sids), arr, ctypes.byref(token)):
|
|
291
|
+
fail('CreateRestrictedToken')
|
|
292
|
+
# Objects the sandboxed processes create without a parent folder (pipes, events) must stay usable by them.
|
|
293
|
+
owner = sid(user_sid(base))
|
|
294
|
+
size = 8 + sum(8 + adv.GetLengthSid(s) for s in sids + [owner]) + 64
|
|
295
|
+
acl = ctypes.create_string_buffer(size)
|
|
296
|
+
if not adv.InitializeAcl(acl, size, ACL_REVISION): fail('InitializeAcl')
|
|
297
|
+
for s in sids + [owner, sid('S-1-5-18')]:
|
|
298
|
+
if not adv.AddAccessAllowedAce(acl, ACL_REVISION, GENERIC_ALL, s): fail('AddAccessAllowedAce')
|
|
299
|
+
dacl = TOKEN_DEFAULT_DACL(ctypes.cast(acl, ctypes.c_void_p))
|
|
300
|
+
if not adv.SetTokenInformation(token, TokenDefaultDacl, ctypes.byref(dacl), ctypes.sizeof(dacl)): fail('SetTokenInformation')
|
|
301
|
+
return token, (keep, acl, sids)
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def run(argv, sandbox_sid):
|
|
305
|
+
try: share_git_bash_objects(sandbox_sid)
|
|
306
|
+
except Exception: pass # Git Bash then reports its own error; the sandbox itself is unaffected
|
|
307
|
+
token, keep = restricted_token(sandbox_sid)
|
|
308
|
+
job = k32.CreateJobObjectW(None, None)
|
|
309
|
+
if not job: fail('CreateJobObject')
|
|
310
|
+
info = JOBOBJECT_EXTENDED_LIMIT_INFORMATION()
|
|
311
|
+
info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
|
|
312
|
+
if not k32.SetInformationJobObject(job, JobObjectExtendedLimitInformation, ctypes.byref(info), ctypes.sizeof(info)): fail('SetInformationJobObject')
|
|
313
|
+
si = STARTUPINFOW(); si.cb = ctypes.sizeof(si); si.dwFlags = STARTF_USESTDHANDLES
|
|
314
|
+
handles = []
|
|
315
|
+
for i, n in enumerate((-10, -11, -12)):
|
|
316
|
+
h = k32.GetStdHandle(n)
|
|
317
|
+
if h and h != wintypes.HANDLE(-1).value: k32.SetHandleInformation(h, 1, 1)
|
|
318
|
+
handles.append(h)
|
|
319
|
+
si.hStdInput, si.hStdOutput, si.hStdError = handles
|
|
320
|
+
pi = PROCESS_INFORMATION()
|
|
321
|
+
line = ctypes.create_unicode_buffer(subprocess.list2cmdline(argv))
|
|
322
|
+
if not adv.CreateProcessAsUserW(token, None, line, None, None, True, CREATE_SUSPENDED | CREATE_UNICODE_ENVIRONMENT, None, os.getcwd(),
|
|
323
|
+
ctypes.byref(si), ctypes.byref(pi)):
|
|
324
|
+
fail('CreateProcessAsUser')
|
|
325
|
+
if not k32.AssignProcessToJobObject(job, pi.hProcess): fail('AssignProcessToJobObject')
|
|
326
|
+
k32.ResumeThread(pi.hThread); k32.CloseHandle(pi.hThread)
|
|
327
|
+
k32.WaitForSingleObject(pi.hProcess, INFINITE)
|
|
328
|
+
code = wintypes.DWORD()
|
|
329
|
+
k32.GetExitCodeProcess(pi.hProcess, ctypes.byref(code))
|
|
330
|
+
return code.value
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
def main(args):
|
|
334
|
+
sandbox_sid, dirs, grant_only = None, [], False
|
|
335
|
+
while args and args[0] != '--':
|
|
336
|
+
a = args.pop(0)
|
|
337
|
+
if a == '--sid': sandbox_sid = args.pop(0)
|
|
338
|
+
elif a == '--write': dirs.append(args.pop(0))
|
|
339
|
+
elif a == '--grant-only': grant_only = True
|
|
340
|
+
else: raise SystemExit('unknown option ' + a)
|
|
341
|
+
if not sandbox_sid: raise SystemExit('--sid is required')
|
|
342
|
+
grant(dirs, sandbox_sid)
|
|
343
|
+
if grant_only: return 0
|
|
344
|
+
if not args or args.pop(0) != '--' or not args: raise SystemExit('missing -- program')
|
|
345
|
+
return run(args, sandbox_sid)
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
if __name__ == '__main__':
|
|
349
|
+
try: sys.exit(main(sys.argv[1:]))
|
|
350
|
+
except OSError as e:
|
|
351
|
+
sys.stderr.write('ML AI Code sandbox: ' + str(e) + '\n'); sys.exit(70)
|