Add MariaDB 10.1.13 offsets. Format the code.
parent
051b1fd67e
commit
80646620e9
File diff suppressed because it is too large
Load Diff
|
@ -43,13 +43,13 @@ extern void MD5_Init(MD5_CTX *ctx);
|
|||
extern void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size);
|
||||
extern void MD5_Final(unsigned char *result, MD5_CTX *ctx);
|
||||
|
||||
//define the my_MD5* functions
|
||||
// define the my_MD5* functions
|
||||
#define my_MD5Context MD5_CTX
|
||||
#define my_MD5Init MD5_Init
|
||||
#define my_MD5Update MD5_Update
|
||||
#define my_MD5Final MD5_Final
|
||||
|
||||
|
||||
#endif //#if MYSQL_VERSION_ID >= 50600
|
||||
#endif // #if MYSQL_VERSION_ID >= 50600
|
||||
|
||||
#endif
|
||||
|
|
|
@ -8,8 +8,8 @@
|
|||
#define MYSQL_DYNAMIC_PLUGIN 1
|
||||
#define MYSQL_SERVER 1
|
||||
|
||||
//Fix for VIO. We don't want to using method mapping as then a change in the struct will cause the offsets compiled with to
|
||||
//be wrong. As is the case with ndb which uses a version of Vio with support for ipv6 similar to 5.5 but different from 5.1
|
||||
// Fix for VIO. We don't want to using method mapping as then a change in the struct will cause the offsets compiled with to
|
||||
// be wrong. As is the case with ndb which uses a version of Vio with support for ipv6 similar to 5.5 but different from 5.1
|
||||
#define DONT_MAP_VIO
|
||||
|
||||
#include <my_config.h>
|
||||
|
@ -19,7 +19,7 @@
|
|||
#include <mysql_priv.h>
|
||||
#else
|
||||
|
||||
//version 5.5.x doesn't contain mysql_priv.h . We need to add the includes provided by it.
|
||||
// version 5.5.x doesn't contain mysql_priv.h . We need to add the includes provided by it.
|
||||
#if MYSQL_VERSION_ID >= 50505
|
||||
|
||||
// These two are not present in 5.7.9
|
||||
|
@ -43,7 +43,7 @@
|
|||
#include <sql/sql_table.h>
|
||||
#include <sql/sql_view.h>
|
||||
|
||||
//TODO: use mysql mutex instead of pthread
|
||||
// TODO: use mysql mutex instead of pthread
|
||||
/*
|
||||
#define pthread_mutex_lock mysql_mutex_lock
|
||||
#define pthread_mutex_unlock mysql_mutex_unlock
|
||||
|
@ -67,8 +67,8 @@
|
|||
#include <my_dir.h>
|
||||
#include <my_sys.h>
|
||||
|
||||
//5.5 use my_free with a single param. 5.1 use with 2 params
|
||||
//based on: http://bazaar.launchpad.net/~mysql/myodbc/5.1/view/head:/util/stringutil.h
|
||||
// 5.5 use my_free with a single param. 5.1 use with 2 params
|
||||
// based on: http://bazaar.launchpad.net/~mysql/myodbc/5.1/view/head:/util/stringutil.h
|
||||
#ifndef x_free
|
||||
# if MYSQL_VERSION_ID >= 50500
|
||||
# define x_free(A) { void *tmp= (A); if (tmp) my_free((char *) tmp); }
|
||||
|
@ -77,23 +77,19 @@
|
|||
# endif
|
||||
#endif
|
||||
|
||||
//MariaDB doesn't have my_getsystime (returns 100 nano seconds) function. They replaced with my_hrtime_t my_hrtime() which returns microseconds
|
||||
// MariaDB doesn't have my_getsystime (returns 100 nano seconds) function. They replaced with my_hrtime_t my_hrtime() which returns microseconds
|
||||
#if defined(MARIADB_BASE_VERSION)
|
||||
|
||||
#define my_getsystime() ((my_hrtime()).val * 10)
|
||||
//MariaDB has a kill service that overrides thd_killed as a macro. It also has thd_killed function defined for backwards compatibility, so we redefine it.
|
||||
// MariaDB has a kill service that overrides thd_killed as a macro. It also has thd_killed function defined for backwards compatibility, so we redefine it.
|
||||
#undef thd_killed
|
||||
extern "C" int thd_killed(const MYSQL_THD thd);
|
||||
|
||||
//MariadDB 10.0.10 removed the include for thd_security_context
|
||||
// MariadDB 10.0.10 removed the include for thd_security_context
|
||||
#if MYSQL_VERSION_ID >= 100010
|
||||
extern "C" char *thd_security_context(MYSQL_THD thd, char *buffer, unsigned int length, unsigned int max_query_len);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif //MYSQL_INCL_H
|
||||
|
||||
|
||||
#endif // MYSQL_INCL_H
|
||||
|
|
|
@ -1,43 +1,43 @@
|
|||
/*
|
||||
* compile_assert.h
|
||||
*
|
||||
* Created on: Mar 6, 2011
|
||||
* Author: Guyl
|
||||
*/
|
||||
|
||||
#ifndef COMPILE_STATIC_ASSERT_H_
|
||||
#define COMPILE_STATIC_ASSERT_H_
|
||||
|
||||
/**
|
||||
* Taken from: http://stackoverflow.com/questions/807244/c-compiler-asserts-how-to-implement
|
||||
* A compile time assertion check.
|
||||
*
|
||||
* Validate at compile time that the predicate is true without
|
||||
* generating code. This can be used at any point in a source file
|
||||
* where typedef is legal.
|
||||
*
|
||||
* On success, compilation proceeds normally.
|
||||
*
|
||||
* On failure, attempts to typedef an array type of negative size. The
|
||||
* offending line will look like
|
||||
* typedef assertion_failed_file_h_42[-1]
|
||||
* where file is the content of the second parameter which should
|
||||
* typically be related in some obvious way to the containing file
|
||||
* name, 42 is the line number in the file on which the assertion
|
||||
* appears, and -1 is the result of a calculation based on the
|
||||
* predicate failing.
|
||||
*
|
||||
* \param predicate The predicate to test. It must evaluate to
|
||||
* something that can be coerced to a normal C boolean.
|
||||
*
|
||||
* \param file A sequence of legal identifier characters that should
|
||||
* uniquely identify the source file in which this condition appears.
|
||||
*/
|
||||
|
||||
|
||||
#define CASSERT(predicate, file) _impl_CASSERT_LINE(predicate,__LINE__,file)
|
||||
#define _impl_PASTE(a,b) a##b
|
||||
#define _impl_CASSERT_LINE(predicate, line, file) \
|
||||
typedef char _impl_PASTE(assertion_failed_##file##_,line)[2*!!(predicate)-1];
|
||||
|
||||
#endif /* COMPILE_STATIC_ASSERT_H_ */
|
||||
/*
|
||||
* compile_assert.h
|
||||
*
|
||||
* Created on: Mar 6, 2011
|
||||
* Author: Guyl
|
||||
*/
|
||||
|
||||
#ifndef COMPILE_STATIC_ASSERT_H_
|
||||
#define COMPILE_STATIC_ASSERT_H_
|
||||
|
||||
/**
|
||||
* Taken from: http://stackoverflow.com/questions/807244/c-compiler-asserts-how-to-implement
|
||||
* A compile time assertion check.
|
||||
*
|
||||
* Validate at compile time that the predicate is true without
|
||||
* generating code. This can be used at any point in a source file
|
||||
* where typedef is legal.
|
||||
*
|
||||
* On success, compilation proceeds normally.
|
||||
*
|
||||
* On failure, attempts to typedef an array type of negative size. The
|
||||
* offending line will look like
|
||||
* typedef assertion_failed_file_h_42[-1]
|
||||
* where file is the content of the second parameter which should
|
||||
* typically be related in some obvious way to the containing file
|
||||
* name, 42 is the line number in the file on which the assertion
|
||||
* appears, and -1 is the result of a calculation based on the
|
||||
* predicate failing.
|
||||
*
|
||||
* \param predicate The predicate to test. It must evaluate to
|
||||
* something that can be coerced to a normal C boolean.
|
||||
*
|
||||
* \param file A sequence of legal identifier characters that should
|
||||
* uniquely identify the source file in which this condition appears.
|
||||
*/
|
||||
|
||||
|
||||
#define CASSERT(predicate, file) _impl_CASSERT_LINE(predicate,__LINE__,file)
|
||||
#define _impl_PASTE(a,b) a##b
|
||||
#define _impl_CASSERT_LINE(predicate, line, file) \
|
||||
typedef char _impl_PASTE(assertion_failed_##file##_,line)[2*!!(predicate)-1];
|
||||
|
||||
#endif /* COMPILE_STATIC_ASSERT_H_ */
|
||||
|
|
1028
src/audit_handler.cc
1028
src/audit_handler.cc
File diff suppressed because it is too large
Load Diff
1748
src/audit_offsets.cc
1748
src/audit_offsets.cc
File diff suppressed because it is too large
Load Diff
2167
src/audit_plugin.cc
2167
src/audit_plugin.cc
File diff suppressed because it is too large
Load Diff
292
src/hot_patch.cc
292
src/hot_patch.cc
|
@ -11,33 +11,33 @@
|
|||
#define ULONG_PTR uint32_t
|
||||
#endif
|
||||
|
||||
static const char * log_prefix = "Audit Plugin:";
|
||||
static const char *log_prefix = "Audit Plugin:";
|
||||
|
||||
static const unsigned long PAGE_SIZE = GETPAGESIZE() ;
|
||||
|
||||
//used to indicate how to do the protect/unprotect
|
||||
// used to indicate how to do the protect/unprotect
|
||||
static bool use_exec_prot = true;
|
||||
|
||||
static int protect(void *addr, size_t len)
|
||||
{
|
||||
int res = 0;
|
||||
if(use_exec_prot)
|
||||
if (use_exec_prot)
|
||||
{
|
||||
res = mprotect(addr,len,PROT_READ|PROT_EXEC);
|
||||
}
|
||||
else //try doing in a 2 step fashion
|
||||
else // try doing in a 2 step fashion
|
||||
{
|
||||
mprotect(addr,len,PROT_READ);
|
||||
res = mprotect(addr,len,PROT_READ|PROT_EXEC);
|
||||
res = mprotect(addr,len,PROT_READ|PROT_EXEC);
|
||||
}
|
||||
if(res)
|
||||
if (res)
|
||||
{
|
||||
sql_print_information(
|
||||
"%s unable to protect mode: PROT_READ|PROT_EXEC. Page: %p, Size: %zu, errno: %d, res %d.",
|
||||
log_prefix, (void *)addr, len, errno, res);
|
||||
//fail only if nx bit is enabled
|
||||
FILE * fp = fopen("/proc/cpuinfo", "r");
|
||||
if(NULL == fp)
|
||||
// fail only if nx bit is enabled
|
||||
FILE *fp = fopen("/proc/cpuinfo", "r");
|
||||
if (NULL == fp)
|
||||
{
|
||||
sql_print_error(
|
||||
"%s unable to verify nx bit. Failed checking /proc/cpuinfo. This may happen if you have SELinux enabled. Disable SELinux execmod protection for mysqld. Page: %p, Size: %zu, errno: %d.",
|
||||
|
@ -45,21 +45,21 @@ static int protect(void *addr, size_t len)
|
|||
return res;
|
||||
}
|
||||
char buff[1024] = {0};
|
||||
const char * flags = "flags";
|
||||
bool nxchecked = false;
|
||||
while(fgets(buff, 1024, fp) != NULL)
|
||||
const char *flags = "flags";
|
||||
bool nxchecked = false;
|
||||
while (fgets(buff, 1024, fp) != NULL)
|
||||
{
|
||||
char * line = buff;
|
||||
//trim white space at start
|
||||
char *line = buff;
|
||||
// trim white space at start
|
||||
while ((strlen(line) > 0) && (isspace(line[0])))
|
||||
{
|
||||
line++;
|
||||
}
|
||||
if(strncmp(line, flags, strlen(flags)) == 0)
|
||||
if (strncmp(line, flags, strlen(flags)) == 0)
|
||||
{
|
||||
nxchecked = true;
|
||||
sql_print_information("%s cpuinfo flags line: %s. ",log_prefix, line);
|
||||
if(strstr(line, " nx")) //nx enabled so fail
|
||||
if (strstr(line, " nx")) // nx enabled so fail
|
||||
{
|
||||
sql_print_error(
|
||||
"%s unable to protect page and nx bit enabled. This may happen if you have SELinux enabled. Disable SELinux execmod protection for mysqld. Page: %p, Size: %zu.",
|
||||
|
@ -71,7 +71,7 @@ static int protect(void *addr, size_t len)
|
|||
}
|
||||
}
|
||||
fclose(fp);
|
||||
if(!nxchecked) //we didn't find flags string for some reason
|
||||
if (! nxchecked) // we didn't find flags string for some reason
|
||||
{
|
||||
sql_print_error(
|
||||
"%s unable to verify nx bit. Failed finding: %s in /proc/cpuinfo. This may happen if you have SELinux enabled. Disable SELinux execmod protection for mysqld. Page: %p, Size: %zu.",
|
||||
|
@ -82,66 +82,67 @@ static int protect(void *addr, size_t len)
|
|||
return 0;
|
||||
}
|
||||
|
||||
//will try to unprotect with PROT_READ|PROT_WRITE|PROT_EXEC. If fails (might happen under SELinux)
|
||||
//will use PROT_READ|PROT_WRITE
|
||||
// will try to unprotect with PROT_READ|PROT_WRITE|PROT_EXEC. If fails (might happen under SELinux)
|
||||
// will use PROT_READ|PROT_WRITE
|
||||
static int unprotect(void *addr, size_t len)
|
||||
{
|
||||
{
|
||||
int res;
|
||||
if(use_exec_prot)
|
||||
if (use_exec_prot)
|
||||
{
|
||||
res = mprotect(addr,len,PROT_READ|PROT_WRITE|PROT_EXEC);
|
||||
if(res)
|
||||
res = mprotect(addr, len, PROT_READ|PROT_WRITE|PROT_EXEC);
|
||||
if (res)
|
||||
{
|
||||
sql_print_information(
|
||||
"%s unable to unprotect. Page: %p, Size: %zu, errno: %d. Using NO EXEC mode.",
|
||||
log_prefix, (void *)addr, len, errno);
|
||||
"%s unable to unprotect. Page: %p, Size: %zu, errno: %d. Using NO EXEC mode.",
|
||||
log_prefix, (void *)addr, len, errno);
|
||||
use_exec_prot = false;
|
||||
//do a sanity test that we can actually unprotect/protect and that nx bit is off
|
||||
// do a sanity test that we can actually unprotect/protect and that nx bit is off
|
||||
res = unprotect(addr, len);
|
||||
if(res)
|
||||
if (res)
|
||||
{
|
||||
sql_print_error(
|
||||
"%s unable to unprotect page. This may happen if you have SELinux enabled. Disable SELinux execmod protection for mysqld. Aborting. Page: %p, Size: %zu, errno: %d.",
|
||||
log_prefix, (void *)addr, len, errno);
|
||||
return res;
|
||||
sql_print_error(
|
||||
"%s unable to unprotect page. This may happen if you have SELinux enabled. Disable SELinux execmod protection for mysqld. Aborting. Page: %p, Size: %zu, errno: %d.",
|
||||
log_prefix, (void *)addr, len, errno);
|
||||
return res;
|
||||
}
|
||||
res = protect(addr, len);
|
||||
sql_print_information("%s protect res: %d", log_prefix, res);
|
||||
if(res)
|
||||
{
|
||||
if (res)
|
||||
{
|
||||
sql_print_error(
|
||||
"%s unable to protect page. This may happen if you have SELinux enabled. Disable SELinux execmod protection for mysqld. Aborting. Page: %p, Size: %zu, errno: %d.",
|
||||
log_prefix, (void *)addr, len, errno);
|
||||
return res;
|
||||
}
|
||||
"%s unable to protect page. This may happen if you have SELinux enabled. Disable SELinux execmod protection for mysqld. Aborting. Page: %p, Size: %zu, errno: %d.",
|
||||
log_prefix, (void *)addr, len, errno);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
else //all is good
|
||||
else // all is good
|
||||
{
|
||||
return res;
|
||||
}
|
||||
}
|
||||
res = mprotect(addr,len,PROT_READ|PROT_WRITE);
|
||||
if(0 != res) //log the failure
|
||||
|
||||
res = mprotect(addr, len, PROT_READ|PROT_WRITE);
|
||||
if (0 != res) // log the failure
|
||||
{
|
||||
sql_print_error(
|
||||
"%s unable to unprotect. Page: %p, Size: %zu, errno: %d. Error.",
|
||||
log_prefix, (void *)addr, len, errno);
|
||||
"%s unable to unprotect. Page: %p, Size: %zu, errno: %d. Error.",
|
||||
log_prefix, (void *)addr, len, errno);
|
||||
}
|
||||
return res;
|
||||
return res;
|
||||
}
|
||||
|
||||
//macro to log via sql_print_information only if cond test is enabled
|
||||
#define cond_info_print(cond_test, ...) do{if(cond_test) sql_print_information(__VA_ARGS__);}while(0)
|
||||
// macro to log via sql_print_information only if cond test is enabled
|
||||
#define cond_info_print(cond_test, ...) do { if (cond_test) sql_print_information(__VA_ARGS__);} while (0)
|
||||
|
||||
|
||||
/*
|
||||
* Get the page address of a given pointer
|
||||
*/
|
||||
static DATATYPE_ADDRESS get_page_address(void * pointer)
|
||||
static DATATYPE_ADDRESS get_page_address(void *pointer)
|
||||
{
|
||||
DATATYPE_ADDRESS pageMask = ( ~(PAGE_SIZE - 1) ) ;
|
||||
DATATYPE_ADDRESS longp = (unsigned long) pointer;
|
||||
return (longp & pageMask);
|
||||
return (longp & pageMask);
|
||||
}
|
||||
|
||||
//
|
||||
|
@ -164,112 +165,115 @@ unsigned int jump_size()
|
|||
|
||||
static void WriteJump(void *pAddress, ULONG_PTR JumpTo)
|
||||
{
|
||||
DATATYPE_ADDRESS AddressPage = get_page_address(pAddress);
|
||||
unprotect((void*)AddressPage, PAGE_SIZE);
|
||||
DATATYPE_ADDRESS AddressPage = get_page_address(pAddress);
|
||||
unprotect((void*)AddressPage, PAGE_SIZE);
|
||||
|
||||
BYTE *pCur = (BYTE *) pAddress;
|
||||
BYTE *pCur = (BYTE *) pAddress;
|
||||
#ifndef __x86_64__
|
||||
|
||||
BYTE * pbJmpSrc = pCur + 5;
|
||||
*pCur++ = 0xE9; // jmp +imm32
|
||||
*((ULONG_PTR *)pCur) = JumpTo - (ULONG_PTR)pbJmpSrc;
|
||||
*pCur++ = 0xE9; // jmp +imm32
|
||||
*((ULONG_PTR *)pCur) = JumpTo - (ULONG_PTR)pbJmpSrc;
|
||||
|
||||
#else
|
||||
|
||||
*pCur = 0xff; // jmp [rip+addr]
|
||||
*(++pCur) = 0x25;
|
||||
*((DWORD *) ++pCur) = 0; // addr = 0
|
||||
pCur += sizeof (DWORD);
|
||||
*((ULONG_PTR *)pCur) = JumpTo;
|
||||
*pCur = 0xff; // jmp [rip+addr]
|
||||
*(++pCur) = 0x25;
|
||||
*((DWORD *) ++pCur) = 0; // addr = 0
|
||||
pCur += sizeof (DWORD);
|
||||
*((ULONG_PTR *)pCur) = JumpTo;
|
||||
|
||||
#endif
|
||||
//}
|
||||
// DWORD dwBuf = 0; // necessary othewrise the function fails
|
||||
|
||||
//DWORD dwBuf = 0; // nessary othewrise the function fails
|
||||
|
||||
protect((void*)AddressPage, PAGE_SIZE);
|
||||
protect((void*)AddressPage, PAGE_SIZE);
|
||||
}
|
||||
|
||||
//
|
||||
// Hooks a function
|
||||
//
|
||||
static bool HookFunction(ULONG_PTR targetFunction, ULONG_PTR newFunction, ULONG_PTR trampolineFunction,
|
||||
static bool HookFunction(ULONG_PTR targetFunction, ULONG_PTR newFunction, ULONG_PTR trampolineFunction,
|
||||
unsigned int *trampolinesize)
|
||||
{
|
||||
#define MAX_INSTRUCTIONS 100
|
||||
uint8_t raw[MAX_INSTRUCTIONS];
|
||||
unsigned int uCurrentSize =0;
|
||||
#define MAX_INSTRUCTIONS 100
|
||||
uint8_t raw[MAX_INSTRUCTIONS];
|
||||
unsigned int uCurrentSize =0;
|
||||
|
||||
#ifndef __x86_64__
|
||||
#define ASM_MODE 32
|
||||
#define ASM_MODE 32
|
||||
#else
|
||||
#define ASM_MODE 64
|
||||
#define ASM_MODE 64
|
||||
#endif
|
||||
memcpy (raw,(void*)targetFunction,MAX_INSTRUCTIONS);
|
||||
ud_t ud_obj;
|
||||
ud_init(&ud_obj);
|
||||
ud_set_input_buffer(&ud_obj, raw, MAX_INSTRUCTIONS);
|
||||
ud_set_mode(&ud_obj, ASM_MODE);
|
||||
ud_set_syntax(&ud_obj, UD_SYN_INTEL);
|
||||
ud_set_pc(&ud_obj, targetFunction);
|
||||
memcpy(raw, (void*)targetFunction, MAX_INSTRUCTIONS);
|
||||
ud_t ud_obj;
|
||||
ud_init(&ud_obj);
|
||||
ud_set_input_buffer(&ud_obj, raw, MAX_INSTRUCTIONS);
|
||||
ud_set_mode(&ud_obj, ASM_MODE);
|
||||
ud_set_syntax(&ud_obj, UD_SYN_INTEL);
|
||||
ud_set_pc(&ud_obj, targetFunction);
|
||||
|
||||
DWORD InstrSize = 0;
|
||||
DATATYPE_ADDRESS trampolineFunctionPage = get_page_address((void*)trampolineFunction);
|
||||
if(unprotect((void*)trampolineFunctionPage, PAGE_SIZE) != 0)
|
||||
DWORD InstrSize = 0;
|
||||
DATATYPE_ADDRESS trampolineFunctionPage = get_page_address((void*)trampolineFunction);
|
||||
if (unprotect((void*)trampolineFunctionPage, PAGE_SIZE) != 0)
|
||||
{
|
||||
sql_print_error(
|
||||
"%s unable to unprotect trampoline function page: %p. Aborting.",
|
||||
log_prefix, (void *)trampolineFunctionPage);
|
||||
"%s unable to unprotect trampoline function page: %p. Aborting.",
|
||||
log_prefix, (void *)trampolineFunctionPage);
|
||||
return false;
|
||||
}
|
||||
bool disassemble_valid = false;
|
||||
while (ud_disassemble(&ud_obj))
|
||||
{
|
||||
if(ud_obj.mnemonic == UD_Iinvalid)
|
||||
{
|
||||
sql_print_error(
|
||||
"%s unable to disassemble at address: %p. Aborting.",
|
||||
log_prefix, (void *)(InstrSize + targetFunction));
|
||||
break;
|
||||
}
|
||||
//make sure there isn't a jmp/call (or similar operand) as these use
|
||||
//relative addressing and if we copy as is we will mess up the jmp/call target
|
||||
if(ud_obj.mnemonic == UD_Ijmp || ud_obj.mnemonic == UD_Icall ||
|
||||
ud_obj.operand[0].type == UD_OP_JIMM)
|
||||
{
|
||||
sql_print_error(
|
||||
"%s unable to disassemble at address: 0x%p. Found relative addressing for instruction: [%s]. Aborting.",
|
||||
log_prefix, (void *)(InstrSize + targetFunction), ud_insn_asm(&ud_obj));
|
||||
break;
|
||||
}
|
||||
|
||||
BYTE *pCurInstr = (BYTE *) (InstrSize + (ULONG_PTR) targetFunction);
|
||||
memcpy((BYTE*)trampolineFunction + uCurrentSize,
|
||||
(void *) pCurInstr, ud_insn_len (&ud_obj));
|
||||
bool disassemble_valid = false;
|
||||
while (ud_disassemble(&ud_obj))
|
||||
{
|
||||
if (ud_obj.mnemonic == UD_Iinvalid)
|
||||
{
|
||||
sql_print_error(
|
||||
"%s unable to disassemble at address: %p. Aborting.",
|
||||
log_prefix, (void *)(InstrSize + targetFunction));
|
||||
break;
|
||||
}
|
||||
|
||||
uCurrentSize += ud_insn_len (&ud_obj);
|
||||
InstrSize += ud_insn_len (&ud_obj);
|
||||
if (InstrSize >= jump_size()) //we have enough space so break
|
||||
{
|
||||
disassemble_valid = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(protect((void*)trampolineFunctionPage, PAGE_SIZE)) //0 valid return
|
||||
// make sure there isn't a jmp/call (or similar operand) as these use
|
||||
// relative addressing and if we copy as is we will mess up the jmp/call target
|
||||
if (ud_obj.mnemonic == UD_Ijmp || ud_obj.mnemonic == UD_Icall ||
|
||||
ud_obj.operand[0].type == UD_OP_JIMM)
|
||||
{
|
||||
sql_print_error(
|
||||
"%s unable to disassemble at address: 0x%p. Found relative addressing for instruction: [%s]. Aborting.",
|
||||
log_prefix, (void *)(InstrSize + targetFunction), ud_insn_asm(&ud_obj));
|
||||
break;
|
||||
}
|
||||
|
||||
BYTE *pCurInstr = (BYTE *) (InstrSize + (ULONG_PTR) targetFunction);
|
||||
memcpy((BYTE*)trampolineFunction + uCurrentSize,
|
||||
(void *) pCurInstr, ud_insn_len (&ud_obj));
|
||||
|
||||
uCurrentSize += ud_insn_len (&ud_obj);
|
||||
InstrSize += ud_insn_len (&ud_obj);
|
||||
if (InstrSize >= jump_size()) // we have enough space so break
|
||||
{
|
||||
disassemble_valid = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (protect((void*)trampolineFunctionPage, PAGE_SIZE)) // 0 valid return
|
||||
{
|
||||
sql_print_error(
|
||||
"%s unable to protect page. Error. Page: %p.",
|
||||
log_prefix, (void *)trampolineFunctionPage);
|
||||
"%s unable to protect page. Error. Page: %p.",
|
||||
log_prefix, (void *)trampolineFunctionPage);
|
||||
return false;
|
||||
}
|
||||
if(!disassemble_valid) //something went wrong. log was written before so return false
|
||||
{
|
||||
return false;
|
||||
}
|
||||
WriteJump( (BYTE*)trampolineFunction + uCurrentSize, targetFunction + InstrSize);
|
||||
WriteJump((void *) targetFunction, newFunction);
|
||||
*trampolinesize = uCurrentSize;
|
||||
return true;
|
||||
|
||||
if (! disassemble_valid) // something went wrong. log was written before so return false
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
WriteJump((BYTE*)trampolineFunction + uCurrentSize, targetFunction + InstrSize);
|
||||
WriteJump((void *) targetFunction, newFunction);
|
||||
*trampolinesize = uCurrentSize;
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
|
@ -277,18 +281,18 @@ static bool HookFunction(ULONG_PTR targetFunction, ULONG_PTR newFunction, ULONG
|
|||
//
|
||||
|
||||
|
||||
static void UnhookFunction(ULONG_PTR Function,ULONG_PTR trampolineFunction , unsigned int trampolinesize)
|
||||
static void UnhookFunction(ULONG_PTR Function, ULONG_PTR trampolineFunction, unsigned int trampolinesize)
|
||||
{
|
||||
DATATYPE_ADDRESS FunctionPage = get_page_address((void*)Function);
|
||||
if(unprotect((void*)FunctionPage, PAGE_SIZE) != 0)
|
||||
DATATYPE_ADDRESS FunctionPage = get_page_address((void*)Function);
|
||||
if (unprotect((void*)FunctionPage, PAGE_SIZE) != 0)
|
||||
{
|
||||
sql_print_error(
|
||||
"%s Unhook not able to unprotect function page: %p. Aborting.",
|
||||
log_prefix, (void * )FunctionPage);
|
||||
"%s Unhook not able to unprotect function page: %p. Aborting.",
|
||||
log_prefix, (void * )FunctionPage);
|
||||
return;
|
||||
}
|
||||
memcpy((void *) Function, (void*)trampolineFunction,trampolinesize);
|
||||
protect((void*)FunctionPage, PAGE_SIZE);
|
||||
memcpy((void *) Function, (void*)trampolineFunction,trampolinesize);
|
||||
protect((void*)FunctionPage, PAGE_SIZE);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -303,24 +307,24 @@ static void UnhookFunction(ULONG_PTR Function,ULONG_PTR trampolineFunction , uns
|
|||
* @param newFunction the new function to be called instead of the targetFunction
|
||||
* @param trampolineFunction a function which will contain a jump back to the targetFunction. Function need to have
|
||||
* enough space of TRAMPOLINE_COPY_LENGTH + MIN_REQUIRED_FOR_DETOUR. Recommended to use a static function
|
||||
* which contains a bunch of nops.
|
||||
* which contains a bunch of nops.
|
||||
* @param info_print if true will print info as progressing
|
||||
* @Return 0 on success otherwise failure
|
||||
* @See MS Detours paper: http://research.microsoft.com/pubs/68568/huntusenixnt99.pdf for some background info.
|
||||
* @See MS Detours paper: http:// research.microsoft.com/pubs/68568/huntusenixnt99.pdf for some background info.
|
||||
*/
|
||||
int hot_patch_function (void* targetFunction, void* newFunction, void * trampolineFunction, unsigned int *trampolinesize, bool info_print)
|
||||
int hot_patch_function(void *targetFunction, void *newFunction, void *trampolineFunction, unsigned int *trampolinesize, bool info_print)
|
||||
{
|
||||
DATATYPE_ADDRESS trampolinePage = get_page_address(trampolineFunction);
|
||||
cond_info_print(info_print, "%s hot patching function: %p, trampolineFunction: %p trampolinePage: %p",log_prefix, (void *)targetFunction, (void *)trampolineFunction, (void *)trampolinePage);
|
||||
if (HookFunction((ULONG_PTR) targetFunction, (ULONG_PTR) newFunction,
|
||||
(ULONG_PTR) trampolineFunction, trampolinesize))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if (HookFunction((ULONG_PTR) targetFunction, (ULONG_PTR) newFunction,
|
||||
(ULONG_PTR) trampolineFunction, trampolinesize))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -332,15 +336,15 @@ int hot_patch_function (void* targetFunction, void* newFunction, void * trampoli
|
|||
* @param trampolineFunction a function which contains a jump back to the targetFunction.
|
||||
* @param log_file if not null will log about progress of installing the plugin
|
||||
*/
|
||||
void remove_hot_patch_function (void* targetFunction, void * trampolineFunction, unsigned int trampolinesize, bool info_print)
|
||||
void remove_hot_patch_function(void *targetFunction, void *trampolineFunction, unsigned int trampolinesize, bool info_print)
|
||||
{
|
||||
if(trampolinesize == 0)
|
||||
if (trampolinesize == 0)
|
||||
{
|
||||
//nothing todo. As hot patch was not set.
|
||||
// nothing todo. As hot patch was not set.
|
||||
return;
|
||||
}
|
||||
DATATYPE_ADDRESS targetPage = get_page_address(targetFunction);
|
||||
cond_info_print(info_print, "%s removing hot patching function: %p targetPage: %p trampolineFunction: %p",log_prefix, (void *)targetFunction, (void *)targetPage, (void *)trampolineFunction);
|
||||
UnhookFunction ((ULONG_PTR) targetFunction, (ULONG_PTR)trampolineFunction,trampolinesize);
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -1,297 +1,297 @@
|
|||
/*
|
||||
* This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
|
||||
* MD5 Message-Digest Algorithm (RFC 1321).
|
||||
*
|
||||
* Homepage:
|
||||
* http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
|
||||
*
|
||||
* Author:
|
||||
* Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
|
||||
*
|
||||
* This software was written by Alexander Peslyak in 2001. No copyright is
|
||||
* claimed, and the software is hereby placed in the public domain.
|
||||
* In case this attempt to disclaim copyright and place the software in the
|
||||
* public domain is deemed null and void, then the software is
|
||||
* Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
|
||||
* general public under the following terms:
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted.
|
||||
*
|
||||
* There's ABSOLUTELY NO WARRANTY, express or implied.
|
||||
*
|
||||
* (This is a heavily cut-down "BSD license".)
|
||||
*
|
||||
* This differs from Colin Plumb's older public domain implementation in that
|
||||
* no exactly 32-bit integer data type is required (any 32-bit or wider
|
||||
* unsigned integer data type will do), there's no compile-time endianness
|
||||
* configuration, and the function prototypes match OpenSSL's. No code from
|
||||
* Colin Plumb's implementation has been reused; this comment merely compares
|
||||
* the properties of the two independent implementations.
|
||||
*
|
||||
* The primary goals of this implementation are portability and ease of use.
|
||||
* It is meant to be fast, but not as fast as possible. Some known
|
||||
* optimizations are not included to reduce source code size and avoid
|
||||
* compile-time configuration.
|
||||
*
|
||||
* Modified: to have naming convention as used in MySQL 5.1 and 5.5
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "md5.h"
|
||||
|
||||
#if MYSQL_VERSION_ID >= 50600
|
||||
|
||||
/*
|
||||
* The basic MD5 functions.
|
||||
*
|
||||
* F and G are optimized compared to their RFC 1321 definitions for
|
||||
* architectures that lack an AND-NOT instruction, just like in Colin Plumb's
|
||||
* implementation.
|
||||
*/
|
||||
#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
|
||||
#define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
|
||||
#define H(x, y, z) ((x) ^ (y) ^ (z))
|
||||
#define I(x, y, z) ((y) ^ ((x) | ~(z)))
|
||||
|
||||
/*
|
||||
* The MD5 transformation for all four rounds.
|
||||
*/
|
||||
#define STEP(f, a, b, c, d, x, t, s) \
|
||||
(a) += f((b), (c), (d)) + (x) + (t); \
|
||||
(a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
|
||||
(a) += (b);
|
||||
|
||||
/*
|
||||
* SET reads 4 input bytes in little-endian byte order and stores them
|
||||
* in a properly aligned word in host byte order.
|
||||
*
|
||||
* The check for little-endian architectures that tolerate unaligned
|
||||
* memory accesses is just an optimization. Nothing will break if it
|
||||
* doesn't work.
|
||||
*/
|
||||
#if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
|
||||
#define SET(n) \
|
||||
(*(MD5_u32plus *)&ptr[(n) * 4])
|
||||
#define GET(n) \
|
||||
SET(n)
|
||||
#else
|
||||
#define SET(n) \
|
||||
(ctx->block[(n)] = \
|
||||
(MD5_u32plus)ptr[(n) * 4] | \
|
||||
((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \
|
||||
((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \
|
||||
((MD5_u32plus)ptr[(n) * 4 + 3] << 24))
|
||||
#define GET(n) \
|
||||
(ctx->block[(n)])
|
||||
#endif
|
||||
|
||||
/*
|
||||
* This processes one or more 64-byte data blocks, but does NOT update
|
||||
* the bit counters. There are no alignment requirements.
|
||||
*/
|
||||
static void *body(MD5_CTX *ctx, void *data, unsigned long size)
|
||||
{
|
||||
unsigned char *ptr;
|
||||
MD5_u32plus a, b, c, d;
|
||||
MD5_u32plus saved_a, saved_b, saved_c, saved_d;
|
||||
|
||||
ptr = (unsigned char *)data;
|
||||
|
||||
a = ctx->a;
|
||||
b = ctx->b;
|
||||
c = ctx->c;
|
||||
d = ctx->d;
|
||||
|
||||
do {
|
||||
saved_a = a;
|
||||
saved_b = b;
|
||||
saved_c = c;
|
||||
saved_d = d;
|
||||
|
||||
/* Round 1 */
|
||||
STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
|
||||
STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
|
||||
STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
|
||||
STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
|
||||
STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
|
||||
STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
|
||||
STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
|
||||
STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
|
||||
STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
|
||||
STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
|
||||
STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
|
||||
STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
|
||||
STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
|
||||
STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
|
||||
STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
|
||||
STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
|
||||
|
||||
/* Round 2 */
|
||||
STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
|
||||
STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
|
||||
STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
|
||||
STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
|
||||
STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
|
||||
STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
|
||||
STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
|
||||
STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
|
||||
STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
|
||||
STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
|
||||
STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
|
||||
STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
|
||||
STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
|
||||
STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
|
||||
STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
|
||||
STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
|
||||
|
||||
/* Round 3 */
|
||||
STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
|
||||
STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
|
||||
STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
|
||||
STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23)
|
||||
STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
|
||||
STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11)
|
||||
STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
|
||||
STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23)
|
||||
STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
|
||||
STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11)
|
||||
STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
|
||||
STEP(H, b, c, d, a, GET(6), 0x04881d05, 23)
|
||||
STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
|
||||
STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11)
|
||||
STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
|
||||
STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
|
||||
|
||||
/* Round 4 */
|
||||
STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
|
||||
STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
|
||||
STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
|
||||
STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
|
||||
STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
|
||||
STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
|
||||
STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
|
||||
STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
|
||||
STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
|
||||
STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
|
||||
STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
|
||||
STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
|
||||
STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
|
||||
STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
|
||||
STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
|
||||
STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
|
||||
|
||||
a += saved_a;
|
||||
b += saved_b;
|
||||
c += saved_c;
|
||||
d += saved_d;
|
||||
|
||||
ptr += 64;
|
||||
} while (size -= 64);
|
||||
|
||||
ctx->a = a;
|
||||
ctx->b = b;
|
||||
ctx->c = c;
|
||||
ctx->d = d;
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void MD5_Init(MD5_CTX *ctx)
|
||||
{
|
||||
ctx->a = 0x67452301;
|
||||
ctx->b = 0xefcdab89;
|
||||
ctx->c = 0x98badcfe;
|
||||
ctx->d = 0x10325476;
|
||||
|
||||
ctx->lo = 0;
|
||||
ctx->hi = 0;
|
||||
}
|
||||
|
||||
void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size)
|
||||
{
|
||||
MD5_u32plus saved_lo;
|
||||
unsigned long used, free;
|
||||
|
||||
saved_lo = ctx->lo;
|
||||
if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
|
||||
ctx->hi++;
|
||||
ctx->hi += size >> 29;
|
||||
|
||||
used = saved_lo & 0x3f;
|
||||
|
||||
if (used) {
|
||||
free = 64 - used;
|
||||
|
||||
if (size < free) {
|
||||
memcpy(&ctx->buffer[used], data, size);
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(&ctx->buffer[used], data, free);
|
||||
data = (unsigned char *)data + free;
|
||||
size -= free;
|
||||
body(ctx, ctx->buffer, 64);
|
||||
}
|
||||
|
||||
if (size >= 64) {
|
||||
data = body(ctx, data, size & ~(unsigned long)0x3f);
|
||||
size &= 0x3f;
|
||||
}
|
||||
|
||||
memcpy(ctx->buffer, data, size);
|
||||
}
|
||||
|
||||
void MD5_Final(unsigned char *result, MD5_CTX *ctx)
|
||||
{
|
||||
unsigned long used, free;
|
||||
|
||||
used = ctx->lo & 0x3f;
|
||||
|
||||
ctx->buffer[used++] = 0x80;
|
||||
|
||||
free = 64 - used;
|
||||
|
||||
if (free < 8) {
|
||||
memset(&ctx->buffer[used], 0, free);
|
||||
body(ctx, ctx->buffer, 64);
|
||||
used = 0;
|
||||
free = 64;
|
||||
}
|
||||
|
||||
memset(&ctx->buffer[used], 0, free - 8);
|
||||
|
||||
ctx->lo <<= 3;
|
||||
ctx->buffer[56] = ctx->lo;
|
||||
ctx->buffer[57] = ctx->lo >> 8;
|
||||
ctx->buffer[58] = ctx->lo >> 16;
|
||||
ctx->buffer[59] = ctx->lo >> 24;
|
||||
ctx->buffer[60] = ctx->hi;
|
||||
ctx->buffer[61] = ctx->hi >> 8;
|
||||
ctx->buffer[62] = ctx->hi >> 16;
|
||||
ctx->buffer[63] = ctx->hi >> 24;
|
||||
|
||||
body(ctx, ctx->buffer, 64);
|
||||
|
||||
result[0] = ctx->a;
|
||||
result[1] = ctx->a >> 8;
|
||||
result[2] = ctx->a >> 16;
|
||||
result[3] = ctx->a >> 24;
|
||||
result[4] = ctx->b;
|
||||
result[5] = ctx->b >> 8;
|
||||
result[6] = ctx->b >> 16;
|
||||
result[7] = ctx->b >> 24;
|
||||
result[8] = ctx->c;
|
||||
result[9] = ctx->c >> 8;
|
||||
result[10] = ctx->c >> 16;
|
||||
result[11] = ctx->c >> 24;
|
||||
result[12] = ctx->d;
|
||||
result[13] = ctx->d >> 8;
|
||||
result[14] = ctx->d >> 16;
|
||||
result[15] = ctx->d >> 24;
|
||||
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
}
|
||||
|
||||
#endif
|
||||
/*
|
||||
* This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
|
||||
* MD5 Message-Digest Algorithm (RFC 1321).
|
||||
*
|
||||
* Homepage:
|
||||
* http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
|
||||
*
|
||||
* Author:
|
||||
* Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
|
||||
*
|
||||
* This software was written by Alexander Peslyak in 2001. No copyright is
|
||||
* claimed, and the software is hereby placed in the public domain.
|
||||
* In case this attempt to disclaim copyright and place the software in the
|
||||
* public domain is deemed null and void, then the software is
|
||||
* Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
|
||||
* general public under the following terms:
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted.
|
||||
*
|
||||
* There's ABSOLUTELY NO WARRANTY, express or implied.
|
||||
*
|
||||
* (This is a heavily cut-down "BSD license".)
|
||||
*
|
||||
* This differs from Colin Plumb's older public domain implementation in that
|
||||
* no exactly 32-bit integer data type is required (any 32-bit or wider
|
||||
* unsigned integer data type will do), there's no compile-time endianness
|
||||
* configuration, and the function prototypes match OpenSSL's. No code from
|
||||
* Colin Plumb's implementation has been reused; this comment merely compares
|
||||
* the properties of the two independent implementations.
|
||||
*
|
||||
* The primary goals of this implementation are portability and ease of use.
|
||||
* It is meant to be fast, but not as fast as possible. Some known
|
||||
* optimizations are not included to reduce source code size and avoid
|
||||
* compile-time configuration.
|
||||
*
|
||||
* Modified: to have naming convention as used in MySQL 5.1 and 5.5
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "md5.h"
|
||||
|
||||
#if MYSQL_VERSION_ID >= 50600
|
||||
|
||||
/*
|
||||
* The basic MD5 functions.
|
||||
*
|
||||
* F and G are optimized compared to their RFC 1321 definitions for
|
||||
* architectures that lack an AND-NOT instruction, just like in Colin Plumb's
|
||||
* implementation.
|
||||
*/
|
||||
#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
|
||||
#define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
|
||||
#define H(x, y, z) ((x) ^ (y) ^ (z))
|
||||
#define I(x, y, z) ((y) ^ ((x) | ~(z)))
|
||||
|
||||
/*
|
||||
* The MD5 transformation for all four rounds.
|
||||
*/
|
||||
#define STEP(f, a, b, c, d, x, t, s) \
|
||||
(a) += f((b), (c), (d)) + (x) + (t); \
|
||||
(a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
|
||||
(a) += (b);
|
||||
|
||||
/*
|
||||
* SET reads 4 input bytes in little-endian byte order and stores them
|
||||
* in a properly aligned word in host byte order.
|
||||
*
|
||||
* The check for little-endian architectures that tolerate unaligned
|
||||
* memory accesses is just an optimization. Nothing will break if it
|
||||
* doesn't work.
|
||||
*/
|
||||
#if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
|
||||
#define SET(n) \
|
||||
(*(MD5_u32plus *)&ptr[(n) * 4])
|
||||
#define GET(n) \
|
||||
SET(n)
|
||||
#else
|
||||
#define SET(n) \
|
||||
(ctx->block[(n)] = \
|
||||
(MD5_u32plus)ptr[(n) * 4] | \
|
||||
((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \
|
||||
((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \
|
||||
((MD5_u32plus)ptr[(n) * 4 + 3] << 24))
|
||||
#define GET(n) \
|
||||
(ctx->block[(n)])
|
||||
#endif
|
||||
|
||||
/*
|
||||
* This processes one or more 64-byte data blocks, but does NOT update
|
||||
* the bit counters. There are no alignment requirements.
|
||||
*/
|
||||
static void *body(MD5_CTX *ctx, void *data, unsigned long size)
|
||||
{
|
||||
unsigned char *ptr;
|
||||
MD5_u32plus a, b, c, d;
|
||||
MD5_u32plus saved_a, saved_b, saved_c, saved_d;
|
||||
|
||||
ptr = (unsigned char *)data;
|
||||
|
||||
a = ctx->a;
|
||||
b = ctx->b;
|
||||
c = ctx->c;
|
||||
d = ctx->d;
|
||||
|
||||
do {
|
||||
saved_a = a;
|
||||
saved_b = b;
|
||||
saved_c = c;
|
||||
saved_d = d;
|
||||
|
||||
/* Round 1 */
|
||||
STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
|
||||
STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
|
||||
STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
|
||||
STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
|
||||
STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
|
||||
STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
|
||||
STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
|
||||
STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
|
||||
STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
|
||||
STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
|
||||
STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
|
||||
STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
|
||||
STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
|
||||
STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
|
||||
STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
|
||||
STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
|
||||
|
||||
/* Round 2 */
|
||||
STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
|
||||
STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
|
||||
STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
|
||||
STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
|
||||
STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
|
||||
STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
|
||||
STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
|
||||
STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
|
||||
STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
|
||||
STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
|
||||
STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
|
||||
STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
|
||||
STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
|
||||
STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
|
||||
STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
|
||||
STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
|
||||
|
||||
/* Round 3 */
|
||||
STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
|
||||
STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
|
||||
STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
|
||||
STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23)
|
||||
STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
|
||||
STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11)
|
||||
STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
|
||||
STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23)
|
||||
STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
|
||||
STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11)
|
||||
STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
|
||||
STEP(H, b, c, d, a, GET(6), 0x04881d05, 23)
|
||||
STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
|
||||
STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11)
|
||||
STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
|
||||
STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
|
||||
|
||||
/* Round 4 */
|
||||
STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
|
||||
STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
|
||||
STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
|
||||
STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
|
||||
STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
|
||||
STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
|
||||
STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
|
||||
STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
|
||||
STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
|
||||
STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
|
||||
STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
|
||||
STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
|
||||
STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
|
||||
STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
|
||||
STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
|
||||
STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
|
||||
|
||||
a += saved_a;
|
||||
b += saved_b;
|
||||
c += saved_c;
|
||||
d += saved_d;
|
||||
|
||||
ptr += 64;
|
||||
} while (size -= 64);
|
||||
|
||||
ctx->a = a;
|
||||
ctx->b = b;
|
||||
ctx->c = c;
|
||||
ctx->d = d;
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void MD5_Init(MD5_CTX *ctx)
|
||||
{
|
||||
ctx->a = 0x67452301;
|
||||
ctx->b = 0xefcdab89;
|
||||
ctx->c = 0x98badcfe;
|
||||
ctx->d = 0x10325476;
|
||||
|
||||
ctx->lo = 0;
|
||||
ctx->hi = 0;
|
||||
}
|
||||
|
||||
void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size)
|
||||
{
|
||||
MD5_u32plus saved_lo;
|
||||
unsigned long used, free;
|
||||
|
||||
saved_lo = ctx->lo;
|
||||
if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
|
||||
ctx->hi++;
|
||||
ctx->hi += size >> 29;
|
||||
|
||||
used = saved_lo & 0x3f;
|
||||
|
||||
if (used) {
|
||||
free = 64 - used;
|
||||
|
||||
if (size < free) {
|
||||
memcpy(&ctx->buffer[used], data, size);
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(&ctx->buffer[used], data, free);
|
||||
data = (unsigned char *)data + free;
|
||||
size -= free;
|
||||
body(ctx, ctx->buffer, 64);
|
||||
}
|
||||
|
||||
if (size >= 64) {
|
||||
data = body(ctx, data, size & ~(unsigned long)0x3f);
|
||||
size &= 0x3f;
|
||||
}
|
||||
|
||||
memcpy(ctx->buffer, data, size);
|
||||
}
|
||||
|
||||
void MD5_Final(unsigned char *result, MD5_CTX *ctx)
|
||||
{
|
||||
unsigned long used, free;
|
||||
|
||||
used = ctx->lo & 0x3f;
|
||||
|
||||
ctx->buffer[used++] = 0x80;
|
||||
|
||||
free = 64 - used;
|
||||
|
||||
if (free < 8) {
|
||||
memset(&ctx->buffer[used], 0, free);
|
||||
body(ctx, ctx->buffer, 64);
|
||||
used = 0;
|
||||
free = 64;
|
||||
}
|
||||
|
||||
memset(&ctx->buffer[used], 0, free - 8);
|
||||
|
||||
ctx->lo <<= 3;
|
||||
ctx->buffer[56] = ctx->lo;
|
||||
ctx->buffer[57] = ctx->lo >> 8;
|
||||
ctx->buffer[58] = ctx->lo >> 16;
|
||||
ctx->buffer[59] = ctx->lo >> 24;
|
||||
ctx->buffer[60] = ctx->hi;
|
||||
ctx->buffer[61] = ctx->hi >> 8;
|
||||
ctx->buffer[62] = ctx->hi >> 16;
|
||||
ctx->buffer[63] = ctx->hi >> 24;
|
||||
|
||||
body(ctx, ctx->buffer, 64);
|
||||
|
||||
result[0] = ctx->a;
|
||||
result[1] = ctx->a >> 8;
|
||||
result[2] = ctx->a >> 16;
|
||||
result[3] = ctx->a >> 24;
|
||||
result[4] = ctx->b;
|
||||
result[5] = ctx->b >> 8;
|
||||
result[6] = ctx->b >> 16;
|
||||
result[7] = ctx->b >> 24;
|
||||
result[8] = ctx->c;
|
||||
result[9] = ctx->c >> 8;
|
||||
result[10] = ctx->c >> 16;
|
||||
result[11] = ctx->c >> 24;
|
||||
result[12] = ctx->d;
|
||||
result[13] = ctx->d >> 8;
|
||||
result[14] = ctx->d >> 16;
|
||||
result[15] = ctx->d >> 24;
|
||||
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue