]> git.ipfire.org Git - thirdparty/openssh-portable.git/commitdiff
Wrap mkstemp calls with umask set/restore.
authorDarren Tucker <dtucker@dtucker.net>
Fri, 10 Mar 2023 02:27:29 +0000 (13:27 +1100)
committerDarren Tucker <dtucker@dtucker.net>
Fri, 10 Mar 2023 02:27:29 +0000 (13:27 +1100)
glibc versions 2.06 and earlier did not set a umask on files created by
mkstemp created the world-writable.  Wrap mkstemp to set and restore
the umask.  From Coverity (CIDs 291826 291886 291891), ok djm@

openbsd-compat/mktemp.c
openbsd-compat/openbsd-compat.h

index ac922c1ecbe5c579233cf1ba44c98bfd59e31def..4b13b9834957180ad9620ae2ddb528415232f380 100644 (file)
 #include <ctype.h>
 #include <unistd.h>
 
+#ifdef mkstemp
+#undef mkstemp
+#endif
+
+/*
+ * From glibc man page: 'In glibc versions 2.06 and earlier, the file is
+ * created with permissions 0666, that is, read and write for all users.'
+ * Provide a wrapper to make sure the mask is reasonable (POSIX requires
+ * mode 0600, so mask off any other bits).
+ */
+int
+_ssh_mkstemp(char *template)
+{
+       mode_t mask;
+       int ret;
+
+       mask = umask(0177);
+       ret = mkstemp(template);
+       (void)umask(mask);
+       return ret;
+}
+
 #if !defined(HAVE_MKDTEMP)
 
 #define MKTEMP_NAME    0
index 895ecf9ea111dbb35f827fad79b901fb12184822..cc4cf2055bcff78fec05066c47c93a1a6e18fc19 100644 (file)
@@ -141,6 +141,8 @@ int mkstemp(char *path);
 char *mkdtemp(char *path);
 #endif
 
+#define mkstemp(x) _ssh_mkstemp(x)
+
 #ifndef HAVE_DAEMON
 int daemon(int nochdir, int noclose);
 #endif