Skip to content

Commit 131530a

Browse files
committed
patch 8.2.3245: the crypt key may appear in a swap partition
Problem: The crypt key may appear in a swap partition. Solution: When using xchaha20 use sodium_mlock(). (Christian Brabandt, closes #8657)
1 parent 41114a2 commit 131530a

7 files changed

Lines changed: 25 additions & 13 deletions

File tree

src/buffer.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,6 +2306,10 @@ free_buf_options(
23062306
clear_string_option(&buf->b_p_fex);
23072307
#endif
23082308
#ifdef FEAT_CRYPT
2309+
# ifdef FEAT_SODIUM
2310+
if (buf->b_p_key != NULL && (crypt_get_method_nr(buf) == CRYPT_M_SOD))
2311+
sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
2312+
# endif
23092313
clear_string_option(&buf->b_p_key);
23102314
#endif
23112315
clear_string_option(&buf->b_p_kp);

src/crypt.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
*/
1313
#include "vim.h"
1414

15-
#ifdef FEAT_SODIUM
16-
# include <sodium.h>
17-
#endif
18-
1915
#if defined(FEAT_CRYPT) || defined(PROTO)
2016
/*
2117
* Optional encryption support.
@@ -447,6 +443,8 @@ crypt_free_state(cryptstate_T *state)
447443
#ifdef FEAT_SODIUM
448444
if (state->method_nr == CRYPT_M_SOD)
449445
{
446+
sodium_munlock(((sodium_state_T *)state->method_state)->key,
447+
crypto_box_SEEDBYTES);
450448
sodium_memzero(state->method_state, sizeof(sodium_state_T));
451449
sodium_free(state->method_state);
452450
}
@@ -726,6 +724,7 @@ crypt_sodium_init(
726724
// crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES
727725
unsigned char dkey[crypto_box_SEEDBYTES]; // 32
728726
sodium_state_T *sd_state;
727+
int retval = 0;
729728

730729
if (sodium_init() < 0)
731730
return FAIL;
@@ -743,6 +742,16 @@ crypt_sodium_init(
743742
return FAIL;
744743
}
745744
memcpy(sd_state->key, dkey, crypto_box_SEEDBYTES);
745+
746+
retval += sodium_mlock(sd_state->key, crypto_box_SEEDBYTES);
747+
retval += sodium_mlock(key, STRLEN(key));
748+
749+
if (retval < 0)
750+
{
751+
emsg(_(e_encryption_sodium_mlock_failed));
752+
sodium_free(sd_state);
753+
return FAIL;
754+
}
746755
sd_state->count = 0;
747756
state->method_state = sd_state;
748757

src/errors.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,3 +641,5 @@ EXTERN char e_list_or_dict_or_blob_required_for_argument_nr[]
641641
INIT(= N_("E1228: List or Dictionary or Blob required for argument %d"));
642642
EXTERN char e_expected_dictionary_for_using_key_str_but_got_str[]
643643
INIT(= N_("E1229: Expected dictionary for using key \"%s\", but got %s"));
644+
EXTERN char e_encryption_sodium_mlock_failed[]
645+
INIT(= N_("E1230: encryption: sodium_mlock() failed"));

src/fileio.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313

1414
#include "vim.h"
1515

16-
#ifdef FEAT_SODIUM
17-
# include <sodium.h>
18-
#endif
19-
2016
#if defined(__TANDEM)
2117
# include <limits.h> // for SSIZE_MAX
2218
#endif

src/memline.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@
4848
# include <time.h>
4949
#endif
5050

51-
// for randombytes_buf
52-
#ifdef FEAT_SODIUM
53-
# include <sodium.h>
54-
#endif
55-
5651
#if defined(SASC) || defined(__amigaos4__)
5752
# include <proto/dos.h> // for Open() and Close()
5853
#endif

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,8 @@ static char *(features[]) =
755755

756756
static int included_patches[] =
757757
{ /* Add new patch number below this line */
758+
/**/
759+
3245,
758760
/**/
759761
3244,
760762
/**/

src/vim.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,10 @@ typedef unsigned int u8char_T; // int is 32 bits or more
486486
# endif
487487
#endif
488488

489+
#ifdef HAVE_SODIUM
490+
# include <sodium.h>
491+
#endif
492+
489493
// ================ end of the header file puzzle ===============
490494

491495
/*

0 commit comments

Comments
 (0)