diff --git a/rx b/rx index 6185740..01236bb 100755 --- a/rx +++ b/rx @@ -20,6 +20,18 @@ -- slots for the few values a caller may supply. See rx(8), rx.conf(5). -- +-- Load modules only from trusted, absolute locations. flua's default +-- package.path and package.cpath include the current directory +-- ("./?.lua", "./?.so"), and LUA_PATH/LUA_CPATH in the environment can +-- redirect them further. rx verifies the owner and mode of every file +-- it trusts; the code it imports must meet the same bar, or a ucl.lua +-- planted in the working directory would run in place of the base +-- system module. Pin both to flua's own LUA_LDIR and LUA_CDIR before +-- the first require, so neither the cwd nor the environment is trusted. +package.path = "/usr/share/flua/?.lua;/usr/share/flua/?/init.lua;" + .. "/usr/lib/flua/?.lua;/usr/lib/flua/?/init.lua" +package.cpath = "/usr/lib/flua/?.so" + local ucl = require("ucl") local lfs = require("lfs") local unistd = require("posix.unistd") diff --git a/tests/rx_test b/tests/rx_test index 7d5e68e..d52367e 100755 --- a/tests/rx_test +++ b/tests/rx_test @@ -384,6 +384,24 @@ usage_errors_body() { atf_check -s exit:64 -e match:usage "$(rx)" -c "$(pwd)/etc" check x } +# A ucl.lua planted in the working directory must never be loaded in +# place of the base system module (see the module-path pinning near the +# top of rx). This is the regression test for that fix: without it, any +# invocation of rx from an attacker-writable cwd - including the root +# `rx check` in the setup instructions - would run the planted code. +atf_test_case ignores_cwd_module +ignores_cwd_module_body() { + setup + cat > ucl.lua <<-'LUA' + os.execute("touch cwd_module_loaded") + LUA + atf_check -o ignore "$(rx)" -c "$(pwd)/etc" check + atf_check test ! -e cwd_module_loaded + # the real request path must be unaffected by the hostile cwd + ask alice "show tag=v1.2.3" -o match:'^\[tag=v1\.2\.3\]$' + atf_check test ! -e cwd_module_loaded +} + atf_init_test_cases() { atf_add_test_case env_is_built_from_config atf_add_test_case verb_env_and_vars @@ -413,4 +431,5 @@ atf_init_test_cases() { atf_add_test_case merges_conf_d atf_add_test_case rejects_bad_config atf_add_test_case explain_shows_the_command + atf_add_test_case ignores_cwd_module }