nuageinit: fix TOCTOU in addsshkey, adddoas, addsudo
Replace check-then-create patterns with direct creation: - addsshkey: check what exists before creation, use mkdir_p() for .ssh directory, handle errors with warnmsg() instead of assert(). Apply chmod/chown only on newly created files/directories. - adddoas: same pattern for doas.conf and the etc directory. - addsudo: same pattern for the sudoers file and sudoers.d directory. All three functions now use warnmsg() for error handling instead of returning nil,err or using assert().
This commit is contained in:
+52
-40
@@ -349,25 +349,33 @@ local function addgroup(grp)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function addsshkey(homedir, key)
|
local function addsshkey(homedir, key)
|
||||||
local chownak = false
|
|
||||||
local chowndotssh = false
|
|
||||||
local root = os.getenv("NUAGE_FAKE_ROOTDIR")
|
local root = os.getenv("NUAGE_FAKE_ROOTDIR")
|
||||||
if root then
|
if root then
|
||||||
homedir = root .. "/" .. homedir
|
homedir = root .. "/" .. homedir
|
||||||
end
|
end
|
||||||
local ak_path = homedir .. "/.ssh/authorized_keys"
|
local ak_path = homedir .. "/.ssh/authorized_keys"
|
||||||
local dotssh_path = homedir .. "/.ssh"
|
local dotssh_path = homedir .. "/.ssh"
|
||||||
local dirattrs = lfs.attributes(ak_path)
|
|
||||||
if dirattrs == nil then
|
-- Check what already exists before creating anything
|
||||||
chownak = true
|
local ak_exists = lfs.attributes(ak_path) ~= nil
|
||||||
dirattrs = lfs.attributes(dotssh_path)
|
local dotssh_exists = lfs.attributes(dotssh_path) ~= nil
|
||||||
if dirattrs == nil then
|
|
||||||
assert(lfs.mkdir(dotssh_path))
|
-- Ensure .ssh directory exists
|
||||||
chowndotssh = true
|
if not dotssh_exists then
|
||||||
dirattrs = lfs.attributes(homedir)
|
local r, err = mkdir_p(dotssh_path)
|
||||||
|
if not r then
|
||||||
|
warnmsg("cannot create " .. dotssh_path .. ": " .. err)
|
||||||
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Get homedir attributes for ownership
|
||||||
|
local dirattrs = lfs.attributes(homedir)
|
||||||
|
if not dirattrs then
|
||||||
|
warnmsg("cannot get attributes for " .. homedir)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
local f = io.open(ak_path, "a")
|
local f = io.open(ak_path, "a")
|
||||||
if not f then
|
if not f then
|
||||||
warnmsg("impossible to open " .. ak_path)
|
warnmsg("impossible to open " .. ak_path)
|
||||||
@@ -375,19 +383,19 @@ local function addsshkey(homedir, key)
|
|||||||
end
|
end
|
||||||
f:write(key .. "\n")
|
f:write(key .. "\n")
|
||||||
f:close()
|
f:close()
|
||||||
if chownak then
|
|
||||||
|
-- Set permissions and ownership on newly created files/dirs
|
||||||
|
if not ak_exists then
|
||||||
chmod(ak_path, "0600")
|
chmod(ak_path, "0600")
|
||||||
chown(ak_path, dirattrs.uid, dirattrs.gid)
|
chown(ak_path, dirattrs.uid, dirattrs.gid)
|
||||||
end
|
end
|
||||||
if chowndotssh then
|
if not dotssh_exists then
|
||||||
chmod(dotssh_path, "0700")
|
chmod(dotssh_path, "0700")
|
||||||
chown(dotssh_path, dirattrs.uid, dirattrs.gid)
|
chown(dotssh_path, dirattrs.uid, dirattrs.gid)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function adddoas(pwd)
|
local function adddoas(pwd)
|
||||||
local chmodetcdir = false
|
|
||||||
local chmoddoasconf = false
|
|
||||||
local root = os.getenv("NUAGE_FAKE_ROOTDIR")
|
local root = os.getenv("NUAGE_FAKE_ROOTDIR")
|
||||||
local localbase = getlocalbase()
|
local localbase = getlocalbase()
|
||||||
local etcdir = localbase .. "/etc"
|
local etcdir = localbase .. "/etc"
|
||||||
@@ -395,18 +403,19 @@ local function adddoas(pwd)
|
|||||||
etcdir= root .. etcdir
|
etcdir= root .. etcdir
|
||||||
end
|
end
|
||||||
local doasconf = etcdir .. "/doas.conf"
|
local doasconf = etcdir .. "/doas.conf"
|
||||||
local doasconf_attr = lfs.attributes(doasconf)
|
|
||||||
if doasconf_attr == nil then
|
local doasconf_exists = lfs.attributes(doasconf) ~= nil
|
||||||
chmoddoasconf = true
|
local etcdir_exists = lfs.attributes(etcdir) ~= nil
|
||||||
local dirattrs = lfs.attributes(etcdir)
|
|
||||||
if dirattrs == nil then
|
-- Ensure etc directory exists
|
||||||
local r, err = mkdir_p(etcdir)
|
if not etcdir_exists then
|
||||||
if not r then
|
local r, err = mkdir_p(etcdir)
|
||||||
return nil, err .. " (creating " .. etcdir .. ")"
|
if not r then
|
||||||
end
|
warnmsg("cannot create " .. etcdir .. ": " .. err)
|
||||||
chmodetcdir = true
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local f = io.open(doasconf, "a")
|
local f = io.open(doasconf, "a")
|
||||||
if not f then
|
if not f then
|
||||||
warnmsg("impossible to open " .. doasconf)
|
warnmsg("impossible to open " .. doasconf)
|
||||||
@@ -424,17 +433,17 @@ local function adddoas(pwd)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
f:close()
|
f:close()
|
||||||
if chmoddoasconf then
|
|
||||||
|
-- Set permissions on newly created files/dirs
|
||||||
|
if not doasconf_exists then
|
||||||
chmod(doasconf, "0640")
|
chmod(doasconf, "0640")
|
||||||
end
|
end
|
||||||
if chmodetcdir then
|
if not etcdir_exists then
|
||||||
chmod(etcdir, "0755")
|
chmod(etcdir, "0755")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function addsudo(pwd)
|
local function addsudo(pwd)
|
||||||
local chmodsudoersd = false
|
|
||||||
local chmodsudoers = false
|
|
||||||
local root = os.getenv("NUAGE_FAKE_ROOTDIR")
|
local root = os.getenv("NUAGE_FAKE_ROOTDIR")
|
||||||
local localbase = getlocalbase()
|
local localbase = getlocalbase()
|
||||||
local sudoers_dir = localbase .. "/etc/sudoers.d"
|
local sudoers_dir = localbase .. "/etc/sudoers.d"
|
||||||
@@ -442,18 +451,19 @@ local function addsudo(pwd)
|
|||||||
sudoers_dir= root .. sudoers_dir
|
sudoers_dir= root .. sudoers_dir
|
||||||
end
|
end
|
||||||
local sudoers = sudoers_dir .. "/90-nuageinit-users"
|
local sudoers = sudoers_dir .. "/90-nuageinit-users"
|
||||||
local sudoers_attr = lfs.attributes(sudoers)
|
|
||||||
if sudoers_attr == nil then
|
local sudoers_exists = lfs.attributes(sudoers) ~= nil
|
||||||
chmodsudoers = true
|
local sudoers_dir_exists = lfs.attributes(sudoers_dir) ~= nil
|
||||||
local dirattrs = lfs.attributes(sudoers_dir)
|
|
||||||
if dirattrs == nil then
|
-- Ensure sudoers.d directory exists
|
||||||
local r, err = mkdir_p(sudoers_dir)
|
if not sudoers_dir_exists then
|
||||||
if not r then
|
local r, err = mkdir_p(sudoers_dir)
|
||||||
return nil, err .. " (creating " .. sudoers_dir .. ")"
|
if not r then
|
||||||
end
|
warnmsg("cannot create " .. sudoers_dir .. ": " .. err)
|
||||||
chmodsudoersd = true
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local f = io.open(sudoers, "a")
|
local f = io.open(sudoers, "a")
|
||||||
if not f then
|
if not f then
|
||||||
warnmsg("impossible to open " .. sudoers)
|
warnmsg("impossible to open " .. sudoers)
|
||||||
@@ -467,10 +477,12 @@ local function addsudo(pwd)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
f:close()
|
f:close()
|
||||||
if chmodsudoers then
|
|
||||||
|
-- Set permissions on newly created files/dirs
|
||||||
|
if not sudoers_exists then
|
||||||
chmod(sudoers, "0440")
|
chmod(sudoers, "0440")
|
||||||
end
|
end
|
||||||
if chmodsudoersd then
|
if not sudoers_dir_exists then
|
||||||
chmod(sudoers_dir, "0750")
|
chmod(sudoers_dir, "0750")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user