Separate an explicit handle from ambient lookup
Giving a helper a filename lets it resolve that name using its own process permissions. Passing an already opened read handle makes the intended object and operation more explicit. This experiment uses Linux file descriptors to inspect the restrictions and shared state associated with that handle. It does not implement a capability operating system or a sandbox for arbitrary code. A write rejected through a read-only handle is different from preventing the process from writing through every other route. A final test deliberately reopens the same path with ordinary ambient permissions and succeeds at writing. Observing what remains permitted is as important as observing the restricted operation, because it defines the actual scope of the evidence.
Prepare a disposable six-byte file
Run the complete program as handle_test.py using Python 3.12 or a compatible version on Linux. It creates a private temporary directory and a document containing the six ASCII bytes abcdef, then removes that directory when the experiment finishes. Do not substitute a real user document or production storage path. The file is opened with O_RDONLY. Reads use os.read rather than mixing buffered high-level file operations with descriptor operations, keeping the observed movement aligned with the operating-system file offset. Buffered read-ahead could otherwise complicate that comparison. Descriptor numbers are allocated by the environment; the test does not assume that any handle is specifically three or four. It compares the behavior attached to the handles instead.
A duplicate need not have an independent offset
Reading two bytes from the original descriptor returns ab and advances the offset. Reading the next two bytes through the duplicate returns cd, not ab again. Duplication did not create an independently reopened file with a fresh position. The descriptors refer to shared open-file state. Two distinct numeric handles therefore do not imply two independent cursors. A helper receiving such a handle needs a contract for that shared state as well as for allowed operations. If one reader changes what the other reads next, ignoring order during concurrent use can produce surprising behavior. Designs requiring independent positions must choose an appropriate different operation or opening strategy rather than assuming duplication supplies it automatically.
Closing one handle does not revoke every copy
After closing original, reading ef through duplicate still succeeds. Closing one descriptor releases that reference; it does not invalidate every duplicated reference previously handed out. This is a concrete reason to design authority transfer and revocation separately. The example keeps both handles in one process, but the questions about shared state and continued validity also matter when authority is transferred between actors. Do not infer behavior for renaming or unlinking from this test: those actions were not performed. The specific evidence is that the duplicate remains usable after the original descriptor closes. An API requiring stronger revocation must supply an additional mechanism and verify its stated effect rather than relabeling close as a universal revocation operation.
Test both the denied operation and the remaining route
Writing through duplicate is expected to fail on Linux with EBADF because the descriptor is read-only. The test compares errno as well as the exception class, then verifies that content remains abcdef. After closing the descriptors, the process reopens the same path using ordinary writing authority and changes it to changed. That successful write demonstrates the authority not removed by this experiment. Do not hide it as an inconvenient failure of the lesson. Read it as the boundary of the claim: the operating system denied writing through that descriptor, but the process was not isolated from all other ways to modify the file. Match the four printed checks to the sequence of actual operations.
import os, errno, tempfile
from pathlib import Path
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / 'document.txt'
path.write_bytes(b'abcdef')
original = os.open(path, os.O_RDONLY)
duplicate = None
try:
assert os.read(original, 2) == b'ab'
duplicate = os.dup(original)
assert os.read(duplicate, 2) == b'cd'
os.close(original)
original = None
assert os.read(duplicate, 2) == b'ef'
try:
os.write(duplicate, b'X')
except OSError as error:
assert error.errno == errno.EBADF
else:
raise AssertionError('read-only handle accepted write')
assert path.read_bytes() == b'abcdef'
finally:
if original is not None: os.close(original)
if duplicate is not None: os.close(duplicate)
path.write_bytes(b'changed')
assert path.read_bytes() == b'changed'
print('shared offset, retained handle, rejected descriptor write, ambient reopen: passed')Identify what a stronger isolation design would add
Applying this idea to privilege separation requires more than selecting a read handle. Review unintended inherited descriptors and alternative name-based access, along with separate permissions for networking, process creation and configuration changes. If an operating-system mechanism is meant to enforce those restrictions, verify its active configuration and actual denied operations in a dedicated environment. A sandbox label on an API is not sufficient evidence. The current small experiment still teaches explicit target authority and shared state without claiming those stronger guarantees. Preserve that staged reasoning. Granting broad permissions merely to make every error disappear defeats the purpose of testing a restriction. Pair required successful operations with intentionally prohibited ones and explain which component enforces each result.
Exercise: predict offset and lifetime behavior
Change the first read to one byte before duplicating the descriptor. The duplicate’s next two bytes are bc because the shared offset has advanced past a. Next explain why closing original does not necessarily make duplicate unusable: another retained reference still exists, so releasing one handle is not global revocation. Finally ask whether handing out an O_RDONLY descriptor proves that the process cannot read another path. This experiment does not establish that; the successful ambient reopening already demonstrates remaining authority outside the handle. Distinguishing all three questions separates authority scope, shared state and reference lifetime. The temporary directory is removed after completion, and the finally block closes the descriptors opened by the test.