From nobody Thu Jun 4 21:15:46 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4gWcmf6LNDz6gxsZ for ; Thu, 04 Jun 2026 21:15:46 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R13" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4gWcmf3mTjz3Lnn for ; Thu, 04 Jun 2026 21:15:46 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1780607746; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=zqEg6alcdH385g6GwgFB/O9Jw7RBcjv74QkfXmUnHXk=; b=TRZXwnC6v8BpOCn4hK1MjHvNbVQYZG3mCaMnZDXulpMRpHkXMy0cDY/WAlJVhmjuc/e/gK kyrdNXEYf2Ro5P4oRP7Sk0T2087VjCXttci2Iq1umjNF2OXa1wCbvKfRPHsE4Xp7GN0euV a8c1XFDkMEwllVCOGxrDub9wRoSJidfeH8V5wn3lOQYpiMjtM1e+VMn/kZ5QqBh13zHjUk JEtBcbs2aCYhCCqbo6id/LZX+EXz3BNgM8mIZW+kqEtYIusbQYG2KWEgQgWfSls/Q6SrbW yfmvYfxJOBkJQmqWUkTlnnpQnWpihd0PBgvbDnONWcvEf62XNtNQ2vAEoryJPw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1780607746; a=rsa-sha256; cv=none; b=Gy31OR1VlbAL18DJ4EmLaAtVpTUGzpSMNC6CYGZJ/IMJUHsd815JjV5HKhD5LbvVuca4QL hIPV1iwokFuKNn/jBn4EcTO4KuPqGaGWbg1pGiun5f0lGAEMcJPNcb0kwqPNsHV5LZWlMr 55pIEy2TCielmFPLn3gMSQFh1PVK7TdhsM9dIE0XBCSnJF98MtRHPeA39+uKFBXoOCzWRf Mw7vrd9alHW7obA3F4PvwnkKEZGOInZ9dxip08tXxR+mhsg1hNwP8HqU8E4JxtdR0ebM5v B4tW7j8AZWx8OY6+uZ2J32TRsIp0nhlIkIAbR0IYj6gnIZwGAhbHQ1jWWpgP5w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1780607746; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=zqEg6alcdH385g6GwgFB/O9Jw7RBcjv74QkfXmUnHXk=; b=LrMOhATiF1wOKFuo4UZ89IJcIyMIzl7+IrNzue4ftyo7lLMAutefqx0UNSI+Z+9NhTE+g6 K9EpoqZzuYa+mDIosBv5Qn5p7OdmJAnub+GnA9DPN3kXPLqoACvsczfrw4E7LLf/amZag4 0drNYIv9ESFxH7iYfUOq+C84hJJpzK8tz48el/8BPxG4BGaxgQaWH703EWUWkVOkSqiY/C 0TZ5zbqiWm0EBFoPaNV5KEf2AVcO3C642XyGTI0EGl/433DoWARbXTbe4ie8lg51lTdj1S Nd14FdU+Wm3OFa4/EFhaK5RPjiLXjNdoaWBuZwy5fFLskgEPFq7tNUReTKntdg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4gWcmf3JLNz1DYV for ; Thu, 04 Jun 2026 21:15:46 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 3c431 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Thu, 04 Jun 2026 21:15:46 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Baptiste Daroussin Subject: git: cf5722ed60cf - main - nuageinit: fix TOCTOU in addsshkey, adddoas, addsudo List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: bapt X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: cf5722ed60cf271e516927684c90464debb37496 Auto-Submitted: auto-generated Date: Thu, 04 Jun 2026 21:15:46 +0000 Message-Id: <6a21eb02.3c431.db2bf38@gitrepo.freebsd.org> The branch main has been updated by bapt: URL: https://cgit.FreeBSD.org/src/commit/?id=cf5722ed60cf271e516927684c90464debb37496 commit cf5722ed60cf271e516927684c90464debb37496 Author: Baptiste Daroussin AuthorDate: 2026-06-04 19:06:35 +0000 Commit: Baptiste Daroussin CommitDate: 2026-06-04 19:06:35 +0000 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(). --- libexec/nuageinit/nuage.lua | 92 +++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 40 deletions(-) diff --git a/libexec/nuageinit/nuage.lua b/libexec/nuageinit/nuage.lua index 7e7b2a64305f..56d1ccf9d52e 100644 --- a/libexec/nuageinit/nuage.lua +++ b/libexec/nuageinit/nuage.lua @@ -349,25 +349,33 @@ local function addgroup(grp) end local function addsshkey(homedir, key) - local chownak = false - local chowndotssh = false local root = os.getenv("NUAGE_FAKE_ROOTDIR") if root then homedir = root .. "/" .. homedir end local ak_path = homedir .. "/.ssh/authorized_keys" local dotssh_path = homedir .. "/.ssh" - local dirattrs = lfs.attributes(ak_path) - if dirattrs == nil then - chownak = true - dirattrs = lfs.attributes(dotssh_path) - if dirattrs == nil then - assert(lfs.mkdir(dotssh_path)) - chowndotssh = true - dirattrs = lfs.attributes(homedir) + + -- Check what already exists before creating anything + local ak_exists = lfs.attributes(ak_path) ~= nil + local dotssh_exists = lfs.attributes(dotssh_path) ~= nil + + -- Ensure .ssh directory exists + if not dotssh_exists then + local r, err = mkdir_p(dotssh_path) + if not r then + warnmsg("cannot create " .. dotssh_path .. ": " .. err) + return 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") if not f then warnmsg("impossible to open " .. ak_path) @@ -375,19 +383,19 @@ local function addsshkey(homedir, key) end f:write(key .. "\n") f:close() - if chownak then + + -- Set permissions and ownership on newly created files/dirs + if not ak_exists then chmod(ak_path, "0600") chown(ak_path, dirattrs.uid, dirattrs.gid) end - if chowndotssh then + if not dotssh_exists then chmod(dotssh_path, "0700") chown(dotssh_path, dirattrs.uid, dirattrs.gid) end end local function adddoas(pwd) - local chmodetcdir = false - local chmoddoasconf = false local root = os.getenv("NUAGE_FAKE_ROOTDIR") local localbase = getlocalbase() local etcdir = localbase .. "/etc" @@ -395,18 +403,19 @@ local function adddoas(pwd) etcdir= root .. etcdir end local doasconf = etcdir .. "/doas.conf" - local doasconf_attr = lfs.attributes(doasconf) - if doasconf_attr == nil then - chmoddoasconf = true - local dirattrs = lfs.attributes(etcdir) - if dirattrs == nil then - local r, err = mkdir_p(etcdir) - if not r then - return nil, err .. " (creating " .. etcdir .. ")" - end - chmodetcdir = true + + local doasconf_exists = lfs.attributes(doasconf) ~= nil + local etcdir_exists = lfs.attributes(etcdir) ~= nil + + -- Ensure etc directory exists + if not etcdir_exists then + local r, err = mkdir_p(etcdir) + if not r then + warnmsg("cannot create " .. etcdir .. ": " .. err) + return end end + local f = io.open(doasconf, "a") if not f then warnmsg("impossible to open " .. doasconf) @@ -424,17 +433,17 @@ local function adddoas(pwd) end end f:close() - if chmoddoasconf then + + -- Set permissions on newly created files/dirs + if not doasconf_exists then chmod(doasconf, "0640") end - if chmodetcdir then + if not etcdir_exists then chmod(etcdir, "0755") end end local function addsudo(pwd) - local chmodsudoersd = false - local chmodsudoers = false local root = os.getenv("NUAGE_FAKE_ROOTDIR") local localbase = getlocalbase() local sudoers_dir = localbase .. "/etc/sudoers.d" @@ -442,18 +451,19 @@ local function addsudo(pwd) sudoers_dir= root .. sudoers_dir end local sudoers = sudoers_dir .. "/90-nuageinit-users" - local sudoers_attr = lfs.attributes(sudoers) - if sudoers_attr == nil then - chmodsudoers = true - local dirattrs = lfs.attributes(sudoers_dir) - if dirattrs == nil then - local r, err = mkdir_p(sudoers_dir) - if not r then - return nil, err .. " (creating " .. sudoers_dir .. ")" - end - chmodsudoersd = true + + local sudoers_exists = lfs.attributes(sudoers) ~= nil + local sudoers_dir_exists = lfs.attributes(sudoers_dir) ~= nil + + -- Ensure sudoers.d directory exists + if not sudoers_dir_exists then + local r, err = mkdir_p(sudoers_dir) + if not r then + warnmsg("cannot create " .. sudoers_dir .. ": " .. err) + return end end + local f = io.open(sudoers, "a") if not f then warnmsg("impossible to open " .. sudoers) @@ -467,10 +477,12 @@ local function addsudo(pwd) end end f:close() - if chmodsudoers then + + -- Set permissions on newly created files/dirs + if not sudoers_exists then chmod(sudoers, "0440") end - if chmodsudoersd then + if not sudoers_dir_exists then chmod(sudoers_dir, "0750") end end