1
0
mirror of https://github.com/mariadb-corporation/mariadb-connector-c.git synced 2025-09-11 08:30:59 +03:00

1089 Commits

Author SHA1 Message Date
Vladislav Vaintroub
536d9e2b9e MDEV-33430 Fix unexpected "SSL certificate self-signed" errors on Windows
These errors stem from generating self-signed certificates, and enabling
SSL + SSL certificate validation in clients.

This was done without considering Windows specifics, such as
- TCP connections by default
- non-password based plugin, auth_gssapi, in every installation

To provide some relief, consider local TCP connections to be secure
transport and skip self-signed errors for them at least.
2024-02-09 02:09:38 +01:00
Sergei Golubchik
8dffd56936 MDEV-31857 enable MYSQL_OPT_SSL_VERIFY_SERVER_CERT by default
because the default value of every option is 0
(option and option.extension are bzero-ed to reset),
tls_verify_server_cert was renamed to tls_allow_invalid_server_cert
with the default value of 0, "do not allow".

API didn't change, it's still MYSQL_OPT_SSL_VERIFY_SERVER_CERT
2024-02-04 22:17:25 +01:00
Sergei Golubchik
79a746f290 unix socket and named pipes are secure
as in --require-secure-transport
2024-02-04 22:17:25 +01:00
Sergei Golubchik
a99570c118 MDEV-31855 SSL cert validation protocol extension
* extend the client auth plugin API with a new callback
* relax the plugin version check to allow load a plugin with the
  same major version, even if the minor versions differ
* implement the protocol extension:
  - don't abort at once if the certificate is self signed and
    no CA was explicitly specified
  - allow it if it passes fingerprint check
  - allow it if plugin has hash_password_bin callback, password was
    non-empty and the control hash matches server's
2024-02-04 22:17:25 +01:00
Sergei Golubchik
2f6b5a523c typo in the fp commit, cert_fp unused
also rename fp->f to avoid shadowing a variable
2024-02-04 22:17:25 +01:00
Sergei Golubchik
830d137387 don't use the output printf buffer as a %s parameter
followup for ebcb9eca29
2024-02-04 22:17:24 +01:00
Georg Richter
9aa15e72a7 TLS fingerprint
Beside SHA1 fingerprint hash, Connector/C now also supports
SHA224 (OpenSSL and GnuTLS only), SHA256, SHA384 and SHA512
fingerprint hashes.
2024-02-04 22:17:22 +01:00
Vladislav Vaintroub
83951feec9 CONC-686 Error 2026 TLS error messages truncated
Do not truncate error message to arbitrary length. TLS messages can be long
2024-01-24 11:10:50 +01:00
Sergei Golubchik
77a2e6ac5d don't warn about the authenticity of client-side errors
they cannot be sent by the server (ma_net_safe_read() guarantees that)
so they all should be safe and not forged

also, use existing macros to check for error ranges, they are
sufficiently parenthesized to avoid compiler warnings (errors with -Werror)
about "you might want to add parentheses here"
2023-12-21 19:00:35 +01:00
Daniel Lenski
bd87353512 Remove unreachable code section
Based on Sergei Golubchik's question about this code section in
https://github.com/mariadb-corporation/mariadb-connector-c/pull/223#issuecomment-1773728383,
eventually culminating in the conclusion that it's literally impossible to
reach this code section based on the types and signedess of the variables
involved:
https://github.com/mariadb-corporation/mariadb-connector-c/pull/223#issuecomment-1854720364

