Re: [PATCH 08/10] tools/thermal/tmon: Open log file with O_NOFOLLOW before validating and truncating
From: Rafael J. Wysocki (Intel)
Date: Fri Dec 04 2026 - 15:34:19 EST
On Thu, Dec 3, 2026 at 8:56 PM Lee Jones <lee@xxxxxxxxxx> wrote:
>
> 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@xxxxxxxxxxxxxxx
> Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
> ---
> 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;
> }
>
> --
Applied as 7.4-rc material, thanks!