diff --git a/docs/decisions/0009-command-allowlist-and-privileges.md b/docs/decisions/0009-command-allowlist-and-privileges.md index 236bb47..781944d 100644 --- a/docs/decisions/0009-command-allowlist-and-privileges.md +++ b/docs/decisions/0009-command-allowlist-and-privileges.md @@ -1,6 +1,6 @@ # 0009. Run three allowlisted tools with fixed arguments; no smpquery, no sudo, no ibdiagnet -Status: Accepted (2026-11-15) +Status: Accepted (2026-11-15); allowlisted tool files added to the sweep check (2026-11-19) ## Context @@ -19,7 +19,7 @@ Status: Accepted (2026-11-15) - A test enumerates every vector the builder produces. - Commands run without a shell. They run with `PATH` and `LC_ALL` only, from `/`, with standard input from `/dev/null`, and with a time and output limit. Output that is cut short is never parsed. A failure is recorded as an error, never as a zero. - No `smpquery`. A link below best is reported as below best with the cause unknown, and its degraded value is unknown (`U`) until the UFM backend supplies enabled values. -- `sudo` is never used and never documented. The documented grant is a dedicated group with mode 0660 on `umad*`. The plugin records on its health page when it runs as root, set-ID, from a tool directory others can write to, or with a world-accessible UMAD node. The tool directory itself must be an absolute, canonical path to a real directory with no symbolic link in it, owned by root, and writable by neither group nor others. The settings form refuses any other directory, and each sweep checks it again and runs nothing when it fails. +- `sudo` is never used and never documented. The documented grant is a dedicated group with mode 0660 on `umad*`. The plugin records on its health page when it runs as root, set-ID, from a tool directory others can write to, or with a world-accessible UMAD node. The tool directory itself must be an absolute, canonical path to a real directory with no symbolic link in it, owned by root, and writable by neither group nor others. The settings form refuses any other directory, and each sweep checks it again and runs nothing when it fails. Each allowlisted tool present in it must also be owned by root, writable by neither group nor others, and not set-ID; a sweep that finds one that is not runs nothing, because whoever can write the file chooses what the poller runs. The health page still records these conditions as warnings. - `ibdiagnet` is excluded from phases 1 to 3. ## Consequences diff --git a/lib/PrivilegeProbe.php b/lib/PrivilegeProbe.php index 83acd6c..f3be3e3 100644 --- a/lib/PrivilegeProbe.php +++ b/lib/PrivilegeProbe.php @@ -40,11 +40,12 @@ public function __construct(private readonly string $toolDir, private readonly s /** * Why no tool may run from the directory, checked again before every - * sweep because the directory can change after the setting was saved. - * Null when it passes ToolDirectory's checks. + * sweep because the directory or a tool can change after the setting was + * saved. Null when the directory and every allowlisted tool in it pass + * ToolDirectory's checks. */ public function refusal(): ?string { - return ToolDirectory::problem($this->toolDir, $this->toolOwner); + return ToolDirectory::problem($this->toolDir, $this->toolOwner) ?? ToolDirectory::toolProblem($this->toolDir, $this->toolOwner); } /** diff --git a/lib/ToolDirectory.php b/lib/ToolDirectory.php index bcd882f..b91be2f 100644 --- a/lib/ToolDirectory.php +++ b/lib/ToolDirectory.php @@ -28,7 +28,7 @@ * writable by neither its group nor others. */ final class ToolDirectory { - public const CODES = ['path', 'missing', 'symlink', 'owner', 'writable']; + public const CODES = ['path', 'missing', 'symlink', 'owner', 'writable', 'tool_owner', 'tool_writable', 'tool_setid']; /** * @param int $owner the required owner; root except in tests, which @@ -64,14 +64,50 @@ public static function problem(string $dir, int $owner = 0): ?string { return ($stat['mode'] & 0o022) !== 0 ? 'writable' : null; } + /** + * The first check an allowlisted tool inside the directory fails, or + * null. A directory that passes still lets a tool be replaced in place + * by whoever can write the file, and a set-ID tool runs as someone else. + * A tool that does not exist is not a problem here; running it fails + * and is recorded as such. A symbolic link is judged by its target. + */ + public static function toolProblem(string $dir, int $owner = 0): ?string { + foreach (Tool::cases() as $tool) { + $path = $dir . '/' . $tool->value; + + clearstatcache(true, $path); + + if (!is_file($path) || ($stat = stat($path)) === false) { + continue; + } + + if ($stat['uid'] !== $owner) { + return 'tool_owner'; + } + + if (($stat['mode'] & 0o022) !== 0) { + return 'tool_writable'; + } + + if (($stat['mode'] & 0o6000) !== 0) { + return 'tool_setid'; + } + } + + return null; + } + public static function message(string $code): string { return match ($code) { - 'path' => __('The tool directory must be an absolute path of letters, digits, dots, dashes, underscores, and slashes, with no trailing slash.', 'fabric'), - 'missing' => __('The tool directory does not exist or is not a directory.', 'fabric'), - 'symlink' => __('The tool directory path contains a symbolic link. Enter the real path.', 'fabric'), - 'owner' => __('The tool directory is not owned by root.', 'fabric'), - 'writable' => __('The tool directory can be written by its group or by others.', 'fabric'), - default => __('Unknown warning', 'fabric'), + 'path' => __('The tool directory must be an absolute path of letters, digits, dots, dashes, underscores, and slashes, with no trailing slash.', 'fabric'), + 'missing' => __('The tool directory does not exist or is not a directory.', 'fabric'), + 'symlink' => __('The tool directory path contains a symbolic link. Enter the real path.', 'fabric'), + 'owner' => __('The tool directory is not owned by root.', 'fabric'), + 'writable' => __('The tool directory can be written by its group or by others.', 'fabric'), + 'tool_owner' => __('A diagnostics tool is not owned by root.', 'fabric'), + 'tool_writable' => __('A diagnostics tool can be written by its group or by others.', 'fabric'), + 'tool_setid' => __('A diagnostics tool has the setuid or setgid bit. The plugin never needs it.', 'fabric'), + default => __('Unknown warning', 'fabric'), }; } } diff --git a/locales/LC_MESSAGES/ar-SA.mo b/locales/LC_MESSAGES/ar-SA.mo index 93665e6..70b9744 100644 Binary files a/locales/LC_MESSAGES/ar-SA.mo and b/locales/LC_MESSAGES/ar-SA.mo differ diff --git a/locales/LC_MESSAGES/es-419.mo b/locales/LC_MESSAGES/es-419.mo index 776d3a1..e668c82 100644 Binary files a/locales/LC_MESSAGES/es-419.mo and b/locales/LC_MESSAGES/es-419.mo differ diff --git a/locales/LC_MESSAGES/es-ES.mo b/locales/LC_MESSAGES/es-ES.mo index 9a1cbd1..6e598f1 100644 Binary files a/locales/LC_MESSAGES/es-ES.mo and b/locales/LC_MESSAGES/es-ES.mo differ diff --git a/locales/LC_MESSAGES/hi-IN.mo b/locales/LC_MESSAGES/hi-IN.mo index 0a01954..b5cf54e 100644 Binary files a/locales/LC_MESSAGES/hi-IN.mo and b/locales/LC_MESSAGES/hi-IN.mo differ diff --git a/locales/LC_MESSAGES/zh-CN.mo b/locales/LC_MESSAGES/zh-CN.mo index 6e6929e..1250b15 100644 Binary files a/locales/LC_MESSAGES/zh-CN.mo and b/locales/LC_MESSAGES/zh-CN.mo differ diff --git a/locales/po/ar-SA.po b/locales/po/ar-SA.po index c463da7..7b75f85 100644 --- a/locales/po/ar-SA.po +++ b/locales/po/ar-SA.po @@ -837,7 +837,7 @@ msgstr "عرض Fabric" msgid "The sweep runs as root. Run the poller as an unprivileged account with read access to the umad devices through a group." msgstr "يعمل الفحص بصلاحيات root. شغّل المُستطلِع بحساب غير ذي امتيازات يملك حق قراءة أجهزة umad عبر مجموعة." -#: lib/PrivilegeProbe.php +#: lib/PrivilegeProbe.php lib/ToolDirectory.php msgid "A diagnostics tool has the setuid or setgid bit. The plugin never needs it." msgstr "تحتوي إحدى أدوات التشخيص على بت setuid أو setgid. لا تحتاج الإضافة إليهما مطلقًا." @@ -957,6 +957,14 @@ msgstr "دليل الأدوات غير مملوك لـ root." msgid "The tool directory can be written by its group or by others." msgstr "يمكن للمجموعة أو للآخرين الكتابة إلى دليل الأدوات." +#: lib/ToolDirectory.php +msgid "A diagnostics tool is not owned by root." +msgstr "إحدى أدوات التشخيص غير مملوكة لـ root." + +#: lib/ToolDirectory.php +msgid "A diagnostics tool can be written by its group or by others." +msgstr "يمكن للمجموعة أو للآخرين الكتابة إلى إحدى أدوات التشخيص." + #: setup.php #, php-format msgid "The Fabric plugin requires PHP %s or later. This server runs PHP %s, so the plugin was not installed or enabled." diff --git a/locales/po/cacti.pot b/locales/po/cacti.pot index d248bcf..fa3a7fb 100644 --- a/locales/po/cacti.pot +++ b/locales/po/cacti.pot @@ -839,7 +839,7 @@ msgstr "" msgid "The sweep runs as root. Run the poller as an unprivileged account with read access to the umad devices through a group." msgstr "" -#: lib/PrivilegeProbe.php +#: lib/PrivilegeProbe.php lib/ToolDirectory.php msgid "A diagnostics tool has the setuid or setgid bit. The plugin never needs it." msgstr "" @@ -959,6 +959,14 @@ msgstr "" msgid "The tool directory can be written by its group or by others." msgstr "" +#: lib/ToolDirectory.php +msgid "A diagnostics tool is not owned by root." +msgstr "" + +#: lib/ToolDirectory.php +msgid "A diagnostics tool can be written by its group or by others." +msgstr "" + #: setup.php #, php-format msgid "The Fabric plugin requires PHP %s or later. This server runs PHP %s, so the plugin was not installed or enabled." diff --git a/locales/po/es-419.po b/locales/po/es-419.po index a38a13f..160c899 100644 --- a/locales/po/es-419.po +++ b/locales/po/es-419.po @@ -837,7 +837,7 @@ msgstr "Vista de Fabric" msgid "The sweep runs as root. Run the poller as an unprivileged account with read access to the umad devices through a group." msgstr "La consulta se ejecuta como root. Ejecute el sondeador con una cuenta sin privilegios que tenga acceso de lectura a los dispositivos umad mediante un grupo." -#: lib/PrivilegeProbe.php +#: lib/PrivilegeProbe.php lib/ToolDirectory.php msgid "A diagnostics tool has the setuid or setgid bit. The plugin never needs it." msgstr "Una herramienta de diagnóstico tiene el bit setuid o setgid. El complemento nunca lo necesita." @@ -957,6 +957,14 @@ msgstr "El directorio de herramientas no pertenece a root." msgid "The tool directory can be written by its group or by others." msgstr "El grupo u otros usuarios pueden escribir en el directorio de herramientas." +#: lib/ToolDirectory.php +msgid "A diagnostics tool is not owned by root." +msgstr "Una herramienta de diagnóstico no pertenece a root." + +#: lib/ToolDirectory.php +msgid "A diagnostics tool can be written by its group or by others." +msgstr "El grupo u otros usuarios pueden escribir en una herramienta de diagnóstico." + #: setup.php #, php-format msgid "The Fabric plugin requires PHP %s or later. This server runs PHP %s, so the plugin was not installed or enabled." diff --git a/locales/po/es-ES.po b/locales/po/es-ES.po index daa11ac..357cd56 100644 --- a/locales/po/es-ES.po +++ b/locales/po/es-ES.po @@ -837,7 +837,7 @@ msgstr "Vista de Fabric" msgid "The sweep runs as root. Run the poller as an unprivileged account with read access to the umad devices through a group." msgstr "La consulta se ejecuta como root. Ejecute el sondeador con una cuenta sin privilegios que tenga acceso de lectura a los dispositivos umad mediante un grupo." -#: lib/PrivilegeProbe.php +#: lib/PrivilegeProbe.php lib/ToolDirectory.php msgid "A diagnostics tool has the setuid or setgid bit. The plugin never needs it." msgstr "Una herramienta de diagnóstico tiene el bit setuid o setgid. El complemento nunca lo necesita." @@ -957,6 +957,14 @@ msgstr "El directorio de herramientas no pertenece a root." msgid "The tool directory can be written by its group or by others." msgstr "El grupo u otros usuarios pueden escribir en el directorio de herramientas." +#: lib/ToolDirectory.php +msgid "A diagnostics tool is not owned by root." +msgstr "Una herramienta de diagnóstico no pertenece a root." + +#: lib/ToolDirectory.php +msgid "A diagnostics tool can be written by its group or by others." +msgstr "El grupo u otros usuarios pueden escribir en una herramienta de diagnóstico." + #: setup.php #, php-format msgid "The Fabric plugin requires PHP %s or later. This server runs PHP %s, so the plugin was not installed or enabled." diff --git a/locales/po/hi-IN.po b/locales/po/hi-IN.po index c45dec0..4674724 100644 --- a/locales/po/hi-IN.po +++ b/locales/po/hi-IN.po @@ -837,7 +837,7 @@ msgstr "Fabric दृश्य" msgid "The sweep runs as root. Run the poller as an unprivileged account with read access to the umad devices through a group." msgstr "सर्वेक्षण root के रूप में चलता है। पोलर को ऐसे गैर-विशेषाधिकार खाते से चलाएँ जिसे समूह के माध्यम से umad डिवाइस पढ़ने की अनुमति हो।" -#: lib/PrivilegeProbe.php +#: lib/PrivilegeProbe.php lib/ToolDirectory.php msgid "A diagnostics tool has the setuid or setgid bit. The plugin never needs it." msgstr "किसी निदान उपकरण पर setuid या setgid बिट सेट है। प्लगइन को इसकी कभी आवश्यकता नहीं होती।" @@ -957,6 +957,14 @@ msgstr "उपकरण निर्देशिका का स्वामी msgid "The tool directory can be written by its group or by others." msgstr "उपकरण निर्देशिका को उसका समूह या अन्य उपयोगकर्ता लिख सकते हैं।" +#: lib/ToolDirectory.php +msgid "A diagnostics tool is not owned by root." +msgstr "कोई निदान उपकरण root के स्वामित्व में नहीं है।" + +#: lib/ToolDirectory.php +msgid "A diagnostics tool can be written by its group or by others." +msgstr "किसी निदान उपकरण को उसका समूह या अन्य उपयोगकर्ता लिख सकते हैं।" + #: setup.php #, php-format msgid "The Fabric plugin requires PHP %s or later. This server runs PHP %s, so the plugin was not installed or enabled." diff --git a/locales/po/zh-CN.po b/locales/po/zh-CN.po index 856c170..3b98746 100644 --- a/locales/po/zh-CN.po +++ b/locales/po/zh-CN.po @@ -837,7 +837,7 @@ msgstr "Fabric 查看" msgid "The sweep runs as root. Run the poller as an unprivileged account with read access to the umad devices through a group." msgstr "扫描以 root 身份运行。请使用非特权账户运行采集器,并通过组授予其 umad 设备读取权限。" -#: lib/PrivilegeProbe.php +#: lib/PrivilegeProbe.php lib/ToolDirectory.php msgid "A diagnostics tool has the setuid or setgid bit. The plugin never needs it." msgstr "某个诊断工具设置了 setuid 或 setgid 位。插件永远不需要这些位。" @@ -957,6 +957,14 @@ msgstr "工具目录不属于 root。" msgid "The tool directory can be written by its group or by others." msgstr "工具目录可由其所属组或其他用户写入。" +#: lib/ToolDirectory.php +msgid "A diagnostics tool is not owned by root." +msgstr "某个诊断工具的所有者不是 root。" + +#: lib/ToolDirectory.php +msgid "A diagnostics tool can be written by its group or by others." +msgstr "某个诊断工具可由其所属组或其他用户写入。" + #: setup.php #, php-format msgid "The Fabric plugin requires PHP %s or later. This server runs PHP %s, so the plugin was not installed or enabled." diff --git a/setup.php b/setup.php index 01d9807..b25e45a 100644 --- a/setup.php +++ b/setup.php @@ -321,11 +321,22 @@ function fabric_config_arrays(): void { $menu[__('Management')]['plugins/fabric/' . $file] = $title; } - if (in_array(get_current_page(), \Somethingwithproof\Fabric\Pages::upgradeTriggers(), true)) { + if (fabric_repair_on_page(get_current_page())) { plugin_fabric_upgrade(); } } +/** + * Whether config_arrays may repair the install on this page. Core fires + * config_arrays before include/auth.php checks the session, so a request + * that is about to be redirected to the login page gets here too. Only a + * request that already carries a user may start the migrations and the + * template import; an anonymous one leaves them to the next page load. + */ +function fabric_repair_on_page(string $page): bool { + return !empty($_SESSION['sess_user_id']) && in_array($page, \Somethingwithproof\Fabric\Pages::upgradeTriggers(), true); +} + function fabric_config_settings(): void { global $tabs, $settings; diff --git a/tests/Unit/BootTest.php b/tests/Unit/BootTest.php index 2165afd..d57e3de 100644 --- a/tests/Unit/BootTest.php +++ b/tests/Unit/BootTest.php @@ -85,3 +85,25 @@ 'device_remove' => 'fabric_device_remove', ]); }); + +// core fires config_arrays before auth.php checks the session +it('repairs the install from config_arrays only for a request that carries a user', function () { + $saved = $_SESSION ?? null; + + try { + $_SESSION = []; + expect(fabric_repair_on_page('plugins.php'))->toBeFalse() + ->and(fabric_repair_on_page('fabric.php'))->toBeFalse(); + + $_SESSION = ['sess_user_id' => 3]; + expect(fabric_repair_on_page('plugins.php'))->toBeTrue() + ->and(fabric_repair_on_page('fabric.php'))->toBeTrue() + ->and(fabric_repair_on_page('graphs.php'))->toBeFalse(); + } finally { + if ($saved === null) { + unset($_SESSION); + } else { + $_SESSION = $saved; + } + } +}); diff --git a/tests/Unit/ValueTest.php b/tests/Unit/ValueTest.php index f8bac60..baf02d5 100644 --- a/tests/Unit/ValueTest.php +++ b/tests/Unit/ValueTest.php @@ -215,6 +215,38 @@ } }); +it('refuses a tool that is not root-owned, is writable by group or others, or is set-ID', function () { + $scratch = new Somethingwithproof\Fabric\Tests\Scratch(); + + try { + $dir = (string) realpath($scratch->dir); + $owner = (int) fileowner($dir); + chmod($dir, 0755); + $tool = $scratch->file('perfquery', '#!/bin/sh', 0755); + + $probe = new Somethingwithproof\Fabric\PrivilegeProbe($dir, toolOwner: $owner); + + // An absent tool is not a refusal; running it fails and is recorded. + expect($probe->refusal())->toBeNull(); + + foreach ([0775 => 'tool_writable', 0757 => 'tool_writable', 04755 => 'tool_setid', 02755 => 'tool_setid', 0755 => null] as $mode => $code) { + chmod($tool, $mode); + expect($probe->refusal())->toBe($code); + } + + expect((new Somethingwithproof\Fabric\PrivilegeProbe($dir, toolOwner: $owner + 1))->refusal())->toBe('owner'); + + // The directory passes for its owner; the tool is judged on its own owner. + expect(ToolDirectory::toolProblem($dir, $owner + 1))->toBe('tool_owner'); + + $scratch->file('iblinkinfo', '#!/bin/sh', 0777); + chmod($tool, 0755); + expect($probe->refusal())->toBe('tool_writable'); + } finally { + $scratch->remove(); + } +}); + it('reads settings with defaults, bounds, and safe paths', function () { $defaults = Settings::fromRow([]);