#3623·mujoco

Rejected mjs_delete on a foreign element corrupts both specifications and causes use-after-free

Author: peachtree0222Created Sep 26, 2026Updated Oct 1, 2026
Labelsbug

Intro

Hi!

I found a failed-mutation atomicity problem in mjs_delete: asking one specification to delete an element owned by another returns an error, but transfers the element into the wrong specification's detached ownership list before validating ownership.

My setup

  • MuJoCo source build from main at 1fb91483f045c6aaf5207323e2372614043a64fc
  • C API, double precision
  • Ubuntu 24.04.5 LTS, x86_64
  • GCC 13.3.0 normal and AddressSanitizer builds

What's happening? What did you expect?

I expected mjs_delete(wrong, foreign) to return -1 without changing either specification. It does return -1 with Error: element is not in this model, but mjCModel::operator-= has already appended the foreign pointer to wrong's detached_ list and reset its tree lists. Destroying wrong frees the object still owned and indexed by the original spec. Compiling the owner then reads freed memory.

foreign_delete_result=-1
wrong_error=Error: element is not in this model
wrong_spec_deleted
ERROR: AddressSanitizer: heap-use-after-free
READ in mjCModel::ProcessList_ ... src/user/user_model.cc:4560

Relevant source:

Steps for reproduction

  1. Check out the commit above and make a normal or ASan build.
  2. Compile poc/reproducer.cc using the command in poc/README.md.
  3. Run it. The normal build crashes while compiling the owner; ASan identifies the freed specification element.

Reproduction package:

foreign-spec-delete-ownership-corruption-poc.zip

Minimal model for reproduction

The reproducer creates two empty specifications and adds one site programmatically; no model asset is required.

Code required for reproduction

See poc/reproducer.cc. I also prepared a patch that performs the ownership check before changing body/plugin ownership, detached_, or tree lists. The focused regression passes.

Confirmations