Skip to content

Commit

Permalink
[plugin] PS-269: Initial Percona Server 8.0.12 tree
Browse files Browse the repository at this point in the history
PS-5741: Incorrect use of memset_s in keyring_vault.

Fixed the usage of memset_s. The arguments should be:
void memset_s(void *dest, size_t dest_max, int c, size_t n)
where the 2nd argument is size of buffer and the 3rd is
argument is character to fill.

---------------------------------------------------------------------------

PS-7769 - Fix use-after-return error in audit_log_exclude_accounts_validate

---

*Problem:*

`st_mysql_value::val_str` might return a pointer to `buf` which after
the function called is deleted. Therefore the value in `save`, after
reuturnin from the function, is invalid.

In this particular case, the error is not manifesting as val_str`
returns memory allocated with `thd_strmake` and it does not use `buf`.

*Solution:*

Allocate memory with `thd_strmake` so the memory in `save` is not local.

---------------------------------------------------------------------------

Fix test main.bug12969156 when WITH_ASAN=ON

*Problem:*

ASAN complains about stack-buffer-overflow on function `mysql_heartbeat`:

```
==90890==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fe746d06d14 at pc 0x7fe760f5b017 bp 0x7fe746d06cd0 sp 0x7fe746d06478
WRITE of size 24 at 0x7fe746d06d14 thread T16777215

Address 0x7fe746d06d14 is located in stack of thread T26 at offset 340 in frame
    #0 0x7fe746d0a55c in mysql_heartbeat(void*) /home/yura/ws/percona-server/plugin/daemon_example/daemon_example.cc:62

  This frame has 4 object(s):
    [48, 56) 'result' (line 66)
    [80, 112) '_db_stack_frame_' (line 63)
    [144, 200) 'tm_tmp' (line 67)
    [240, 340) 'buffer' (line 65) <== Memory access at offset 340 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
      (longjmp and C++ exceptions *are* supported)
Thread T26 created by T25 here:
    #0 0x7fe760f5f6d5 in __interceptor_pthread_create ../../../../src/libsanitizer/asan/asan_interceptors.cpp:216
    #1 0x557ccbbcb857 in my_thread_create /home/yura/ws/percona-server/mysys/my_thread.c:104
    percona#2 0x7fe746d0b21a in daemon_example_plugin_init /home/yura/ws/percona-server/plugin/daemon_example/daemon_example.cc:148
    percona#3 0x557ccb4c69c7 in plugin_initialize /home/yura/ws/percona-server/sql/sql_plugin.cc:1279
    percona#4 0x557ccb4d19cd in mysql_install_plugin /home/yura/ws/percona-server/sql/sql_plugin.cc:2279
    percona#5 0x557ccb4d218f in Sql_cmd_install_plugin::execute(THD*) /home/yura/ws/percona-server/sql/sql_plugin.cc:4664
    percona#6 0x557ccb47695e in mysql_execute_command(THD*, bool) /home/yura/ws/percona-server/sql/sql_parse.cc:5160
    percona#7 0x557ccb47977c in mysql_parse(THD*, Parser_state*, bool) /home/yura/ws/percona-server/sql/sql_parse.cc:5952
    percona#8 0x557ccb47b6c2 in dispatch_command(THD*, COM_DATA const*, enum_server_command) /home/yura/ws/percona-server/sql/sql_parse.cc:1544
    percona#9 0x557ccb47de1d in do_command(THD*) /home/yura/ws/percona-server/sql/sql_parse.cc:1065
    percona#10 0x557ccb6ac294 in handle_connection /home/yura/ws/percona-server/sql/conn_handler/connection_handler_per_thread.cc:325
    percona#11 0x557ccbbfabb0 in pfs_spawn_thread /home/yura/ws/percona-server/storage/perfschema/pfs.cc:2198
    percona#12 0x7fe760ab544f in start_thread nptl/pthread_create.c:473
