52 lines
1.3 KiB
Python
Executable File
52 lines
1.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Sandbox escape hatch for execute_code elevated steps.
|
|
|
|
Connects to the socket at OLLIE_ELEVATE_SOCKET, sends the command and cwd,
|
|
and returns the result. The adapter listening on that socket is responsible
|
|
for running the command outside the Landlock sandbox.
|
|
|
|
Usage: elevate -- <cmd>
|
|
"""
|
|
import json
|
|
import os
|
|
import socket
|
|
import sys
|
|
|
|
|
|
def main():
|
|
sock_path = os.environ.get("OLLIE_ELEVATE_SOCKET")
|
|
if not sock_path:
|
|
print("elevate: OLLIE_ELEVATE_SOCKET not set", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
if len(sys.argv) < 3:
|
|
print("usage: elevate -- <cmd>", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
req = json.dumps({"cmd": sys.argv[2], "cwd": os.getcwd()}).encode() + b"\n"
|
|
|
|
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
try:
|
|
sock.connect(sock_path)
|
|
except OSError as e:
|
|
print(f"elevate: connect {sock_path}: {e}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
with sock:
|
|
sock.sendall(req)
|
|
sock.shutdown(socket.SHUT_WR)
|
|
data = b""
|
|
while chunk := sock.recv(65536):
|
|
data += chunk
|
|
|
|
resp = json.loads(data)
|
|
if resp.get("stdout"):
|
|
sys.stdout.write(resp["stdout"])
|
|
if resp.get("stderr"):
|
|
sys.stderr.write(resp["stderr"])
|
|
sys.exit(resp.get("exit_code", 0))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|