Skip to content

Positional writes by path drop setuid/setgid/sticky when restoring the mode after the owner-write retry #972

Open

Description

@tannevaled

Summary

When a write by path is refused for permission, the VM retries with the owner-write bit set, then restores the mode. The saved mode is st.Mode().Perm(), which strips setuid, setgid and sticky. The restore therefore clears those bits.

Affected code:

Reproduction (found by audit, measured with rubydiff against MRI 4.0.7)

q = "t.bin"; File.write(q, "0123456789")
f = File.open(q, "r+")
File.chmod(0o1444, q)
f.pwrite("AB", 0)
printf("%o\n", File.stat(q).mode & 0o7777)
  • MRI 4.0.7 prints 1444.
  • rbgo prints 444.

Related weakness (same code)

The stat, chmod, open and restore are four separate lookups of the path. If the path is swapped between them, the write or the restored mode can land on another file, or u+w can be left behind. Swapping in a FIFO blocks the O_WRONLY open forever. This needs write access to the directory, and the README already documents the path model.

Hardening: open first, then fstat the handle and compare it with os.SameFile. Use f.Chmod on the handle instead of os.Chmod on the path, and save the full Mode() & (ModePerm|ModeSetuid|ModeSetgid|ModeSticky).

Fix

Save and restore the full mode, including the special bits. Add a test row with 0o1444, and one with 0o4755 where the platform allows it.

Prior art: priorart "pwrite pread huge offset overflow slice bounds" at 2026-11-21 20:43 CET reported prior art; checked: priorart 'pwrite chmod setuid sticky mode restore': 155 hits, none on special-bit loss; nearest are the pwrite path changes themselves (#970, #949, #971)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions