Heap corruption in file_browse_exec #96691

Closed
opened 2022-03-21 22:25:07 +01:00 by Tom Edwards · 9 comments

System Information
Operating system: Windows 10
Graphics card: N/A

Blender Version
Broken: 3.1
Worked: 3.0.1

Short description of error
The method file_browse_exec in source\blender\editors\space_buttons\buttons_ops.c corrupts the heap when a relative directory path becomes longer than the absolute directory path.

The bug occurs between lines 210 and 213. path_len is the length of the absolute path (str), not the relative path (path) as seems to be intended.

The public release of Blender 3.1 suffers from these crashes, as do current builds of Git master.

The callstack:

ucrtbased.dll!free_dbg_nolock(void * const block, const int block_use) Line 952	C++
ucrtbased.dll!_free_dbg(void * block, int block_use) Line 1030	C++
ucrtbased.dll!free(void * block) Line 32	C++
blender.exe!MEM_lockfree_freeN(void * vmemh) Line 118	C
blender.exe!file_browse_exec(bContext * C, wmOperator * op) Line 231	C
blender.exe!wm_handler_fileselect_do(bContext * C, ListBase * handlers, wmEventHandler_Op * handler, int val) Line 2571	C
blender.exe!wm_handler_fileselect_call(bContext * C, ListBase * handlers, wmEventHandler_Op * handler, const wmEvent * event) Line 2670	C
blender.exe!wm_handlers_do_intern(bContext * C, wmWindow * win, wmEvent * event, ListBase * handlers) Line 3143	C
blender.exe!wm_handlers_do(bContext * C, wmEvent * event, ListBase * handlers) Line 3199	C
blender.exe!wm_event_do_handlers(bContext * C) Line 3767	C
blender.exe!WM_main(bContext * C) Line 626	C
blender.exe!main(int argc, const unsigned char * * UNUSED_argv_c) Line 551	C

The debug assert message is:

