Files
glacier-ng/tests/test_package_symlinks.lua
2026-10-29 09:15:34 -04:00

242 lines
11 KiB
Lua

-- test_package_symlinks.lua
--
-- Symlinks shipped inside packages: they used to be silently dropped at
-- extraction (only dirs and regular files were handled), which meant
-- e.g. musl's loader lib/ld-musl-x86_64.so.1 -> /usr/lib/libc.so was
-- never installed. Now they're extracted verbatim and linked like files.
--
-- The security half: an archive must never be able to write THROUGH a
-- symlink it ships (`x -> /victim`, then `x/pwn`). Symlinks are created
-- only after all regular content, and any entry whose parent is a
-- symlink is refused. Linking must likewise never descend through
-- another package's symlink into that package's store directory.
--
-- Needs GNU tar (the hostile archives are built with --append).
local glacier = require("glacier_ffi")
local h = require("test_helpers")
h.reset_glacier_root()
local uid = 1004
io.write("== test_package_symlinks (uid " .. uid .. ") ==\n")
glacier.lib.gl_init_user(uid, false)
local base = "/glacier/usr/" .. uid
local store, links = base .. "/store", base .. "/links"
local function readlink(path)
local p = io.popen("readlink " .. h.quote(path) .. " 2>/dev/null")
local out = p:read("*l")
p:close()
return out
end
local function is_symlink(path)
local ok = os.execute("test -L " .. h.quote(path))
return ok == true or ok == 0
end
local function read_file(path)
local f = io.open(path, "r")
if not f then return nil end
local c = f:read("*a")
f:close()
return c
end
-- Install `pkgs` (list of .gpkg paths) as one staged transaction.
-- Returns the first non-OK install status, or the commit status.
local function install(pkgs)
local ctx = glacier.new_context()
local st = glacier.lib.gl_init_stage_context(ctx, glacier.SCOPE_USR, uid)
if tonumber(st) ~= 0 then return "stage:" .. glacier.txn_name(st) end
for _, p in ipairs(pkgs) do
local r = glacier.lib.gl_install_pkg(ctx, p)
if tonumber(r) ~= 0 then
glacier.lib.gl_abort_transaction(ctx)
return glacier.install_name(r)
end
end
local c = glacier.lib.gl_commit_transaction(ctx)
if tonumber(c) ~= 0 then glacier.lib.gl_abort_transaction(ctx) end
return glacier.txn_name(c)
end
-- ---------------------------------------------------------------------
io.write("-- legitimate symlinks are installed and linked\n")
local good = h.build_fixture_pkg{
name = "symok", ver = "1.0.0", repo = "core",
files = {
["usr/lib/libsymok.so.1.0.0"] = "the library\n",
},
symlinks = {
["usr/lib/libsymok.so.1"] = "libsymok.so.1.0.0", -- relative (soname)
["lib/ld-symok.so.1"] = "/usr/lib/libsymok.so.1", -- absolute (loader-style)
["usr/share/symok-lib"] = "../lib", -- directory symlink
["usr/bin/symok-dangling"] = "/nonexistent/on/purpose", -- dangles by design
},
}
h.check(install{ good } == "GL_TXN_OK", "install package carrying symlinks")
local ver_dir = store .. "/core/symok/symok-1.0.0"
h.check(readlink(ver_dir .. "/usr/lib/libsymok.so.1") == "libsymok.so.1.0.0",
"relative symlink extracted verbatim into the store")
h.check(readlink(ver_dir .. "/lib/ld-symok.so.1") == "/usr/lib/libsymok.so.1",
"absolute symlink extracted verbatim (target means the RUNTIME path)")
h.check(readlink(ver_dir .. "/usr/share/symok-lib") == "../lib",
"directory symlink extracted verbatim")
h.check(readlink(links .. "/lib/libsymok.so.1") == ver_dir .. "/usr/lib/libsymok.so.1",
"links tree points at the store symlink")
h.check(read_file(links .. "/lib/libsymok.so.1") == "the library\n",
"reading through links -> store symlink -> real file works")
h.check(is_symlink(links .. "/lib/ld-symok.so.1"),
"loader-style symlink outside usr/ is linked too (lib/ld-* -> links/lib/)")
h.check(is_symlink(links .. "/bin/symok-dangling"),
"a package's intentionally dangling symlink is linked")
-- Another commit runs a full prune + relink; the dangling one must not
-- be mistaken for stale (its STORE entry still exists).
local other = h.build_fixture_pkg{ name = "unrelated", ver = "1.0.0", repo = "core" }
h.check(install{ other } == "GL_TXN_OK", "unrelated second transaction")
h.check(is_symlink(links .. "/bin/symok-dangling"),
"dangling package symlink survives the next commit's prune")
h.check(read_file(links .. "/lib/libsymok.so.1") == "the library\n",
"soname link still resolves after the next commit")
-- ---------------------------------------------------------------------
io.write("-- hostile archives are refused, victim untouched\n")
local function fresh_victim(dir)
h.sh("rm -rf " .. h.quote(dir) .. " && mkdir -p " .. h.quote(dir))
end
-- A: symlink x -> victim, then a FILE x/pwn (no directory entry for x).
fresh_victim("/tmp/victimA")
local evilA = h.build_fixture_pkg{
name = "evila", ver = "1.0.0", repo = "core",
symlinks = { ["x"] = "/tmp/victimA" },
append = { { files = { ["x/pwn"] = "owned\n" } } },
}
h.check(install{ evilA } ~= "GL_TXN_OK", "A: symlink then file-through-it is refused")
h.check(not h.exists("/tmp/victimA/pwn"), "A: nothing written through the symlink")
-- B: symlink x -> victim, then a SYMLINK x/evil. Both are deferred, so
-- this exercises the parent-is-a-symlink check on the deferred pass.
fresh_victim("/tmp/victimB")
local evilB = h.build_fixture_pkg{
name = "evilb", ver = "1.0.0", repo = "core",
append = {
{ symlinks = { ["x"] = "/tmp/victimB" } },
{ symlinks = { ["x/evil"] = "whatever" } },
},
}
h.check(install{ evilB } ~= "GL_TXN_OK", "B: symlink then symlink-through-it is refused")
h.check(not is_symlink("/tmp/victimB/evil") and not h.exists("/tmp/victimB/evil"),
"B: nothing created through the symlink")
-- C: a symlink and a directory at the same path.
local evilC = h.build_fixture_pkg{
name = "evilc", ver = "1.0.0", repo = "core",
files = { ["usr/share/both/f"] = "x\n" },
append = { { symlinks = { ["usr/share/both"] = "/tmp" } } },
}
h.check(install{ evilC } ~= "GL_TXN_OK", "C: symlink colliding with a directory is refused")
h.check(not h.exists(store .. "/core/evila") and not h.exists(store .. "/core/evilb")
and not h.exists(store .. "/core/evilc"),
"refused packages left nothing in the live store")
-- A refused package must not leave a half-extracted version dir behind:
-- in a LIVE context (no stage to throw away) that used to make every
-- later attempt report GL_INSTALL_ERR_ALREADY_INSTALLED.
local live = glacier.new_context()
glacier.lib.gl_init_live_context(live, glacier.SCOPE_USR, uid)
local r1 = glacier.lib.gl_install_pkg(live, evilA)
local r2 = glacier.lib.gl_install_pkg(live, evilA)
h.check(glacier.install_name(r1) == "GL_INSTALL_ERR_EXTRACT"
and glacier.install_name(r2) == "GL_INSTALL_ERR_EXTRACT",
"live context: refused twice for the same reason, not 'already installed' the 2nd time ("
.. glacier.install_name(r2) .. ")")
h.check(not h.exists(store .. "/core/evila/evila-1.0.0"),
"live context: no half-extracted version dir left in the store")
-- ---------------------------------------------------------------------
io.write("-- linking never descends through another package's symlink\n")
local pkgA = h.build_fixture_pkg{
name = "dira", ver = "1.0.0", repo = "core",
files = { ["usr/lib/a.so"] = "a\n" },
symlinks = { ["usr/lib64"] = "lib" },
}
local pkgB = h.build_fixture_pkg{
name = "dirb", ver = "1.0.0", repo = "core",
files = { ["usr/lib64/b.so"] = "b\n" },
}
-- Since ROADMAP 2.6 the pair is a file collision (A ships lib64 as a
-- symlink, B as a directory) and is refused before anything is linked.
h.check(install{ pkgA, pkgB } == "GL_TXN_ERR_DEPS",
"A (lib64 -> lib) and B (lib64/b.so) together: refused as a collision")
h.check(install{ pkgA } == "GL_TXN_OK", "A alone installs")
-- The link pass's own defence still matters for a store that got that
-- way without the check (one from before it, say): put B into the live
-- store by hand and relink.
local b_dir = store .. "/core/dirb/dirb-1.0.0"
h.sh("mkdir -p " .. h.quote(b_dir) .. " && tar xzf " .. h.quote(pkgB) ..
" --strip-components=2 -C " .. h.quote(b_dir) .. " dirb-1.0.0/files")
local relive = glacier.new_context()
glacier.lib.gl_init_live_context(relive, glacier.SCOPE_USR, uid)
glacier.lib.gl_relink_store(relive)
local a_store_lib = store .. "/core/dira/dira-1.0.0/usr/lib"
h.check(not h.exists(a_store_lib .. "/b.so") and not is_symlink(a_store_lib .. "/b.so"),
"B's file was NOT created inside A's store directory")
h.check(read_file(a_store_lib .. "/a.so") == "a\n", "A's own content untouched")
-- ---------------------------------------------------------------------
io.write("-- system scope: a dangling package symlink that displaced a file stays put\n")
-- Only /usr keeps links across commits (user/sys links trees are swapped
-- wholesale), so this is where a wrong prune is visible: pruning a
-- dangling-by-design link would also restore the original it displaced,
-- and the relink would then be refused by that original.
local victim = "/usr/bin/glacier-fixture-dangle"
h.sh("printf 'original\\n' > " .. victim)
h.check(tonumber(glacier.lib.gl_init_sys(false)) == 0, "gl_init_sys")
local function sys_install(pkgs, overwrite)
local ctx = glacier.new_context()
local st = glacier.lib.gl_init_stage_context(ctx, glacier.SCOPE_SYS, 0)
if tonumber(st) ~= 0 then return "stage:" .. glacier.txn_name(st) end
ctx.overwrite = overwrite and 1 or 0
for _, p in ipairs(pkgs) do
local r = glacier.lib.gl_install_pkg(ctx, p)
if tonumber(r) ~= 0 then
glacier.lib.gl_abort_transaction(ctx)
return glacier.install_name(r)
end
end
local c = glacier.lib.gl_commit_transaction(ctx)
if tonumber(c) ~= 0 then glacier.lib.gl_abort_transaction(ctx) end
return glacier.txn_name(c)
end
local dangler = h.build_fixture_pkg{
name = "dangler", ver = "1.0.0", repo = "base",
symlinks = { ["usr/bin/glacier-fixture-dangle"] = "/nonexistent/on/purpose" },
}
h.check(sys_install({ dangler }, true) == "GL_TXN_OK", "install base pkg with -O over an existing file")
h.check(is_symlink(victim), "dangling package symlink displaced the original")
local filler = h.build_fixture_pkg{ name = "sysfiller", ver = "1.0.0", repo = "extra" }
h.check(sys_install({ filler }, false) == "GL_TXN_OK", "unrelated system transaction")
h.check(is_symlink(victim),
"after the next commit it is still the package's symlink (not pruned + original restored)")
h.sh("rm -f " .. victim)
h.summary()