Security¶
Ask two questions for every plugin that runs as root (it ships in assets/sudoers/, or its README tells the admin to grant sudo), and again for every parameter you add:
- What else can a local attacker who controls all arguments do? The monitoring user (
icinga/nagios) supplies every value and owns what they can create (/tmp, their home). Follow each parameter to the file, command or socket it reaches, assuming value and filesystem are hostile. -
Who may set the parameter, and with what privileges does the resulting action run? Selecting data the plugin reads back and filters is low risk; redirecting a privileged action (file opened as root, binary executed, socket used) hands over that privilege unless confined. That is the class behind GHSA-q8c8-wxhc-3h4c and GHSA-f54c-p5vg-mr5c.
-
External commands: Use
lib.shell.shell_exec()with an argv list (alwaysshell=False), neveros.system()orsubprocesswithshell=True. Put each user-supplied value in its own element (['restic', f'--repo={repo}', 'check'],['ping', '-q', hostname]), never assemble a command string. A value starting with-can still become an option (an ssh destination-oProxyCommand=...,ping -f), so guard values that reach a command as a positional argument or target withcoe(lib.shell.safe_cli_value(args.HOSTNAME, '--hostname')). Unlike the official Monitoring Plugins guidelines, we accept PATH-based resolution for cross-platform compatibility, aware that a compromised PATH could redirect commands. - Input validation: Validate all user input; use
argparsetype converters (type=int,type=float,type=lib.args.csv). - Temporary files: Avoid them; prefer
lib.db_sqliteorlib.cache. If unavoidable, fail cleanly when the file cannot be created, and delete it when done. -
Confining a path a privileged plugin was pointed at: When a root plugin opens, stats, globs or lists a location a caller-supplied value can steer (
--path,--filename,--socket, a scanned directory or a file found inside it), a planted symlink makes root read e.g./etc/shadow;--path=/var/crashand--path=/tmp/attackerlook alike without a containment check. Confine every edge (directory, each file found, final read):- Reading a file: let
lib.diskopen it,success, content = lib.disk.read_file(candidate, allowed_roots=[root], nofollow=True). Never check and thenopen()yourself (check-then-use;O_NOFOLLOWguards only the last component):lib.disk.open_file(), behindread_file()andread_env(), verifies the opened handle against the path and accepts only regular files. Take everything from that handle; a second look at the path (os.stat(),file_exists(),open()) may see another file. For log lines,lib.logsource.read(..., allowed_roots=...)does the same. - Executing a file, connecting to a socket, or taking an account from a file's owner: these cannot be checked on a handle, so call
success, resolved = lib.disk.resolve_trusted_path(candidate)(lib.base.cu(resolved)on failure) and useresolved, nevercandidate. It requires that only root (and the givenowners) can change the object and every directory above it. References:--commandinapache-httpd-securityandnginx-security,--socketinfail2banandstrongswan-connections, andlib.nextcloud.run_occ(), which runsoccas the owner ofconfig/config.phpand refuses a symlinkedconfig.php. - A location is never trusted by its prefix: the monitoring user owns
/run/icinga2and/var/log/icinga2,/run/user/<uid>exists per session, and/opttrees often belong to service accounts. Only the ownership check decides. - Answer "does not exist" and "not allowed" identically for paths outside the roots, and check the roots first, otherwise the plugin reveals which files exist. Under
--test, read fixtures only throughlib.lftest.test_text()orlib.lftest.test_json(), never withfile_exists()first. - Never weaken this to a filename check (
if name == 'vmcore-dmesg.txt'), which binds the symlink's name, not its target. Where a sharedlibfunction reads, the guard belongs in thelib. As defense in depth, pin arguments in the sudoers entry (see theLF_LIBRENMS_VALIDATEalias inassets/sudoers/), but never rely on it alone. Treat a socket like a binary: a planted one feeds root a chosen response, which is code execution when the client deserializes it (fail2ban-clientusespickle).
- Reading a file: let
-
Credentials: Never log or print passwords, tokens or other secrets, not even in verbose mode.
- Network communication: HTTPS by default. Support
--insecurefor self-signed certificates, never make insecure the default. - Internal management endpoints (Icinga API, BMC, storage controller, backup appliance) are the one exception: they practically always use their own CA, so plugins for them may set
DEFAULT_INSECURE = Trueand must then offer--no-insecure(help=lib.args.help('--no-insecure'),action='store_false') as the counterpart to--insecure(action='store_true'). Both sharedest='INSECURE'and the samedefault=DEFAULT_INSECURE, so declaration order does not matter. Checks against public or customer-facing endpoints keepDEFAULT_INSECURE = Falseand offer--insecureonly.
sudoers File¶
If the plugin requires sudo, add it to the sudoers files of all supported operating systems in assets/sudoers/. File names match ansible_facts['os_family'] (Debian, RedHat, Suse), each with a -logging companion; use symbolic links to prevent duplicates (Suse.sudoers links to RedHat.sudoers).
Attention: The newline at the end is required!