]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
vipw: Use fmkomstemp for temporary file
authorTobias Stoeckmann <tobias@stoeckmann.org>
Wed, 7 Jan 2026 16:39:42 +0000 (17:39 +0100)
committerAlejandro Colomar <foss+github@alejandro-colomar.es>
Fri, 9 Jan 2026 13:13:59 +0000 (14:13 +0100)
Avoid fixed and thus predictable temporary file names. Especially avoid
just opening already existing ones.

Reviewed-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
src/vipw.c

index a3d2d7f48ce671d515b5fe9f13774a75f89a3b5e..4f3b53d58758e36da472369f0f4c7ca6cbb5660e 100644 (file)
@@ -43,6 +43,7 @@
 #endif                         /* WITH_TCB */
 #include "shadowlog.h"
 #include "sssd.h"
+#include "fs/mkstemp/fmkomstemp.h"
 #include "string/sprintf/aprintf.h"
 #include "string/sprintf/snprintf.h"
 #include "string/strcmp/streq.h"
@@ -72,7 +73,7 @@ static bool tcb_mode = false;
 
 /* local function prototypes */
 static void usage (int status);
-static int create_backup_file (FILE *, const char *, struct stat *);
+static int create_backup_file (FILE *, char *, struct stat *);
 static void vipwexit (const char *msg, int syserr, int ret);
 static void vipwedit (const char *, int (*)(void), int (*)(bool));
 
@@ -103,16 +104,13 @@ static void usage (int status)
 /*
  *
  */
-static int create_backup_file (FILE * fp, const char *backup, struct stat *sb)
+static int create_backup_file (FILE * fp, char *backup, struct stat *sb)
 {
        struct utimbuf ub;
        FILE *bkfp;
        int c;
-       mode_t mask;
 
-       mask = umask (077);
-       bkfp = fopen (backup, "w");
-       (void) umask (mask);
+       bkfp = fmkomstemp(backup, 0, 0600);
        if (NULL == bkfp) {
                return -1;
        }
@@ -217,11 +215,11 @@ vipwedit (const char *file, int (*file_lock) (void), int (*file_unlock) (bool))
                        vipwexit (_("failed to drop privileges"), errno, 1);
                }
                stprintf_a(fileedit,
-                        TCB_DIR "/" SHADOWTCB_SCRATCHDIR "/.vipw.shadow.%s",
-                        user);
+                        TCB_DIR "/" SHADOWTCB_SCRATCHDIR "/.%s.shadow.%s.XXXXXX",
+                        Prog, user);
        } else {
 #endif                         /* WITH_TCB */
-               stprintf_a(fileedit, "%s.edit", file);
+               stprintf_a(fileedit, "/etc/.%s.XXXXXX", Prog);
 #ifdef WITH_TCB
        }
 #endif                         /* WITH_TCB */