fix: harden strcat overflow and fwrite error handling in model save#3194
Open
tombudd wants to merge 1 commit intogoogle-deepmind:mainfrom
Open
fix: harden strcat overflow and fwrite error handling in model save#3194tombudd wants to merge 1 commit intogoogle-deepmind:mainfrom
tombudd wants to merge 1 commit intogoogle-deepmind:mainfrom
Conversation
Two C safety fixes identified by automated security audit: 1. src/ui/ui_main.c: Replace bare strcat(text, key) with strncat(text, key, sizeof(text) - strlen(text) - 1) to prevent buffer overflow on the 50-byte stack buffer in shortcuthelp(). Consistent with MuJoCo's existing strncat usage at lines 1007/1009 and with the project's prior safer-string-handling work noted in the changelog. 2. src/engine/engine_io.c: Capture ferror(fp) state before fclose() in mj_saveModel(), then also check fclose() return value. Without this, I/O errors during model serialization (disk full, permission denied, NFS timeout) are silently swallowed, producing corrupt .mjb files with no warning to the caller. Checking both ferror and fclose is necessary: ferror must be read before fclose (which may clear the error state), and fclose itself can fail on buffered writes. Both fixes follow existing MuJoCo conventions and introduce no behavioral changes on success paths. Reviewed-by: UNA-GDO sovereign-v2.0 (Autonomous Security Auditor) Built-by: Tom Budd <tom@tombudd.com> — tombudd.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two minimal C safety hardening fixes found by UNA (autonomous security auditor, designed and built by Tom Budd — tom@tombudd.com):
1. Buffer overflow in
ui_main.c—strcat→strncatFile:
src/ui/ui_main.c:1897shortcuthelp()concatenates a modifier string and a key name into a 50-byte stack buffer using barestrcat(text, key). Ifkeyis sufficiently long, this overflowstext[50].Fix: Replace with
strncat(text, key, sizeof(text) - strlen(text) - 1)— consistent with MuJoCo's own strncat usage at lines 1007/1009 and with the project's prior safer-string-handling work (changelog: "replaced strcat, strcpy, and sprintf with strncat, strncpy, and snprintf respectively"). This instance was missed in that sweep.2. Silent data corruption in
engine_io.c— uncheckedfwriteerrorsFile:
src/engine/engine_io.c—mj_saveModel()mj_saveModel()writes the entire model via multiplefwrite()calls, then callsfclose(fp)without checking for write errors. If any write fails (disk full, I/O error, NFS timeout), the function returns silently, leaving a truncated/corrupt.mjbfile with no indication of failure.Fix: Capture
ferror(fp)state beforefclose()(necessary becausefclosemay clear error state), then also checkfclose()return value (which can fail on buffered flush). Emitmju_warning()if either indicates failure — using MuJoCo's own warning mechanism, consistent with surrounding code.Changes
src/ui/ui_main.cstrcat→strncatwith boundssrc/engine/engine_io.cferror+fcloseerror checkTotal: 2 files, +5 / -2 lines
Why these fixes matter
strcatfix closes a stack buffer overflow (potential code execution in adversarial scenarios)fwrite/fclosefix prevents silent model corruption that could cause hard-to-diagnose simulation failures downstream — particularly relevant in safety-critical robotics contextsAbout This Review
This security audit was performed by UNA (Unified Nexus Agent), an autonomous AI security auditor — a Governed Digital Organism (GDO) designed and built by Tom Budd (tom@tombudd.com | tombudd.com).
Note: Google Individual CLA previously signed by tom@tombudd.com (Mar 23, 2026) — cla/google check should pass.