Skip to content

Commit

Permalink
Workspace: Add sudo
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorNelson committed Sep 7, 2024
1 parent ab73b2a commit fb5e863
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
2 changes: 1 addition & 1 deletion workspace/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fi

nix build --print-build-logs ".#$DOJO_WORKSPACE" --out-link /out/nix/var/nix/profiles/default
nix copy --to /out --no-require-sigs ".#$DOJO_WORKSPACE"
for suid_binary in "python-suid" "bash-suid" "sh-suid" "windows"; do
for suid_binary in "python-suid" "bash-suid" "sh-suid" "sudo"; do
suid_binary_path=$(realpath "/out/nix/var/nix/profiles/default/bin/${suid_binary}")
FILE="/out/${suid_binary_path}"
if [ -e $FILE ]; then
Expand Down
8 changes: 2 additions & 6 deletions workspace/core/init.nix
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,8 @@ let
exec > /run/dojo/var/root/init.log 2>&1
chmod 600 /run/dojo/var/root/init.log
# TODO: Better support privileged mode
if [ "$DOJO_MODE" = "privileged" ] && [ -f /usr/bin/sudo ]; then
chmod 4755 /usr/bin/sudo
usermod -aG sudo hacker
echo 'hacker ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
passwd -d root
if [ "$DOJO_MODE" = "privileged" ]; then
touch /run/dojo/var/root/privileged
fi
if [ -x "/challenge/.init" ]; then
Expand Down
21 changes: 21 additions & 0 deletions workspace/core/sudo.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{ pkgs }:

pkgs.stdenv.mkDerivation {
name = "sudo";
src = ./sudo.py;

unpackPhase = ''
runHook preUnpack
cp $src $PWD
runHook postUnpack
'';

installPhase = ''
runHook preInstall
mkdir -p $out/bin
echo "#!/usr/bin/env python-suid" > $out/bin/sudo
cat ${./sudo.py} >> $out/bin/sudo
chmod +x $out/bin/sudo
runHook postInstall
'';
}
35 changes: 35 additions & 0 deletions workspace/core/sudo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import os
import shutil
import sys


def error(message):
print(message, file=sys.stderr)
sys.exit(1)


def main():
program = os.path.basename(sys.argv[0])

if not os.path.exists("/run/dojo/var/root/priviliged"):
error(f"{program}: workspace is not privileged")

os.setuid(os.geteuid())

if len(sys.argv) < 2:
error(f"Usage: {program} <command> [args...]")

command = sys.argv[1]
command_path = shutil.which(sys.argv[1])
if not command_path:
error(f"{program}: {command}: command not found")
argv = sys.argv[1:]

try:
os.execve(command_path, argv, os.environ)
except:
os.exit(1)


if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions workspace/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

init = import ./core/init.nix { inherit pkgs; };
suid-interpreter = import ./core/suid-interpreter.nix { inherit pkgs; };
sudo = import ./core/sudo.nix { inherit pkgs; };
ssh-entrypoint = import ./core/ssh-entrypoint.nix { inherit pkgs; };
service = import ./services/service.nix { inherit pkgs; };
code-service = import ./services/code.nix { inherit pkgs; };
Expand Down Expand Up @@ -66,6 +67,7 @@
(lib.hiPrio ldd)

init
sudo
ssh-entrypoint
service
code-service
Expand Down

0 comments on commit fb5e863

Please sign in to comment.