```

The reason is that `my_thread_cancel` is used to finish the daemon thread. This is not and orderly way of finishing the thread. ASAN does not register the stack variables are not used anymore which generates the error above.

This is a benign error as all the variables are on the stack.

*Solution*:

Finish the thread in orderly way by using a signalling variable.

---------------------------------------------------------------------------

PS-8204: Fix XML escape rules for audit plugin

https://jira.percona.com/browse/PS-8204

There was a wrong length specified for some XML
escape rules. As a result of this terminating null symbol from
replacement rule was copied into resulting string. This lead to
quer text truncation in audit log file.
In addition added empty replacement rules for '\b' and 'f' symbols
which just remove them from resulting string. These symboles are
not supported in XML 1.0.

---------------------------------------------------------------------------

PS-8854: Add main.percona_udf MTR test

Add a test to check FNV1A_64, FNV_64, and MURMUR_HASH user-defined functions.

---------------------------------------------------------------------------

PS-9218: Merge MySQL 8.4.0 (fix gcc-14 build)

https://perconadev.atlassian.net/browse/PS-9218
  • Loading branch information
inikep authored and dlenev committed Jul 25, 2024
1 parent 3f677ee commit c60a42f
Show file tree
Hide file tree
Showing 44 changed files with 2,702 additions and 12 deletions.
4 changes: 2 additions & 2 deletions include/my_bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ extern uint bitmap_bits_set(const MY_BITMAP *map);
extern void bitmap_free(MY_BITMAP *map);
extern void bitmap_set_above(MY_BITMAP *map, uint from_byte, bool use_bit);
extern void bitmap_set_prefix(MY_BITMAP *map, uint prefix_size);
extern void bitmap_intersect(MY_BITMAP *to, const MY_BITMAP *from);
extern void bitmap_intersect(MY_BITMAP *map, const MY_BITMAP *map2);
extern void bitmap_subtract(MY_BITMAP *map, const MY_BITMAP *map2);
extern void bitmap_union(MY_BITMAP *map, const MY_BITMAP *map2);
extern void bitmap_xor(MY_BITMAP *map, const MY_BITMAP *map2);
Expand Down Expand Up @@ -136,4 +136,4 @@ static inline void bitmap_set_all(MY_BITMAP *map) {
memset(map->bitmap, 0xFF, 4 * no_words_in_map(map));
}

#endif // MY_BITMAP_INCLUDED
#endif /* MY_BITMAP_INCLUDED */
3 changes: 3 additions & 0 deletions include/my_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,9 @@ extern size_t my_fwrite(FILE *stream, const uchar *Buffer, size_t Count,
myf MyFlags);
extern my_off_t my_fseek(FILE *stream, my_off_t pos, int whence);
extern my_off_t my_ftell(FILE *stream);
#if !defined(HAVE_MEMSET_S)
void memset_s(void *dest, size_t dest_max, int c, size_t n);
#endif

/* implemented in my_syslog.c */

Expand Down
7 changes: 7 additions & 0 deletions mysql-test/include/plugin.defs
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,10 @@ component_test_execute_prepared_statement plugin_output_directory no COMPONEN

# component_test_execute_regular_statement
component_test_execute_regular_statement plugin_output_directory no COMPONENT_TEST_EXECUTE_REGULAR_STATEMENT

# Percona additions
auth_socket plugin_output_directory no SOCKET_AUTH
ha_rocksdb plugin_output_directory no ROCKSDB rocksdb,rocksdb_cfstats,rocksdb_dbstats,rocksdb_perf_context,rocksdb_perf_context_global,rocksdb_cf_options,rocksdb_compaction_history,rocksdb_compaction_stats,rocksdb_active_compaction_stats,rocksdb_global_info,rocksdb_ddl,rocksdb_index_file_map,rocksdb_locks,rocksdb_trx,rocksdb_deadlock,rocksdb_sst_props,rocksdb_live_files_metadata
auth_pam plugin_output_directory no AUTH_PAM
auth_pam_compat plugin_output_directory no AUTH_PAM_COMPAT
keyring_vault plugin_output_directory no KEYRING_VAULT_PLUGIN keyring_vault
37 changes: 37 additions & 0 deletions mysql-test/r/percona_udf.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
*** checking if UDF works without creating the function
SELECT MURMUR_HASH('hello', 'world');
ERROR 42000: FUNCTION test.MURMUR_HASH does not exist

*** creating UDF functions
CREATE FUNCTION fnv1a_64 RETURNS INTEGER SONAME 'libfnv1a_udf.so';
CREATE FUNCTION fnv_64 RETURNS INTEGER SONAME 'libfnv_udf.so';
CREATE FUNCTION murmur_hash RETURNS INTEGER SONAME 'libmurmur_udf.so';

*** checking UDF functions
include/assert.inc [checking hash value for FNV1A_64()]
include/assert.inc [checking hash value for FNV_64()]
include/assert.inc [checking hash value for MURMUR_HASH()]

*** creating t1 table
CREATE TABLE t1 (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
col1 INT,
col2 VARCHAR(16),
col3 TEXT,
PRIMARY KEY(id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO t1(col1, col2, col3) VALUES (1, "b", "c");
INSERT INTO t1(col1, col2, col3) VALUES (2, "3240", "4000");
INSERT INTO t1(col1, col2, col3) VALUES (3, "short", "words");

*** checking UDF functions with t1 table
include/assert.inc [checking hash value for BIT_XOR(FNV1A_64())]
include/assert.inc [checking hash value for BIT_XOR(FNV_64())]
include/assert.inc [checking hash value for BIT_XOR(MURMUR_HASH())]
include/assert.inc [checking hash value for FNV1A_64(col1, col2, col3)]

*** cleaning up
DROP FUNCTION fnv1a_64;
DROP FUNCTION fnv_64;
DROP FUNCTION murmur_hash;
DROP TABLE t1;
65 changes: 65 additions & 0 deletions mysql-test/t/percona_udf.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
--echo *** checking if UDF works without creating the function
--error ER_SP_DOES_NOT_EXIST
SELECT MURMUR_HASH('hello', 'world');

--echo
--echo *** creating UDF functions
CREATE FUNCTION fnv1a_64 RETURNS INTEGER SONAME 'libfnv1a_udf.so';
CREATE FUNCTION fnv_64 RETURNS INTEGER SONAME 'libfnv_udf.so';
CREATE FUNCTION murmur_hash RETURNS INTEGER SONAME 'libmurmur_udf.so';

--echo
--echo *** checking UDF functions

--let $assert_text = checking hash value for FNV1A_64()
--let $assert_cond = `SELECT FNV1A_64('hello', 'world') = 1214055856804091265`
--source include/assert.inc

--let $assert_text = checking hash value for FNV_64()
--let $assert_cond = `SELECT FNV_64('hello', 'world') = 2924375256774005480`
--source include/assert.inc

--let $assert_text = checking hash value for MURMUR_HASH()
--let $assert_cond = `SELECT MURMUR_HASH('hello', 'world') = -7857203399167365490`
--source include/assert.inc

--echo
--echo *** creating t1 table
CREATE TABLE t1 (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
col1 INT,
col2 VARCHAR(16),
col3 TEXT,
PRIMARY KEY(id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO t1(col1, col2, col3) VALUES (1, "b", "c");
INSERT INTO t1(col1, col2, col3) VALUES (2, "3240", "4000");
INSERT INTO t1(col1, col2, col3) VALUES (3, "short", "words");

--echo
--echo *** checking UDF functions with t1 table

--let $assert_text = checking hash value for BIT_XOR(FNV1A_64())
--let $assert_cond = [SELECT BIT_XOR(CAST(FNV1A_64(col1, col2, col3) AS UNSIGNED)) FROM t1] = 9092447622868433808
--source include/assert.inc

--let $assert_text = checking hash value for BIT_XOR(FNV_64())
--let $assert_cond = [SELECT BIT_XOR(CAST(FNV_64(col1, col2, col3) AS UNSIGNED)) FROM t1] = 3813096360487945067
--source include/assert.inc

--let $assert_text = checking hash value for BIT_XOR(MURMUR_HASH())
--let $assert_cond = [SELECT BIT_XOR(CAST(MURMUR_HASH(col1, col2, col3) AS UNSIGNED)) FROM t1] = 9393970666296144275
--source include/assert.inc

--let $assert_text = checking hash value for FNV1A_64(col1, col2, col3)
--let $assert_cond = ["SELECT FNV1A_64(col1, col2, col3) AS Hash_Val FROM t1", Hash_Val, 1] = 5882002141665055715
--source include/assert.inc

--echo
--echo *** cleaning up
DROP FUNCTION fnv1a_64;
DROP FUNCTION fnv_64;
DROP FUNCTION murmur_hash;

DROP TABLE t1;
2 changes: 2 additions & 0 deletions mysys/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ SET(MY_TIME_SOURCES my_time.cc my_systime.cc)

ADD_CONVENIENCE_LIBRARY(mytime ${MY_TIME_SOURCES})

INCLUDE_DIRECTORIES(SYSTEM ${BOOST_PATCHES_DIR} ${BOOST_INCLUDE_DIR})

SET(MYSYS_SOURCES
array.cc
charset.cc
Expand Down
13 changes: 13 additions & 0 deletions mysys/my_malloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,16 @@ char *my_strndup(PSI_memory_key key, const char *from, size_t length,
}
return ptr;
}

#if !defined(HAVE_MEMSET_S)
void memset_s(void *dest, size_t dest_max, int c, size_t n) {
#if defined(WIN32)
SecureZeroMemory(dest, n);
#else
volatile unsigned char *p = static_cast<unsigned char *>(dest);
while (dest_max-- && n--) {
*p++ = c;
}
#endif
}
#endif
7 changes: 5 additions & 2 deletions plugin/daemon_example/daemon_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <atomic>

#include "m_string.h" // strlen
#include "my_dbug.h"
Expand Down Expand Up @@ -63,6 +64,7 @@ static void init_deamon_example_psi_keys() {
struct mysql_heartbeat_context {
my_thread_handle heartbeat_thread;
File heartbeat_file;
std::atomic_bool done;
};

static void *mysql_heartbeat(void *p) {
Expand All @@ -72,7 +74,7 @@ static void *mysql_heartbeat(void *p) {
time_t result;
struct tm tm_tmp;

while (true) {
while (!con->done.load()) {
sleep(5);

result = time(nullptr);
Expand Down Expand Up @@ -125,6 +127,7 @@ static int daemon_example_plugin_init(void *p) {
MY_REPLACE_EXT | MY_UNPACK_FILENAME);
unlink(heartbeat_filename);
con->heartbeat_file = my_open(heartbeat_filename, O_CREAT | O_RDWR, MYF(0));
con->done.store(false);

/*
No threads exist at this point in time, so this is thread safe.
Expand Down Expand Up @@ -173,7 +176,7 @@ static int daemon_example_plugin_deinit(void *p) {
struct tm tm_tmp;
void *dummy_retval;

my_thread_cancel(&con->heartbeat_thread);
con->done.store(true);

localtime_r(&result, &tm_tmp);
snprintf(buffer, sizeof(buffer),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@

#include "openssl/engine.h"

#if OPENSSL_VERSION_NUMBER >= 0x30000000L
#include <openssl/evp.h>
#include <openssl/provider.h>
#endif

#include "xcom/retry.h"
#include "xcom/task_debug.h"
#include "xcom/x_platform.h"
Expand Down
2 changes: 1 addition & 1 deletion plugin/keyring/common/i_keyring_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace keyring {

class IKeyring_io : public Keyring_alloc {
public:
virtual bool init(std::string *keyring_storage_url) = 0;
virtual bool init(const std::string *keyring_storage_url) = 0;
virtual bool flush_to_backup(ISerialized_object *serialized_object) = 0;
virtual bool flush_to_storage(ISerialized_object *serialized_object) = 0;

Expand Down
1 change: 1 addition & 0 deletions plugin/keyring/common/i_keyring_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct IKey : public Keyring_alloc {
virtual size_t get_key_data_size() = 0;
virtual size_t get_key_pod_size() const = 0;
virtual uchar *release_key_data() = 0;
virtual void xor_data(uchar *data, size_t data_len) = 0;
virtual void xor_data() = 0;
virtual void set_key_data(uchar *key_data, size_t key_data_size) = 0;
virtual void set_key_type(const std::string *key_type) = 0;
Expand Down
12 changes: 8 additions & 4 deletions plugin/keyring/common/keyring_key.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,16 @@ size_t Key::get_key_pod_size() const {
return key_pod_size_aligned;
}

void Key::xor_data() {
if (key == nullptr) return;
void Key::xor_data(uchar *data, size_t data_len) {
static const char *obfuscate_str = "*305=Ljt0*!@$Hnm(*-9-w;:";
for (size_t i = 0, l = 0; i < key_len;
for (size_t i = 0, l = 0; i < data_len;
++i, l = ((l + 1) % strlen(obfuscate_str)))
key.get()[i] ^= obfuscate_str[l];
data[i] ^= obfuscate_str[l];
}

void Key::xor_data() {
if (key == nullptr) return;
xor_data(key.get(), key_len);
}

bool Key::is_key_id_valid() { return key_id.length() > 0; }
Expand Down
3 changes: 2 additions & 1 deletion plugin/keyring/common/keyring_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct Key : IKey {
size_t get_key_data_size() override;
size_t get_key_pod_size() const override;
uchar *release_key_data() override;
void xor_data(uchar *data, size_t data_len) override;
void xor_data() override;
void set_key_data(uchar *key_data, size_t key_data_size) override;
void set_key_type(const std::string *key_type) override;
Expand All @@ -69,7 +70,7 @@ struct Key : IKey {
const void *a_key, size_t a_key_len);

void clear_key_data();
void create_key_signature() const;
virtual void create_key_signature() const;
bool load_string_from_buffer(const uchar *buffer, size_t *buffer_position,
size_t key_pod_size, std::string *string,
size_t string_length);
Expand Down
53 changes: 52 additions & 1 deletion plugin/keyring/common/keyring_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
#ifndef MYSQL_KEYRING_MEMORY_H
#define MYSQL_KEYRING_MEMORY_H

#include <mysql/plugin_keyring.h>
#include <string.h>
#include <limits>
#include <memory>

#include "my_sys.h"
#include "mysql/service_mysql_alloc.h"

namespace keyring {

extern PSI_memory_key key_memory_KEYRING;
Expand All @@ -49,6 +52,54 @@ class Keyring_alloc {
static void operator delete(void *ptr, std::size_t) { my_free(ptr); }
static void operator delete[](void *ptr, std::size_t) { my_free(ptr); }
};

template <class T>
class Secure_allocator {
public:
using size_type = size_t;
using difference_type = ptrdiff_t;
using pointer = T *;
using const_pointer = const T *;
using reference = T &;
using const_reference = const T &;
using value_type = T;

Secure_allocator() noexcept {}

template <class U>
Secure_allocator(const Secure_allocator<U> &) noexcept {}

T *allocate(size_t n) {
if (n == 0)
return nullptr;
else if (n > INT_MAX)
throw std::bad_alloc();
return keyring_malloc<T *>(n * sizeof(T));
}

void deallocate(T *p, size_t n) noexcept {
memset_s(p, n, 0, n);
my_free(p);
}

template <class U>
struct rebind {
typedef Secure_allocator<U> other;
};
};

template <typename T1, typename T2>
bool operator==(const Secure_allocator<T1> &,
const Secure_allocator<T2> &) noexcept {
return true;
}

template <typename T1, typename T2>
bool operator!=(const Secure_allocator<T1> &,
const Secure_allocator<T2> &) noexcept {
return false;
}

} // namespace keyring

#endif // MYSQL_KEYRING_MEMORY_H
34 changes: 34 additions & 0 deletions plugin/keyring/common/secure_string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* Copyright (c) 2018 Percona LLC and/or its affiliates. All rights reserved.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */

#ifndef MYSQL_KEYRING_SECURE_STRING
#define MYSQL_KEYRING_SECURE_STRING

#include <sstream>
#include "keyring_memory.h"

namespace keyring {
typedef std::basic_string<char, std::char_traits<char>, Secure_allocator<char>>
Secure_string;
typedef std::basic_ostringstream<char, std::char_traits<char>,
Secure_allocator<char>>
Secure_ostringstream;
typedef std::basic_istringstream<char, std::char_traits<char>,
Secure_allocator<char>>
Secure_istringstream;
} // namespace keyring

#endif // MYSQL_KEYRING_SECURE_STRING
8 changes: 8 additions & 0 deletions plugin/keyring/keyring_file.version
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
KEYRING_FILE_VERSION_1.0 {
global:
_mysql_*;
mysql_malloc_service;
my_plugin_log_service;
security_context_service;
local: *;
};
Loading

0 comments on commit c60a42f

Please sign in to comment.