Skip to content

Commit

Permalink
Fix a few more problems with sodium
Browse files Browse the repository at this point in the history
1) Make sure to sodium_mlock the key, even when using :set key=
Prevents a segfault in Windows, when the key hasn't been mlocked before

2) Prevent segfault in ml_preserve on Windows
  • Loading branch information
chrisbra committed Jun 27, 2023
1 parent f5f3e0d commit 2dc1d59
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,16 @@ crypt_append_msg(
}
}

void
crypt_sodium_lock_key(
char_u *key)
{
if (sodium_init() < 0)
return;
sodium_mlock(key, STRLEN(key));
return;
}

static int
crypt_sodium_init_(
cryptstate_T *state UNUSED,
Expand Down
13 changes: 11 additions & 2 deletions src/memline.c
Original file line number Diff line number Diff line change
Expand Up @@ -2504,8 +2504,6 @@ ml_sync_all(int check_file, int check_char)
// close the swapfile
mf_close_file(buf, TRUE);
buf->b_p_swf = FALSE;
vim_free(buf->b_p_key);
buf->b_p_key = empty_option;
continue;
}
#endif
Expand Down Expand Up @@ -2567,6 +2565,17 @@ ml_preserve(buf_T *buf, int message)
emsg(_(e_cannot_preserve_there_is_no_swap_file));
return;
}
#ifdef FEAT_CRYPT
// Safety Check
if (crypt_method_is_sodium(crypt_get_method_nr(buf))
&& *buf->b_p_key != NUL)
{
// close the swapfile
mf_close_file(buf, TRUE);
buf->b_p_swf = FALSE;
return;
}
#endif

// We only want to stop when interrupted here, not when interrupted
// before.
Expand Down
4 changes: 4 additions & 0 deletions src/optionstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,10 @@ did_set_cryptkey(optset_T *args)
*curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
changed_internal();
}
#ifdef FEAT_SODIUM
if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
crypt_sodium_lock_key(args->os_newval.string);
#endif

return NULL;
}
Expand Down
2 changes: 2 additions & 0 deletions src/proto/crypt.pro
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ int crypt_sodium_munlock(void *const addr, const size_t len);
void crypt_sodium_randombytes_buf(void *const buf, const size_t size);
int crypt_sodium_init(void);
uint32_t crypt_sodium_randombytes_random(void);
void crypt_sodium_lock_key(char_u *key);

/* vim: set ft=c : */
2 changes: 0 additions & 2 deletions src/testdir/test_crypt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,6 @@ endfunc

func Test_crypt_set_key_segfault()
CheckFeature sodium
" This test fails on Windows, why?
CheckNotMSWindows

defer delete('Xtest2.txt')
new Xtest2.txt
Expand Down

0 comments on commit 2dc1d59

Please sign in to comment.