]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Never allow empty passwords (#2116)
authorTobias Stoeckmann <stoeckmann@users.noreply.github.com>
Sat, 13 Apr 2024 05:44:51 +0000 (05:44 +0000)
committerGitHub <noreply@github.com>
Sat, 13 Apr 2024 05:44:51 +0000 (22:44 -0700)
Passwords for encryption must not be empty. Neither through command line
option nor through interactive input.

With this PR applied:
```
$ bsdtar --format zip --options zip:encryption -cf archive.zip input.txt
Enter passphrase: <press enter>
bsdtar: Encryption needs passphrase
```

Output with command line argument (unaffected by this PR):
```
$ bsdtar --format zip --options zip:encryption --passphrase '' -cf archive.zip input.txt
bsdtar: Empty passphrase is unacceptable
```

The outputs differ due to internal difference in handling the results.

It is still possible to supply a passphrase through command line
argument which cannot be entered interactively, i.e. $'\r\n'. See
https://github.com/libarchive/libarchive/pull/2115 for more details.

libarchive/archive_write_set_passphrase.c

index 977fc4a9ee6beca4f405bbf2c70fb214b86ef9af..f871c8e2f8105c4291e33a0f8c069228c52e370d 100644 (file)
 #endif
 #include "archive_write_private.h"
 
-int
-archive_write_set_passphrase(struct archive *_a, const char *p)
+static int
+set_passphrase(struct archive_write *a, const char *p)
 {
-       struct archive_write *a = (struct archive_write *)_a;
-
-       archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW,
-               "archive_write_set_passphrase");
-
        if (p == NULL || p[0] == '\0') {
                archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
                    "Empty passphrase is unacceptable");
@@ -54,6 +49,18 @@ archive_write_set_passphrase(struct archive *_a, const char *p)
 }
 
 
+int
+archive_write_set_passphrase(struct archive *_a, const char *p)
+{
+       struct archive_write *a = (struct archive_write *)_a;
+
+       archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW,
+               "archive_write_set_passphrase");
+
+       return (set_passphrase(a, p));
+}
+
+
 int
 archive_write_set_passphrase_callback(struct archive *_a, void *client_data,
     archive_passphrase_callback *cb)
@@ -80,15 +87,9 @@ __archive_write_get_passphrase(struct archive_write *a)
                const char *p;
                p = a->passphrase_callback(&a->archive,
                    a->passphrase_client_data);
-               if (p != NULL) {
-                       a->passphrase = strdup(p);
-                       if (a->passphrase == NULL) {
-                               archive_set_error(&a->archive, ENOMEM,
-                                   "Can't allocate data for passphrase");
-                               return (NULL);
-                       }
-                       return (a->passphrase);
-               }
+               set_passphrase(a, p);
+               a->passphrase_callback = NULL;
+               a->passphrase_client_data = NULL;
        }
-       return (NULL);
+       return (a->passphrase);
 }