2015-03-02 10:55:48 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
|
|
|
|
Copyright (C) 2013, 2015, Google Inc. All Rights Reserved.
|
MDEV-25791: Remove UNIV_INTERN
Back in 2006 or 2007, when MySQL AB and Innobase Oy existed as
separately controlled entities (Innobase had been acquired by
Oracle Corporation), MySQL 5.1 introduced a storage engine plugin
interface and Oracle made use of it by distributing a separate
InnoDB Plugin, which would contain some more bug fixes and
improvements, compared to the version of InnoDB that was statically
linked with the mysqld server that was distributed by MySQL AB.
The built-in InnoDB would export global symbols, which would clash
with the symbols of the dynamic InnoDB Plugin (which was supposed
to override the built-in one when present).
The solution to this problem was to declare all global symbols with
UNIV_INTERN, so that they would get the GCC function attribute that
specifies hidden visibility.
Later, in MariaDB Server, something based on Percona XtraDB (a fork of
MySQL InnoDB) became the statically linked implementation, and something
closer to MySQL InnoDB was available as a dynamic plugin. Starting with
version 10.2, MariaDB Server includes only one InnoDB implementation,
and hence any reason to have the UNIV_INTERN definition was lost.
btr_get_size_and_reserved(): Move to the same compilation unit with
the only caller.
innodb_set_buf_pool_size(): Remove. Modify innobase_buffer_pool_size
directly.
fil_crypt_calculate_checksum(): Merge to the only caller.
ha_innobase::innobase_reset_autoinc(): Merge to the only caller.
thd_query_start_micro(): Remove. Call thd_start_utime() directly.
2021-05-27 10:13:14 +03:00
|
|
|
Copyright (C) 2014, 2021, MariaDB Corporation.
|
2015-03-02 10:55:48 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; version 2 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program; if not, write to the Free Software Foundation, Inc.,
|
2019-05-11 22:19:05 +03:00
|
|
|
51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA
|
2015-03-02 10:55:48 +02:00
|
|
|
|
|
|
|
*****************************************************************************/
|
2014-12-22 16:53:17 +02:00
|
|
|
/**************************************************//**
|
|
|
|
@file log0crypt.cc
|
|
|
|
Innodb log encrypt/decrypt
|
|
|
|
|
2015-03-02 10:55:48 +02:00
|
|
|
Created 11/25/2013 Minli Zhu Google
|
|
|
|
Modified Jan Lindström jan.lindstrom@mariadb.com
|
2017-02-10 12:11:42 +02:00
|
|
|
MDEV-11782: Rewritten for MariaDB 10.2 by Marko Mäkelä, MariaDB Corporation.
|
2014-12-22 16:53:17 +02:00
|
|
|
*******************************************************/
|
2017-06-18 06:42:16 +03:00
|
|
|
#include <my_global.h>
|
2014-12-22 16:53:17 +02:00
|
|
|
#include "log0crypt.h"
|
2017-04-18 16:37:57 +00:00
|
|
|
#include <mysql/service_my_crypt.h>
|
2020-03-20 18:47:35 +03:00
|
|
|
#include "assume_aligned.h"
|
2015-03-31 19:00:51 +02:00
|
|
|
|
2017-02-10 12:11:42 +02:00
|
|
|
#include "log0crypt.h"
|
2014-12-22 16:53:17 +02:00
|
|
|
#include "log0recv.h" // for recv_sys
|
|
|
|
|
2017-02-10 12:11:42 +02:00
|
|
|
/** innodb_encrypt_log: whether to encrypt the redo log */
|
|
|
|
my_bool srv_encrypt_log;
|
2014-12-22 16:53:17 +02:00
|
|
|
|
2017-02-10 12:11:42 +02:00
|
|
|
/** Redo log encryption key ID */
|
2015-04-09 00:37:47 +02:00
|
|
|
#define LOG_DEFAULT_ENCRYPTION_KEY 1
|
|
|
|
|
2015-05-07 19:13:22 +03:00
|
|
|
struct crypt_info_t {
|
2017-02-10 12:11:42 +02:00
|
|
|
ulint checkpoint_no; /*!< checkpoint no; 32 bits */
|
2015-05-07 19:13:22 +03:00
|
|
|
uint key_version; /*!< mysqld key version */
|
2017-02-10 12:11:42 +02:00
|
|
|
/** random string for encrypting the key */
|
2020-03-07 12:01:20 +02:00
|
|
|
alignas(8) byte crypt_msg[MY_AES_BLOCK_SIZE];
|
2017-02-10 12:11:42 +02:00
|
|
|
/** the secret key */
|
2020-03-07 12:01:20 +02:00
|
|
|
alignas(8) byte crypt_key[MY_AES_BLOCK_SIZE];
|
2017-02-10 12:11:42 +02:00
|
|
|
/** a random string for the per-block initialization vector */
|
2020-03-07 12:01:20 +02:00
|
|
|
alignas(4) byte crypt_nonce[4];
|
2015-05-07 19:13:22 +03:00
|
|
|
};
|
2014-12-22 16:53:17 +02:00
|
|
|
|
2017-02-10 12:11:42 +02:00
|
|
|
/** The crypt info */
|
|
|
|
static crypt_info_t info;
|
|
|
|
|
2019-06-27 16:23:03 +05:30
|
|
|
/** Initialization vector used for temporary files/tablespace */
|
|
|
|
static byte tmp_iv[MY_AES_BLOCK_SIZE];
|
|
|
|
|
2017-02-10 12:11:42 +02:00
|
|
|
/** Crypt info when upgrading from 10.1 */
|
2018-05-28 14:20:44 +03:00
|
|
|
static crypt_info_t infos[5 * 2];
|
|
|
|
/** First unused slot in infos[] */
|
|
|
|
static size_t infos_used;
|
2014-12-22 16:53:17 +02:00
|
|
|
|
|
|
|
/*********************************************************************//**
|
|
|
|
Get a log block's start lsn.
|
|
|
|
@return a log block's start lsn */
|
|
|
|
static inline
|
|
|
|
lsn_t
|
|
|
|
log_block_get_start_lsn(
|
|
|
|
/*====================*/
|
|
|
|
lsn_t lsn, /*!< in: checkpoint lsn */
|
|
|
|
ulint log_block_no) /*!< in: log block number */
|
|
|
|
{
|
|
|
|
lsn_t start_lsn =
|
|
|
|
(lsn & (lsn_t)0xffffffff00000000ULL) |
|
|
|
|
(((log_block_no - 1) & (lsn_t)0x3fffffff) << 9);
|
|
|
|
return start_lsn;
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:04:37 +03:00
|
|
|
/** Generate crypt key from crypt msg.
|
|
|
|
@param[in,out] info encryption key
|
|
|
|
@param[in] upgrade whether to use the key in MariaDB 10.1 format
|
|
|
|
@return whether the operation was successful */
|
|
|
|
static bool init_crypt_key(crypt_info_t* info, bool upgrade = false)
|
|
|
|
{
|
|
|
|
byte mysqld_key[MY_AES_MAX_KEY_LENGTH];
|
|
|
|
uint keylen = sizeof mysqld_key;
|
|
|
|
|
2020-03-07 12:01:20 +02:00
|
|
|
compile_time_assert(16 == sizeof info->crypt_key);
|
2019-05-25 22:59:33 +02:00
|
|
|
compile_time_assert(16 == MY_AES_BLOCK_SIZE);
|
2018-08-13 16:04:37 +03:00
|
|
|
|
|
|
|
if (uint rc = encryption_key_get(LOG_DEFAULT_ENCRYPTION_KEY,
|
|
|
|
info->key_version, mysqld_key,
|
|
|
|
&keylen)) {
|
|
|
|
ib::error()
|
|
|
|
<< "Obtaining redo log encryption key version "
|
|
|
|
<< info->key_version << " failed (" << rc
|
|
|
|
<< "). Maybe the key or the required encryption "
|
|
|
|
"key management plugin was not found.";
|
2021-06-23 08:05:27 +03:00
|
|
|
info->key_version = ENCRYPTION_KEY_VERSION_INVALID;
|
2018-08-13 16:04:37 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (upgrade) {
|
|
|
|
while (keylen < sizeof mysqld_key) {
|
|
|
|
mysqld_key[keylen++] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint dst_len;
|
|
|
|
int err= my_aes_crypt(MY_AES_ECB,
|
|
|
|
ENCRYPTION_FLAG_NOPAD | ENCRYPTION_FLAG_ENCRYPT,
|
2020-03-07 12:01:20 +02:00
|
|
|
info->crypt_msg, MY_AES_BLOCK_SIZE,
|
|
|
|
info->crypt_key, &dst_len,
|
2018-08-13 16:04:37 +03:00
|
|
|
mysqld_key, keylen, NULL, 0);
|
|
|
|
|
|
|
|
if (err != MY_AES_OK || dst_len != MY_AES_BLOCK_SIZE) {
|
|
|
|
ib::error() << "Getting redo log crypto key failed: err = "
|
|
|
|
<< err << ", len = " << dst_len;
|
2021-06-23 08:05:27 +03:00
|
|
|
info->key_version = ENCRYPTION_KEY_VERSION_INVALID;
|
2018-08-13 16:04:37 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-10 12:11:42 +02:00
|
|
|
/** Encrypt or decrypt log blocks.
|
|
|
|
@param[in,out] buf log blocks to encrypt or decrypt
|
2017-09-11 16:47:23 +03:00
|
|
|
@param[in] lsn log sequence number of the start of the buffer
|
2017-02-10 12:11:42 +02:00
|
|
|
@param[in] size size of the buffer, in bytes
|
2018-08-13 16:04:37 +03:00
|
|
|
@param[in] op whether to decrypt, encrypt, or rotate key and encrypt
|
|
|
|
@return whether the operation succeeded (encrypt always does) */
|
|
|
|
bool log_crypt(byte* buf, lsn_t lsn, ulint size, log_crypt_t op)
|
2016-03-30 16:08:05 +03:00
|
|
|
{
|
2017-02-10 12:11:42 +02:00
|
|
|
ut_ad(size % OS_FILE_LOG_BLOCK_SIZE == 0);
|
2018-08-13 16:04:37 +03:00
|
|
|
ut_ad(ulint(buf) % OS_FILE_LOG_BLOCK_SIZE == 0);
|
2017-02-10 12:11:42 +02:00
|
|
|
ut_a(info.key_version);
|
2016-03-30 16:08:05 +03:00
|
|
|
|
2020-03-07 12:01:20 +02:00
|
|
|
alignas(8) byte aes_ctr_iv[MY_AES_BLOCK_SIZE];
|
2015-04-09 19:06:11 +02:00
|
|
|
|
2017-02-10 12:11:42 +02:00
|
|
|
#define LOG_CRYPT_HDR_SIZE 4
|
2017-09-11 16:47:23 +03:00
|
|
|
lsn &= ~lsn_t(OS_FILE_LOG_BLOCK_SIZE - 1);
|
2014-12-22 16:53:17 +02:00
|
|
|
|
2017-02-10 12:11:42 +02:00
|
|
|
for (const byte* const end = buf + size; buf != end;
|
2017-09-11 16:47:23 +03:00
|
|
|
buf += OS_FILE_LOG_BLOCK_SIZE, lsn += OS_FILE_LOG_BLOCK_SIZE) {
|
2020-03-07 12:01:20 +02:00
|
|
|
alignas(4) byte dst[OS_FILE_LOG_BLOCK_SIZE - LOG_CRYPT_HDR_SIZE
|
|
|
|
- LOG_BLOCK_CHECKSUM];
|
2017-02-10 12:11:42 +02:00
|
|
|
|
|
|
|
/* The log block number is not encrypted. */
|
2020-03-07 12:01:20 +02:00
|
|
|
memcpy_aligned<4>(dst, buf + LOG_BLOCK_HDR_NO, 4);
|
|
|
|
memcpy_aligned<4>(aes_ctr_iv, buf + LOG_BLOCK_HDR_NO, 4);
|
MDEV-21907: InnoDB: Enable -Wconversion on clang and GCC
The -Wconversion in GCC seems to be stricter than in clang.
GCC at least since version 4.4.7 issues truncation warnings for
assignments to bitfields, while clang 10 appears to only issue
warnings when the sizes in bytes rounded to the nearest integer
powers of 2 are different.
Before GCC 10.0.0, -Wconversion required more casts and would not
allow some operations, such as x<<=1 or x+=1 on a data type that
is narrower than int.
GCC 5 (but not GCC 4, GCC 6, or any later version) is complaining
about x|=y even when x and y are compatible types that are narrower
than int. Hence, we must rewrite some x|=y as
x=static_cast<byte>(x|y) or similar, or we must disable -Wconversion.
In GCC 6 and later, the warning for assigning wider to bitfields
that are narrower than 8, 16, or 32 bits can be suppressed by
applying a bitwise & with the exact bitmask of the bitfield.
For older GCC, we must disable -Wconversion for GCC 4 or 5 in such
cases.
The bitwise negation operator appears to promote short integers
to a wider type, and hence we must add explicit truncation casts
around them. Microsoft Visual C does not allow a static_cast to
truncate a constant, such as static_cast<byte>(1) truncating int.
Hence, we will use the constructor-style cast byte(~1) for such cases.
This has been tested at least with GCC 4.8.5, 5.4.0, 7.4.0, 9.2.1, 10.0.0,
clang 9.0.1, 10.0.0, and MSVC 14.22.27905 (Microsoft Visual Studio 2019)
on 64-bit and 32-bit targets (IA-32, AMD64, POWER 8, POWER 9, ARMv8).
2020-03-12 19:46:41 +02:00
|
|
|
*aes_ctr_iv &= byte(~(LOG_BLOCK_FLUSH_BIT_MASK >> 24));
|
2020-03-07 12:01:20 +02:00
|
|
|
static_assert(LOG_BLOCK_HDR_NO + 4 == LOG_CRYPT_HDR_SIZE,
|
|
|
|
"compatibility");
|
|
|
|
memcpy_aligned<4>(aes_ctr_iv + 4, info.crypt_nonce, 4);
|
|
|
|
mach_write_to_8(my_assume_aligned<8>(aes_ctr_iv + 8), lsn);
|
2017-09-11 16:47:23 +03:00
|
|
|
ut_ad(log_block_get_start_lsn(lsn,
|
|
|
|
log_block_get_hdr_no(buf))
|
|
|
|
== lsn);
|
2018-08-13 16:04:37 +03:00
|
|
|
byte* key_ver = &buf[OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_KEY
|
|
|
|
- LOG_BLOCK_CHECKSUM];
|
MDEV-21907: InnoDB: Enable -Wconversion on clang and GCC
The -Wconversion in GCC seems to be stricter than in clang.
GCC at least since version 4.4.7 issues truncation warnings for
assignments to bitfields, while clang 10 appears to only issue
warnings when the sizes in bytes rounded to the nearest integer
powers of 2 are different.
Before GCC 10.0.0, -Wconversion required more casts and would not
allow some operations, such as x<<=1 or x+=1 on a data type that
is narrower than int.
GCC 5 (but not GCC 4, GCC 6, or any later version) is complaining
about x|=y even when x and y are compatible types that are narrower
than int. Hence, we must rewrite some x|=y as
x=static_cast<byte>(x|y) or similar, or we must disable -Wconversion.
In GCC 6 and later, the warning for assigning wider to bitfields
that are narrower than 8, 16, or 32 bits can be suppressed by
applying a bitwise & with the exact bitmask of the bitfield.
For older GCC, we must disable -Wconversion for GCC 4 or 5 in such
cases.
The bitwise negation operator appears to promote short integers
to a wider type, and hence we must add explicit truncation casts
around them. Microsoft Visual C does not allow a static_cast to
truncate a constant, such as static_cast<byte>(1) truncating int.
Hence, we will use the constructor-style cast byte(~1) for such cases.
This has been tested at least with GCC 4.8.5, 5.4.0, 7.4.0, 9.2.1, 10.0.0,
clang 9.0.1, 10.0.0, and MSVC 14.22.27905 (Microsoft Visual Studio 2019)
on 64-bit and 32-bit targets (IA-32, AMD64, POWER 8, POWER 9, ARMv8).
2020-03-12 19:46:41 +02:00
|
|
|
const size_t dst_size
|
2020-03-16 11:10:01 +03:00
|
|
|
= log_sys.has_encryption_key_rotation()
|
2018-08-13 16:04:37 +03:00
|
|
|
? sizeof dst - LOG_BLOCK_KEY
|
|
|
|
: sizeof dst;
|
2020-03-16 11:10:01 +03:00
|
|
|
if (log_sys.has_encryption_key_rotation()) {
|
2018-08-13 16:04:37 +03:00
|
|
|
const uint key_version = info.key_version;
|
|
|
|
switch (op) {
|
|
|
|
case LOG_ENCRYPT_ROTATE_KEY:
|
|
|
|
info.key_version
|
|
|
|
= encryption_key_get_latest_version(
|
|
|
|
LOG_DEFAULT_ENCRYPTION_KEY);
|
|
|
|
if (key_version != info.key_version
|
|
|
|
&& !init_crypt_key(&info)) {
|
|
|
|
info.key_version = key_version;
|
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
case LOG_ENCRYPT:
|
|
|
|
mach_write_to_4(key_ver, info.key_version);
|
|
|
|
break;
|
|
|
|
case LOG_DECRYPT:
|
|
|
|
info.key_version = mach_read_from_4(key_ver);
|
|
|
|
if (key_version != info.key_version
|
|
|
|
&& !init_crypt_key(&info)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifndef DBUG_OFF
|
|
|
|
if (key_version != info.key_version) {
|
|
|
|
DBUG_PRINT("ib_log", ("key_version: %x -> %x",
|
|
|
|
key_version,
|
|
|
|
info.key_version));
|
|
|
|
}
|
|
|
|
#endif /* !DBUG_OFF */
|
|
|
|
}
|
|
|
|
|
|
|
|
ut_ad(LOG_CRYPT_HDR_SIZE + dst_size
|
|
|
|
== log_sys.trailer_offset());
|
2017-02-10 12:11:42 +02:00
|
|
|
|
2018-08-13 16:04:37 +03:00
|
|
|
uint dst_len;
|
2017-02-10 12:11:42 +02:00
|
|
|
int rc = encryption_crypt(
|
MDEV-21907: InnoDB: Enable -Wconversion on clang and GCC
The -Wconversion in GCC seems to be stricter than in clang.
GCC at least since version 4.4.7 issues truncation warnings for
assignments to bitfields, while clang 10 appears to only issue
warnings when the sizes in bytes rounded to the nearest integer
powers of 2 are different.
Before GCC 10.0.0, -Wconversion required more casts and would not
allow some operations, such as x<<=1 or x+=1 on a data type that
is narrower than int.
GCC 5 (but not GCC 4, GCC 6, or any later version) is complaining
about x|=y even when x and y are compatible types that are narrower
than int. Hence, we must rewrite some x|=y as
x=static_cast<byte>(x|y) or similar, or we must disable -Wconversion.
In GCC 6 and later, the warning for assigning wider to bitfields
that are narrower than 8, 16, or 32 bits can be suppressed by
applying a bitwise & with the exact bitmask of the bitfield.
For older GCC, we must disable -Wconversion for GCC 4 or 5 in such
cases.
The bitwise negation operator appears to promote short integers
to a wider type, and hence we must add explicit truncation casts
around them. Microsoft Visual C does not allow a static_cast to
truncate a constant, such as static_cast<byte>(1) truncating int.
Hence, we will use the constructor-style cast byte(~1) for such cases.
This has been tested at least with GCC 4.8.5, 5.4.0, 7.4.0, 9.2.1, 10.0.0,
clang 9.0.1, 10.0.0, and MSVC 14.22.27905 (Microsoft Visual Studio 2019)
on 64-bit and 32-bit targets (IA-32, AMD64, POWER 8, POWER 9, ARMv8).
2020-03-12 19:46:41 +02:00
|
|
|
buf + LOG_CRYPT_HDR_SIZE, static_cast<uint>(dst_size),
|
2017-05-18 14:29:39 +03:00
|
|
|
reinterpret_cast<byte*>(dst), &dst_len,
|
2020-03-07 12:01:20 +02:00
|
|
|
const_cast<byte*>(info.crypt_key),
|
2019-05-25 22:59:33 +02:00
|
|
|
MY_AES_BLOCK_SIZE,
|
2020-03-07 12:01:20 +02:00
|
|
|
aes_ctr_iv, sizeof aes_ctr_iv,
|
2018-08-13 16:04:37 +03:00
|
|
|
op == LOG_DECRYPT
|
2017-02-10 12:11:42 +02:00
|
|
|
? ENCRYPTION_FLAG_DECRYPT | ENCRYPTION_FLAG_NOPAD
|
|
|
|
: ENCRYPTION_FLAG_ENCRYPT | ENCRYPTION_FLAG_NOPAD,
|
|
|
|
LOG_DEFAULT_ENCRYPTION_KEY,
|
|
|
|
info.key_version);
|
2015-04-01 22:15:11 +02:00
|
|
|
ut_a(rc == MY_AES_OK);
|
2018-08-13 16:04:37 +03:00
|
|
|
ut_a(dst_len == dst_size);
|
|
|
|
memcpy(buf + LOG_CRYPT_HDR_SIZE, dst, dst_size);
|
2015-05-07 19:13:22 +03:00
|
|
|
}
|
2015-06-18 19:58:57 +03:00
|
|
|
|
|
|
|
return true;
|
2015-05-07 19:13:22 +03:00
|
|
|
}
|
|
|
|
|
2017-09-11 16:47:23 +03:00
|
|
|
/** Initialize the redo log encryption key and random parameters
|
|
|
|
when creating a new redo log.
|
|
|
|
The random parameters will be persisted in the log checkpoint pages.
|
|
|
|
@see log_crypt_write_checkpoint_buf()
|
|
|
|
@see log_crypt_read_checkpoint_buf()
|
2017-02-10 12:11:42 +02:00
|
|
|
@return whether the operation succeeded */
|
2020-03-07 12:01:20 +02:00
|
|
|
bool log_crypt_init()
|
2015-05-07 19:13:22 +03:00
|
|
|
{
|
2020-03-07 12:01:20 +02:00
|
|
|
info.key_version=
|
|
|
|
encryption_key_get_latest_version(LOG_DEFAULT_ENCRYPTION_KEY);
|
|
|
|
|
|
|
|
if (info.key_version == ENCRYPTION_KEY_VERSION_INVALID)
|
|
|
|
ib::error() << "log_crypt_init(): cannot get key version";
|
|
|
|
else if (my_random_bytes(tmp_iv, MY_AES_BLOCK_SIZE) != MY_AES_OK ||
|
|
|
|
my_random_bytes(info.crypt_msg, sizeof info.crypt_msg) !=
|
|
|
|
MY_AES_OK ||
|
|
|
|
my_random_bytes(info.crypt_nonce, sizeof info.crypt_nonce) !=
|
|
|
|
MY_AES_OK)
|
|
|
|
ib::error() << "log_crypt_init(): my_random_bytes() failed";
|
|
|
|
else if (init_crypt_key(&info))
|
|
|
|
goto func_exit;
|
|
|
|
|
|
|
|
info.key_version= 0;
|
|
|
|
func_exit:
|
|
|
|
return info.key_version != 0;
|
2014-12-22 16:53:17 +02:00
|
|
|
}
|
|
|
|
|
2017-02-10 20:26:02 +02:00
|
|
|
/** Read the MariaDB 10.1 checkpoint crypto (version, msg and iv) info.
|
|
|
|
@param[in] buf checkpoint buffer
|
|
|
|
@return whether the operation was successful */
|
2020-03-07 12:01:20 +02:00
|
|
|
ATTRIBUTE_COLD bool log_crypt_101_read_checkpoint(const byte* buf)
|
2017-02-10 20:26:02 +02:00
|
|
|
{
|
|
|
|
buf += 20 + 32 * 9;
|
|
|
|
|
|
|
|
const size_t n = *buf++ == 2 ? std::min(unsigned(*buf++), 5U) : 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < n; i++) {
|
2018-05-28 14:20:44 +03:00
|
|
|
struct crypt_info_t& info = infos[infos_used];
|
|
|
|
unsigned checkpoint_no = mach_read_from_4(buf);
|
|
|
|
for (size_t j = 0; j < infos_used; j++) {
|
|
|
|
if (infos[j].checkpoint_no == checkpoint_no) {
|
|
|
|
/* Do not overwrite an existing slot. */
|
|
|
|
goto next_slot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (infos_used >= UT_ARR_SIZE(infos)) {
|
2020-03-10 20:05:17 +02:00
|
|
|
ut_ad("too many checkpoint pages" == 0);
|
2018-05-28 14:20:44 +03:00
|
|
|
goto next_slot;
|
|
|
|
}
|
|
|
|
infos_used++;
|
|
|
|
info.checkpoint_no = checkpoint_no;
|
2017-02-10 20:26:02 +02:00
|
|
|
info.key_version = mach_read_from_4(buf + 4);
|
2020-03-07 12:01:20 +02:00
|
|
|
memcpy(info.crypt_msg, buf + 8, MY_AES_BLOCK_SIZE);
|
|
|
|
memcpy(info.crypt_nonce, buf + 24, sizeof info.crypt_nonce);
|
2017-02-10 20:26:02 +02:00
|
|
|
|
2017-02-10 12:11:42 +02:00
|
|
|
if (!init_crypt_key(&info, true)) {
|
2017-02-10 20:26:02 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-05-28 14:20:44 +03:00
|
|
|
next_slot:
|
2017-02-10 20:26:02 +02:00
|
|
|
buf += 4 + 4 + 2 * MY_AES_BLOCK_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Decrypt a MariaDB 10.1 redo log block.
|
2020-03-02 14:34:52 +02:00
|
|
|
@param[in,out] buf log block
|
|
|
|
@param[in] start_lsn server start LSN
|
2017-02-10 20:26:02 +02:00
|
|
|
@return whether the decryption was successful */
|
2020-03-07 12:01:20 +02:00
|
|
|
ATTRIBUTE_COLD bool log_crypt_101_read_block(byte* buf, lsn_t start_lsn)
|
2017-02-10 20:26:02 +02:00
|
|
|
{
|
2018-05-28 14:20:44 +03:00
|
|
|
const uint32_t checkpoint_no
|
|
|
|
= uint32_t(log_block_get_checkpoint_no(buf));
|
|
|
|
const crypt_info_t* info = infos;
|
|
|
|
for (const crypt_info_t* const end = info + infos_used; info < end;
|
|
|
|
info++) {
|
|
|
|
if (info->key_version
|
2021-06-22 09:11:41 +03:00
|
|
|
&& info->key_version != ENCRYPTION_KEY_VERSION_INVALID
|
2018-05-28 14:20:44 +03:00
|
|
|
&& info->checkpoint_no == checkpoint_no) {
|
|
|
|
goto found;
|
|
|
|
}
|
2017-02-10 20:26:02 +02:00
|
|
|
}
|
|
|
|
|
2018-06-04 11:31:39 +03:00
|
|
|
if (infos_used == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* MariaDB Server 10.1 would use the first key if it fails to
|
|
|
|
find a key for the current checkpoint. */
|
|
|
|
info = infos;
|
2021-06-22 09:11:41 +03:00
|
|
|
if (info->key_version == ENCRYPTION_KEY_VERSION_INVALID) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-28 14:20:44 +03:00
|
|
|
found:
|
2017-02-10 20:26:02 +02:00
|
|
|
byte dst[OS_FILE_LOG_BLOCK_SIZE];
|
|
|
|
uint dst_len;
|
2017-02-10 12:11:42 +02:00
|
|
|
byte aes_ctr_iv[MY_AES_BLOCK_SIZE];
|
2017-02-10 20:26:02 +02:00
|
|
|
|
|
|
|
const uint src_len = OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_HDR_SIZE;
|
|
|
|
|
|
|
|
ulint log_block_no = log_block_get_hdr_no(buf);
|
|
|
|
|
|
|
|
/* The log block header is not encrypted. */
|
|
|
|
memcpy(dst, buf, LOG_BLOCK_HDR_SIZE);
|
|
|
|
|
2020-03-07 12:01:20 +02:00
|
|
|
memcpy(aes_ctr_iv, info->crypt_nonce, 3);
|
2017-02-10 12:11:42 +02:00
|
|
|
mach_write_to_8(aes_ctr_iv + 3,
|
2020-03-02 14:34:52 +02:00
|
|
|
log_block_get_start_lsn(start_lsn, log_block_no));
|
2017-02-10 12:11:42 +02:00
|
|
|
memcpy(aes_ctr_iv + 11, buf, 4);
|
MDEV-21907: InnoDB: Enable -Wconversion on clang and GCC
The -Wconversion in GCC seems to be stricter than in clang.
GCC at least since version 4.4.7 issues truncation warnings for
assignments to bitfields, while clang 10 appears to only issue
warnings when the sizes in bytes rounded to the nearest integer
powers of 2 are different.
Before GCC 10.0.0, -Wconversion required more casts and would not
allow some operations, such as x<<=1 or x+=1 on a data type that
is narrower than int.
GCC 5 (but not GCC 4, GCC 6, or any later version) is complaining
about x|=y even when x and y are compatible types that are narrower
than int. Hence, we must rewrite some x|=y as
x=static_cast<byte>(x|y) or similar, or we must disable -Wconversion.
In GCC 6 and later, the warning for assigning wider to bitfields
that are narrower than 8, 16, or 32 bits can be suppressed by
applying a bitwise & with the exact bitmask of the bitfield.
For older GCC, we must disable -Wconversion for GCC 4 or 5 in such
cases.
The bitwise negation operator appears to promote short integers
to a wider type, and hence we must add explicit truncation casts
around them. Microsoft Visual C does not allow a static_cast to
truncate a constant, such as static_cast<byte>(1) truncating int.
Hence, we will use the constructor-style cast byte(~1) for such cases.
This has been tested at least with GCC 4.8.5, 5.4.0, 7.4.0, 9.2.1, 10.0.0,
clang 9.0.1, 10.0.0, and MSVC 14.22.27905 (Microsoft Visual Studio 2019)
on 64-bit and 32-bit targets (IA-32, AMD64, POWER 8, POWER 9, ARMv8).
2020-03-12 19:46:41 +02:00
|
|
|
aes_ctr_iv[11] &= byte(~(LOG_BLOCK_FLUSH_BIT_MASK >> 24));
|
2017-02-10 12:11:42 +02:00
|
|
|
aes_ctr_iv[15] = 0;
|
2017-02-10 20:26:02 +02:00
|
|
|
|
|
|
|
int rc = encryption_crypt(buf + LOG_BLOCK_HDR_SIZE, src_len,
|
|
|
|
dst + LOG_BLOCK_HDR_SIZE, &dst_len,
|
2020-03-07 12:01:20 +02:00
|
|
|
const_cast<byte*>(info->crypt_key),
|
2017-02-10 20:26:02 +02:00
|
|
|
MY_AES_BLOCK_SIZE,
|
2017-02-10 12:11:42 +02:00
|
|
|
aes_ctr_iv, MY_AES_BLOCK_SIZE,
|
2017-02-10 20:26:02 +02:00
|
|
|
ENCRYPTION_FLAG_DECRYPT
|
|
|
|
| ENCRYPTION_FLAG_NOPAD,
|
|
|
|
LOG_DEFAULT_ENCRYPTION_KEY,
|
|
|
|
info->key_version);
|
|
|
|
|
2018-05-28 14:20:44 +03:00
|
|
|
if (rc != MY_AES_OK || dst_len != src_len) {
|
2017-02-10 20:26:02 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buf, dst, sizeof dst);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-10 12:11:42 +02:00
|
|
|
/** Add the encryption information to a redo log checkpoint buffer.
|
|
|
|
@param[in,out] buf checkpoint buffer */
|
MDEV-25791: Remove UNIV_INTERN
Back in 2006 or 2007, when MySQL AB and Innobase Oy existed as
separately controlled entities (Innobase had been acquired by
Oracle Corporation), MySQL 5.1 introduced a storage engine plugin
interface and Oracle made use of it by distributing a separate
InnoDB Plugin, which would contain some more bug fixes and
improvements, compared to the version of InnoDB that was statically
linked with the mysqld server that was distributed by MySQL AB.
The built-in InnoDB would export global symbols, which would clash
with the symbols of the dynamic InnoDB Plugin (which was supposed
to override the built-in one when present).
The solution to this problem was to declare all global symbols with
UNIV_INTERN, so that they would get the GCC function attribute that
specifies hidden visibility.
Later, in MariaDB Server, something based on Percona XtraDB (a fork of
MySQL InnoDB) became the statically linked implementation, and something
closer to MySQL InnoDB was available as a dynamic plugin. Starting with
version 10.2, MariaDB Server includes only one InnoDB implementation,
and hence any reason to have the UNIV_INTERN definition was lost.
btr_get_size_and_reserved(): Move to the same compilation unit with
the only caller.
innodb_set_buf_pool_size(): Remove. Modify innobase_buffer_pool_size
directly.
fil_crypt_calculate_checksum(): Merge to the only caller.
ha_innobase::innobase_reset_autoinc(): Merge to the only caller.
thd_query_start_micro(): Remove. Call thd_start_utime() directly.
2021-05-27 10:13:14 +03:00
|
|
|
void log_crypt_write_checkpoint_buf(byte *buf)
|
2014-12-22 16:53:17 +02:00
|
|
|
{
|
2017-02-10 12:11:42 +02:00
|
|
|
ut_ad(info.key_version);
|
2020-03-07 12:01:20 +02:00
|
|
|
compile_time_assert(16 == sizeof info.crypt_msg);
|
2019-05-25 22:59:33 +02:00
|
|
|
compile_time_assert(16 == MY_AES_BLOCK_SIZE);
|
2017-02-10 12:11:42 +02:00
|
|
|
compile_time_assert(LOG_CHECKPOINT_CRYPT_MESSAGE
|
|
|
|
- LOG_CHECKPOINT_CRYPT_NONCE
|
|
|
|
== sizeof info.crypt_nonce);
|
|
|
|
|
2020-03-07 12:01:20 +02:00
|
|
|
memcpy(buf + LOG_CHECKPOINT_CRYPT_MESSAGE, info.crypt_msg,
|
2019-05-25 22:59:33 +02:00
|
|
|
MY_AES_BLOCK_SIZE);
|
2020-03-07 12:01:20 +02:00
|
|
|
memcpy(buf + LOG_CHECKPOINT_CRYPT_NONCE, info.crypt_nonce,
|
2017-02-10 12:11:42 +02:00
|
|
|
sizeof info.crypt_nonce);
|
|
|
|
mach_write_to_4(buf + LOG_CHECKPOINT_CRYPT_KEY, info.key_version);
|
2015-05-07 19:13:22 +03:00
|
|
|
}
|
|
|
|
|
2017-02-10 12:11:42 +02:00
|
|
|
/** Read the checkpoint crypto (version, msg and iv) info.
|
|
|
|
@param[in] buf checkpoint buffer
|
|
|
|
@return whether the operation was successful */
|
2020-03-07 12:01:20 +02:00
|
|
|
bool log_crypt_read_checkpoint_buf(const byte* buf)
|
2015-08-07 15:21:20 +03:00
|
|
|
{
|
2017-02-10 12:11:42 +02:00
|
|
|
info.checkpoint_no = mach_read_from_4(buf + (LOG_CHECKPOINT_NO + 4));
|
|
|
|
info.key_version = mach_read_from_4(buf + LOG_CHECKPOINT_CRYPT_KEY);
|
2015-08-07 15:21:20 +03:00
|
|
|
|
2017-02-10 12:11:42 +02:00
|
|
|
#if MY_AES_BLOCK_SIZE != 16
|
|
|
|
# error "MY_AES_BLOCK_SIZE != 16; redo log checkpoint format affected"
|
|
|
|
#endif
|
2020-03-07 12:01:20 +02:00
|
|
|
compile_time_assert(16 == sizeof info.crypt_msg);
|
2019-05-25 22:59:33 +02:00
|
|
|
compile_time_assert(16 == MY_AES_BLOCK_SIZE);
|
2017-02-10 12:11:42 +02:00
|
|
|
compile_time_assert(LOG_CHECKPOINT_CRYPT_MESSAGE
|
|
|
|
- LOG_CHECKPOINT_CRYPT_NONCE
|
|
|
|
== sizeof info.crypt_nonce);
|
2015-08-07 15:21:20 +03:00
|
|
|
|
2020-03-07 12:01:20 +02:00
|
|
|
memcpy(info.crypt_msg, buf + LOG_CHECKPOINT_CRYPT_MESSAGE,
|
2019-05-25 22:59:33 +02:00
|
|
|
MY_AES_BLOCK_SIZE);
|
2020-03-07 12:01:20 +02:00
|
|
|
memcpy(info.crypt_nonce, buf + LOG_CHECKPOINT_CRYPT_NONCE,
|
2017-02-10 12:11:42 +02:00
|
|
|
sizeof info.crypt_nonce);
|
2015-08-07 15:21:20 +03:00
|
|
|
|
2017-02-10 12:11:42 +02:00
|
|
|
return init_crypt_key(&info);
|
2015-08-07 15:21:20 +03:00
|
|
|
}
|
2015-05-07 19:13:22 +03:00
|
|
|
|
2017-09-16 22:31:53 +03:00
|
|
|
/** Encrypt or decrypt a temporary file block.
|
|
|
|
@param[in] src block to encrypt or decrypt
|
|
|
|
@param[in] size size of the block
|
|
|
|
@param[out] dst destination block
|
|
|
|
@param[in] offs offset to block
|
|
|
|
@param[in] encrypt true=encrypt; false=decrypt
|
|
|
|
@return whether the operation succeeded */
|
MDEV-25791: Remove UNIV_INTERN
Back in 2006 or 2007, when MySQL AB and Innobase Oy existed as
separately controlled entities (Innobase had been acquired by
Oracle Corporation), MySQL 5.1 introduced a storage engine plugin
interface and Oracle made use of it by distributing a separate
InnoDB Plugin, which would contain some more bug fixes and
improvements, compared to the version of InnoDB that was statically
linked with the mysqld server that was distributed by MySQL AB.
The built-in InnoDB would export global symbols, which would clash
with the symbols of the dynamic InnoDB Plugin (which was supposed
to override the built-in one when present).
The solution to this problem was to declare all global symbols with
UNIV_INTERN, so that they would get the GCC function attribute that
specifies hidden visibility.
Later, in MariaDB Server, something based on Percona XtraDB (a fork of
MySQL InnoDB) became the statically linked implementation, and something
closer to MySQL InnoDB was available as a dynamic plugin. Starting with
version 10.2, MariaDB Server includes only one InnoDB implementation,
and hence any reason to have the UNIV_INTERN definition was lost.
btr_get_size_and_reserved(): Move to the same compilation unit with
the only caller.
innodb_set_buf_pool_size(): Remove. Modify innobase_buffer_pool_size
directly.
fil_crypt_calculate_checksum(): Merge to the only caller.
ha_innobase::innobase_reset_autoinc(): Merge to the only caller.
thd_query_start_micro(): Remove. Call thd_start_utime() directly.
2021-05-27 10:13:14 +03:00
|
|
|
bool log_tmp_block_encrypt(
|
2017-09-16 22:31:53 +03:00
|
|
|
const byte* src,
|
|
|
|
ulint size,
|
|
|
|
byte* dst,
|
|
|
|
uint64_t offs,
|
|
|
|
bool encrypt)
|
2015-08-07 15:21:20 +03:00
|
|
|
{
|
2017-09-16 22:31:53 +03:00
|
|
|
uint dst_len;
|
2019-06-27 16:23:03 +05:30
|
|
|
uint64_t iv[MY_AES_BLOCK_SIZE / sizeof(uint64_t)];
|
|
|
|
iv[0] = offs;
|
|
|
|
memcpy(iv + 1, tmp_iv, sizeof iv - sizeof *iv);
|
2017-09-16 22:31:53 +03:00
|
|
|
|
|
|
|
int rc = encryption_crypt(
|
2019-07-02 17:46:22 +03:00
|
|
|
src, uint(size), dst, &dst_len,
|
2020-03-07 12:01:20 +02:00
|
|
|
const_cast<byte*>(info.crypt_key), MY_AES_BLOCK_SIZE,
|
2019-07-02 17:46:22 +03:00
|
|
|
reinterpret_cast<byte*>(iv), uint(sizeof iv),
|
2017-09-16 22:31:53 +03:00
|
|
|
encrypt
|
|
|
|
? ENCRYPTION_FLAG_ENCRYPT|ENCRYPTION_FLAG_NOPAD
|
|
|
|
: ENCRYPTION_FLAG_DECRYPT|ENCRYPTION_FLAG_NOPAD,
|
|
|
|
LOG_DEFAULT_ENCRYPTION_KEY, info.key_version);
|
2015-08-07 15:21:20 +03:00
|
|
|
|
2017-09-16 22:31:53 +03:00
|
|
|
if (rc != MY_AES_OK) {
|
|
|
|
ib::error() << (encrypt ? "Encryption" : "Decryption")
|
|
|
|
<< " failed for temporary file: " << rc;
|
2015-08-07 15:21:20 +03:00
|
|
|
}
|
|
|
|
|
2017-09-16 22:31:53 +03:00
|
|
|
return rc == MY_AES_OK;
|
2015-08-07 15:21:20 +03:00
|
|
|
}
|