tools/thermal/tmon: Fix predictable temporary path /var/log/tmon.log
cover tools/thermal/tmon: Fix predictable temporary path /var/log/tmon.log
Wolfgang Frisch identified and reported:
tmon only runs as root and logs event to /var/tmp/tmon.log. If fs.protected_symlinks=0, an unprivileged user can overwrite arbitrary files on the system.
nobody@localhost:/var/tmp> ln -s /test/shadow tmon.log root@localhost:/> tmon -l
I could reproduce the issue and after some discussion we came to 3 possible solutions:
1. Use /var/log/tmon.log instead of world-readable /var/tmp
2. Add randomness to the file name, e.g. /var/log/tmon.log.QipZjI
3. Make /var/tmp/tmon.log safe (TM), or say more robust.
Wolfgang came up with a patch which is part of this submission. His intention is to keep /var/tmp/tmon.log, in case other tools/scripts or docs rely on it.
If 3. is a suitable solution, it would be great if someone could pile these patches for next submission round.
Thanks,
Thomas
Thomas Renninger (3):
tools/thermal/tmon: Prevent symlink attack on log file tools/thermal/tmon: Make sure ctrl_cdev is a Null terminated string tools/thermal/tmon: Cleanup only: Use LOG_FILE vars
tools/thermal/tmon/tmon.c | 57 +++++++++++++++++++++++++++------------ tools/thermal/tmon/tmon.h | 3 ++- 2 files changed, 42 insertions(+), 18 deletions(-)
Signature · 1 line
2.49.0
PATCH 1/3 tools/thermal/tmon: Prevent symlink attack on log file
From: Wolfgang Frisch <[email protected]>
If fs.protected_symlinks=0, an unprivileged local user could create a symbolic link at /var/log/tmon.log and, in turn, cause tmon -l to truncate arbitrary files on the system when prepare_logging() is executed.
Change prepare_logging() to use openat() and create the log file securely.
--- tools/thermal/tmon/tmon.c | 50 +++++++++++++++++++++++++++------------ tools/thermal/tmon/tmon.h | 3 ++- 2 files changed, 37 insertions(+), 16 deletions(-)
tools/thermal/tmon/tmon.c +35 -15
@@ -24,6 +24,8 @@#include <math.h> #include <stdarg.h> #include <syslog.h> +#include <fcntl.h> +#include <errno.h> #include "tmon.h"
@@ -132,35 +134,53 @@ static void prepare_logging(void) {int i;struct stat logstat; +int dir_fd =-1 ; +int log_fd =-1 ; + tmon_log =NULL ;if (!logging)return ; -/* open local data log file */ - tmon_log =fopen (TMON_LOG_FILE ,"w+" ); -if (!tmon_log) { -syslog (LOG_ERR ,"failed to open log file %s\n" ,TMON_LOG_FILE ); + +/* get a file descriptor for the log directory */ + dir_fd =open (TMON_LOG_DIR ,O_RDONLY |O_DIRECTORY |O_CLOEXEC ); +if (dir_fd <0 ) { +syslog (LOG_ERR ,"Failed to open log directory %s: %s\n" , +TMON_LOG_DIR ,strerror (errno));return ; } -if (lstat (TMON_LOG_FILE , &logstat) <0 ) { -syslog (LOG_ERR ,"Unable to stat log file %s\n" ,TMON_LOG_FILE ); -fclose (tmon_log); - tmon_log =NULL ; +/* open local data log file securely using openat() */ + log_fd =openat (dir_fd,TMON_LOG_FILENAME , +O_RDWR |O_CREAT |O_TRUNC |O_NOFOLLOW |O_CLOEXEC , +0600 ); +close (dir_fd); + +if (log_fd <0 ) { +syslog (LOG_ERR ,"Failed to open log file %s/%s: %s\n" , +TMON_LOG_DIR ,TMON_LOG_FILENAME ,strerror (errno));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 (fstat (log_fd, &logstat) <0 ) { +syslog (LOG_ERR ,"Unable to stat log file %s/%s\n" , +TMON_LOG_DIR ,TMON_LOG_FILENAME ); +close (log_fd);return ; } +/* The log file must be owned by us */ 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 (log_fd); +return ; + } + +/* All checks passed. Convert the file descriptor to a FILE stream. */ + tmon_log =fdopen (log_fd,"w+" ); +if (!tmon_log) { +syslog (LOG_ERR ,"Failed to fdopen log file descriptor for %s/%s: %s\n" , +TMON_LOG_DIR ,TMON_LOG_FILENAME ,strerror (errno)); +close (log_fd);return ; }
tools/thermal/tmon/tmon.h +2 -1
@@ -25,7 +25,8 @@*/ #define DATA_LEFT_ALIGN 10#define NR_LINES_TZDATA 1 -#define TMON_LOG_FILE "/var/tmp/tmon.log" +#define TMON_LOG_DIR "/var/tmp" +#define TMON_LOG_FILENAME "tmon.log"#include <sys/time.h> #include <pthread.h>
Signature · 1 line
2.49.0
PATCH 2/3 tools/thermal/tmon: Make sure ctrl_cdev is a Null terminated string
In reply to Thomas RenningerFound by compiler warning:
warning: ‘strncpy’ specified bound 20 equals destination size [-Wstringop-truncation]
--- tools/thermal/tmon/tmon.c | 1 + 1 file changed, 1 insertion(+)
tools/thermal/tmon/tmon.c +1 -0
@@ -251,6 +251,7 @@ int main(int argc, char **argv)case 'c' : no_control =0 ;strncpy (ctrl_cdev, optarg,CDEV_NAME_SIZE ); + ctrl_cdev[CDEV_NAME_SIZE -1 ] =0 ;break ;case 'd' :start_daemon_mode ();
Signature · 1 line
2.49.0
PATCH 3/3 tools/thermal/tmon: Cleanup only: Use LOG_FILE vars
In reply to Thomas Renninger--- tools/thermal/tmon/tmon.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
tools/thermal/tmon/tmon.c +4 -2
@@ -55,7 +55,8 @@ void usage(void)printf (" -d, --daemon run as daemon, no TUI\n" );printf (" -g, --debug debug message in syslog\n" );printf (" -h, --help show this help message\n" ); -printf (" -l, --log log data to /var/tmp/tmon.log\n" ); +printf (" -l, --log log data to %s/%s\n" , +TMON_LOG_DIR ,TMON_LOG_FILENAME );printf (" -t, --time-interval sampling time interval, > 1 sec.\n" );printf (" -T, --target-temp initial target temperature\n" );printf (" -v, --version show version\n" );
@@ -271,7 +272,8 @@ int main(int argc, char **argv) target_temp_user = temp;break ;case 'l' : -printf ("Logging data to /var/tmp/tmon.log\n" ); +printf ("Logging data to %s/%s\n" , +TMON_LOG_DIR ,TMON_LOG_FILENAME ); logging =1 ;break ;case 'h' :
Signature · 1 line
2.49.0