All new code of the whole pull request, including one or several files
that are either new files or modified ones, are contributed under the
BSD-new license. I am contributing on behalf of my employer Amazon Web
Services, Inc.
2023-12-21 18:38:12 +01:00
Daniel Lenski
ebcb9eca29 [CONC-648] Do not trust error packets received prior to TLS handshake completion
MariaDB Connector/C does not distinguish [application-layer error
packets](https://mariadb.com/kb/en/err_packet) that it receives prior to TLS
handshake completion from those that it receives immediately after.

(A trivially modified server built from
https://github.com/dlenski/mariadb-server/commit/demonstration_of_CONC-648_vulnerability
can easily be used to demonstrate this.)

Pre-TLS error packet received from this trivially modified server. This packet
should NOT be trusted to actually originate from the server:

    $ mariadb --ssl --ssl-verify-server-cert -uUsername -pVerySecretPassword -h CONC-648.vuln.demo.server.com
    ERROR 1815 (HY000): Internal error: Client will accept this error as genuine even if running with --ssl --ssl-verify-server-cert, and even though this error is sent in plaintext PRIOR TO TLS HANDSHAKE.

Post-(TLS handshake) error packet received from a normal MariaDB server upon
an attempt to connect with incorrect credentials.  This error packet CAN be
trusted to actually originate from the server, assuming transitive trust in
the TLS protocol implementation and PKI-based certificate validation:

    $ mariadb --ssl --ssl-verify-server-cert -uUsername -pWrongPassword -h $NORMAL_MARIADB10.6.14_SERVER
    ERROR 1045 (28000): Access denied for user 'Username'@'A.B.C.D' (using password: YES)

This client behavior opens up MariaDB Connector/C clients to an extremely
straightforward [downgrade attack](https://en.wikipedia.org/wiki/Downgrade_attack).

An on-path or pervasive attacker can inject errors into MariaDB
client→server connections that are intended to be protected by TLS, and the
client has no clear mechanism to distinguish such errors from errors that
actually come from the server.

An attacker could easily use this to DOS a client, or even influence its
behavior.  For example, consider a client application which is configured…

1. To use TLS with server certificate validation
   (`--ssl --ssl-verify-server-cert`), and
2. To wait for a back-off period and then *retry* connection attempts if the server
   responds with `ER_CON_COUNT_ERROR` ("Too many connections") from the
   server, and
3. To give up and shut down if its connection attempts fail with
   `ER_ACCESS_DENIED_ERROR` ("Access denied for user"), on the assumption
   that this is due to an incorrect or expired password, and cannot be
   resolved without human intervention.

An attacker could completely disable the retry mechanism of this application
by intercepting connection attempts and replying with
`ER_ACCESS_DENIED_ERROR` packets.

This patch modifies MariaDB Connector/C so that if the client is configured
to use TLS, error packets received prior to the completion of the TLS
handshake are untrusted, and are changed to a generic `CR_CONNECTION_ERROR`.

    $ mariadb --ssl --ssl-verify-server-cert -uUsername -pVerySecretPassword -h CONC-648.vuln.demo.server.com
    ERROR 2002 (HY000): Received error packet before completion of TLS handshake. The authenticity of the following error cannot be verified:
    1815 - Internal error: Client will accept this error as genuine even if running with --ssl --ssl-verify-server-cert, and even though this error is sent in plaintext PRIOR TO TLS HANDSHAKE.

All new code of the whole pull request, including one or several files
that are either new files or modified ones, are contributed under the
BSD-new license. I am contributing on behalf of my employer Amazon Web
Services, Inc.
2023-12-21 18:38:12 +01:00
Daniel Lenski
4419abe71a Client should reject CLIENT-only error codes sent by the server
Per @vuvova in
https://github.com/mariadb-corporation/mariadb-connector-c/pull/223#issuecomment-1854720364:

> I don't think the client should accept client-side errors from the server
> at all.

If the server sends an error packet with error codes in the ranges
`CR_{MIN,MAX}_ERROR` (codes [2000, 2999]) or `CER_{MIN,MAX}_ERROR` (codes
[5000, 5999]), we will replace these with `CR_MALFORMED_PACKET`, rather than
propagating them to the client user.
2023-12-21 18:38:12 +01:00
Georg Richter
4f5950b4b6 Merge branch '3.1' into 3.3 2023-10-21 19:46:00 +02:00
Georg Richter
8320f0d54d Fix error on 32-bit systems
Problem was introduced with fix for CONC-668.
2023-10-21 19:43:42 +02:00
Georg Richter
642bc31ed2 Follow up of PR-236 (update ma_context):
Since there is no way in the ISO C standard to specify a
non-obsolescent function prototype indicating that a
function will be called with an arbitrary number (including
zero) of arguments of arbitrary types, we have to cast the
callback function in makecontext() call to avoid compiler
warnings/errors.

See also:
https://pubs.opengroup.org/onlinepubs/009695399/functions/makecontext.html
2023-10-21 08:09:40 +02:00
Tildeslash
808312f1e4 Update ma_context.c
Old K&R function declaration is no longer supported when compiling on macOS 13.5.2
2023-10-20 06:52:21 +02:00
Tildeslash
249d838bc0 Update ma_context.c
Old K&R function declaration is no longer supported when compiling on macOS 13.5.2
2023-09-23 02:33:37 +02:00
Vladislav Vaintroub
4692e9cec1 CONC-645 : fix build with clang (v16), clang-cl(v16), and mingw-gcc(v12).
schannel_certs.c - conflicting headers, include winsock2.h before windows.h,

strerror_r is now defined also with mingw

do not build mariadb_config on Windows, getopt.h is missing
2023-09-22 00:59:48 +02:00
Vladislav Vaintroub
463a50e973 Merge remote-tracking branch 'origin/3.1' into 3.3
# Conflicts:
#	include/ma_crypt.h
#	libmariadb/mariadb_lib.c
2023-09-21 23:45:35 +02:00
Vladislav Vaintroub
1b3cf6b441 CONC-669 Cache bcrypt algorithm providers in win_crypt.c
CryptOpenAlgorithmProvider() documentation states:
"We recommend that you cache any algorithm provider handles that you will
use more than once, rather than opening and closing the algorithm providers
over and over."

This patch implements algorithm handle caching,
and also simplifies how hashes are handled in win_crypt.c

- MA_HASH_CTX* is now just BCrypt hash handle.
- We no longer allocate or free memory any memory outselves.

There are small change in the ma_hash API:
ma_hash_new() lost the second parameter, only used on Windows previously
2023-09-21 18:06:01 +02:00
Daniel Black
07ae949c15 MDEV-19511 Remove WORDS_BIGENDIAN - HAVE_BIGENDIAN replaced it
Also note https://cmake.org/cmake/help/latest/module/TestBigEndian.html

Discussion source, https://lists.launchpad.net/maria-developers/msg11819.html

It was solved a while ago with HAVE_BIGENDIAN added to test,
WORDS_BIGENDIAN wasn't cleaned up.
2023-09-21 09:24:40 +10:00
Vladislav Vaintroub
d9626e315e CONC-666: Fix memory allocation issue with prepared statement reexecution.
Connector is using a memory root for the result set, and
mysql_stmt_execute()/mysql_stmt_store_result(), when executed in a loop,
leads to series of ma_alloc_root() and ma_free_root() calls for the same
memory root.

The problem is that ma_alloc_root() calculates the allocation size based
on MA_MEM_ROOT::block_num, this value is incremented for larger allocation,
but is never reset. As a result, the allocation size continuously
grows, for an empty memroot.

This patch resets MA_MEM_ROOT::block_num in ma_free_root().
2023-09-20 23:01:44 +02:00
Georg Richter
9f37c27bc8 Fix for CONC-668:
Fix build error on 32-bit systems.
2023-09-18 16:05:00 +02:00
Georg Richter
4e3905c20a Fix for bcrypt hash functions:
Using a global crypt provider is not thread safe, so we need to load
provider via BCryptOpenProvider in ma_hash_new().
2023-08-23 16:18:50 +02:00
Georg Richter
03195a2f30 Fix for CENTOS7:
Since CentOS7 builder still uses gcc4.8, we need to move c99
declarations out of the loop.
2023-08-16 20:11:45 +02:00
Georg Richter
1acb81e11c Build fixes:
- Fixed build error, which was previously introduced by commit
c8ca89112e

- Treat warnings as errors
2023-08-16 14:09:34 +02:00
Georg Richter
c8ca89112e Fix for CONC-659:
When checking for a semi sync indication header, we need also check if
the undocumented session variable @rpl_semi_sync_slave was set.
Otherwise the timestamp of the event could contain values which match
the 2 bytes of the semi sync header.

Since the variable rpl_semi_sync_slave and it's behavior is not documented,
a new option MARIADB_RPL_SEMI_SYNC was added.
2023-08-11 10:14:26 +02:00
Georg Richter
3a255ee3ea Merge branch '3.1' into 3.3 2023-07-24 11:07:05 +02:00
Lawrin Novitsky
3393fe35d3 Fixes for debug exceptions with runtime checks eanbled with VS /RTCc option
This could be the cases of casting to smaler types with loss of data.
The fix adds bitwise add with correspondent number of 0xff bytes.
2023-07-23 15:27:30 +02:00
Georg Richter
681fbd98ee Fix for CONC-657:
Since extra_data (MySQL ROWSV2 event) is not part of row data,
we need to allocate memory for it's content.

Kudos to Sruli Ganor for reporting this issue and providing a fix.
2023-07-21 13:42:19 +02:00
Georg Richter
8ab517cbc1 Use OPT_SET_EXT_VALUE macro instead of assigning value
directly.
2023-07-13 10:58:15 +02:00
Georg Richter
45feebb99d Remove server certification verification
Since the server certification option is used by client
only, there is no need to have this flag in server and or
client capabilities. The server itself validates client
certificate depending on the user definition.
2023-07-13 09:30:33 +02:00
Marko Mäkelä
5af90f00ff Merge 3.1 into 3.3 2023-06-26 16:28:51 +03:00
Marko Mäkelä
d543bed61b Fix GCC 13 -Wmaybe-uninitialized 2023-06-26 10:59:14 +03:00
Georg Richter
56b515648b Fix for CONC-650:
Added support for XID in GTID_EVENT (but also updated
documentation on https://mariadb.com/kb/en/gtid_event/).
2023-06-19 12:17:27 +02:00
Georg Richter
ec3852338f Removed unnecessary RPL_CHECK_POS checks. 2023-06-09 14:37:29 +02:00
Marko Mäkelä
238cec4e2a Fix clang -Wempty-body
Fixes up 4dca917b7e
2023-05-29 08:30:01 +03:00
Georg Richter
85b7bde184 Merge branch '3.1' into 3.3 2023-05-02 18:01:34 +02:00
Georg Richter
a3bba4639f CONC-619: NULL pointer dereference in unpack_fields
Fixed NULL pointer dereference, thanks to Yury Chaikou which
reported this issue.
2023-05-02 17:59:18 +02:00
Georg Richter
374f0eedc2 Build fix: replace crc32_z by crc32
To make internal builders with zlib < 1.2.8 happy,
we use crc32() instead of crc32_z now.
2023-04-27 09:24:16 +02:00
Georg Richter
dd8962a4b5 Merge branch '3.1' into 3.3 2023-04-25 15:08:28 +02:00
Georg Richter
0e452f66ac MariaDB Server detection fix
Use mariadb_connection() instead of checking 5.5.5 rpl hack
to detect if we are connected to a MariaDB Server.
2023-04-21 07:11:29 +02:00
Georg Richter
2000b06183 Replace SET_CLIENT_STMT_ERROR by stmt_set_error(). 2023-04-11 18:31:19 +02:00
Georg Richter
e82ec9b027 Merge branch '3.1' into 3.3 2023-03-30 13:36:29 +02:00
Georg Richter
17d4f38403 Fix for CONC-642: Set CR_OUT_OF_MEMORY error
Set CR_OUT_OF_MEMORY error in mysql_use_result() api function
if allocation of memory failed.
2023-03-30 13:30:53 +02:00
Georg Richter
4de47e64a5 rpl/binlog api fixes:
- Store artifical checksum in event->checksum instead of raw.
- Fixed build when builing with external zlib library.
2023-03-07 09:58:51 +01:00
Georg Richter
fe405c4fc4 Fixed conversion warning (windows). 2023-02-28 07:41:56 +01:00
Georg Richter
86b903a5e6 Windows build fixes 2023-02-27 17:19:35 +01:00
Georg Richter
bdf6fbb4f6 Replication/Binlog API fix:
When sending filename in SEMI_SYNC_REQUEST to the server, we don't
need to send additional terminating zero in filename.
See https://mariadb.com/kb/en/4-semi-sync-replication/
2023-02-27 10:23:39 +01:00
Georg Richter
ea74aa294f fix for replication/binlog api:
If flag BINLOG_DUMP_NON_BLOCK was set, we need to check the replica_id.
If it was not specified, a default value of 1 will be used.
2023-02-27 10:13:57 +01:00