Skip to content

Commit 0685d44

Browse files
author
Nirbhay Choubey
committed
Bug #14513708 .MYLOGIN.CNF SHOULD BE IGNORED IF ITS
NOT EXCLUSIVELY READ/WRITABLE BY CURR. USER In the option handling system, the file permissions for login file were being checked like other option files. Added a check separately for login file to make sure that it gets ignored if its not exclusively readable/ writable by current user. Also moved the permissions- checking code to a new function. Manually tested.
1 parent eb1b060 commit 0685d44

File tree

2 files changed

+52
-22
lines changed

2 files changed

+52
-22
lines changed

client/mysql_config_editor.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,6 @@ static my_bool check_and_create_login_file(void)
366366

367367
const int access_flag= (O_RDWR | O_BINARY);
368368
const ushort create_mode= (S_IRUSR | S_IWUSR );
369-
const ushort create_mode_all= (S_IRWXU | S_IRWXG | S_IRWXO);
370369

371370
/* Get the login file name. */
372371
if (! my_default_get_login_file(my_login_file, sizeof(my_login_file)))
@@ -418,7 +417,7 @@ static my_bool check_and_create_login_file(void)
418417
#ifdef _WIN32
419418
if (1)
420419
#else
421-
if (!(create_mode ^ (stat_info.st_mode & create_mode_all)))
420+
if (!(stat_info.st_mode & (S_IXUSR | S_IRWXG | S_IRWXO)))
422421
#endif
423422
{
424423
verbose_msg("File has the required permission.\nOpening the file.\n");

mysys_ssl/my_default.cc

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ static int search_default_file_with_ext(Process_option_func func,
155155
const char *dir, const char *ext,
156156
const char *config_file, int recursion_level);
157157
static my_bool mysql_file_getline(char *str, int size, MYSQL_FILE *file);
158+
static int check_file_permissions(const char *file_name);
158159

159160

160161
/**
@@ -861,7 +862,7 @@ static int search_default_file_with_ext(Process_option_func opt_handler,
861862
MYSQL_FILE *fp;
862863
uint line=0;
863864
my_bool found_group=0;
864-
uint i;
865+
uint i, rc;
865866
MY_DIR *search_dir;
866867
FILEINFO *search_file;
867868

@@ -879,25 +880,10 @@ static int search_default_file_with_ext(Process_option_func opt_handler,
879880
strmov(name,config_file);
880881
}
881882
fn_format(name,name,"","",4);
882-
#if !defined(__WIN__)
883-
{
884-
MY_STAT stat_info;
885-
if (!my_stat(name,&stat_info,MYF(0)))
886-
return 1;
887-
/*
888-
Ignore world-writable regular files.
889-
This is mainly done to protect us to not read a file created by
890-
the mysqld server, but the check is still valid in most context.
891-
*/
892-
if ((stat_info.st_mode & S_IWOTH) &&
893-
(stat_info.st_mode & S_IFMT) == S_IFREG)
894-
{
895-
fprintf(stderr, "Warning: World-writable config file '%s' is ignored\n",
896-
name);
897-
return 0;
898-
}
899-
}
900-
#endif
883+
884+
if ((rc= check_file_permissions(name)) < 2)
885+
return (int) rc;
886+
901887
if (is_login_file)
902888
{
903889
if ( !(fp = mysql_file_fopen(key_file_cnf, name, (O_RDONLY | O_BINARY),
@@ -1464,3 +1450,48 @@ int my_default_get_login_file(char *file_name, size_t file_name_size)
14641450

14651451
return 1;
14661452
}
1453+
1454+
/**
1455+
Check file permissions of the option file.
1456+
1457+
@param file_name [in] Name of the option file.
1458+
1459+
@return 0 - Non-allowable file permissions.
1460+
1 - Failed to stat.
1461+
2 - Success.
1462+
*/
1463+
static int check_file_permissions(const char *file_name)
1464+
{
1465+
#if !defined(__WIN__)
1466+
MY_STAT stat_info;
1467+
1468+
if (!my_stat(file_name,&stat_info,MYF(0)))
1469+
return 1;
1470+
/*
1471+
Ignore .mylogin.cnf file if not exclusively readable/writable
1472+
by current user.
1473+
*/
1474+
if (is_login_file && (stat_info.st_mode & (S_IXUSR | S_IRWXG | S_IRWXO))
1475+
&& (stat_info.st_mode & S_IFMT) == S_IFREG)
1476+
{
1477+
fprintf(stderr, "Warning: %s should be readable/writable only by "
1478+
"current user.\n", file_name);
1479+
return 0;
1480+
}
1481+
/*
1482+
Ignore world-writable regular files.
1483+
This is mainly done to protect us to not read a file created by
1484+
the mysqld server, but the check is still valid in most context.
1485+
*/
1486+
else if ((stat_info.st_mode & S_IWOTH) &&
1487+
(stat_info.st_mode & S_IFMT) == S_IFREG)
1488+
1489+
{
1490+
fprintf(stderr, "Warning: World-writable config file '%s' is ignored\n",
1491+
file_name);
1492+
return 0;
1493+
}
1494+
#endif
1495+
return 2; /* Success */
1496+
}
1497+

0 commit comments

Comments
 (0)