Skip to content

[CORE] Add path security check for model serialization #31820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

barnasm1
Copy link
Contributor

Details:

  • add path safety checking
  • unify error messages
  • remove double quote at error messages

Tickets:

@barnasm1 barnasm1 self-assigned this Aug 20, 2025
@barnasm1 barnasm1 requested a review from a team as a code owner August 20, 2025 14:54
@barnasm1 barnasm1 requested review from mryzhov and removed request for a team August 20, 2025 14:54
@github-actions github-actions bot added the category: Core OpenVINO Core (aka ngraph) label Aug 20, 2025
@barnasm1 barnasm1 requested a review from a team as a code owner August 20, 2025 16:23
@mlukasze mlukasze added this to the 2025.3 milestone Aug 21, 2025
@@ -1224,21 +1224,34 @@ void ngfunction_2_ir(pugi::xml_node& netXml,
}
}

const std::filesystem::path check_path_safety(const std::filesystem::path& path) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-use update current utils functions:
Use ov::util::sanitize_path which should remove traversal part from path.

There are also utils like in std like canonical, weakly_canonical

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 1244 to 1245

return check_path_safety(path);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return check_path_safety(path);
OPENVINO_ASSERT(!std::filesystem::is_symlink(path), "Path must not be symbolic link: " , path);
return ov::util::sanitize(path);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

std::filesystem::path provide_bin_path(const std::filesystem::path& xml_path, const std::filesystem::path& bin_path) {
if (bin_path.empty()) {
auto path = xml_path;
path.replace_extension(".bin");
return path;
return check_path_safety(path);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The xml path should be validated and bin path can be created without check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

} else {
return bin_path;
return check_path_safety(bin_path);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if not symlink and use sanitize path to remove traversal part.

Update doxy for class to mention that traversal part from path will be removed and symlinks are not allowed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@barnasm1 barnasm1 requested a review from a team as a code owner August 21, 2025 11:16
@github-actions github-actions bot added category: transformations OpenVINO Runtime library - Transformations category: CPP API OpenVINO CPP API bindings labels Aug 21, 2025
Comment on lines +126 to +127
string path = "a/b/../../../../tensor.data";
EXPECT_STREQ("a/b/tensor.data", ov::util::prevent_path_traversal(path).string().c_str());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why .. is skipped over (ignored)? Shouldn't it end up as ../../tensor.data?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent traversal into parent directories

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proposed solution changes the path in fact. Is it what user expects (is aware of)?
Example path from another test a/b/../tensor.data doesn't leave working directory and should end up as a/tensor.data.
Generally I don't think OV should bother where a user wants its files to be stored in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jszczepa Could you please share the motivation for preventing path traversal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: Core OpenVINO Core (aka ngraph) category: CPP API OpenVINO CPP API bindings category: transformations OpenVINO Runtime library - Transformations Code Freeze
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants