{"record":{"id":"15da9ede819ae376","repo":"jdx/mise","slug":"too-many-symbolic-links-in-managed-directory","errorCode":null,"errorMessage":"too many symbolic links in managed directory {}","messagePattern":"too many symbolic links in managed directory (.+?)","errorType":"error_code","errorClass":"eyre::Report","httpStatus":null,"severity":"error","filePath":"src/system/managed_files.rs","lineNumber":1650,"sourceCode":"/// Open an absolute directory path one component at a time without following\n/// symlinks, creating missing components with process-default metadata. The\n/// returned descriptor binds later metadata changes to the directory that was\n/// actually opened instead of resolving the path again.\n#[cfg(unix)]\nfn open_or_create_directory_tree(path: &Path) -> Result<std::os::fd::OwnedFd> {\n    open_or_create_directory_tree_inner(path, 0)\n}\n\n#[cfg(unix)]\nfn open_or_create_directory_tree_inner(\n    path: &Path,\n    followed_symlinks: usize,\n) -> Result<std::os::fd::OwnedFd> {\n    use nix::fcntl::{AtFlags, OFlag, open, openat};\n    use nix::sys::stat::{Mode, SFlag, fstat, fstatat, mkdirat};\n\n    if followed_symlinks > 40 {\n        bail!(\n            \"too many symbolic links in managed directory {}\",\n            path.display()\n        );\n    }\n\n    let components = path\n        .strip_prefix(Path::new(\"/\"))\n        .wrap_err_with(|| format!(\"managed directory must be absolute: {}\", path.display()))?\n        .components()\n        .map(|component| match component {\n            std::path::Component::Normal(name) => Ok(name.to_os_string()),\n            _ => bail!(\"invalid managed directory path: {}\", path.display()),\n        })\n        .collect::<Result<Vec<_>>>()?;\n\n    let flags = OFlag::O_RDONLY | OFlag::O_DIRECTORY | OFlag::O_NOFOLLOW;\n    let mut directory = open(Path::new(\"/\"), flags, Mode::empty())?;\n    let mut current = PathBuf::from(\"/\");","sourceCodeStart":1632,"sourceCodeEnd":1668,"githubUrl":"https://github.com/jdx/mise/blob/533346cc374382b41ec5ff70536252b2e96e725c/src/system/managed_files.rs#L1632-L1668","documentation":"The managed-directory creation walks path components using openat with O_NOFOLLOW, tracking every symlink it must traverse. If more than 40 symlinks are followed along one path, it aborts with this error to break out of symlink loops or prohibitively deep symlink chains, protecting against infinite recursion (ELOOP-style) and malicious symlink attacks.","triggerScenarios":"Creating a managed directory under a path that contains a symlink cycle (e.g. /a -> /b, /b -> /a) or an unusually deep chain of symlinked directories (>40 hops).","commonSituations":"Misconfigured mount/symlink setups creating directory loops; chroot or overlay environments with recursive symlinks; intentionally deep symlinked directory structures.","solutions":["Inspect the path for symlink cycles with `namei <path>` or `readlink -f` and break the loop","Reduce symlink indirection by targeting real directories","Reconfigure the managed path to avoid traversing attacker- or misconfiguration-controlled symlinks"],"exampleFix":"// before\ncreate_managed_dir(Path::new(\"/linked/loop/etc/app\"))?; // /linked/loop points back through itself\n// after\ncreate_managed_dir(Path::new(\"/etc/app\"))?; // real directory, no symlink chain","handlingStrategy":"validation","validationCode":"fn symlink_depth_ok(path: &std::path::Path) -> bool {\n    std::fs::canonicalize(path).is_ok() // canonicalize itself fails on loops\n}","typeGuard":"fn resolves_without_loop(path: &std::path::Path) -> bool {\n    path.canonicalize().is_ok()\n}","tryCatchPattern":"match result {\n    Err(e) if e.to_string().contains(\"too many symbolic links\") => {\n        eprintln!(\"symlink loop or over-deep chain; inspect with namei\");\n    }\n    other => other?,\n}","preventionTips":["Run `namei -l <path>` on complex symlinked layouts before managed operations","Avoid symlink cycles in deployment directories","Keep symlink indirection shallow in tool-managed trees"],"tags":["symlink","loop","security"],"backgroundTag":"path-traversal-blocked","analyzedSha":"533346cc374382b41ec5ff70536252b2e96e725c","analyzedAt":"2026-09-17T13:35:38.149Z","contentChangedAt":"2026-09-17T13:35:38.149Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}