]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 8.2.3245: the crypt key may appear in a swap partition v8.2.3245
authorBram Moolenaar <Bram@vim.org>
Thu, 29 Jul 2021 18:37:49 +0000 (20:37 +0200)
committerBram Moolenaar <Bram@vim.org>
Thu, 29 Jul 2021 18:37:49 +0000 (20:37 +0200)
Problem:    The crypt key may appear in a swap partition.
Solution:   When using xchaha20 use sodium_mlock(). (Christian Brabandt,
            closes #8657)

src/buffer.c
src/crypt.c
src/errors.h
src/fileio.c
src/memline.c
src/version.c
src/vim.h

index 59397e4a5004ca244e46474ad5d05bef7415e712..628c82f18698c7dcacf6c975572bae2275dc8a0b 100644 (file)
@@ -2306,6 +2306,10 @@ free_buf_options(
     clear_string_option(&buf->b_p_fex);
 #endif
 #ifdef FEAT_CRYPT
+# ifdef FEAT_SODIUM
+    if (buf->b_p_key != NULL && (crypt_get_method_nr(buf) == CRYPT_M_SOD))
+       sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
+# endif
     clear_string_option(&buf->b_p_key);
 #endif
     clear_string_option(&buf->b_p_kp);
index fad6df0ddc483220805ed3391c8cabf0c9cabc29..f2f643f35afa63a51f3f8add457db8ef219b6e56 100644 (file)
  */
 #include "vim.h"
 
-#ifdef FEAT_SODIUM
-# include <sodium.h>
-#endif
-
 #if defined(FEAT_CRYPT) || defined(PROTO)
 /*
  * Optional encryption support.
@@ -447,6 +443,8 @@ crypt_free_state(cryptstate_T *state)
 #ifdef FEAT_SODIUM
     if (state->method_nr == CRYPT_M_SOD)
     {
+       sodium_munlock(((sodium_state_T *)state->method_state)->key,
+                                                        crypto_box_SEEDBYTES);
        sodium_memzero(state->method_state, sizeof(sodium_state_T));
        sodium_free(state->method_state);
     }
@@ -726,6 +724,7 @@ crypt_sodium_init(
     // crypto_box_SEEDBYTES ==  crypto_secretstream_xchacha20poly1305_KEYBYTES
     unsigned char      dkey[crypto_box_SEEDBYTES]; // 32
     sodium_state_T     *sd_state;
+    int                        retval = 0;
 
     if (sodium_init() < 0)
        return FAIL;
@@ -743,6 +742,16 @@ crypt_sodium_init(
        return FAIL;
     }
     memcpy(sd_state->key, dkey, crypto_box_SEEDBYTES);
+
+    retval += sodium_mlock(sd_state->key, crypto_box_SEEDBYTES);
+    retval += sodium_mlock(key, STRLEN(key));
+
+    if (retval < 0)
+    {
+       emsg(_(e_encryption_sodium_mlock_failed));
+       sodium_free(sd_state);
+       return FAIL;
+    }
     sd_state->count = 0;
     state->method_state = sd_state;
 
index 40925f89e6d6f1d5a75d27a6389e5075f4737478..164aafd2a53c97123d978af08ca16eec414d6b9f 100644 (file)
@@ -641,3 +641,5 @@ EXTERN char e_list_or_dict_or_blob_required_for_argument_nr[]
        INIT(= N_("E1228: List or Dictionary or Blob required for argument %d"));
 EXTERN char e_expected_dictionary_for_using_key_str_but_got_str[]
        INIT(= N_("E1229: Expected dictionary for using key \"%s\", but got %s"));
+EXTERN char e_encryption_sodium_mlock_failed[]
+       INIT(= N_("E1230: encryption: sodium_mlock() failed"));
index bd4038c36afd39e29a474cf1eb8345fc990ce930..81a7b5091595fe70ee341d152331bea5077bfe10 100644 (file)
 
 #include "vim.h"
 
-#ifdef FEAT_SODIUM
-# include <sodium.h>
-#endif
-
 #if defined(__TANDEM)
 # include <limits.h>           // for SSIZE_MAX
 #endif
index c2e1074248994dac2dd97761fd76fe282744196b..75c17d2ed7e6e5895c45500214c30bb076476c62 100644 (file)
 # include <time.h>
 #endif
 
-// for randombytes_buf
-#ifdef FEAT_SODIUM
-# include <sodium.h>
-#endif
-
 #if defined(SASC) || defined(__amigaos4__)
 # include <proto/dos.h>            // for Open() and Close()
 #endif
index b0e67ed0b99387911f1b57ca73763e08573467c2..6b372008c54a8a2fbdfc43f85053a1b210b7dab5 100644 (file)
@@ -755,6 +755,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    3245,
 /**/
     3244,
 /**/
index e45a1de2e09e9e088dbac9f014785717ace9edf3..30cf284284014611f496493676e7bba4a861505b 100644 (file)
--- a/src/vim.h
+++ b/src/vim.h
@@ -486,6 +486,10 @@ typedef unsigned int u8char_T;     // int is 32 bits or more
 # endif
 #endif
 
+#ifdef HAVE_SODIUM
+# include <sodium.h>
+#endif
+
 // ================ end of the header file puzzle ===============
 
 /*