All of lore.kernel.org
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: lee@kernel.org, "Rafael J. Wysocki" <rafael@kernel.org>,
	Daniel Lezcano <daniel.lezcano@kernel.org>,
	Zhang Rui <rui.zhang@intel.com>,
	Lukasz Luba <lukasz.luba@arm.com>,
	Jacob Pan <jacob.jun.pan@linux.intel.com>,
	Neil Horman <nhorman@tuxdriver.com>,
	linux-pm@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: [PATCH 08/10] tools/thermal/tmon: Open log file with O_NOFOLLOW before validating and truncating
Date: Thu,  3 Dec 2026 19:54:19 +0000	[thread overview]
Message-ID: <20261203195529.3063246-9-lee@kernel.org> (raw)
In-Reply-To: <20261203195529.3063246-1-lee@kernel.org>

prepare_logging() opens /var/tmp/tmon.log with fopen(..., "w+") before
checking lstat() for symlinks or ownership.  Because tmon runs as root
and fopen("w+") follows symlinks and immediately truncates the target,
a local attacker can clobber arbitrary files via a symlink in /var/tmp.

Open the log file with open(..., O_RDWR | O_CREAT | O_NOFOLLOW |
O_CLOEXEC, 0600) without O_TRUNC, validate that the opened descriptor
refers to a regular file owned by the current user via fstat(), and
only then call ftruncate() and fdopen().

Fixes: 951fda3d8c64 ("tmon: Check log file for common secuirty issues")
Closes: https://sashiko.dev/#/bug/linux-d4578ca3-42ad-4e17-b59d-fc6d213bd412
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
 tools/thermal/tmon/tmon.c | 36 ++++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 12 deletions(-)

diff --git a/tools/thermal/tmon/tmon.c b/tools/thermal/tmon/tmon.c
index 70a2ec50a461..701163b4a08a 100644
--- a/tools/thermal/tmon/tmon.c
+++ b/tools/thermal/tmon/tmon.c
@@ -8,6 +8,7 @@
  */
 
 #include <getopt.h>
+#include <fcntl.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -130,37 +131,48 @@ static void start_syslog(void)
 
 static void prepare_logging(void)
 {
-	int i;
+	int i, fd;
 	struct stat logstat;
 
 	if (!logging)
 		return;
 	/* open local data log file */
-	tmon_log = fopen(TMON_LOG_FILE, "w+");
-	if (!tmon_log) {
+	fd = open(TMON_LOG_FILE, O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC,
+		  0600);
+	if (fd < 0) {
 		syslog(LOG_ERR, "failed to open log file %s\n", TMON_LOG_FILE);
 		return;
 	}
 
-	if (lstat(TMON_LOG_FILE, &logstat) < 0) {
+	if (fstat(fd, &logstat) < 0) {
 		syslog(LOG_ERR, "Unable to stat log file %s\n", TMON_LOG_FILE);
-		fclose(tmon_log);
-		tmon_log = NULL;
+		close(fd);
 		return;
 	}
 
 	/* The log file must be a regular file owned by us */
-	if (S_ISLNK(logstat.st_mode)) {
-		syslog(LOG_ERR, "Log file is a symlink.  Will not log\n");
-		fclose(tmon_log);
-		tmon_log = NULL;
+	if (!S_ISREG(logstat.st_mode)) {
+		syslog(LOG_ERR, "Log file is not a regular file.  Will not log\n");
+		close(fd);
 		return;
 	}
 
 	if (logstat.st_uid != getuid()) {
 		syslog(LOG_ERR, "We don't own the log file.  Not logging\n");
-		fclose(tmon_log);
-		tmon_log = NULL;
+		close(fd);
+		return;
+	}
+
+	if (ftruncate(fd, 0) < 0) {
+		syslog(LOG_ERR, "Unable to truncate log file %s\n", TMON_LOG_FILE);
+		close(fd);
+		return;
+	}
+
+	tmon_log = fdopen(fd, "w+");
+	if (!tmon_log) {
+		syslog(LOG_ERR, "failed to open log file %s\n", TMON_LOG_FILE);
+		close(fd);
 		return;
 	}
 
-- 
2.56.1.911.g883d8bcb20-goog


  parent reply	other threads:[~2026-12-03 19:56 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 19:54 [PATCH 00/10] thermal: Fix Critical and High issues reported by Sashiko Lee Jones
2026-12-03 19:54 ` [PATCH 01/10] thermal: intel: hfi: Pass byte size to alloc_pages_exact() and free_pages_exact() Lee Jones
2026-12-04 20:22   ` Rafael J. Wysocki (Intel)
2026-12-03 19:54 ` [PATCH 02/10] thermal: intel: hfi: Publish hfi_instance only after successful initialisation Lee Jones
2026-12-04 20:29   ` Rafael J. Wysocki (Intel)
2026-12-03 19:54 ` [PATCH 03/10] thermal: int3400: Clean up ODVP attributes after unregistering thermal zone Lee Jones
2026-12-04 20:24   ` Rafael J. Wysocki (Intel)
2026-12-03 19:54 ` [PATCH 04/10] thermal/drivers/qcom/tsens-v1: Validate sensor count before legacy calibration Lee Jones
2026-12-03 19:54 ` [PATCH 05/10] thermal/drivers/qcom/tsens: Bound sensor initialisation by num_sensors Lee Jones
2026-12-03 19:54 ` [PATCH 06/10] thermal/drivers/rcar: Free IRQs before cancelling delayed work on remove Lee Jones
2026-12-04 17:15   ` Niklas Söderlund
2026-12-05  5:38     ` Yaohan Li
2026-12-03 19:54 ` [PATCH 07/10] thermal/drivers/imx91: Acquire runtime PM reference in imx91_tmu_get_temp() Lee Jones
2026-12-03 20:11   ` sashiko-bot
2026-12-03 20:34     ` Frank Li
2026-12-04 10:40   ` Peng Fan (OSS)
2026-12-03 19:54 ` Lee Jones [this message]
2026-12-04 20:34   ` [PATCH 08/10] tools/thermal/tmon: Open log file with O_NOFOLLOW before validating and truncating Rafael J. Wysocki (Intel)
2026-12-03 19:54 ` [PATCH 09/10] thermal/drivers/qcom/tsens: Drop __init from ops init callbacks Lee Jones
2026-12-03 19:54 ` [PATCH 10/10] thermal/drivers/imx91: Use pm_runtime_resume_and_get() in imx91_tmu_change_mode() Lee Jones
2026-12-03 20:21   ` Frank Li
2026-12-04 10:40   ` Peng Fan (OSS)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203195529.3063246-9-lee@kernel.org \
    --to=lee@kernel.org \
    --cc=daniel.lezcano@kernel.org \
    --cc=jacob.jun.pan@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=lukasz.luba@arm.com \
    --cc=nhorman@tuxdriver.com \
    --cc=rafael@kernel.org \
    --cc=rui.zhang@intel.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.