MDEV-41000 LOAD_DATA fails to warn/errror if insufficient permissions on file #6296
would be "LOAD_FILE" as last arg.
good catch!
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.
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.
if LOAD_DATA(NULL) is ok use it here too.
I don't know why this is succeeding on Windows. Ask if stuck.
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.
for some reason github reviewing put this a few lines too early - applies to mysql_file_read and that condition.
I doubt this is correct at this level!
Not sure this is the correct approach, especially at this level. MyFlags are passed down already. Use these instead.
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.
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.
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.
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.
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.
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.
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.
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"?
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.
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.
Makes sense, will switch it to LOAD_FILE(NULL).
Will use LOAD_FILE(NULL) here too.
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.
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.
Done, LOAD_FILE(NULL).
Done, LOAD_FILE(NULL) here too, and the result is unchanged.
Why "not_windows"? Do you have any other test that has "include/windows.inc"?
yes, please.
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.
I can live with disclosing this one. Good.
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.
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].
Something to get you going. Feel free to ignore too: I'm not the final reviewer.
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"
<!-- 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>
</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:
| 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>
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.
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.
Sorry about that. Everything below is now in one push.
thanks. now let's focus on the rest.
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.
grooverdan
SamarthPD-21
Description
LOAD_FILE() returned NULL with no diagnostic in several cases. Following the design agreed on the Jira issue:
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