HEAP CORRUPTION DETECTED: after Normal block (#2927924) at 0x0000014DE8153E80.
CRT detected that the application wrote to memory after end of heap buffer.

Exact steps for others to reproduce the error
file_select_heap_corruption.blend

  1. Open the file above in Blender 3.1 and execute its startup script
  2. Click on the file select dialog that appears in the bottom right corner of the screen (in "Custom Properties")
  3. Click on the gear icon in the top right of the window that opens and check the "Relative Path" option
  4. Select a path distant from the file, so that parent directory segments are required in the relative path (e.g. ../../../..)
  5. Once you click Accept, heap corruption occurs.
**System Information** Operating system: Windows 10 Graphics card: N/A **Blender Version** Broken: 3.1 Worked: 3.0.1 **Short description of error** The method file_browse_exec in source\blender\editors\space_buttons\buttons_ops.c corrupts the heap when a relative directory path becomes longer than the absolute directory path. The bug occurs between lines 210 and 213. `path_len` is the length of the absolute path (`str`), not the relative path (`path`) as seems to be intended. The public release of Blender 3.1 suffers from these crashes, as do current builds of Git master. The callstack: ``` ucrtbased.dll!free_dbg_nolock(void * const block, const int block_use) Line 952 C++ ucrtbased.dll!_free_dbg(void * block, int block_use) Line 1030 C++ ucrtbased.dll!free(void * block) Line 32 C++ blender.exe!MEM_lockfree_freeN(void * vmemh) Line 118 C blender.exe!file_browse_exec(bContext * C, wmOperator * op) Line 231 C blender.exe!wm_handler_fileselect_do(bContext * C, ListBase * handlers, wmEventHandler_Op * handler, int val) Line 2571 C blender.exe!wm_handler_fileselect_call(bContext * C, ListBase * handlers, wmEventHandler_Op * handler, const wmEvent * event) Line 2670 C blender.exe!wm_handlers_do_intern(bContext * C, wmWindow * win, wmEvent * event, ListBase * handlers) Line 3143 C blender.exe!wm_handlers_do(bContext * C, wmEvent * event, ListBase * handlers) Line 3199 C blender.exe!wm_event_do_handlers(bContext * C) Line 3767 C blender.exe!WM_main(bContext * C) Line 626 C blender.exe!main(int argc, const unsigned char * * UNUSED_argv_c) Line 551 C ``` The debug assert message is: > HEAP CORRUPTION DETECTED: after Normal block (#2927924) at 0x0000014DE8153E80. > CRT detected that the application wrote to memory after end of heap buffer. **Exact steps for others to reproduce the error** [file_select_heap_corruption.blend](https://archive.blender.org/developer/F12938904/file_select_heap_corruption.blend) 1. Open the file above in Blender 3.1 and execute its startup script 2. Click on the file select dialog that appears in the bottom right corner of the screen (in "Custom Properties") 3. Click on the gear icon in the top right of the window that opens and check the "Relative Path" option 4. Select a path distant from the file, so that parent directory segments are required in the relative path (e.g. ../../../..) 5. Once you click Accept, heap corruption occurs.
Author

Added subscriber: @artfunkel

Added subscriber: @artfunkel

Added subscriber: @mano-wii

Added subscriber: @mano-wii

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'

I can confirm the problem by generating this relative path: "//..\..\..\..\..\..\..\..\Germano\Instaladores\"
Investigating the code, it looks like something is really wrong. The BLI_path_slash_ensure call seems redundant, the path_len is taken with the path before being converted to relative and MEM_reallocN is called where MEM_freeN + MEM_mallocN seems to be the intent.

Here a quick solution:

diff --git a/source/blender/editors/space_buttons/buttons_ops.c b/source/blender/editors/space_buttons/buttons_ops.c
index f91ed5eb4f3..840d34584e7 100644
--- a/source/blender/editors/space_buttons/buttons_ops.c
+++ b/source/blender/editors/space_buttons/buttons_ops.c
@@ -204,12 +204,11 @@ static int file_browse_exec(bContext *C, wmOperator *op)
     BLI_path_abs(path, id ? ID_BLEND_PATH(bmain, id) : BKE_main_blendfile_path(bmain));
 
     if (BLI_is_dir(path)) {
-      /* Do this first so '*' isn't converted to '*\' on windows. */
-      BLI_path_slash_ensure(path);
       if (is_relative) {
-        const int path_len = BLI_strncpy_rlen(path, str, FILE_MAX);
+        BLI_strncpy(path, str, FILE_MAX);
         BLI_path_rel(path, BKE_main_blendfile_path(bmain));
+        const int path_len = BLI_strnlen(path, FILE_MAX - 1);
         str = MEM_reallocN(str, path_len + 2);
         BLI_strncpy(str, path, FILE_MAX);
       }
       else {

I can confirm the problem by generating this relative path: `"//..\..\..\..\..\..\..\..\Germano\Instaladores\"` Investigating the code, it looks like something is really wrong. The `BLI_path_slash_ensure` call seems redundant, the `path_len` is taken with the path before being converted to relative and `MEM_reallocN` is called where `MEM_freeN` + `MEM_mallocN` seems to be the intent. Here a quick solution: ``` diff --git a/source/blender/editors/space_buttons/buttons_ops.c b/source/blender/editors/space_buttons/buttons_ops.c index f91ed5eb4f3..840d34584e7 100644 --- a/source/blender/editors/space_buttons/buttons_ops.c +++ b/source/blender/editors/space_buttons/buttons_ops.c @@ -204,12 +204,11 @@ static int file_browse_exec(bContext *C, wmOperator *op) BLI_path_abs(path, id ? ID_BLEND_PATH(bmain, id) : BKE_main_blendfile_path(bmain)); if (BLI_is_dir(path)) { - /* Do this first so '*' isn't converted to '*\' on windows. */ - BLI_path_slash_ensure(path); if (is_relative) { - const int path_len = BLI_strncpy_rlen(path, str, FILE_MAX); + BLI_strncpy(path, str, FILE_MAX); BLI_path_rel(path, BKE_main_blendfile_path(bmain)); + const int path_len = BLI_strnlen(path, FILE_MAX - 1); str = MEM_reallocN(str, path_len + 2); BLI_strncpy(str, path, FILE_MAX); } else { ```

Added subscribers: @ideasman42, @mont29

Added subscribers: @ideasman42, @mont29

@ideasman42 0682af0d63 seems to be the direct cause of this issue, mind checking? thanks.

@ideasman42 0682af0d63 seems to be the direct cause of this issue, mind checking? thanks.

This issue was referenced by 9f15ee3c7a

This issue was referenced by 9f15ee3c7ae03c19a09f5a48e29960e18c6628c0

This issue was referenced by 87d9d33c00

This issue was referenced by 87d9d33c0066bdd6eaf3fd38689f99db8e79dd03

Changed status from 'Confirmed' to: 'Resolved'

Changed status from 'Confirmed' to: 'Resolved'
Campbell Barton self-assigned this 2022-03-29 02:49:14 +02:00
Thomas Dinges added this to the 3.1 milestone 2023-02-08 15:52:44 +01:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Code Documentation
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
FBX
Interest
Freestyle
Interest
Geometry Nodes
Interest
glTF
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 & 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
Viewport & EEVEE
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
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
Asset System
Module
Core
Module
Development Management
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Module
Viewport & EEVEE
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Severity
High
Severity
Low
Severity
Normal
Severity
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
5 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#96691
No description provided.