[MDEV-27964] The function ENCRYPT() causes SIGSEGV in WITH_MSAN builds Created: 2022-02-28  Updated: 2023-04-27

Status: Open
Project: MariaDB Server
Component/s: Encryption
Affects Version/s: 10.5, 10.6, 10.7, 10.8
Fix Version/s: 10.5, 10.6

Type: Bug Priority: Major
Reporter: Marko Mäkelä Assignee: Sergei Golubchik
Resolution: Unresolved Votes: 0
Labels: MSAN

Issue Links:
Relates
relates to MDBF-276 Apply changes to the MSAN builder Closed
relates to MDEV-20377 Make WITH_MSAN more usable Closed

 Description   

In both clang-10 and clang-12, a WITH_MSAN build of the server would crash with SIGSEGV if the ENCRYPT() function is evaluated. The problem seems to be related to the MSAN interceptor of the crypt() function. I was unable to reproduce this with a simple test program:

#include <crypt.h>
#include <stdio.h>
 
int main(int argc, char **argv)
{
  printf("crypt: %s\n", crypt(argv[1], "123"));
  return 0;
}

clang-10 -fsanitize=undefined crypt.c -lcrypt
./a.out foo

I see that re-entrant versions of the C function crypt() exist, but we are using the original version with LOCK_crypt around it. Perhaps one of the other variants of the function would be intercepted better?

Note: No memory appears to be uninitialized. The following patch did not trigger anything; the very first sign of trouble was the SIGSEGV.

diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc
index 0635556be40..3f9e023c793 100644
--- a/sql/item_strfunc.cc
+++ b/sql/item_strfunc.cc
@@ -2280,6 +2280,8 @@ String *Item_func_encrypt::val_str(String *str)
     salt_ptr= salt_str->c_ptr_safe();
   }
   mysql_mutex_lock(&LOCK_crypt);
+  MEM_CHECK_DEFINED(res->c_ptr_safe(), res->length);
+  MEM_CHECK_DEFINED(salt_ptr, 2);
   char *tmp= crypt(res->c_ptr_safe(),salt_ptr);
   if (!tmp)
   {

To work around this problem, I will move some encrypt() test cases from the files main.view and main.func_encrypt to the test main.func_crypt, which will be disabled in cmake -DWITH_MSAN=ON builds for now.


Generated at Thu Feb 08 09:56:58 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.