Skip to content

Commit

Permalink
Error if copying into the source directory. (DDoSolitary#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
DDoSolitary committed May 17, 2020
1 parent eb02466 commit d1ff49a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 5 deletions.
3 changes: 2 additions & 1 deletion LxRunOffline/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ const wstr msg_table[] = {
L"Failed to detect filesystem version of the directory \"%1%\".",
L"Installing to the root directory \"%1%\" is known to cause issues.",
L"The configuration flags are invalid.",
L"The action/argument \"%1%\" doesn't support WSL2."
L"The action/argument \"%1%\" doesn't support WSL2.",
L"Copying or moving into a subdirectory of the source directory is not allowed."
};

lro_error::lro_error(const err_msg msg_code, std::vector<wstr> msg_args, const HRESULT err_code)
Expand Down
3 changes: 2 additions & 1 deletion LxRunOffline/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ enum class err_msg {
err_fs_detect,
err_root_dir,
err_invalid_flags,
err_wsl2_unsupported
err_wsl2_unsupported,
err_copy_subdir
};

class lro_error : public std::exception {
Expand Down
16 changes: 16 additions & 0 deletions LxRunOffline/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ void archive_writer::write_hard_link() {
archive_entry_clear(pe.get());
}

bool archive_writer::check_source_path(const file_path &sp) const {
return true;
}

wsl_writer::wsl_writer() : hf_data(nullptr) {}

void wsl_writer::write_data(HANDLE hf, const char *buf, const uint32_t size) const {
Expand Down Expand Up @@ -320,6 +324,11 @@ void wsl_writer::write_hard_link() {
}
}

bool wsl_writer::check_source_path(const file_path &sp) const {
// base_len of a linux_path is always 0, so it will be safely ignored.
return path->data.compare(0, std::min(path->base_len, sp.base_len), sp.data, 0, sp.base_len);
}

wsl_v1_writer::wsl_v1_writer(crwstr base_path) {
path = std::make_unique<wsl_v1_path>(base_path);
target_path = std::make_unique<wsl_v1_path>(base_path);
Expand Down Expand Up @@ -547,6 +556,13 @@ void wsl_reader::run(fs_writer &writer) {
});
}

void wsl_reader::run_checked(fs_writer &writer) {
if (!writer.check_source_path(*path)) {
throw lro_error::from_other(err_msg::err_copy_subdir, {});
}
run(writer);
}

wsl_v1_reader::wsl_v1_reader(crwstr base) {
path = std::make_unique<wsl_v1_path>(base);
}
Expand Down
4 changes: 4 additions & 0 deletions LxRunOffline/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class fs_writer {
virtual void write_directory(const file_attr *) = 0;
virtual void write_symlink(const file_attr *, const char *) = 0;
virtual void write_hard_link() = 0;
virtual bool check_source_path(const file_path &) const = 0;
};

class archive_writer : public fs_writer {
Expand All @@ -38,6 +39,7 @@ class archive_writer : public fs_writer {
void write_directory(const file_attr *) override;
void write_symlink(const file_attr *, const char *) override;
void write_hard_link() override;
bool check_source_path(const file_path &) const override;
};

class wsl_writer : public fs_writer {
Expand All @@ -53,6 +55,7 @@ class wsl_writer : public fs_writer {
void write_directory(const file_attr *) override;
void write_symlink(const file_attr *, const char *) override;
void write_hard_link() override;
bool check_source_path(const file_path &) const override;
};

class wsl_v1_writer : public wsl_writer {
Expand Down Expand Up @@ -101,6 +104,7 @@ class wsl_reader : public fs_reader {
virtual bool is_legacy() const;
public:
void run(fs_writer &) override;
void run_checked(fs_writer &);
};

class wsl_v1_reader : public wsl_reader {
Expand Down
4 changes: 2 additions & 2 deletions LxRunOffline/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ int wmain(int argc, wchar_t **argv) {
if (!move_directory(sp, dir)) {
auto ver = get_distro_version(name);
auto writer = select_wsl_writer(ver, dir);
select_wsl_reader(ver, sp)->run(*writer);
select_wsl_reader(ver, sp)->run_checked(*writer);
delete_directory(sp);
}
set_distro_dir(name, dir);
Expand All @@ -142,7 +142,7 @@ int wmain(int argc, wchar_t **argv) {
register_distro(new_name, dir, nv);
conf.configure_distro(new_name, config_all);
auto writer = select_wsl_writer(nv, dir);
select_wsl_reader(ov, get_distro_dir(name))->run(*writer);
select_wsl_reader(ov, get_distro_dir(name))->run_checked(*writer);
} else if (!wcscmp(argv[1], L"e") || !wcscmp(argv[1], L"export")) {
wstr file;
desc.add_options()(",f", po::wvalue<wstr>(&file)->required(), "Path to the .tar.gz file to export to. A config file will also be exported to this file name with a .xml extension.");
Expand Down
2 changes: 1 addition & 1 deletion LxRunOffline/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class prefix_matcher {

class file_path {
protected:
size_t base_len;
explicit file_path(crwstr);
public:
size_t base_len;
wstr data;
virtual ~file_path() = default;
virtual bool append(wchar_t) = 0;
Expand Down

0 comments on commit d1ff49a

Please sign in to comment.