Compare commits

..

1 Commits
main ... dbus

Author SHA1 Message Date
Martin 920eb49941
add dbus and dri 2021-06-27 18:31:05 +02:00
19 changed files with 51 additions and 659 deletions

4
.gitignore vendored
View File

@ -1,4 +1,2 @@
.idea
bwbox
result
scripts/applications
main

View File

@ -1,13 +0,0 @@
# Package
version = "1.0.0"
author = "mawalu"
description = "An experimental sandbox tool for linux apps"
license = "MIT"
srcDir = "."
bin = @["bwbox"]
# Dependencies
requires "nim >= 1.6.0"

View File

@ -1,6 +1,6 @@
{
"mount": [],
"romount": ["/etc", "/var", "/usr", "/opt"],
"romount": ["/etc", "/var", "/usr", "/opt", ".oh-my-zsh", ".zsh", ".zshrc"],
"symlinks": [
{"src": "usr/lib", "dst": "/lib"},
{"src": "usr/lib64", "dst": "/lib64"},

View File

@ -1,4 +0,0 @@
{
"extends": "shell",
"mountcwd": true
}

View File

@ -1,6 +0,0 @@
{
"extends": "shell",
"romount": [".gitconfig", ".gnupg", "/run/user/1000/gnupg", ".ssh/config"],
"mountcwd": true,
"mount": [".ssh/known_hosts"]
}

View File

@ -1,7 +0,0 @@
{
"extends": "default",
"romount": [".Xauthority", "/tmp/.X11-unix", "/run/user/1000/pulse/native"],
"dbus": true,
"dbuscall": ["org.freedesktop.Notifications.*=@/org/freedesktop/Notifications", "org.freedesktop.portal.*=*"],
"dbusbroadcast": ["org.freedesktop.portal.*=@/org/freedesktop/portal/*"]
}

View File

@ -1,5 +0,0 @@
{
"extends": "default",
"romount": [".oh-my-zsh", ".zsh", ".zshrc", ".zshrc-local"],
"sethostname": true
}

View File

@ -1,7 +0,0 @@
{
"extends": "default",
"romount": ["/run/user/1000/pulse/native", "/run/user/1000/wayland-1"],
"dbus": true,
"dbuscall": ["org.freedesktop.Notifications.*=@/org/freedesktop/Notifications", "org.freedesktop.portal.*=*"],
"dbusbroadcast": ["org.freedesktop.portal.*=@/org/freedesktop/portal/*"]
}

View File

@ -1,26 +0,0 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1720893482,
"narHash": "sha256-fGQczQ3JuvqSK3rYsJvvbE7j8BENLp8DqJH1B0uXYKg=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "94c843e8f05bac70e905c48c965ba7be79bde613",
"type": "github"
},
"original": {
"owner": "NixOS",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

View File

@ -1,20 +0,0 @@
{
description = "An experimental sandboxing tool for linux apps";
inputs.nixpkgs.url = github:NixOS/nixpkgs;
outputs = { self, nixpkgs }: {
packages.x86_64-linux.default =
with import nixpkgs { system = "x86_64-linux"; };
buildNimPackage {
name = "bwbox";
src = self;
nativeBuildInputs = [pkgs.makeWrapper];
postInstall = ''
wrapProgram $out/bin/bwbox \
--prefix PATH ':' ${pkgs.bubblewrap}/bin \
--prefix PATH ':' ${pkgs.xdg-dbus-proxy}/bin
'';
};
};
}

View File

@ -1,14 +1,14 @@
import parseopt
import options
import os
type Args* = object
name*: Option[string]
cmd*: Option[seq[string]]
cmd*: Option[string]
profile*: Option[string]
debug*: bool
proc getCmd*(args: Args): seq[string] =
return args.cmd.get(@[getEnv("SHELL", "/bin/sh")])
proc getCmd*(args: Args): string =
return args.cmd.get(getEnv("SHELL", "/bin/bash"))
proc getProfile*(args: Args): string =
if args.profile.isSome:
@ -16,34 +16,30 @@ proc getProfile*(args: Args): string =
return "default"
proc parseArgs*(): Option[Args] =
var args = Args(debug: false)
var command = newSeq[string]()
var parsingSandboxArgs = true
var i = 1
while i <= paramCount():
var arg = paramStr(i)
if arg == "--name" and parsingSandboxArgs:
args.name = some(paramStr(i + 1))
i += 2
elif arg == "--profile" and parsingSandboxArgs:
args.profile = some(paramStr(i + 1))
i += 2
elif arg == "--debug" and parsingSandboxArgs:
args.debug = true
i += 1
else:
parsingSandboxArgs = false
command.add(arg)
i += 1
if command.len > 0:
args.cmd = some(command)
if args.name.isSome or args.cmd.isSome or args.profile.isSome:
return some(args)
proc parseOpt(args: var Args, key: string, value: string): bool =
case key
of "command", "c":
args.cmd = some(value)
of "profile", "p":
args.profile = some(value)
else:
return none(Args)
return false
return true
proc parseArgs*(): Option[Args] =
var p = initOptParser()
var args = Args()
while true:
p.next()
case p.kind
of cmdEnd: break
of cmdShortOption, cmdLongOption:
if p.val == "" or args.parseOpt(p.key, p.val) == false:
echo "Invalid argument ", p.val
return
of cmdArgument:
args.name = some(p.key.string)
return some(args)

View File

@ -1,9 +1,8 @@
import os
import posix
import sequtils
type BwrapCall* = object
args*: seq[string]
args: seq[string]
proc addArg*(call: var BwrapCall, args: varargs[string]): var BwrapCall {.discardable.} =
for arg in args:
@ -15,4 +14,4 @@ proc addMount*(call: var BwrapCall, mType: string, path: string): var BwrapCall
call
proc exec*(call: var BwrapCall) =
discard execv("/usr/bin/env", allocCStringArray(@["/usr/bin/env", "bwrap"].concat(call.args)))
discard execv("/usr/bin/bwrap", allocCStringArray(@["bwrap"].concat(call.args)))

View File

@ -3,7 +3,6 @@ import options
import bwrap
import utils
import json
import os
type Link* = object
src*: string
@ -24,7 +23,6 @@ type Config* = object
dbusown*: Option[seq[string]]
dbuscall*: Option[seq[string]]
dbusbroadcast*: Option[seq[string]]
devmount*: Option[seq[string]]
proc applyConfig*(call: var BwrapCall, config: Config) =
for mount in config.mount.get(@[]):
@ -36,14 +34,6 @@ proc applyConfig*(call: var BwrapCall, config: Config) =
for symlink in config.symlinks.get(@[]):
call.addArg("--symlink", symlink.src, symlink.dst)
for device in config.devmount.get(@[]):
call.addArg("--dev-bind", device, device)
if config.mountcwd.get(false):
call
.addMount("--bind", getCurrentDir())
.addArg("--chdir", getCurrentDir())
proc loadConfig*(path: string): Config =
return readFile(path)
.parseJson()
@ -63,7 +53,6 @@ proc extendConfig*(config: var Config): Config {.discardable.} =
config.mountcwd = some(config.mountcwd.get(eConf.mountcwd.get(false)))
config.sethostname = some(config.sethostname.get(eConf.sethostname.get(false)))
config.allowdri = some(config.allowdri.get(eConf.allowdri.get(false)))
config.devmount = some(config.devmount.get(eConf.devmount.get(@[])))
config.dbus = some(config.dbus.get(eConf.dbus.get(false)))
config.dbussee = some(config.dbussee.get(@[]).concat(eConf.dbussee.get(@[])))

View File

@ -10,10 +10,10 @@ type DbusProxy* = object
socket*: string
args: seq[string]
proc exec*(proxy: DbusProxy): Process =
proc exec*(proxy: var DbusProxy) =
# todo: start dbus proxy in bwrap
# todo: pass arguments as fd
startProcess("xdg-dbus-proxy", args = proxy.args,
proxy.process = startProcess("xdg-dbus-proxy", args = proxy.args,
options = {poEchoCmd, poParentStreams, poUsePath})
proc startDBusProxy*(config: Config, hostname: string): DbusProxy =
@ -49,6 +49,6 @@ proc startDBusProxy*(config: Config, hostname: string): DbusProxy =
proxy.args.add("--filter")
proxy.args.add("--log")
proxy.process = proxy.exec()
proxy.exec()
proxy

View File

@ -1,5 +1,4 @@
import strutils
import sequtils
import options
import config
import utils
@ -37,15 +36,9 @@ proc sandboxExec*(args: Args) =
config.extendConfig()
call
.addArg("--new-session")
.addArg("--dev", "/dev")
.addMount("--dev-bind", "/dev/random")
.addMount("--dev-bind", "/dev/urandom")
.addMount("--ro-bind", "/sys/block")
.addMount("--ro-bind", "/sys/bus")
.addMount("--ro-bind", "/sys/class")
.addMount("--ro-bind", "/sys/dev")
.addMount("--ro-bind", "/sys/devices")
.addArg("--tmpfs", "/tmp")
.addArg("--tmpfs", "/dev/shm")
.addArg("--proc", "/proc")
@ -55,10 +48,6 @@ proc sandboxExec*(args: Args) =
.addArg("--setenv", "BWSANDBOX", "1")
.applyConfig(config)
if config.sethostname.get(false):
call
.addArg("--hostname", hostname)
if config.dbus.get(false):
# todo: handle process and cleanup later
let proxy = startDBusProxy(config, hostname)
@ -71,11 +60,13 @@ proc sandboxExec*(args: Args) =
if config.allowdri.get(false):
enableDri(call)
# resolve binary path outside of the sandbox
var cmd = args.getCmd
cmd[0] = findExe(cmd[0])
if config.mountcwd.get(false):
call
.addMount("--bind", getCurrentDir())
.addArg("--chdir", getCurrentDir())
echo call.args.join(" ")
echo cmd
if config.sethostname.get(false):
call
.addArg("--hostname", hostname)
call.addArg(cmd).exec()
call.addArg(args.getCmd).exec()

View File

@ -15,19 +15,9 @@ proc checkRelativePath*(p: string): string =
getHomeDir().joinPath(p)
proc getProfilePath*(profile: string): string =
let pid = getCurrentProcessId()
for path in [
getConfigDir().joinPath(APP_NAME),
&"/usr/share/{APP_NAME}",
parentDir(expandSymlink(&"/proc/{pid}/exe")).joinPath("configs")
]:
let file = path.joinPath(profile)
if fileExists(file):
return file
raise newException(IOError, "Profile not found")
getConfigDir()
.joinPath(APP_NAME)
.joinPath(profile)
proc getProfilePath*(args: Args): string =
getProfilePath(args.getProfile())
@ -41,30 +31,17 @@ proc deviceExists(path: string): bool =
var res: Stat
return stat(path, res) >= 0 and S_ISCHR(res.st_mode)
proc mountDriFolder(call: var BwrapCall, path: string) =
for file in walkPattern(&"{path}/*"):
if dirExists(file):
mountDriFolder(call, file)
elif deviceExists(file):
call.addMount("--dev-bind", file)
#else:
# call.addMount("--ro-bin", file)
# https://github.com/flatpak/flatpak/blob/1bdbb80ac57df437e46fce2cdd63e4ff7704718b/common/flatpak-run.c#L1496
proc enableDri*(call: var BwrapCall) =
const folder = "/dev/dri"
const mounts = [
folder, # general
"/dev/dri", # general
"/dev/mali", "/dev/mali0", "/dev/umplock", # mali
"/dev/nvidiactl", "/dev/nvidia-modeset", # nvidia
"/dev/nvidia-uvm", "/dev/nvidia-uvm-tools" # nvidia OpenCl/CUDA
]
if dirExists(folder):
mountDriFolder(call, folder)
for mount in mounts:
if deviceExists(mount) or dirExists(mount):
if deviceExists(mount):
call.addMount("--dev-bind", mount)
for i in 0..20:

441
log

File diff suppressed because one or more lines are too long

View File

@ -7,7 +7,7 @@ proc main(): int =
let args = parseArgs()
if args.isNone:
echo "Usage: bwshell --name=sandbox_name --profile=profile <sandbox_cmd>"
echo "Usage: bwshell --command=cmd --profile=profile <sandbox_name>"
return 1
else:
randomize()

View File

@ -1,29 +0,0 @@
#!/run/current-system/sw/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 <target_dir>"
exit 1
fi
check_dir() {
local dir=$1
local file
for application in "$dir/"*; do
file="$(basename "$application")"
sed "s/^Exec=/Exec=bwbox --name '$file' --profile wayland /gi" "$application" > "$target/$file"
done
}
dirs=($(echo "$XDG_DATA_DIRS" | tr ':' '\n'))
dirs+=("$HOME/.local/share")
target="$1"
mkdir -p "$target"
for dir in "${dirs[@]}"; do
if [ -d "$dir/applications" ]; then
check_dir "$dir/applications"
fi
done