]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Create temporary files in the target directory 2753/head
authorDag-Erling Smørgrav <des@des.no>
Tue, 14 Oct 2025 00:11:31 +0000 (02:11 +0200)
committerDag-Erling Smørgrav <des@des.no>
Wed, 15 Oct 2025 07:41:06 +0000 (09:41 +0200)
Whenever we need to create a temporary file while writing to disk on a
POSIX system, try to create it in the same directory as the final file
instead of the current working directory.  The target directory can
reasonably be expected to be writable (and if it isn't, creating the
file will fail anyway), but the current working directory may not be.

While here, consistently use __archive_mkstemp(), and increase the
template from six to eight random characters.

Fixes: 2e73ea3a7db1 ("Fix max path-length metadata writing (#2243)")
Fixes: e12c955dca63 ("Unify temporary directory handling")

libarchive/archive_read_disk_entry_from_file.c
libarchive/archive_write_disk_posix.c

index 42af4034b07ea779969d24c125be41287356f7fe..121af19872e9424f6cb2ec5d1af4c370130f52ff 100644 (file)
@@ -358,12 +358,10 @@ setup_mac_metadata(struct archive_read_disk *a,
                return (ARCHIVE_OK);
 
        archive_string_init(&tempfile);
-       if (__archive_get_tempdir(&tempfile) != ARCHIVE_OK) {
-               ret = ARCHIVE_WARN;
-               goto cleanup;
-       }
-       archive_strcat(&tempfile, "tar.md.XXXXXX");
-       tempfd = mkstemp(tempfile.s);
+       archive_strcpy(&tempfile, name);
+       archive_string_dirname(&tempfile);
+       archive_strcat(&tempfile, "/tar.XXXXXXXX");
+       tempfd = __archive_mkstemp(tempfile.s);
        if (tempfd < 0) {
                archive_set_error(&a->archive, errno,
                    "Could not open extended attribute file");
index 6fcf3929b5cb69091ab8cdf080af8c3afcb14b2c..cd25620305b648b1c8592f58f24fd052c9255e1e 100644 (file)
@@ -412,12 +412,14 @@ static ssize_t    _archive_write_disk_data_block(struct archive *, const void *,
 static int
 la_mktemp(struct archive_write_disk *a)
 {
+       struct archive_string *tmp = &a->_tmpname_data;
        int oerrno, fd;
        mode_t mode;
 
-       archive_string_empty(&a->_tmpname_data);
-       archive_string_sprintf(&a->_tmpname_data, "%s.XXXXXX", a->name);
-       a->tmpname = a->_tmpname_data.s;
+       archive_strcpy(tmp, a->name);
+       archive_string_dirname(tmp);
+       archive_strcat(tmp, "/tar.XXXXXXXX");
+       a->tmpname = tmp->s;
 
        fd = __archive_mkstemp(a->tmpname);
        if (fd == -1)
@@ -4287,8 +4289,10 @@ create_tempdatafork(struct archive_write_disk *a, const char *pathname)
        int tmpfd;
 
        archive_string_init(&tmpdatafork);
-       archive_strcpy(&tmpdatafork, "tar.md.XXXXXX");
-       tmpfd = mkstemp(tmpdatafork.s);
+       archive_strcpy(&tmpdatafork, pathname);
+       archive_string_dirname(&tmpdatafork);
+       archive_strcat(&tmpdatafork, "/tar.XXXXXXXX");
+       tmpfd = __archive_mkstemp(tmpdatafork.s);
        if (tmpfd < 0) {
                archive_set_error(&a->archive, errno,
                    "Failed to mkstemp");
@@ -4367,8 +4371,10 @@ set_mac_metadata(struct archive_write_disk *a, const char *pathname,
         * silly dance of writing the data to disk just so that
         * copyfile() can read it back in again. */
        archive_string_init(&tmp);
-       archive_strcpy(&tmp, "tar.mmd.XXXXXX");
-       fd = mkstemp(tmp.s);
+       archive_strcpy(&tmp, pathname);
+       archive_string_dirname(&tmp);
+       archive_strcat(&tmp, "/tar.XXXXXXXX");
+       fd = __archive_mkstemp(tmp.s);
 
        if (fd < 0) {
                archive_set_error(&a->archive, errno,