---
title: Avoid world-writable directory permissions
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > Code Security > Static Code Analysis (SAST) > SAST
  Rules > Avoid world-writable directory permissions
---

# Avoid world-writable directory permissions

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com, us2.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ({% placeholder "user-datadog-site-name" /%}).
{% /alert %}

{% /callout %}

## Metadata{% #metadata %}

**ID:** `rust-security/mkdir-world-writable-permissions`

**Language:** Rust

**Severity:** Warning

**Category:** Security

**CWE**: [732](https://cwe.mitre.org/data/definitions/732.html)

**Related CWEs**:

- [276](https://cwe.mitre.org/data/definitions/276.html)
- [281](https://cwe.mitre.org/data/definitions/281.html)
- [766](https://cwe.mitre.org/data/definitions/766.html)

## Description{% #description %}

Creating a directory with `DirBuilder::new().mode(0o777)` (or any other mode whose "other" triplet has the write bit set) lets any local user add, remove, or replace files inside the directory regardless of the individual files' permissions. This can enable symlink attacks, race conditions during file replacement, and privilege escalation if a privileged process later reads or executes files from the directory. Prefer `0o755` for directories that need to be world-readable or `0o750` to restrict to a group.

#### Learn More{% #learn-more %}

- [CWE-732: Incorrect Permission Assignment for Critical Resource](https://cwe.mitre.org/data/definitions/732.html)

## Non-Compliant Code Examples{% #non-compliant-code-examples %}

```rust
use std::fs::DirBuilder;
use std::os::unix::fs::DirBuilderExt;

fn invalid() -> std::io::Result<()> {
    // World-writable — classic mistakes
    DirBuilder::new().mode(0o777).create("/tmp/a")?;
    DirBuilder::new().mode(0o666).create("/tmp/b")?;

    // Other world-writable triplets
    DirBuilder::new().mode(0o776).create("/tmp/c")?;    // last digit 6 — rw for others
    DirBuilder::new().mode(0o002).create("/tmp/d")?;    // last digit 2 — write only
    DirBuilder::new().mode(0o773).create("/tmp/e")?;    // last digit 3 — wx for others

    // 4-digit (with sticky/setuid) — still world-writable
    DirBuilder::new().mode(0o7777).create("/tmp/f")?;

    // Fully qualified path
    std::fs::DirBuilder::new().mode(0o777).create("/tmp/g")?;

    // Underscore separators
    DirBuilder::new().mode(0o7_7_7).create("/tmp/h")?;

    Ok(())
}
```

## Compliant Code Examples{% #compliant-code-examples %}

```rust
use std::fs::DirBuilder;
use std::os::unix::fs::DirBuilderExt;

fn valid() -> std::io::Result<()> {
    // Valid modes — no world write
    DirBuilder::new().mode(0o755).create("/tmp/a")?;     // world-readable, not writable
    DirBuilder::new().mode(0o750).create("/tmp/b")?;     // group-readable only
    DirBuilder::new().mode(0o700).create("/tmp/c")?;     // owner-only
    DirBuilder::new().mode(0o770).create("/tmp/d")?;     // group-writable but NOT world
    DirBuilder::new().mode(0o740).create("/tmp/e")?;     // last digit 0 — fine

    Ok(())
}
```
  Seamless integrations. Try Datadog Code SecurityDatadog Code Security 
{% icon name="icon-external-link" /%}
 