Skip to content

Commit

Permalink
Add a guard to prevent a buffer overflow when saving a game (#6647)
Browse files Browse the repository at this point in the history
  • Loading branch information
Garanas authored Feb 8, 2025
1 parent e725005 commit 410dc04
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/snippets/fix.6647.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- (#6647) Fix a buffer overflow exploit in the `InternalSaveGame` user global
43 changes: 43 additions & 0 deletions lua/ui/globals/InternalSaveGame.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---@declare-global

--******************************************************************************************************
--** Copyright (c) 2024 Willem 'Jip' Wijnia
--**
--** Permission is hereby granted, free of charge, to any person obtaining a copy
--** of this software and associated documentation files (the "Software"), to deal
--** in the Software without restriction, including without limitation the rights
--** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
--** copies of the Software, and to permit persons to whom the Software is
--** furnished to do so, subject to the following conditions:
--**
--** The above copyright notice and this permission notice shall be included in all
--** copies or substantial portions of the Software.
--**
--** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
--** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
--** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
--** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
--** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
--** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
--** SOFTWARE.
--******************************************************************************************************

do
local DebugAllocatedSize = debug.allocatedsize
local oldInternalSaveGame = _G.InternalSaveGame

--- Hook to fix a buffer overflow security issue in the engine
---@param filename string
_G.InternalSaveGame = function(filename, friendlyFilename, onCompletionCallback)
local characterLimit = 100
if DebugAllocatedSize(filename) > characterLimit then
filename = filename:sub(1, characterLimit)
end

if DebugAllocatedSize(friendlyFilename) > characterLimit then
friendlyFilename = friendlyFilename:sub(1, characterLimit)
end

return oldInternalSaveGame(filename, friendlyFilename, onCompletionCallback)
end
end
1 change: 1 addition & 0 deletions lua/userInit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ end
-- # Global (and shared) init
doscript '/lua/globalInit.lua'
doscript '/lua/ui/globals/GpgNetSend.lua'
doscript '/lua/ui/globals/InternalSaveGame.lua'

-- Do we have an custom language set inside user-options ?
local selectedlanguage = import("/lua/user/prefs.lua").GetFromCurrentProfile('options').selectedlanguage
Expand Down

0 comments on commit 410dc04

Please sign in to comment.