← Back to Pull requests

MDEV-41000 LOAD_DATA fails to warn/errror if insufficient permissions on file #6296

Open
SamarthPD-21 opened this pull request with 401 lines diff across 30 files
View on GitHub
SamarthPD-21 SamarthPD-21 commented on Dec 01, 2026 11:59 UTC
Author

Description


LOAD_FILE() returned NULL with no diagnostic in several cases. Following the design agreed on the Jira issue:

  • The file is opened first and its size taken with fstat, instead of a stat before the open (TOCTOU). The world-readable (S_IROTH) check is removed.

  • Open/read failures push a warning and still return NULL; strict mode turns it into an error inside INSERT/UPDATE, as it already does for the max_allowed_packet warning. my_open() and my_read() now pass ME_WARNING through, so this is done with MY_WME | ME_WARNING.

  • A missing FILE privilege and a path outside secure_file_priv are errors.

  • The file is closed on all error paths.
  • Side effect: a path whose directory does not exist fails is_secure_file_path() when secure_file_priv is set, so it now gets the secure_file_priv error (main.partition_error, main.loaddata).

    How can this PR be tested?


    New test main.load_file_errors. Updated results for the new warnings/errors: main.func_str, main.group_by, main.having_cond_pushdown, main.outfile, main.loaddata, main.partition_error, json.json_table. All tests using LOAD_FILE pass locally, also with --ps-protocol.

    Basing the PR against the correct MariaDB version


  • This is a bug fix, and the PR is based against the earliest maintained branch in which the bug can be reproduced.
  • Diff: 401 lines (+312 / -89) across 30 files Reviewer: grooverdan Review duration: 7403m
    All Vouch review confidence checks passed
    Residual risk 0.07 = Change Risk (0.91) × (1 − Review Confidence (0.92)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.91 · 401 lines diff across 30 files
    0.91
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.93 · 5 substantive comment(s)
    0.93
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    92%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.359
    Sensitive Paths sensitive_paths
    0.091
    Mass File Touch mass_file_touch
    0.080
    Real PR Conversation Timeline
    41 items
    grooverdan grooverdan commented on code on Dec 01, 2026 22:47 UTC
    Code Review
    sql/item_strfunc.cc:1
    @@ -4396,59 +4396,58 @@ String *Item_load_file::val_str(String *str)
       MY_STAT stat_info;
       char path[FN_REFLEN];
       ulonglong file_size;
    +  THD *thd= current_thd;
       DBUG_ENTER("load_file");
     
    -  if (!(file_name= args[0]->val_str(str))
    +  if (!(file_name= args[0]->val_str(str)))
    +    goto err;
    +
     #ifndef NO_EMBEDDED_ACCESS_CHECKS
    -      || !(current_thd->security_ctx->master_access & FILE_ACL)
    -#endif
    -      )
    +  if (!(thd->security_ctx->master_access & FILE_ACL))
    +  {
    +    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "FILE");

    would be "LOAD_FILE" as last arg.

    grooverdan grooverdan commented on code on Dec 01, 2026 22:48 UTC
    Code Review
    mysys/my_open.c:1
    @@ -154,7 +154,7 @@ File my_register_filename(File fd, const char *FileName, enum file_type
         if (my_errno == EMFILE)
           error_message_number= EE_OUT_OF_FILERESOURCES;
         my_error(error_message_number,
    -             MYF(ME_BELL | (MyFlags & (ME_NOTE | ME_ERROR_LOG))),
    +             MYF(ME_BELL | (MyFlags & (ME_NOTE | ME_WARNING | ME_ERROR_LOG))),

    good catch!

    grooverdan grooverdan commented on code on Dec 01, 2026 23:00 UTC
    Code Review
    sql/item_strfunc.cc:1
    @@ -4396,59 +4396,58 @@ String *Item_load_file::val_str(String *str)
       MY_STAT stat_info;
       char path[FN_REFLEN];
       ulonglong file_size;
    +  THD *thd= current_thd;
       DBUG_ENTER("load_file");
     
    -  if (!(file_name= args[0]->val_str(str))
    +  if (!(file_name= args[0]->val_str(str)))
    +    goto err;
    +
     #ifndef NO_EMBEDDED_ACCESS_CHECKS
    -      || !(current_thd->security_ctx->master_access & FILE_ACL)
    -#endif
    -      )
    +  if (!(thd->security_ctx->master_access & FILE_ACL))
    +  {
    +    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "FILE");
         goto err;
    +  }
    +#endif
     
       (void) fn_format(path, file_name->c_ptr_safe(), mysql_real_data_home, "",
                        MY_RETURN_REAL_PATH | MY_UNPACK_FILENAME);
     
       /* Read only allowed from within dir specified by secure_file_priv */
       if (!is_secure_file_path(path))
    +  {
    +    my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--secure-file-priv");
         goto err;
    +  }
     
    -  if (!mysql_file_stat(key_file_loadfile, path, &stat_info, MYF(0)))
    +  if ((file= mysql_file_open(key_file_loadfile, path, O_RDONLY,
    +                             MYF(MY_NOSYMLINKS | MY_WME | ME_WARNING))) < 0)
         goto err;
     
    -  if (!(stat_info.st_mode & S_IROTH))
    -  {
    -    /* my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr()); */
    -    goto err;
    -  }
    +  if (mysql_file_fstat(file, &stat_info, MYF(MY_WME)))
    +    goto err_close;
       file_size= stat_info.st_size;
     
    +  if (file_size >= thd->variables.max_allowed_packet)
       {
    -    THD *thd= current_thd;
    -    if (file_size >= thd->variables.max_allowed_packet)
    -    {
    -      push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
    -                          ER_WARN_ALLOWED_PACKET_OVERFLOWED,
    -                          ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
    -                          func_name(), thd->variables.max_allowed_packet);
    -      goto err;
    -    }
    +    push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
    +                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
    +                        ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
    +                        func_name(), thd->variables.max_allowed_packet);
    +    goto err_close;
       }

    mysql_file_read returns the filesize too.

    so cases are here:
    0, is doubling as 0 bytes read and error because of mappings - MDEV-32568: Fix wrong use of my_read() return type. - #2801 - best keep as error for now
    < file_size - probably ok, as just means the file shrunk between fstat and read, Will need to use the return value for the tmp_value.length()
    = file_size - ok, it never read read more than what was specified.
    which leads to there may have been a file size increase after fstat but before read. - options - and I'm happy with any of them:
    1. ignore and leave for another bug, perhaps leaving a code comment in the mean time
    2. replace with while loop that terminates on 0 bytes read and increases the buffer, and maps it against the max packet size to ensure that isn't exceeded.
    3. push a note/warning on this, (but would need to allocate an extra byte and add 1 to args to be able to detect this case).

    I appreciate that writing test cases for any of the non-exceptional cases here is hard and would required a debug syncpoint, so as long as the common case is handled.

    grooverdan grooverdan commented on code on Dec 01, 2026 23:03 UTC
    Code Review
    mysql-test/main/group_by.test:1
    @@ -2057,9 +2057,11 @@ drop table t1;
     #
     
     CREATE TABLE t1 ( pk int, i1 int, v1 varchar(1));
    +--replace_regex /'[^']*\/data/'MYSQLD_DATADIR/

    given these tests are about the behaviour rather than the error a LOAD_DATA(NULL) I think was the quick path through that could be used instead of the non-existent file a.

    grooverdan grooverdan commented on code on Dec 01, 2026 23:04 UTC
    Code Review
    mysql-test/main/having_cond_pushdown.test:1
    @@ -1747,6 +1747,7 @@ DROP TABLE t1;
     
     CREATE TABLE t1 (a CHAR(3)) CHARSET=sjis;
     INSERT INTO t1 VALUES ('foo'),('bar');
    +--replace_regex /'[^']*\/data/'MYSQLD_DATADIR/

    if LOAD_DATA(NULL) is ok use it here too.

    grooverdan grooverdan commented on code on Dec 01, 2026 23:08 UTC
    Code Review
    mysql-test/main/partition_error.test:1
    @@ -948,6 +948,7 @@ partitions 3
      partition x2 tablespace ts2,
      partition x3 tablespace ts3);
     
    +--error ER_OPTION_PREVENTS_STATEMENT

    I don't know why this is succeeding on Windows. Ask if stuck.

    grooverdan grooverdan requested changes on Dec 01, 2026 23:31 UTC
    Changes Requested Reviewer

    Overall, good implementation. Tests need a bit of work

    Regex doesn't work on Windows replacing data dir ( \ as path separator
    Probably simplest to truncate the Errno:.... from the output due to platform differences in the actual code/message.

    is_secure_file_path has been significantly changed recently, so it could be the cause of some of the windows partition_error test result differences.

    grooverdan grooverdan commented on code on Dec 01, 2026 23:32 UTC
    Code Review
    sql/item_strfunc.cc:1
    @@ -4396,59 +4396,58 @@ String *Item_load_file::val_str(String *str)
       MY_STAT stat_info;
       char path[FN_REFLEN];
       ulonglong file_size;
    +  THD *thd= current_thd;
       DBUG_ENTER("load_file");
     
    -  if (!(file_name= args[0]->val_str(str))
    +  if (!(file_name= args[0]->val_str(str)))
    +    goto err;
    +
     #ifndef NO_EMBEDDED_ACCESS_CHECKS
    -      || !(current_thd->security_ctx->master_access & FILE_ACL)
    -#endif
    -      )
    +  if (!(thd->security_ctx->master_access & FILE_ACL))
    +  {
    +    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "FILE");
         goto err;
    +  }
    +#endif
     
       (void) fn_format(path, file_name->c_ptr_safe(), mysql_real_data_home, "",
                        MY_RETURN_REAL_PATH | MY_UNPACK_FILENAME);
     
       /* Read only allowed from within dir specified by secure_file_priv */
       if (!is_secure_file_path(path))
    +  {
    +    my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--secure-file-priv");
         goto err;
    +  }
     
    -  if (!mysql_file_stat(key_file_loadfile, path, &stat_info, MYF(0)))
    +  if ((file= mysql_file_open(key_file_loadfile, path, O_RDONLY,
    +                             MYF(MY_NOSYMLINKS | MY_WME | ME_WARNING))) < 0)
         goto err;
     
    -  if (!(stat_info.st_mode & S_IROTH))
    -  {
    -    /* my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr()); */
    -    goto err;
    -  }
    +  if (mysql_file_fstat(file, &stat_info, MYF(MY_WME)))
    +    goto err_close;
       file_size= stat_info.st_size;
     
    +  if (file_size >= thd->variables.max_allowed_packet)
       {
    -    THD *thd= current_thd;
    -    if (file_size >= thd->variables.max_allowed_packet)
    -    {
    -      push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
    -                          ER_WARN_ALLOWED_PACKET_OVERFLOWED,
    -                          ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
    -                          func_name(), thd->variables.max_allowed_packet);
    -      goto err;
    -    }
    +    push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
    +                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
    +                        ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
    +                        func_name(), thd->variables.max_allowed_packet);
    +    goto err_close;
       }

    for some reason github reviewing put this a few lines too early - applies to mysql_file_read and that condition.

    gkodinov gkodinov commented on code on Dec 02, 2026 09:52 UTC
    Code Review
    mysys/my_read.c:1
    @@ -90,11 +90,11 @@ size_t my_read(File Filedes, uchar *Buffer, size_t Count, myf MyFlags)
           {
             if (readbytes == (size_t) -1)
               my_error(EE_READ,
    -                   MYF(ME_BELL | (MyFlags & (ME_NOTE | ME_ERROR_LOG))),
    +                   MYF(ME_BELL | (MyFlags & (ME_NOTE | ME_WARNING | ME_ERROR_LOG))),

    I doubt this is correct at this level!

    gkodinov gkodinov commented on code on Dec 02, 2026 09:53 UTC
    Code Review
    mysys/my_open.c:1
    @@ -154,7 +154,7 @@ File my_register_filename(File fd, const char *FileName, enum file_type
         if (my_errno == EMFILE)
           error_message_number= EE_OUT_OF_FILERESOURCES;
         my_error(error_message_number,
    -             MYF(ME_BELL | (MyFlags & (ME_NOTE | ME_ERROR_LOG))),
    +             MYF(ME_BELL | (MyFlags & (ME_NOTE | ME_WARNING | ME_ERROR_LOG))),

    Not sure this is the correct approach, especially at this level. MyFlags are passed down already. Use these instead.

    gkodinov gkodinov commented on code on Dec 02, 2026 09:57 UTC
    Code Review
    sql/item_strfunc.cc:1
    @@ -4396,59 +4396,58 @@ String *Item_load_file::val_str(String *str)
       MY_STAT stat_info;
       char path[FN_REFLEN];
       ulonglong file_size;
    +  THD *thd= current_thd;
       DBUG_ENTER("load_file");
     
    -  if (!(file_name= args[0]->val_str(str))
    +  if (!(file_name= args[0]->val_str(str)))
    +    goto err;
    +
     #ifndef NO_EMBEDDED_ACCESS_CHECKS
    -      || !(current_thd->security_ctx->master_access & FILE_ACL)
    -#endif
    -      )
    +  if (!(thd->security_ctx->master_access & FILE_ACL))
    +  {
    +    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "FILE");
         goto err;
    +  }
    +#endif
     
       (void) fn_format(path, file_name->c_ptr_safe(), mysql_real_data_home, "",
                        MY_RETURN_REAL_PATH | MY_UNPACK_FILENAME);
     
       /* Read only allowed from within dir specified by secure_file_priv */
       if (!is_secure_file_path(path))
    +  {
    +    my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--secure-file-priv");

    You do not want to say to the end user that something failed due to --secure-file-priv. This could be abused to try to circumvent this option and its protection. This is covered by MITRE CWE-209: Generation of Error Message Containing Sensitive Information.

    gkodinov gkodinov requested changes on Dec 02, 2026 09:59 UTC
    Changes Requested Reviewer

    Thank you for your contribution! This is a preliminary review.

    I believe the premise of this bug is questionable. See my comments below.

    I will add our security architect as a reviewer to this.

    SamarthPD-21 SamarthPD-21 commented on code on Dec 02, 2026 11:17 UTC
    Code Review
    mysys/my_read.c:1
    @@ -90,11 +90,11 @@ size_t my_read(File Filedes, uchar *Buffer, size_t Count, myf MyFlags)
           {
             if (readbytes == (size_t) -1)
               my_error(EE_READ,
    -                   MYF(ME_BELL | (MyFlags & (ME_NOTE | ME_ERROR_LOG))),
    +                   MYF(ME_BELL | (MyFlags & (ME_NOTE | ME_WARNING | ME_ERROR_LOG))),

    The mask there drops ME_WARNING, so MY_WME | ME_WARNING from LOAD_FILE still ends up as an error. Do you mean I should not pass MY_WME and instead push the warning in Item_load_file::val_str() from my_errno? Happy to do that.

    SamarthPD-21 SamarthPD-21 commented on code on Dec 02, 2026 11:19 UTC
    Code Review
    sql/item_strfunc.cc:1
    @@ -4396,59 +4396,58 @@ String *Item_load_file::val_str(String *str)
       MY_STAT stat_info;
       char path[FN_REFLEN];
       ulonglong file_size;
    +  THD *thd= current_thd;
       DBUG_ENTER("load_file");
     
    -  if (!(file_name= args[0]->val_str(str))
    +  if (!(file_name= args[0]->val_str(str)))
    +    goto err;
    +
     #ifndef NO_EMBEDDED_ACCESS_CHECKS
    -      || !(current_thd->security_ctx->master_access & FILE_ACL)
    -#endif
    -      )
    +  if (!(thd->security_ctx->master_access & FILE_ACL))
    +  {
    +    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "FILE");
         goto err;
    +  }
    +#endif
     
       (void) fn_format(path, file_name->c_ptr_safe(), mysql_real_data_home, "",
                        MY_RETURN_REAL_PATH | MY_UNPACK_FILENAME);
     
       /* Read only allowed from within dir specified by secure_file_priv */
       if (!is_secure_file_path(path))
    +  {
    +    my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--secure-file-priv");

    Fair point. Daniel suggested an error here on the Jira issue, so I'll wait for @vuvova . Would you prefer returning NULL silently as before for this case, or a generic access denied error? CI also shows the embedded locale tests relying on the old NULL for a relative path.

    gkodinov gkodinov commented on code on Dec 02, 2026 11:19 UTC
    Code Review
    mysys/my_read.c:1
    @@ -90,11 +90,11 @@ size_t my_read(File Filedes, uchar *Buffer, size_t Count, myf MyFlags)
           {
             if (readbytes == (size_t) -1)
               my_error(EE_READ,
    -                   MYF(ME_BELL | (MyFlags & (ME_NOTE | ME_ERROR_LOG))),
    +                   MYF(ME_BELL | (MyFlags & (ME_NOTE | ME_WARNING | ME_ERROR_LOG))),

    I do not think you should be manipulating the flags passed that deep down. Your change basically makes ME_WARNING a no-op. And I don't think this is right.

    grooverdan grooverdan commented on code on Dec 02, 2026 11:20 UTC
    Code Review
    sql/item_strfunc.cc:1
    @@ -4396,59 +4396,58 @@ String *Item_load_file::val_str(String *str)
       MY_STAT stat_info;
       char path[FN_REFLEN];
       ulonglong file_size;
    +  THD *thd= current_thd;
       DBUG_ENTER("load_file");
     
    -  if (!(file_name= args[0]->val_str(str))
    +  if (!(file_name= args[0]->val_str(str)))
    +    goto err;
    +
     #ifndef NO_EMBEDDED_ACCESS_CHECKS
    -      || !(current_thd->security_ctx->master_access & FILE_ACL)
    -#endif
    -      )
    +  if (!(thd->security_ctx->master_access & FILE_ACL))
    +  {
    +    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "FILE");
         goto err;
    +  }
    +#endif
     
       (void) fn_format(path, file_name->c_ptr_safe(), mysql_real_data_home, "",
                        MY_RETURN_REAL_PATH | MY_UNPACK_FILENAME);
     
       /* Read only allowed from within dir specified by secure_file_priv */
       if (!is_secure_file_path(path))
    +  {
    +    my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--secure-file-priv");

    To be fair, its already some with FILE privs, and its not disclosing the secure path, like select @@secure_file_priv`, which they have access to.

    SamarthPD-21 SamarthPD-21 commented on code on Dec 02, 2026 11:25 UTC
    Code Review
    mysys/my_read.c:1
    @@ -90,11 +90,11 @@ size_t my_read(File Filedes, uchar *Buffer, size_t Count, myf MyFlags)
           {
             if (readbytes == (size_t) -1)
               my_error(EE_READ,
    -                   MYF(ME_BELL | (MyFlags & (ME_NOTE | ME_ERROR_LOG))),
    +                   MYF(ME_BELL | (MyFlags & (ME_NOTE | ME_WARNING | ME_ERROR_LOG))),

    OK, I'll revert the mysys changes and raise the warning in Item_load_file::val_str() with my_error(..., MYF(ME_WARNING)) and my_errno instead.

    SamarthPD-21 SamarthPD-21 commented on code on Dec 02, 2026 11:26 UTC
    Code Review
    sql/item_strfunc.cc:1
    @@ -4396,59 +4396,58 @@ String *Item_load_file::val_str(String *str)
       MY_STAT stat_info;
       char path[FN_REFLEN];
       ulonglong file_size;
    +  THD *thd= current_thd;
       DBUG_ENTER("load_file");
     
    -  if (!(file_name= args[0]->val_str(str))
    +  if (!(file_name= args[0]->val_str(str)))
    +    goto err;
    +
     #ifndef NO_EMBEDDED_ACCESS_CHECKS
    -      || !(current_thd->security_ctx->master_access & FILE_ACL)
    -#endif
    -      )
    +  if (!(thd->security_ctx->master_access & FILE_ACL))
    +  {
    +    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "FILE");

    ER_SPECIFIC_ACCESS_DENIED_ERROR reads "you need (at least one of) the %s privilege(s) for this operation", so "LOAD_FILE" would come out as "the LOAD_FILE privilege". Did you mean a different error code, or should it stay "FILE"?

    SamarthPD-21 SamarthPD-21 commented on code on Dec 02, 2026 11:27 UTC
    Code Review
    mysys/my_open.c:1
    @@ -154,7 +154,7 @@ File my_register_filename(File fd, const char *FileName, enum file_type
         if (my_errno == EMFILE)
           error_message_number= EE_OUT_OF_FILERESOURCES;
         my_error(error_message_number,
    -             MYF(ME_BELL | (MyFlags & (ME_NOTE | ME_ERROR_LOG))),
    +             MYF(ME_BELL | (MyFlags & (ME_NOTE | ME_WARNING | ME_ERROR_LOG))),

    Joro preferred not touching the flags in mysys, so I reverted this. The warning is now raised in Item_load_file::val_str() with ME_WARNING and my_errno, and gives the same message.

    SamarthPD-21 SamarthPD-21 commented on code on Dec 02, 2026 11:28 UTC
    Code Review
    sql/item_strfunc.cc:1
    @@ -4396,59 +4396,58 @@ String *Item_load_file::val_str(String *str)
       MY_STAT stat_info;
       char path[FN_REFLEN];
       ulonglong file_size;
    +  THD *thd= current_thd;
       DBUG_ENTER("load_file");
     
    -  if (!(file_name= args[0]->val_str(str))
    +  if (!(file_name= args[0]->val_str(str)))
    +    goto err;
    +
     #ifndef NO_EMBEDDED_ACCESS_CHECKS
    -      || !(current_thd->security_ctx->master_access & FILE_ACL)
    -#endif
    -      )
    +  if (!(thd->security_ctx->master_access & FILE_ACL))
    +  {
    +    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "FILE");
         goto err;
    +  }
    +#endif
     
       (void) fn_format(path, file_name->c_ptr_safe(), mysql_real_data_home, "",
                        MY_RETURN_REAL_PATH | MY_UNPACK_FILENAME);
     
       /* Read only allowed from within dir specified by secure_file_priv */
       if (!is_secure_file_path(path))
    +  {
    +    my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--secure-file-priv");
         goto err;
    +  }
     
    -  if (!mysql_file_stat(key_file_loadfile, path, &stat_info, MYF(0)))
    +  if ((file= mysql_file_open(key_file_loadfile, path, O_RDONLY,
    +                             MYF(MY_NOSYMLINKS | MY_WME | ME_WARNING))) < 0)
         goto err;
     
    -  if (!(stat_info.st_mode & S_IROTH))
    -  {
    -    /* my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr()); */
    -    goto err;
    -  }
    +  if (mysql_file_fstat(file, &stat_info, MYF(MY_WME)))
    +    goto err_close;
       file_size= stat_info.st_size;
     
    +  if (file_size >= thd->variables.max_allowed_packet)
       {
    -    THD *thd= current_thd;
    -    if (file_size >= thd->variables.max_allowed_packet)
    -    {
    -      push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
    -                          ER_WARN_ALLOWED_PACKET_OVERFLOWED,
    -                          ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
    -                          func_name(), thd->variables.max_allowed_packet);
    -      goto err;
    -    }
    +    push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
    +                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
    +                        ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
    +                        func_name(), thd->variables.max_allowed_packet);
    +    goto err_close;
       }

    Thanks for the breakdown. I'll keep 0 / -1 as an error and use the return value for tmp_value.length(), so a shrunk file is fine. I'll leave the file growing after fstat for a separate bug.

    SamarthPD-21 SamarthPD-21 commented on code on Dec 02, 2026 11:28 UTC
    Code Review
    mysql-test/main/group_by.test:1
    @@ -2057,9 +2057,11 @@ drop table t1;
     #
     
     CREATE TABLE t1 ( pk int, i1 int, v1 varchar(1));
    +--replace_regex /'[^']*\/data/'MYSQLD_DATADIR/

    Makes sense, will switch it to LOAD_FILE(NULL).

    SamarthPD-21 SamarthPD-21 commented on code on Dec 02, 2026 11:28 UTC
    Code Review
    mysql-test/main/having_cond_pushdown.test:1
    @@ -1747,6 +1747,7 @@ DROP TABLE t1;
     
     CREATE TABLE t1 (a CHAR(3)) CHARSET=sjis;
     INSERT INTO t1 VALUES ('foo'),('bar');
    +--replace_regex /'[^']*\/data/'MYSQLD_DATADIR/

    Will use LOAD_FILE(NULL) here too.

    SamarthPD-21 SamarthPD-21 commented on code on Dec 02, 2026 11:29 UTC
    Code Review
    mysql-test/main/partition_error.test:1
    @@ -948,6 +948,7 @@ partitions 3
      partition x2 tablespace ts2,
      partition x3 tablespace ts3);
     
    +--error ER_OPTION_PREVENTS_STATEMENT

    Those lines pass the literal '$MYSQLD_DATADIR/test/t1.par' (it isn't eval'd), so the path doesn't exist. My guess is the realpath / is_secure_file_path handling differs on Windows for a missing directory. I'll dig in and ask if I get stuck.

    SamarthPD-21 SamarthPD-21 commented on code on Dec 02, 2026 11:40 UTC
    Code Review
    sql/item_strfunc.cc:1
    @@ -4396,59 +4396,58 @@ String *Item_load_file::val_str(String *str)
       MY_STAT stat_info;
       char path[FN_REFLEN];
       ulonglong file_size;
    +  THD *thd= current_thd;
       DBUG_ENTER("load_file");
     
    -  if (!(file_name= args[0]->val_str(str))
    +  if (!(file_name= args[0]->val_str(str)))
    +    goto err;
    +
     #ifndef NO_EMBEDDED_ACCESS_CHECKS
    -      || !(current_thd->security_ctx->master_access & FILE_ACL)
    -#endif
    -      )
    +  if (!(thd->security_ctx->master_access & FILE_ACL))
    +  {
    +    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "FILE");
         goto err;
    +  }
    +#endif
     
       (void) fn_format(path, file_name->c_ptr_safe(), mysql_real_data_home, "",
                        MY_RETURN_REAL_PATH | MY_UNPACK_FILENAME);
     
       /* Read only allowed from within dir specified by secure_file_priv */
       if (!is_secure_file_path(path))
    +  {
    +    my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--secure-file-priv");
         goto err;
    +  }
     
    -  if (!mysql_file_stat(key_file_loadfile, path, &stat_info, MYF(0)))
    +  if ((file= mysql_file_open(key_file_loadfile, path, O_RDONLY,
    +                             MYF(MY_NOSYMLINKS | MY_WME | ME_WARNING))) < 0)
         goto err;
     
    -  if (!(stat_info.st_mode & S_IROTH))
    -  {
    -    /* my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr()); */
    -    goto err;
    -  }
    +  if (mysql_file_fstat(file, &stat_info, MYF(MY_WME)))
    +    goto err_close;
       file_size= stat_info.st_size;
     
    +  if (file_size >= thd->variables.max_allowed_packet)
       {
    -    THD *thd= current_thd;
    -    if (file_size >= thd->variables.max_allowed_packet)
    -    {
    -      push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
    -                          ER_WARN_ALLOWED_PACKET_OVERFLOWED,
    -                          ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
    -                          func_name(), thd->variables.max_allowed_packet);
    -      goto err;
    -    }
    +    push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
    +                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
    +                        ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
    +                        func_name(), thd->variables.max_allowed_packet);
    +    goto err_close;
       }

    Done. Using the return value for the length, 0/-1 stays an error, and an empty file still returns ''. I left the file growing after fstat for a separate bug.

    SamarthPD-21 SamarthPD-21 commented on code on Dec 02, 2026 11:40 UTC
    Code Review
    mysql-test/main/group_by.test:1
    @@ -2057,9 +2057,11 @@ drop table t1;
     #
     
     CREATE TABLE t1 ( pk int, i1 int, v1 varchar(1));
    +--replace_regex /'[^']*\/data/'MYSQLD_DATADIR/

    Done, LOAD_FILE(NULL).

    SamarthPD-21 SamarthPD-21 commented on code on Dec 02, 2026 11:40 UTC
    Code Review
    mysql-test/main/having_cond_pushdown.test:1
    @@ -1747,6 +1747,7 @@ DROP TABLE t1;
     
     CREATE TABLE t1 (a CHAR(3)) CHARSET=sjis;
     INSERT INTO t1 VALUES ('foo'),('bar');
    +--replace_regex /'[^']*\/data/'MYSQLD_DATADIR/

    Done, LOAD_FILE(NULL) here too, and the result is unchanged.

    vaintroub vaintroub commented on code on Dec 02, 2026 11:45 UTC
    Code Review
    mysql-test/main/load_file_errors.test:1
    @@ -0,0 +1,58 @@
    +--source include/not_embedded.inc
    +--source include/not_as_root.inc
    +--source include/not_windows.inc

    Why "not_windows"? Do you have any other test that has "include/windows.inc"?

    gkodinov gkodinov commented on code on Dec 02, 2026 11:50 UTC
    Code Review
    mysys/my_read.c:1
    @@ -90,11 +90,11 @@ size_t my_read(File Filedes, uchar *Buffer, size_t Count, myf MyFlags)
           {
             if (readbytes == (size_t) -1)
               my_error(EE_READ,
    -                   MYF(ME_BELL | (MyFlags & (ME_NOTE | ME_ERROR_LOG))),
    +                   MYF(ME_BELL | (MyFlags & (ME_NOTE | ME_WARNING | ME_ERROR_LOG))),

    yes, please.

    gkodinov gkodinov commented on code on Dec 02, 2026 11:54 UTC
    Code Review
    sql/item_strfunc.cc:1
    @@ -4396,59 +4397,72 @@ String *Item_load_file::val_str(String *str)
       MY_STAT stat_info;
       char path[FN_REFLEN];
       ulonglong file_size;
    +  size_t read_length= 0;
    +  THD *thd= current_thd;
       DBUG_ENTER("load_file");
     
    -  if (!(file_name= args[0]->val_str(str))
    +  if (!(file_name= args[0]->val_str(str)))
    +    goto err;

    This can return NULL for a number of reasons, e.g. the argument being NULL. I do not think an error is generated in this case. So going to error is probably not the right move here.

    gkodinov gkodinov commented on code on Dec 02, 2026 11:55 UTC
    Code Review
    sql/item_strfunc.cc:1
    @@ -4396,59 +4396,58 @@ String *Item_load_file::val_str(String *str)
       MY_STAT stat_info;
       char path[FN_REFLEN];
       ulonglong file_size;
    +  THD *thd= current_thd;
       DBUG_ENTER("load_file");
     
    -  if (!(file_name= args[0]->val_str(str))
    +  if (!(file_name= args[0]->val_str(str)))
    +    goto err;
    +
     #ifndef NO_EMBEDDED_ACCESS_CHECKS
    -      || !(current_thd->security_ctx->master_access & FILE_ACL)
    -#endif
    -      )
    +  if (!(thd->security_ctx->master_access & FILE_ACL))
    +  {
    +    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "FILE");

    I can live with disclosing this one. Good.

    gkodinov gkodinov commented on code on Dec 02, 2026 11:57 UTC
    Code Review
    sql/item_strfunc.cc:1
    @@ -4396,59 +4396,58 @@ String *Item_load_file::val_str(String *str)
       MY_STAT stat_info;
       char path[FN_REFLEN];
       ulonglong file_size;
    +  THD *thd= current_thd;
       DBUG_ENTER("load_file");
     
    -  if (!(file_name= args[0]->val_str(str))
    +  if (!(file_name= args[0]->val_str(str)))
    +    goto err;
    +
     #ifndef NO_EMBEDDED_ACCESS_CHECKS
    -      || !(current_thd->security_ctx->master_access & FILE_ACL)
    -#endif
    -      )
    +  if (!(thd->security_ctx->master_access & FILE_ACL))
    +  {
    +    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "FILE");
         goto err;
    +  }
    +#endif
     
       (void) fn_format(path, file_name->c_ptr_safe(), mysql_real_data_home, "",
                        MY_RETURN_REAL_PATH | MY_UNPACK_FILENAME);
     
       /* Read only allowed from within dir specified by secure_file_priv */
       if (!is_secure_file_path(path))
    +  {
    +    my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--secure-file-priv");

    Would you prefer returning NULL silently as before for this case, or a generic access denied error? CI also shows the embedded locale tests relying on the old NULL for a relative path.

    My personal 2 cents: Well, it's an error. I'd say: return a generic error. But, maybe, for backward compatibility purposes we must keep the NULL.

    gkodinov gkodinov commented on code on Dec 02, 2026 12:05 UTC
    Code Review
    sql/item_strfunc.cc:1
    @@ -4396,59 +4397,72 @@ String *Item_load_file::val_str(String *str)
       MY_STAT stat_info;
       char path[FN_REFLEN];
       ulonglong file_size;
    +  size_t read_length= 0;
    +  THD *thd= current_thd;
       DBUG_ENTER("load_file");
     
    -  if (!(file_name= args[0]->val_str(str))
    +  if (!(file_name= args[0]->val_str(str)))
    +    goto err;
    +
     #ifndef NO_EMBEDDED_ACCESS_CHECKS
    -      || !(current_thd->security_ctx->master_access & FILE_ACL)
    -#endif
    -      )
    +  if (!(thd->security_ctx->master_access & FILE_ACL))
    +  {
    +    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "FILE");
         goto err;
    +  }
    +#endif
     
       (void) fn_format(path, file_name->c_ptr_safe(), mysql_real_data_home, "",
                        MY_RETURN_REAL_PATH | MY_UNPACK_FILENAME);
     
       /* Read only allowed from within dir specified by secure_file_priv */
       if (!is_secure_file_path(path))
    +  {
    +    my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--secure-file-priv");
         goto err;
    +  }
     
    -  if (!mysql_file_stat(key_file_loadfile, path, &stat_info, MYF(0)))
    +  if ((file= mysql_file_open(key_file_loadfile, path, O_RDONLY,
    +                             MYF(MY_NOSYMLINKS))) < 0)
    +  {
    +    my_error(EE_FILENOTFOUND, MYF(ME_WARNING), path, my_errno);

    This is the one I strongly object to. This will add the full server path to the error message. Some attackers would be very happy about that. If you need to print anything, print whatever the user has supplied as argv[0].

    gkodinov gkodinov requested changes on Dec 02, 2026 12:19 UTC
    Changes Requested Reviewer

    Something to get you going. Feel free to ignore too: I'm not the final reviewer.

    grooverdan grooverdan commented on Dec 03, 2026 02:06 UTC
    Contributor

    Noting fulltest run: https://buildbot.mariadb.org/#/grid?branch=refs%2Fpull%2F6296%2Fhead

    tests needing correction:
    main.secure_file_priv_win
    engines.iuds
    windows: main.loaddata, main.partition_error - "succeeded - should have failed with error ER_OPTION_PREVENTS_STATEMENT"

    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Dec 03, 2026 02:27 UTC
    Reviewer

    <!-- ccr-overview-v2 -->

    🟡 Changes recommended

    Active correctness and verification issues remain, including truncated reads being accepted without the promised error and several diagnostics, test cleanup, plan annotations, and coverage gaps.

    <details open>
    <summary><strong>5 open issues</strong></summary>

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> Treat short reads as LOAD_FILE read failures · New

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.png" alt="Low severity" width="62" height="18" align="texttop"></picture> Add the expected-plan annotation to the result · New

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.png" alt="Low severity" width="62" height="18" align="texttop"></picture> Suppress warnings during DROP USER cleanup · New

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.png" alt="Low severity" width="62" height="18" align="texttop"></picture> Capture fstat errno before reporting the warning · New

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.png" alt="Low severity" width="62" height="18" align="texttop"></picture> Report test coverage of new LOAD_FILE branches · New

  • </details>

    <details>
    <summary><strong>What changed in this PR</strong></summary>

    Improves LOAD_FILE() diagnostics and permission handling by enforcing FILE/secure_file_priv, warning on file I/O failures, and closing files on error paths.

    Changes:

  • Adds validation and regression coverage for privilege, path, open, stat, and read failures.

  • Updates affected test expectations and portable temporary-file paths.
  • | File | Description |
    | ---- | ----------- |
    | sql/​item_strfunc.cc | Implements revised LOAD_FILE() validation, diagnostics, and file handling. |
    | mysql-test/​suite/​json/​t/​json_table.test | Normalizes LOAD_FILE() warning expectations in JSON_TABLE tests. |
    | mysql-test/​suite/​json/​r/​json_table.result | Records updated JSON_TABLE warning output. |
    | mysql-test/​main/​partition_error.test | Updates partition error tests for secure-file failures. |
    | mysql-test/​main/​partition_error.result | Records updated partition error results. |
    | mysql-test/​main/​outfile.test | Updates outfile tests for missing-file warnings. |
    | mysql-test/​main/​outfile.result | Records updated outfile warning output. |
    | mysql-test/​main/​mysql_locale_posix.test | Uses an allowed temporary test-directory path. |
    | mysql-test/​main/​loaddata.test | Updates LOAD DATA tests for secure-file errors. |
    | mysql-test/​main/​loaddata.result | Records updated LOAD DATA error results. |
    | mysql-test/​main/​load_file_errors.test | Adds regression coverage for LOAD_FILE() error and warning cases. |
    | mysql-test/​main/​load_file_errors.result | Records expected LOAD_FILE() diagnostics. |
    | mysql-test/​main/​having_cond_pushdown.test | Avoids unintended file diagnostics in having-condition tests. |
    | mysql-test/​main/​having_cond_pushdown.result | Updates expected having-condition query output. |
    | mysql-test/​main/​group_by.test | Avoids unintended file diagnostics in GROUP BY tests. |
    | mysql-test/​main/​group_by.result | Updates expected GROUP BY query output. |
    | mysql-test/​main/​func_str.test | Updates string-function tests for missing-file warnings. |
    | mysql-test/​main/​func_str.result | Records updated string-function warning output. |
    | mysql-test/​main/​ctype_gbk_export_import.test | Uses an allowed temporary test-directory path. |
    </details>

    <sub>Review effort: Lite</sub>

    SamarthPD-21 SamarthPD-21 commented on Dec 03, 2026 19:37 UTC
    Author

    MSAN caught a memcmp in my_open_parent_dir_nosymlinks() reading past the NUL for a 1-char name (LOAD_FILE('x')). LOAD_FILE now opens before stat, so it reaches that code. Switched to strcmp.

    SamarthPD-21 SamarthPD-21 commented on Dec 03, 2026 22:36 UTC
    Author

    In case it helps: the S_IROTH check is in the initial 2000 import, so no commit explains it. LOAD DATA INFILE has no such check; it only requires a regular file or FIFO, not a symlink. LOAD_FILE now opens with MY_NOSYMLINKS.

    SamarthPD-21 SamarthPD-21 commented on Dec 04, 2026 13:03 UTC
    Author

    Sorry about that. Everything below is now in one push.

    gkodinov gkodinov commented on Dec 04, 2026 13:08 UTC
    Contributor

    thanks. now let's focus on the rest.

    SamarthPD-21 SamarthPD-21 commented on Dec 06, 2026 00:35 UTC
    Author

    engines.iuds failed the same way as group_by: the tests passed file paths that don't exist and only ever got NULL. Switched them to LOAD_FILE(NULL), so the results are unchanged.

    Reviewers
    Assignees
    SamarthPD-21 SamarthPD-21
    Labels
    risk: low confidence: 92.3% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 90.6% (HIGH) · Higher = larger blast radius
    90.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 92.3% · Lower = rushed or shallow review
    92.3%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 7.0% · Safe to merge
    7.0%
    Repository