{
  "id": "CB-UBU-AUTH-005",
  "standard": "ubuntu",
  "version": "main",
  "url": "https://baselines.cirsec.org/b/ubuntu/CB-UBU-AUTH-005",
  "json": "https://baselines.cirsec.org/b/ubuntu/CB-UBU-AUTH-005.json",
  "yaml": "https://baselines.cirsec.org/b/ubuntu/CB-UBU-AUTH-005.yml",
  "profiles_effective": [
    "core",
    "server",
    "server-hardened",
    "desktop",
    "desktop-hardened"
  ],
  "license": "Text CC BY 4.0, code Apache-2.0",
  "attribution": "CIRSEC Baselines, https://baselines.cirsec.org",
  "control": {
    "id": "CB-UBU-AUTH-005",
    "title": "Configure sudo to use a pseudo-terminal, a log file and a short credential cache",
    "finding": {
      "title": "sudo defaults allow terminal hijacking, keep no local log or cache credentials too long",
      "description": "Commands run with sudo can escape to the user's terminal, leave traces only in syslog, and a forgotten session stays privileged."
    },
    "status": "active",
    "profiles": [
      "core"
    ],
    "risk": {
      "severity": "medium"
    },
    "assessment": "automated",
    "tags": [
      "sudo",
      "privilege-escalation",
      "logging"
    ],
    "applicability": {
      "facts": [
        "sudo"
      ]
    },
    "description": "Three sudo defaults, set with `Defaults` lines in `/etc/sudoers` or `/etc/sudoers.d/`:\n\n- `use_pty`: run each command in a new pseudo-terminal, so it cannot inject input into the\n  user's terminal after sudo exits;\n- `logfile`: write every sudo command to a dedicated file, in addition to syslog;\n- `timestamp_timeout`: how many minutes a successful authentication is cached before sudo asks\n  for the password again.\n",
    "rationale": "Without `use_pty`, a malicious program run through sudo can keep running in the background and\npush keystrokes into the user's shell. A dedicated log file keeps a readable record of\nprivileged commands even if syslog is lost or flooded. A short cache limits how long an\nunattended terminal can run privileged commands without a password.\n",
    "impact": "Some interactive programs behave differently in a pseudo-terminal (rare). Users type their\npassword again after {{ timestamp_timeout }} minutes of not using sudo.\n",
    "notes": "Ubuntu sets `use_pty` in `/etc/sudoers` since 22.04 (not on 20.04) and caches credentials for\n15 minutes. With the original sudo, the effective values are read with `sudo -V` as root, which\nmerges every sudoers file.\n\nOn Ubuntu 26.04 the `sudo` command is `sudo-rs`. It always runs commands in a pseudo-terminal\nunless `!use_pty` is set, sends every command to syslog (the journal), and has no `logfile`\noption: `visudo` rejects that line as an unknown setting. Its `sudo -V` only prints the version,\nso the checks read the sudoers files instead, and the `logfile` check is not applicable.\n",
    "setting": {
      "location": "/etc/sudoers.d/60-cirsec",
      "default_value": "20.04: no use_pty; 22.04 and later: use_pty; no log file, 15 minutes",
      "recommended_value": "use_pty, logfile={{ log_file }} (not with sudo-rs), timestamp_timeout at most {{ timestamp_timeout }}"
    },
    "parameters": [
      {
        "name": "log_file",
        "type": "string",
        "description": "Path of the sudo log file. Not used by sudo-rs, which logs to syslog only.",
        "default": "/var/log/sudo.log"
      },
      {
        "name": "timestamp_timeout",
        "type": "integer",
        "description": "Maximum number of minutes sudo caches credentials.",
        "unit": "minutes",
        "default": 15,
        "profile_defaults": {
          "server-hardened": 5,
          "desktop-hardened": 5
        },
        "minimum": 0,
        "maximum": 15
      }
    ],
    "audit": {
      "manual": "With the original sudo, run `sudo -V` as root and check that:\n1. it prints `Always run commands in a pseudo-tty`;\n2. `Path to log file:` is set;\n3. `Authentication timestamp timeout:` is at most {{ timestamp_timeout }} minutes.\n\nWith sudo-rs (`sudo --version` starts with `sudo-rs`), read `/etc/sudoers` and\n`/etc/sudoers.d/*`: there is no `Defaults !use_pty`, and the last `timestamp_timeout` is at most\n{{ timestamp_timeout }} (15 when absent).\n",
      "checks": [
        {
          "id": "use-pty",
          "engine": "shell",
          "description": "sudo runs commands in a pseudo-terminal.",
          "spec": {
            "script": "if sudo --version 2>/dev/null | head -n 1 | grep -q '^sudo-rs'; then\n  l=$(cat /etc/sudoers /etc/sudoers.d/* 2>/dev/null | grep -E '^[[:space:]]*Defaults.*![[:space:]]*use_pty')\n  [ -z \"$l\" ] || { echo \"sudo-rs: $l\"; exit 1; }\n  echo \"sudo-rs: use_pty (default, not disabled)\"\n  exit 0\nfi\nsudo -V 2>/dev/null | grep -q 'Always run commands in a pseudo-tty' || { echo \"use_pty not set\"; exit 1; }\necho \"use_pty set\"\n"
          }
        },
        {
          "id": "logfile",
          "engine": "shell",
          "description": "sudo writes to a log file (not applicable to sudo-rs, which only logs to syslog).",
          "spec": {
            "script": "if sudo --version 2>/dev/null | head -n 1 | grep -q '^sudo-rs'; then\n  echo \"sudo-rs has no logfile option; commands are logged to syslog\"\n  exit 2\nfi\nf=$(sudo -V 2>/dev/null | sed -n 's/^Path to log file: //p')\necho \"logfile=${f:-none}\"\n[ -n \"$f\" ]\n"
          }
        },
        {
          "id": "timeout",
          "engine": "shell",
          "description": "sudo credential cache lifetime.",
          "spec": {
            "script": "if sudo --version 2>/dev/null | head -n 1 | grep -q '^sudo-rs'; then\n  t=$(cat /etc/sudoers /etc/sudoers.d/* 2>/dev/null | grep -E '^[[:space:]]*Defaults' \\\n    | sed -n 's/.*timestamp_timeout[[:space:]]*=[[:space:]]*\\([0-9-]*\\).*/\\1/p' | tail -n 1)\n  t=${t:-15}\nelse\n  t=$(sudo -V 2>/dev/null | sed -n 's/^Authentication timestamp timeout: \\([0-9-]*\\).*/\\1/p')\nfi\necho \"timestamp_timeout=${t:-unknown} minutes\"\n[ -n \"$t\" ] || exit 3\n[ \"$t\" -ge 0 ] && [ \"$t\" -le \"$CB_TIMESTAMP_TIMEOUT\" ]\n"
          }
        }
      ]
    },
    "remediation": {
      "manual": "Create `/etc/sudoers.d/60-cirsec` with `visudo -f /etc/sudoers.d/60-cirsec`:\n```\nDefaults use_pty\nDefaults logfile=\"{{ log_file }}\"\nDefaults timestamp_timeout={{ timestamp_timeout }}\n```\nLeave out the `logfile` line with sudo-rs (Ubuntu 26.04): sudo-rs rejects it. Remove any\n`timestamp_timeout` with a higher value from other sudoers files.\n",
      "effects": {
        "restart": "none",
        "disruption": "low",
        "lockout_risk": false
      },
      "rollback": "Delete `/etc/sudoers.d/60-cirsec`.\n",
      "scripts": [
        {
          "id": "dropin",
          "engine": "shell",
          "content": "f=/etc/sudoers.d/60-cirsec\nt=$(mktemp)\n{\n  echo '# Written by CIRSEC Baselines CB-UBU-AUTH-005'\n  echo 'Defaults use_pty'\n  sudo --version 2>/dev/null | head -n 1 | grep -q '^sudo-rs' || printf 'Defaults logfile=\"%s\"\\n' \"$CB_LOG_FILE\"\n  echo \"Defaults timestamp_timeout=$CB_TIMESTAMP_TIMEOUT\"\n} >\"$t\"\nif ! visudo -cqf \"$t\" >/dev/null; then rm -f \"$t\"; echo \"invalid sudoers drop-in\" >&2; exit 1; fi\ncb_backup \"$f\"\ninstall -m 0440 -o root -g root \"$t\" \"$f\"\nrm -f \"$t\"\n"
        }
      ]
    },
    "references": [
      {
        "title": "sudoers(5), Ubuntu manual pages",
        "url": "https://manpages.ubuntu.com/manpages/noble/man5/sudoers.5.html",
        "publisher": "Canonical",
        "kind": "vendor-doc",
        "accessed": "2026-10-19"
      },
      {
        "title": "sudo-rs(8), Ubuntu manual pages",
        "url": "https://manpages.ubuntu.com/manpages/noble/man8/sudo-rs.8.html",
        "publisher": "Canonical",
        "kind": "vendor-doc",
        "accessed": "2026-10-19"
      },
      {
        "title": "Ubuntu Server documentation - sudo-rs and Rust coreutils",
        "url": "https://documentation.ubuntu.com/server/reference/other-tools/sudo-rs/",
        "publisher": "Canonical",
        "kind": "vendor-doc",
        "accessed": "2026-10-19"
      },
      {
        "title": "ANSSI-BP-028 v2.0 - Recommandations de configuration d'un système GNU/Linux (R39)",
        "url": "https://messervices.cyber.gouv.fr/documents-guides/fr_np_linux_configuration-v2.0.pdf",
        "publisher": "ANSSI",
        "kind": "guide",
        "accessed": "2026-10-19"
      }
    ],
    "mappings": [
      {
        "framework": "anssi-linux",
        "framework_version": "2.0",
        "id": "R39",
        "relation": "partial",
        "note": "R39 lists several sudo directives; this control covers use_pty, logging and the cache."
      },
      {
        "framework": "nist-800-53r5",
        "framework_version": "5.1.1",
        "id": "AU-2",
        "relation": "related"
      }
    ],
    "threats": [
      {
        "framework": "mitre-attack",
        "id": "T1548.003"
      }
    ],
    "authors": [
      {
        "name": "Alice",
        "github": "Sn0wAlice"
      }
    ],
    "verified": [
      {
        "date": "2026-10-19",
        "by": "Sn0wAlice",
        "versions": [
          "20.04"
        ],
        "environment": "Proxmox VM, Ubuntu 20.04 cloud image, kernel 5.4.0-216-generic",
        "notes": "Fails on a fresh install (sudo 1.8.31, no use_pty, no log file); drop-in validated by visudo fixes it; sudo of the default user still works; 5 minutes in server-hardened; kept after reboot."
      },
      {
        "date": "2026-10-19",
        "by": "Sn0wAlice",
        "versions": [
          "22.04"
        ],
        "environment": "Proxmox VM, Ubuntu 22.04 cloud image, kernel 5.15.0-198-generic",
        "notes": "Fails on a fresh install (no log file); drop-in validated by visudo fixes it; sudo of the default user still works; 5 minutes in server-hardened; kept after reboot."
      },
      {
        "date": "2026-10-19",
        "by": "Sn0wAlice",
        "versions": [
          "24.04"
        ],
        "environment": "Proxmox VM, Ubuntu 24.04 cloud image, kernel 6.8.0-147-generic",
        "notes": "Fails on a fresh install (no log file); drop-in validated by visudo fixes it; sudo of the default user still works; 5 minutes in server-hardened; kept after reboot."
      },
      {
        "date": "2026-10-19",
        "by": "Sn0wAlice",
        "versions": [
          "26.04"
        ],
        "environment": "Proxmox VM, Ubuntu 26.04 cloud image, kernel 7.0.0-38-generic",
        "notes": "sudo-rs 0.2.13 - use_pty by default passes, logfile not applicable, timeout read from sudoers; drop-in without logfile accepted by visudo, sudo still works, 5 minutes in server-hardened; kept after reboot."
      }
    ],
    "introduced_in": "0.1.0",
    "created": "2026-10-19",
    "updated": "2026-10-19"
  }
}