Skip to content

Commit

Permalink
Restrict namespace arguments to Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
blechschmidt committed Apr 7, 2024
1 parent 80675ef commit 7d3c48a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@ pub struct Args {

/// Create a tun interface in a newly created unprivileged namespace
/// while maintaining proxy connectivity via the global network namespace.
#[cfg(target_os = "linux")]
#[arg(long)]
pub unshare: bool,

/// File descriptor for UNIX datagram socket meant to transfer
/// network sockets from global namespace to the new one.
/// See `unshare(1)`, `namespaces(7)`, `sendmsg(2)`, `unix(7)`.
#[arg(long, value_name = "fd")]
#[cfg(target_os = "linux")]
#[arg(long, value_name = "fd", hide(true))]
pub socket_transfer_fd: Option<i32>,

/// Specify a command to run with root-like capabilities in the new namespace.
/// Specify a command to run with root-like capabilities in the new namespace
/// when using `--unshare`.
/// This could be useful to start additional daemons, e.g. `openvpn` instance.
#[cfg(target_os = "linux")]
#[arg(requires = "unshare")]
pub admin_command: Vec<OsString>,

Expand Down Expand Up @@ -91,8 +95,11 @@ impl Default for Args {
proxy: ArgProxy::default(),
tun: None,
tun_fd: None,
#[cfg(target_os = "linux")]
unshare: false,
#[cfg(target_os = "linux")]
socket_transfer_fd: None,
#[cfg(target_os = "linux")]
admin_command: Vec::new(),
ipv6_enabled: false,
setup,
Expand Down
9 changes: 5 additions & 4 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ async fn main() -> Result<(), BoxError> {
let join_handle = tokio::spawn({
let shutdown_token = shutdown_token.clone();
async move {
#[cfg(target_os = "linux")]
if args.unshare && args.socket_transfer_fd.is_none() {
#[cfg(target_os = "linux")]
if let Err(err) = namespace_proxy_main(args, shutdown_token).await {
log::error!("namespace proxy error: {}", err);
}
#[cfg(not(target_os = "linux"))]
log::error!("Your platform doesn't support unprivileged namespaces");
} else if let Err(err) = tun2proxy::desktop_run_async(args, shutdown_token).await {
return;
}

if let Err(err) = tun2proxy::desktop_run_async(args, shutdown_token).await {
log::error!("main loop error: {}", err);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/desktop_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ pub async fn desktop_run_async(args: Args, shutdown_token: tokio_util::sync::Can
run_ip_util(format!("-6 route delete 80::/1 dev {}", tproxy_args.tun_name));
}

#[cfg(target_os = "linux")]
if setup && args.unshare {
// New namespace doesn't have any other routing device by default
// So our `tun` device should act as such to make space for other proxies.
Expand All @@ -166,6 +167,7 @@ pub async fn desktop_run_async(args: Args, shutdown_token: tokio_util::sync::Can
}
}

#[cfg(target_os = "linux")]
let mut admin_command_args = args.admin_command.iter();
if let Some(command) = admin_command_args.next() {
let child = tokio::process::Command::new(command)
Expand Down

0 comments on commit 7d3c48a

Please sign in to comment.