From nobody Wed Apr 03 15:31:53 2024 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 4V8pdP6vj3z5FGGG; Wed, 3 Apr 2024 15:31:53 +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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4V8pdP61pcz4kXN; Wed, 3 Apr 2024 15:31:53 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1712158313; 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=XMWjKYRnFVfktqGhszzsr6gDrNaUg7V7C9dRus+k6zw=; b=LoyqCyuvSE7xJv01ftplyK28UOyHa2EGie8KsbQNCdFDn7mVRok3lvrJUgj4b8HjZLlR6z N3fhbxG5Ru9ZgwM6HsFsg2R6ekdLwFqICy79U6bDTn2JtXAw/wFTduF4O8lon4PpyN6n1D pD1eX485rmR8CGSpF0XbSXDmWa9o4PFIUI8ZfPzz9eNdp2l26fEEPCK2uUIkZkibDn+Z4g aBhD/Yso1wVzwMCiUOz//ShhuUkW1A/6Er4YpGderP085pAhBDGGKKvkuil7naLbomw5JK brhmYUwz0jmW7koZMjiliokdSjpFa0sZ6wTfIDZEmDOoBcTqnK73YN7Sa3LISg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1712158313; a=rsa-sha256; cv=none; b=a25/vV8G36n7ZGKYqR/2zOA/6c4rIeS+nezsGfoA6FCTtoZRzHkUpxFMZHFPGOemAYEjkK 5KngZdilLrk2xPxC87Sy02/SLCGg9C1xrsmWatLavISQBum+8GdLmCJLEaf0UAHQlKSopD XF16J9uavNFs3rMcvg9M/sOeKOP8iAh5MVxmGKGbE2v6sL9gvmaPy6Jz5+Zyvt/zh7qI7A 7cs+ytQcGg21khkSy+trRxglCo3vqyUN0uLtRre8ecQjwXBneWxCvnQHFfis+Q3x6/HaA/ LJDLDyJKyvXLvD5PWu4Z9CYRmqN7zscuq23qVaT+mrcQBEUgtEKmnLPyaDTbTQ== 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=1712158313; 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=XMWjKYRnFVfktqGhszzsr6gDrNaUg7V7C9dRus+k6zw=; b=atezxzcekFPOtyp83lnq3RuFgLTYM46auMqrDsCdnFKGPXMO4ltd3rlMCiPNPxQQSVtrZ5 0oi79hr8H2UVSO3MonBeKPu4wWbLfx0S9IAjFURRweGTc+yEOSOUPd4Nf9q5wLV5wAEv0m mgo5nyl/xsskQ8APWwKGSLHw3D4UZZnhx1ouDRSvJJv3mY1L8l0ja830PMmYLMeWoourpR Mf7XxymVMjkQML1lerxvNZw7G4apx9v5TYTvT3jVg5Qwy3ad4EHE4Bbz8Clnd0P4M63w7U /l6/DwgsK3LJa/tE19APtZpDRdSeIlKGqXlDov4dpLwr8Eqmo8vEfg56icLzsw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4V8pdP5dDwzdgb; Wed, 3 Apr 2024 15:31:53 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 433FVreH026128; Wed, 3 Apr 2024 15:31:53 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 433FVrAB026125; Wed, 3 Apr 2024 15:31:53 GMT (envelope-from git) Date: Wed, 3 Apr 2024 15:31:53 GMT Message-Id: <202404031531.433FVrAB026125@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 87e63f2e7f92 - main - nextboot: Write nextboot.conf safely 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: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 87e63f2e7f92e192c1e8b01ac7cc45b88c03a478 Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=87e63f2e7f92e192c1e8b01ac7cc45b88c03a478 commit 87e63f2e7f92e192c1e8b01ac7cc45b88c03a478 Author: Mark Johnston AuthorDate: 2024-04-03 15:29:25 +0000 Commit: Mark Johnston CommitDate: 2024-04-03 15:31:43 +0000 nextboot: Write nextboot.conf safely As in the old nextboot.sh script: - First write everything to a tempfile instead of /boot/nextboot.conf. - fsync() the tempfile before renaming it to nextboot.conf. Fixes: fd6d47375a78 ("rescue,nextboot: Install nextboot as a link to reboot, rm nextboot.sh") Reviewed by: imp Differential Revision: https://reviews.freebsd.org/D44572 --- sbin/reboot/reboot.c | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/sbin/reboot/reboot.c b/sbin/reboot/reboot.c index 3cc77aebe4d4..1b806a90de1d 100644 --- a/sbin/reboot/reboot.c +++ b/sbin/reboot/reboot.c @@ -113,8 +113,10 @@ zfsbootcfg(const char *pool, bool force) static void write_nextboot(const char *fn, const char *env, bool force) { + char tmp[PATH_MAX]; FILE *fp; struct statfs sfs; + int tmpfd; bool supported = false; bool zfs = false; @@ -138,21 +140,39 @@ write_nextboot(const char *fn, const char *env, bool force) zfsbootcfg(sfs.f_mntfromname, force); } - fp = fopen(fn, "w"); + if (strlcpy(tmp, fn, sizeof(tmp)) >= sizeof(tmp)) + E("Path too long %s", fn); + if (strlcat(tmp, ".XXXXXX", sizeof(tmp)) >= sizeof(tmp)) + E("Path too long %s", fn); + tmpfd = mkstemp(tmp); + if (tmpfd == -1) + E("mkstemp %s", tmp); + + fp = fdopen(tmpfd, "w"); if (fp == NULL) - E("Can't create %s", fn); + E("fdopen %s", tmp); - if (fprintf(fp,"%s%s", + if (fprintf(fp, "%s%s", supported ? "nextboot_enable=\"YES\"\n" : "", env != NULL ? env : "") < 0) { int e; e = errno; - fclose(fp); - if (unlink(fn)) - warn("unlink %s", fn); + if (unlink(tmp)) + warn("unlink %s", tmp); + errno = e; + E("Can't write %s", tmp); + } + if (fsync(fileno(fp)) != 0) + E("Can't fsync %s", fn); + if (rename(tmp, fn) != 0) { + int e; + + e = errno; + if (unlink(tmp)) + warn("unlink %s", tmp); errno = e; - E("Can't write %s", fn); + E("Can't rename %s to %s", tmp, fn); } fclose(fp); } @@ -194,8 +214,8 @@ add_env(char **env, const char *key, const char *value) /* * Different options are valid for different programs. */ -#define GETOPT_REBOOT "cDde:k:lNno:pqr" -#define GETOPT_NEXTBOOT "De:k:o:" +#define GETOPT_REBOOT "cDde:fk:lNno:pqr" +#define GETOPT_NEXTBOOT "De:fk:o:" int main(int argc, char *argv[])