summaryrefslogtreecommitdiff
path: root/drivers
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2023-01-21 11:28:31 +0100
committerRémi Verschelde <rverschelde@gmail.com>2023-01-21 11:28:31 +0100
commita58f6a9dd6dd86743134b838fd1c89f844bd961b (patch)
tree640198ae02a408aab50b2d4821f2402070af04dd /drivers
parent7b622ce6e8a40508dbfccb21380e5f0b2dd45fac (diff)
parentc5390f203d6756db1ea74456a80247498679168e (diff)
Merge pull request #71220 from reduz/prevent-opening-windows-console-files
Prevent opening Windows console files
Diffstat (limited to 'drivers')
-rw-r--r--drivers/windows/file_access_windows.cpp45
-rw-r--r--drivers/windows/file_access_windows.h6
2 files changed, 50 insertions, 1 deletions
diff --git a/drivers/windows/file_access_windows.cpp b/drivers/windows/file_access_windows.cpp
index 37a94ce4cc..ea40622afc 100644
--- a/drivers/windows/file_access_windows.cpp
+++ b/drivers/windows/file_access_windows.cpp
@@ -34,7 +34,6 @@
#include "core/os/os.h"
#include "core/string/print_string.h"
-
#include <share.h> // _SH_DENYNO
#include <shlwapi.h>
#define WIN32_LEAN_AND_MEAN
@@ -58,7 +57,27 @@ void FileAccessWindows::check_errors() const {
}
}
+bool FileAccessWindows::is_path_invalid(const String &p_path) {
+ // Check for invalid operating system file.
+ String fname = p_path;
+ int dot = fname.find(".");
+ if (dot != -1) {
+ fname = fname.substr(0, dot);
+ }
+ fname = fname.to_lower();
+ return invalid_files.has(fname);
+}
+
Error FileAccessWindows::open_internal(const String &p_path, int p_mode_flags) {
+ if (is_path_invalid(p_path)) {
+#ifdef DEBUG_ENABLED
+ if (p_mode_flags != READ) {
+ WARN_PRINT("The path :" + p_path + " is a reserved Windows system pipe, so it can't be used for creating files.");
+ }
+#endif
+ return ERR_INVALID_PARAMETER;
+ }
+
_close();
path_src = p_path;
@@ -313,6 +332,10 @@ void FileAccessWindows::store_buffer(const uint8_t *p_src, uint64_t p_length) {
}
bool FileAccessWindows::file_exists(const String &p_name) {
+ if (is_path_invalid(p_name)) {
+ return false;
+ }
+
String filename = fix_path(p_name);
FILE *g = _wfsopen((LPCWSTR)(filename.utf16().get_data()), L"rb", _SH_DENYNO);
if (g == nullptr) {
@@ -324,6 +347,10 @@ bool FileAccessWindows::file_exists(const String &p_name) {
}
uint64_t FileAccessWindows::_get_modified_time(const String &p_file) {
+ if (is_path_invalid(p_file)) {
+ return 0;
+ }
+
String file = fix_path(p_file);
if (file.ends_with("/") && file != "/") {
file = file.substr(0, file.length() - 1);
@@ -352,4 +379,20 @@ FileAccessWindows::~FileAccessWindows() {
_close();
}
+HashSet<String> FileAccessWindows::invalid_files;
+
+void FileAccessWindows::initialize() {
+ static const char *reserved_files[]{
+ "con", "prn", "aux", "nul", "com0", "com1", "com2", "com3", "com4", "com5", "com6", "com7", "com8", "com9", "lpt0", "lpt1", "lpt2", "lpt3", "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9", nullptr
+ };
+ int reserved_file_index = 0;
+ while (reserved_files[reserved_file_index] != nullptr) {
+ invalid_files.insert(reserved_files[reserved_file_index]);
+ reserved_file_index++;
+ }
+}
+void FileAccessWindows::finalize() {
+ invalid_files.clear();
+}
+
#endif // WINDOWS_ENABLED
diff --git a/drivers/windows/file_access_windows.h b/drivers/windows/file_access_windows.h
index 832cef3363..2b9960d494 100644
--- a/drivers/windows/file_access_windows.h
+++ b/drivers/windows/file_access_windows.h
@@ -50,6 +50,9 @@ class FileAccessWindows : public FileAccess {
void _close();
+ static bool is_path_invalid(const String &p_path);
+ static HashSet<String> invalid_files;
+
public:
virtual Error open_internal(const String &p_path, int p_mode_flags) override; ///< open a file
virtual bool is_open() const override; ///< true when file is open
@@ -79,6 +82,9 @@ public:
virtual uint32_t _get_unix_permissions(const String &p_file) override;
virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) override;
+ static void initialize();
+ static void finalize();
+
FileAccessWindows() {}
virtual ~FileAccessWindows();
};