Welcome to little lamb

Code » limb » commit 10c7944

Fix EXDEV on open_write{,v}_closeat/shldata_write

author Olivier Brunel
2023-10-02 10:45:21 UTC
committer Olivier Brunel
2024-01-01 19:10:12 UTC
parent e0999f3229385af8da2d973f5797f28b2647d6c2

Fix EXDEV on open_write{,v}_closeat/shldata_write

Temporary filename wasn't based on the actual/destination one, so it
wasn't in the correct directory nor, possibly, on the same device. Which
could lead to all kinds of permissions or cross-device errors.

src/liblimb/djbunix.h/open_write_closeat.c +6 -1
src/liblimb/djbunix.h/open_writev_closeat.c +6 -1
src/liblimb/shldata-rw.h/shldata_write.c +6 -1

diff --git a/src/liblimb/djbunix.h/open_write_closeat.c b/src/liblimb/djbunix.h/open_write_closeat.c
index 33f0b97..a70d0b4 100644
--- a/src/liblimb/djbunix.h/open_write_closeat.c
+++ b/src/liblimb/djbunix.h/open_write_closeat.c
@@ -5,10 +5,15 @@
 #include <limb/allreadwrite.h>
 #include <limb/djbunix.h>
 
+#define SUFFIX ":tmp.limb.XXXXXX"
+
 int
 open_write_closeat(int bfd, const char *file, const char *data, size_t dlen)
 {
-    char tmp[] = ".tmp.limb.XXXXXX";
+    size_t flen = strlen(file);
+    char tmp[flen + sizeof(SUFFIX)];
+    memcpy(tmp, file, flen);
+    memcpy(tmp + flen, SUFFIX, sizeof(SUFFIX));
     int fd = open_tmpat(bfd, tmp);
     if (fd < 0) return 0;
 
diff --git a/src/liblimb/djbunix.h/open_writev_closeat.c b/src/liblimb/djbunix.h/open_writev_closeat.c
index 452f193..ba10ec4 100644
--- a/src/liblimb/djbunix.h/open_writev_closeat.c
+++ b/src/liblimb/djbunix.h/open_writev_closeat.c
@@ -6,10 +6,15 @@
 #include <limb/djbunix.h>
 #include <limb/siovec.h>
 
+#define SUFFIX ":tmp.limb.XXXXXX"
+
 int
 open_writev_closeat(int bfd, const char *file, const struct iovec *v, unsigned n)
 {
-    char tmp[] = ".tmp.limb.XXXXXX";
+    size_t flen = strlen(file);
+    char tmp[flen + sizeof(SUFFIX)];
+    memcpy(tmp, file, flen);
+    memcpy(tmp + flen, SUFFIX, sizeof(SUFFIX));
     int fd = open_tmpat(bfd, tmp);
     if (fd < 0) return 0;
 
diff --git a/src/liblimb/shldata-rw.h/shldata_write.c b/src/liblimb/shldata-rw.h/shldata_write.c
index 807326d..fbe8449 100644
--- a/src/liblimb/shldata-rw.h/shldata_write.c
+++ b/src/liblimb/shldata-rw.h/shldata_write.c
@@ -10,6 +10,8 @@
 #include <limb/shldata-rw.h>
 #include <limb/siovec.h>
 
+#define SUFFIX ":tmp.limb.XXXXXX"
+
 int
 shldata_write(int bfd, const char *file, u32 magic, u64 ver,
               const char *pwd, size_t plen, unsigned algo, unsigned iter,
@@ -18,7 +20,10 @@ shldata_write(int bfd, const char *file, u32 magic, u64 ver,
     int ret = 0;
     char buf[4096];
     buffer b = BUFFER_INIT(&buffer_write, -1, buf, sizeof(buf));
-    char tmp[] = ".tmp.shldata.XXXXXX";
+    size_t flen = strlen(file);
+    char tmp[flen + sizeof(SUFFIX)];
+    memcpy(tmp, file, flen);
+    memcpy(tmp + flen, SUFFIX, sizeof(SUFFIX));
 
     if (!shldata_chkmagic(magic))
         return 0;