Re: sort: another mkstemp use case

2015-04-01 Thread Todd C. Miller
On Wed, 01 Apr 2015 21:55:14 +0200, Tobias Stoeckmann wrote:

> When creating a new temporary file name, use mkstemp instead of just
> taking a rather predictable path, which could even be a symlink by
> a malicious user (granted, that is very unlikely).

Heh, you beat me to that one.  I'm going to revisit how temp files
are used at some later point but this is OK for now.

With this change I think we no longer need the umask dance in
openfile().

 - todd



sort: another mkstemp use case

2015-04-01 Thread Tobias Stoeckmann
When creating a new temporary file name, use mkstemp instead of just
taking a rather predictable path, which could even be a symlink by
a malicious user (granted, that is very unlikely).

Index: file.c
===
RCS file: /cvs/src/usr.bin/sort/file.c,v
retrieving revision 1.6
diff -u -p -r1.6 file.c
--- file.c  1 Apr 2015 19:06:18 -   1.6
+++ file.c  1 Apr 2015 19:48:25 -
@@ -167,12 +167,13 @@ file_is_tmp(const char *fn)
 char *
 new_tmp_file_name(void)
 {
-   static size_t tfcounter = 0;
-   static const char *fn = ".bsdsort.";
char *ret;
+   int fd;
 
-   sort_asprintf(&ret, "%s/%s%d.%lu", tmpdir, fn, (int)getpid(),
-   (unsigned long)(tfcounter++));
+   sort_asprintf(&ret, "%s/.bsdsort.XX", tmpdir);
+   if ((fd = mkstemp(ret)) == -1)
+   err(2, "%s", ret);
+   close(fd);
tmp_file_atexit(ret);
return ret;
 }