summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2014-01-28 13:26:48 +0100
committerLennart Poettering <lennart@poettering.net>2014-01-28 13:26:48 +0100
commit7736202ce9149942e96e525c08d508daa448aff5 (patch)
treecc21da170b957c6589278ed39c95780e3d889419 /src/shared
parentc09918f97a3c2fc4c8320d327eafd287bc2138da (diff)
util: pick slightly safer open() flags when creating temporary files
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/util.c7
1 files changed, 5 insertions, 2 deletions
diff --git a/src/shared/util.c b/src/shared/util.c
index a6e86148d1..f9cbb2073c 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -6136,7 +6136,7 @@ int mkostemp_safe(char *pattern, int flags) {
for (i = 0; i < 6; i++)
s[i] = ALPHANUMERICAL[(unsigned) s[i] % (sizeof(ALPHANUMERICAL)-1)];
- fd = open(pattern, flags|O_EXCL|O_CREAT, S_IRUSR|S_IWUSR);
+ fd = open(pattern, flags|O_EXCL|O_CREAT|O_NOCTTY|O_NOFOLLOW, S_IRUSR|S_IWUSR);
if (fd >= 0)
return fd;
if (!IN_SET(errno, EEXIST, EINTR))
@@ -6153,10 +6153,13 @@ int open_tmpfile(const char *path, int flags) {
assert(path);
#ifdef O_TMPFILE
- fd = open(path, flags|O_TMPFILE|O_NOCTTY, S_IRUSR|S_IWUSR);
+ /* Try O_TMPFILE first, if it is supported */
+ fd = open(path, flags|O_TMPFILE, S_IRUSR|S_IWUSR);
if (fd >= 0)
return fd;
#endif
+
+ /* Fall back to unguessable name + unlinking */
p = strappenda(path, "/systemd-tmp-XXXXXX");
fd = mkostemp_safe(p, flags);