summaryrefslogtreecommitdiff
path: root/extra
diff options
context:
space:
mode:
authorMikhail Chalov <mcchalov@amazon.com>2022-11-15 14:39:30 -0800
committerVicențiu Ciorbaru <cvicentiu@gmail.com>2023-01-20 15:18:52 +0200
commit567b68129943a1cceab1d7b4c68e2a4ba011cdc0 (patch)
tree310e63987deb21ca563cfb86a2cf461f5b9ca133 /extra
parentea270178b09bb1e2e1131957e95f36cd1f611b22 (diff)
downloadmariadb-git-567b68129943a1cceab1d7b4c68e2a4ba011cdc0.tar.gz
Minimize unsafe C functions usage - replace strcat() and strcpy() (and strncat() and strncpy()) with custom safe_strcat() and safe_strcpy() functions
The MariaDB code base uses strcat() and strcpy() in several places. These are known to have memory safety issues and their usage is discouraged. Common security scanners like Flawfinder flags them. In MariaDB we should start using modern and safer variants on these functions. This is similar to memory issues fixes in 19af1890b56c6c147c296479bb6a4ad00fa59dbb and 9de9f105b5cb88249acc39af73d32af337d6fd5f but now replace use of strcat() and strcpy() with safer options strncat() and strncpy(). However, add '\0' forcefully to make sure the result string is correct since for these two functions it is not guaranteed what new string will be null-terminated. Example: size_t dest_len = sizeof(g->Message); strncpy(g->Message, "Null json tree", dest_len); strncat(g->Message, ":", sizeof(g->Message) - strlen(g->Message)); size_t wrote_sz = strlen(g->Message); size_t cur_len = wrote_sz >= dest_len ? dest_len - 1 : wrote_sz; g->Message[cur_len] = '\0'; 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 -- Reviewer and co-author Vicențiu Ciorbaru <vicentiu@mariadb.org> -- Reviewer additions: * The initial function implementation was flawed. Replaced with a simpler and also correct version. * Simplified code by making use of snprintf instead of chaining strcat. * Simplified code by removing dynamic string construction in the first place and using static strings if possible. See connect storage engine changes.
Diffstat (limited to 'extra')
-rw-r--r--extra/innochecksum.cc8
-rw-r--r--extra/mariabackup/xbcloud.cc7
-rw-r--r--extra/mariabackup/xtrabackup.cc11
3 files changed, 14 insertions, 12 deletions
diff --git a/extra/innochecksum.cc b/extra/innochecksum.cc
index 3169b647ea2..a65114a1679 100644
--- a/extra/innochecksum.cc
+++ b/extra/innochecksum.cc
@@ -844,7 +844,7 @@ parse_page(
{
unsigned long long id;
uint16_t undo_page_type;
- char str[20]={'\0'};
+ const char *str;
ulint n_recs;
uint32_t page_no, left_page_no, right_page_no;
ulint data_bytes;
@@ -852,11 +852,7 @@ parse_page(
ulint size_range_id;
/* Check whether page is doublewrite buffer. */
- if(skip_page) {
- strcpy(str, "Double_write_buffer");
- } else {
- strcpy(str, "-");
- }
+ str = skip_page ? "Double_write_buffer" : "-";
switch (mach_read_from_2(page + FIL_PAGE_TYPE)) {
diff --git a/extra/mariabackup/xbcloud.cc b/extra/mariabackup/xbcloud.cc
index fed937be834..cee76e5f3d7 100644
--- a/extra/mariabackup/xbcloud.cc
+++ b/extra/mariabackup/xbcloud.cc
@@ -1676,8 +1676,11 @@ container_list_add_object(container_list *list, const char *name,
list->object_count += object_count_step;
}
assert(list->idx <= list->object_count);
- strcpy(list->objects[list->idx].name, name);
- strcpy(list->objects[list->idx].hash, hash);
+ safe_strcpy(list->objects[list->idx].name,
+ sizeof(list->objects[list->idx].name), name);
+ safe_strcpy(list->objects[list->idx].hash,
+ sizeof(list->objects[list->idx].hash), hash);
+
list->objects[list->idx].bytes = bytes;
++list->idx;
}
diff --git a/extra/mariabackup/xtrabackup.cc b/extra/mariabackup/xtrabackup.cc
index f7910fc9c23..ee954919a55 100644
--- a/extra/mariabackup/xtrabackup.cc
+++ b/extra/mariabackup/xtrabackup.cc
@@ -4235,11 +4235,13 @@ static bool xtrabackup_backup_low()
dst_log_file = NULL;
- if(!xtrabackup_incremental) {
- strcpy(metadata_type, "full-backuped");
+ if (!xtrabackup_incremental) {
+ safe_strcpy(metadata_type, sizeof(metadata_type),
+ "full-backuped");
metadata_from_lsn = 0;
} else {
- strcpy(metadata_type, "incremental");
+ safe_strcpy(metadata_type, sizeof(metadata_type),
+ "incremental");
metadata_from_lsn = incremental_lsn;
}
metadata_last_lsn = log_copy_scanned_lsn;
@@ -5987,7 +5989,8 @@ static bool xtrabackup_prepare_func(char** argv)
if (ok) {
char filename[FN_REFLEN];
- strcpy(metadata_type, "log-applied");
+ safe_strcpy(metadata_type, sizeof(metadata_type),
+ "log-applied");
if(xtrabackup_incremental
&& metadata_to_lsn < incremental_to_lsn)