From 4f8d7f0a6b72a2e7da9d15a1ead9e8436be914e6 Mon Sep 17 00:00:00 2001 From: Yaroslav Pietukhov <77742707+valsoray-dev@users.noreply.github.com> Date: Sat, 31 May 2025 00:22:52 +0300 Subject: [PATCH] Disallow running Zed with root privileges (#31331) This will fix a lot of weird problems that are based on file access issues. As discussed in https://github.com/zed-industries/zed/pull/31219#issuecomment-2905371710, for now it's better to just prevent running Zed with root privileges. Release Notes: - Explicitly disallow running Zed with root privileges --------- Co-authored-by: Peter Tripp --- crates/zed/Cargo.toml | 2 +- crates/zed/src/main.rs | 18 ++++++++++++++++++ tooling/workspace-hack/Cargo.toml | 8 ++++---- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/crates/zed/Cargo.toml b/crates/zed/Cargo.toml index f51e5b3251a038f0f5626097440adad0c3c69f7f..a38064b8d8584759a568fbb5e0d425ef8d6067ed 100644 --- a/crates/zed/Cargo.toml +++ b/crates/zed/Cargo.toml @@ -85,7 +85,7 @@ markdown_preview.workspace = true menu.workspace = true migrator.workspace = true mimalloc = { version = "0.1", optional = true } -nix = { workspace = true, features = ["pthread", "signal"] } +nix = { workspace = true, features = ["pthread", "signal", "user"] } node_runtime.workspace = true notifications.workspace = true outline.workspace = true diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index 54606f76b0ec3dfb1270eb4f1544ff32d1195d95..04bd9b7140ffa16302bffc5e3acecdd040c1441b 100644 --- a/crates/zed/src/main.rs +++ b/crates/zed/src/main.rs @@ -164,6 +164,24 @@ fn fail_to_open_window(e: anyhow::Error, _cx: &mut App) { } fn main() { + #[cfg(unix)] + { + let is_root = nix::unistd::geteuid().is_root(); + let allow_root = env::var("ZED_ALLOW_ROOT").is_ok_and(|val| val == "true"); + + // Prevent running Zed with root privileges on Unix systems unless explicitly allowed + if is_root && !allow_root { + eprintln!( + "\ +Error: Running Zed as root or via sudo is unsupported. + Doing so (even once) may subtly break things for all subsequent non-root usage of Zed. + It is untested and not recommended, don't complain when things break. + If you wish to proceed anyways, set `ZED_ALLOW_ROOT=true` in your environment." + ); + process::exit(1); + } + } + // Check if there is a pending installer // If there is, run the installer and exit // And we don't want to run the installer if we are not the first instance diff --git a/tooling/workspace-hack/Cargo.toml b/tooling/workspace-hack/Cargo.toml index f84209c55b95c5fcd6d5c733ae40cf0b719ff907..e830fe1524927b8f172c6d379a5b1eea111680b2 100644 --- a/tooling/workspace-hack/Cargo.toml +++ b/tooling/workspace-hack/Cargo.toml @@ -289,7 +289,7 @@ gimli = { version = "0.31", default-features = false, features = ["read", "std", hyper-rustls = { version = "0.27", default-features = false, features = ["http1", "http2", "native-tokio", "ring", "tls12"] } itertools-5ef9efb8ec2df382 = { package = "itertools", version = "0.12" } naga = { version = "25", features = ["msl-out", "wgsl-in"] } -nix = { version = "0.29", features = ["fs", "pthread", "signal"] } +nix = { version = "0.29", features = ["fs", "pthread", "signal", "user"] } objc2 = { version = "0.6" } objc2-foundation = { version = "0.3", default-features = false, features = ["NSArray", "NSAttributedString", "NSBundle", "NSCoder", "NSData", "NSDate", "NSDictionary", "NSEnumerator", "NSError", "NSGeometry", "NSNotification", "NSNull", "NSObjCRuntime", "NSObject", "NSProcessInfo", "NSRange", "NSRunLoop", "NSString", "NSURL", "NSUndoManager", "NSValue", "objc2-core-foundation", "std"] } objc2-metal = { version = "0.3" } @@ -318,7 +318,7 @@ gimli = { version = "0.31", default-features = false, features = ["read", "std", hyper-rustls = { version = "0.27", default-features = false, features = ["http1", "http2", "native-tokio", "ring", "tls12"] } itertools-5ef9efb8ec2df382 = { package = "itertools", version = "0.12" } naga = { version = "25", features = ["msl-out", "wgsl-in"] } -nix = { version = "0.29", features = ["fs", "pthread", "signal"] } +nix = { version = "0.29", features = ["fs", "pthread", "signal", "user"] } objc2 = { version = "0.6" } objc2-foundation = { version = "0.3", default-features = false, features = ["NSArray", "NSAttributedString", "NSBundle", "NSCoder", "NSData", "NSDate", "NSDictionary", "NSEnumerator", "NSError", "NSGeometry", "NSNotification", "NSNull", "NSObjCRuntime", "NSObject", "NSProcessInfo", "NSRange", "NSRunLoop", "NSString", "NSURL", "NSUndoManager", "NSValue", "objc2-core-foundation", "std"] } objc2-metal = { version = "0.3" } @@ -347,7 +347,7 @@ gimli = { version = "0.31", default-features = false, features = ["read", "std", hyper-rustls = { version = "0.27", default-features = false, features = ["http1", "http2", "native-tokio", "ring", "tls12"] } itertools-5ef9efb8ec2df382 = { package = "itertools", version = "0.12" } naga = { version = "25", features = ["msl-out", "wgsl-in"] } -nix = { version = "0.29", features = ["fs", "pthread", "signal"] } +nix = { version = "0.29", features = ["fs", "pthread", "signal", "user"] } objc2 = { version = "0.6" } objc2-foundation = { version = "0.3", default-features = false, features = ["NSArray", "NSAttributedString", "NSBundle", "NSCoder", "NSData", "NSDate", "NSDictionary", "NSEnumerator", "NSError", "NSGeometry", "NSNotification", "NSNull", "NSObjCRuntime", "NSObject", "NSProcessInfo", "NSRange", "NSRunLoop", "NSString", "NSURL", "NSUndoManager", "NSValue", "objc2-core-foundation", "std"] } objc2-metal = { version = "0.3" } @@ -376,7 +376,7 @@ gimli = { version = "0.31", default-features = false, features = ["read", "std", hyper-rustls = { version = "0.27", default-features = false, features = ["http1", "http2", "native-tokio", "ring", "tls12"] } itertools-5ef9efb8ec2df382 = { package = "itertools", version = "0.12" } naga = { version = "25", features = ["msl-out", "wgsl-in"] } -nix = { version = "0.29", features = ["fs", "pthread", "signal"] } +nix = { version = "0.29", features = ["fs", "pthread", "signal", "user"] } objc2 = { version = "0.6" } objc2-foundation = { version = "0.3", default-features = false, features = ["NSArray", "NSAttributedString", "NSBundle", "NSCoder", "NSData", "NSDate", "NSDictionary", "NSEnumerator", "NSError", "NSGeometry", "NSNotification", "NSNull", "NSObjCRuntime", "NSObject", "NSProcessInfo", "NSRange", "NSRunLoop", "NSString", "NSURL", "NSUndoManager", "NSValue", "objc2-core-foundation", "std"] } objc2-metal = { version = "0.3" }