MySQL 9.5.0
Source Code Documentation
secure_file.h
Go to the documentation of this file.
1/* Copyright (c) 2025, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24/**
25 @file include/secure_file.h
26*/
27
28#include "my_sys.h"
30#include "nulls.h"
31
32/**
33 Test a file path to determine if the path is compatible with the secure file
34 path restriction.
35
36 @param path null terminated character string
37 @param opt_secure_file_priv secure_file_priv content
38 @param system_charset_info system charset
39 @param files_charset_info files charset
40 @param lower_case_file_system true if lower case file system, false otherwise
41
42 @retval true The path is secure
43 @retval false The path isn't secure
44*/
45
46bool is_secure_file_path(const char *path, const char *opt_secure_file_priv,
50 char buff1[FN_REFLEN], buff2[FN_REFLEN];
51 size_t opt_secure_file_priv_len;
52 /*
53 All paths are secure if opt_secure_file_priv is 0
54 */
55 if (!opt_secure_file_priv[0]) return true;
56
57 opt_secure_file_priv_len = strlen(opt_secure_file_priv);
58
59 if (strlen(path) >= FN_REFLEN) return false;
60
62 return false;
63
64 if (my_realpath(buff1, path, 0)) {
65 /*
66 The supplied file path might have been a file and not a directory.
67 */
68 const int length = (int)dirname_length(path);
69 if (length >= FN_REFLEN) return false;
70 memcpy(buff2, path, length);
71 buff2[length] = '\0';
72 if (length == 0 || my_realpath(buff1, buff2, 0)) return false;
73 }
74 convert_dirname(buff2, buff1, NullS);
76 if (strncmp(opt_secure_file_priv, buff2, opt_secure_file_priv_len))
77 return false;
78 } else {
79 assert(opt_secure_file_priv_len < FN_REFLEN);
80 buff2[opt_secure_file_priv_len] = '\0';
83 return false;
84 }
85 return true;
86}
char * convert_dirname(char *to, const char *from, const char *from_end)
Convert directory name to use under this system.
Definition: mf_dirname.cc:207
size_t dirname_length(const char *name)
Get the string length of the directory part of name, including the last FN_LIBCHAR.
Definition: mf_dirname.cc:62
int my_realpath(char *to, const char *filename, myf MyFlags)
Definition: my_symlink.cc:139
A better implementation of the UNIX ctype(3) library.
int my_strcasecmp(const CHARSET_INFO *cs, const char *s1, const char *s2)
Definition: m_ctype.h:651
MYSQL_PLUGIN_IMPORT CHARSET_INFO * system_charset_info
Definition: mysqld.cc:1574
#define FN_REFLEN
Definition: my_io.h:87
Common header for many mysys elements.
bool lower_case_file_system
Definition: mysqld.cc:1300
const char * opt_secure_file_priv
Definition: mysqld.cc:1297
static char * path
Definition: mysqldump.cc:150
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
#define NullS
Definition of the null string (a null pointer of type char *), used in some of our string handling co...
Definition: nulls.h:33
bool is_secure_file_path(const char *path, const char *opt_secure_file_priv, CHARSET_INFO *system_charset_info, CHARSET_INFO *files_charset_info, bool lower_case_file_system)
Test a file path to determine if the path is compatible with the secure file path restriction.
Definition: secure_file.h:46
MYSQL_PLUGIN_IMPORT CHARSET_INFO * files_charset_info
Definition: mysqld.cc:1574
Definition: m_ctype.h:421
MY_COLLATION_HANDLER * coll
Definition: m_ctype.h:454
int(* strcasecmp)(const CHARSET_INFO *, const char *, const char *)
Definition: m_ctype.h:310