Skip to contentSkip to conversation

sandbox: shell-quote the command written to .sandboxrc

PATCH In review Stephen Smalley 1 participant 2 messages Sep 28 – Sep 29 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/1 sandbox: shell-quote the command written to .sandboxrc

sandbox -X writes the argv into ~/.sandboxrc as

    cmd 'arg1' 'arg2' ...

with argv[0] unquoted and argv[1:] wrapped in bare single quotes with no escaping. An argument containing a single quote terminates the quoting and the remainder is parsed as shell, and any space or shell metacharacter in argv[0] is interpreted rather than passed through. The same string is also written after "#TITLE:" and read back by sandboxX.sh for the Xephyr window title, so a newline in an argument turns the tail into a second uncommented script line.

This runs after setexeccon() inside the sandbox domain with the invoker's own argv, so no boundary is crossed; it just means 'sandbox -X -- prog "two words"' and similar have never worked.

Build the command with shlex.join() so every element is quoted, and derive the #TITLE: line separately with control characters stripped and truncated to what sandboxX.sh's cut -b8-80 will keep.

---
 sandbox/sandbox | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
sandbox/sandbox +6 -4
index d909fc44..6a18b660 100644
@@ -27,6 +27,7 @@ import socket
 import random
 import fcntl
 import shutil
+import shlex
 import re
 import subprocess
 import selinux
@@ -274,9 +275,10 @@ class Sandbox:
 /etc/gdm/Xsession
 """)
         else:
-            command = self.__paths[0] + " "
-            for p in self.__paths[1:]:
-                command += "'%s' " % p
+            command = shlex.join(self.__paths)
+            # #TITLE: is read back by sandboxX.sh with grep|cut for the
+            # window title; keep it on one line and printable.
+            title = re.sub(r"[^ -~]", " ", " ".join(self.__paths))[:72]
             fd.write("""#! /bin/sh
 #TITLE: %s
 # /usr/bin/test -r ~/.xmodmap && /usr/bin/xmodmap ~/.xmodmap
@@ -285,7 +287,7 @@ if which dbus-run-session >/dev/null 2>&1; then
 else
     dbus-launch --exit-with-session %s
 fi
-""" % (command, command, command))
+""" % (title, command, command))
         fd.close()
         os.chmod(execfile, 0o700)
 
Signature · 1 line
2.55.0
author maintainer
#1 lore raw
Quoted 17 lines · Stephen Smalley

sandbox -X writes the argv into ~/.sandboxrc as

    cmd 'arg1' 'arg2' ...

with argv[0] unquoted and argv[1:] wrapped in bare single quotes with no escaping. An argument containing a single quote terminates the quoting and the remainder is parsed as shell, and any space or shell metacharacter in argv[0] is interpreted rather than passed through. The same string is also written after "#TITLE:" and read back by sandboxX.sh for the Xephyr window title, so a newline in an argument turns the tail into a second uncommented script line.

This runs after setexeccon() inside the sandbox domain with the invoker's own argv, so no boundary is crossed; it just means 'sandbox -X -- prog "two words"' and similar have never worked.

Quoted 5 lines

Build the command with shlex.join() so every element is quoted, and derive the #TITLE: line separately with control characters stripped and truncated to what sandboxX.sh's cut -b8-80 will keep.

This has been merged.