Fix security warning generated by std::tmpnam #105987

Closed
Sergey Sharybin wants to merge 2 commits from Sergey:fix-tmpnam into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.

Effectively replicate the behavior of the function in the manner
which is used for autosave file.

There might be better a solution which is cross-platform and does
not suffer from the time of check, time of use (TOCTOU) vector of
attack. This seems to be a bigger project to figure out, so until
then silence the warning: it is fine since the directory is only
used to chdir to, so worst case an external attacker can introduce
is a test failure.

Effectively replicate the behavior of the function in the manner which is used for autosave file. There might be better a solution which is cross-platform and does not suffer from the time of check, time of use (TOCTOU) vector of attack. This seems to be a bigger project to figure out, so until then silence the warning: it is fine since the directory is only used to chdir to, so worst case an external attacker can introduce is a test failure.
Sergey Sharybin added the
Module
Core
label 2023-03-22 12:02:08 +01:00
Sergey Sharybin added 1 commit 2023-03-22 12:02:18 +01:00
buildbot/vexp-code-patch-coordinator Build done. Details
dcb7b7c69f
Fix security warning generated by std::tmpnam
Effectively replicate the behavior of the function in the manner
which is used for autosave file.

There might be better a solution which is cross-platform and does
not suffer from the time of check, time of use (TOCTOU) vector of
attack. This seems to be a bigger project to figure out, so until
then silence the warning: it is fine since the directory is only
used to chdir to, so worst case an external attacker can introduce
is a test failure.
Author
Owner

@blender-bot build

@blender-bot build
Sergey Sharybin added this to the Core project 2023-03-22 12:02:37 +01:00
Sergey Sharybin requested review from Brecht Van Lommel 2023-03-22 15:00:51 +01:00

This is the first usage of std::filesystem in Blender. We can use that now with macOS 10.15 as the minimum, however there were also other concerns in #90379.

In particular, the BLI API assumes utf-8 but std::filesystem does not on Windows, so this may fail if the temp directory includes non-ascii characters.

This is the first usage of `std::filesystem` in Blender. We can use that now with macOS 10.15 as the minimum, however there were also other concerns in #90379. In particular, the BLI API assumes utf-8 but `std::filesystem` does not on Windows, so this may fail if the temp directory includes non-ascii characters.

Maybe it's easier to not use a temporary directory, but use a fixed one like tests/fileops similar to tests/cycles or tests/io_curve_svg.

Maybe it's easier to not use a temporary directory, but use a fixed one like `tests/fileops` similar to `tests/cycles` or `tests/io_curve_svg`.
Sergey Sharybin added 1 commit 2023-03-22 16:57:30 +01:00
9fbcc950f5 Avoid use of system-wide temporary directory
Use temporary directory provided via the command line flags,
which points to a directory within a build folder.
Brecht Van Lommel approved these changes 2023-03-22 17:08:38 +01:00
Author
Owner

I am not sure why it is considered to be important how the fs::path stores data internally. It stores the data in the native to the OS manner, which avoids conversion to the wide character format on every file system access (which is required when one uses UTF-8 for the storage).

In the issue you've linked the statement goes by "needs investigation", so not sure how that is escalated to a "concern". In practice there are some tricky aspects of getting UTF-8 path to fs::path, however those are non-intuitive mainly C++20. In C++17 the u8path does the proper thing in a cross-platform manner (at least in own tests). The tricky part is the C++20 which declares u8path deprecated and tells to use path(u8string) constructor signature, and that did not seem to work correctly. The access to the UTF-8 string is something that generic_u8string() (and that is something it seems I've missed in the original patch).

Interestingly, there is no difference in the behavior of std::tmpnam() and fs::temp_directory_path(): they both encode non-ASCII part of a path in some coding scheme, and it does not matter whether generic_string() or generic_u8string() was used.

I've updated the patch so that it avoids use of std::filesystem, but I do not think it solves more problems than introduces new. The comparison with the io_curve_svg I do not find really fair: it uses tests/io_curve_svg to store data which is considered an output. For the temporary files it still uses Python's tempfile.

Adding such explicit temporary directory specification makes it tricky to understand what is to be used and when. Like, why the fileops test has to use this explicit temporary directory, but the PLY test can use the generic Blender temporary directory.

And last but not least, with such change it is required to pass an extra command line argument to the test so that ./bin/tests/blenlib_test --gtest_filter='*change_working_directory' is no longer enough (and keeping tests as easy to run as possible is something I find important).

The proper solution could be to move where_is_temp to BLI. It provides close-to-XDG way of accessing the temp directory, and is not really Blender application specific, so it is not really justified to be in the BKE. Doing so also moves us closer to the more proper and transparent substitute of the std::filepath.

So if there is really requirement of avoiding std::filesystem the proper solution would require to first un-entangle some extra mess in the BKE/BLI. It should happen as a separate review. As for this change, I really do not feel motivated committing this mess, so closing.

I am not sure why it is considered to be important how the `fs::path` stores data internally. It stores the data in the native to the OS manner, which avoids conversion to the wide character format on every file system access (which is required when one uses UTF-8 for the storage). In the issue you've linked the statement goes by "needs investigation", so not sure how that is escalated to a "concern". In practice there are some tricky aspects of getting UTF-8 path to `fs::path`, however those are non-intuitive mainly C++20. In C++17 the `u8path` does the proper thing in a cross-platform manner (at least in own tests). The tricky part is the C++20 which declares `u8path` deprecated and tells to use `path(u8string)` constructor signature, and that did not seem to work correctly. The access to the UTF-8 string is something that `generic_u8string()` (and that is something it seems I've missed in the original patch). Interestingly, there is no difference in the behavior of `std::tmpnam()` and `fs::temp_directory_path()`: they both encode non-ASCII part of a path in some coding scheme, and it does not matter whether `generic_string()` or `generic_u8string()` was used. I've updated the patch so that it avoids use of `std::filesystem`, but I do not think it solves more problems than introduces new. The comparison with the `io_curve_svg` I do not find really fair: it uses `tests/io_curve_svg` to store data which is considered an output. For the temporary files it still uses Python's tempfile. Adding such explicit temporary directory specification makes it tricky to understand what is to be used and when. Like, why the fileops test has to use this explicit temporary directory, but the PLY test can use the generic Blender temporary directory. And last but not least, with such change it is required to pass an extra command line argument to the test so that `./bin/tests/blenlib_test --gtest_filter='*change_working_directory'` is no longer enough (and keeping tests as easy to run as possible is something I find important). The proper solution could be to move `where_is_temp` to BLI. It provides close-to-XDG way of accessing the temp directory, and is not really Blender application specific, so it is not really justified to be in the BKE. Doing so also moves us closer to the more proper and transparent substitute of the `std::filepath`. So if there is really requirement of avoiding `std::filesystem` the proper solution would require to first un-entangle some extra mess in the BKE/BLI. It should happen as a separate review. As for this change, I really do not feel motivated committing this mess, so closing.
Sergey Sharybin closed this pull request 2023-03-22 17:22:09 +01:00
Sergey Sharybin removed this from the Core project 2023-03-23 11:21:05 +01:00

Pull request closed

Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#105987
No description provided.