Skip to content

Validate the source descriptor when Linux dup2 receives identical descriptors #779

Closed

Description

@LunaStev

Problem

The ARM64 and RISC-V64 filesystem providers return newfd immediately when oldfd == newfd. LoongArch64 reuses the RISC-V provider. Consequently dup2(2147483647, 2147483647) falsely reports success for an invalid descriptor. Linux amd64 correctly returns -9.

The Linux dup2 contract permits the no-op only for a valid source descriptor.

Starting points: std/sys/linux/arm64/fs.wave, std/sys/linux/riscv64/fs.wave, std/sys/linux/loong64/fs.wave.

Reproduction

Tested at 4c3372157d8889fa8bbef0a7ccd2e72aab1c5c6c on Linux amd64 with the checkout-built Wave compiler, LLVM/Clang 21.1.8 and QEMU user emulation.

import("audit_std::sys::fs")::{dup2};
fun main() -> i32 {
    if (dup2(2147483647, 2147483647) == -9) { return 0; }
    return 1;
}

Save as probe.wave, then prepare checkout std:

python3 - <<'PYSETUP'
from pathlib import Path
import tempfile
root = Path(tempfile.mkdtemp(prefix="wave-std-audit-"))
for source in Path("std").rglob("*.wave"):
    dest = root / source.relative_to("std")
    dest.parent.mkdir(parents=True, exist_ok=True)
    dest.write_text(source.read_text().replace('"std::', '"audit_std::'))
Path("/tmp/wave-audit-std-path").write_text(str(root))
PYSETUP
audit_std="$(cat /tmp/wave-audit-std-path)"
mkdir -p /tmp/wave-dup2-audit
target/debug/wavec build probe.wave --dep "audit_std=$audit_std" --target aarch64-unknown-linux-gnu --emit=obj -O0 --out-dir /tmp/wave-dup2-audit
clang --target=aarch64-unknown-linux-gnu -ffreestanding -fno-builtin -c tests/fixtures/linux_case_runtime/start.c -o /tmp/wave-dup2-audit/start.o
clang --target=aarch64-unknown-linux-gnu -fuse-ld=lld -nostdlib -static -Wl,-e,_start /tmp/wave-dup2-audit/start.o /tmp/wave-dup2-audit/probe.o -o /tmp/wave-dup2-audit/run
qemu-aarch64 /tmp/wave-dup2-audit/run

Observed exit 1 on ARM64, RISC-V64 and LoongArch64 at O0/O2; expected 0. The amd64 control passes.

Scope

Keep the no-op for a valid descriptor and validate invalid descriptors through existing raw syscall support. Preserve atomic replacement when the descriptors differ; no libc FFI is needed.

Difficulty: easy.

Completion criteria

  • Invalid equal descriptors return EBADF, including a closed descriptor, a negative descriptor and an out-of-range value.
  • Valid equal descriptors keep the same open file and descriptor flags.
  • Differing descriptors retain existing behavior on all four Linux architectures.

Activity

  1. added
    bug
    good first issue
    help wanted
    cross-platform
    on Sep 26, 2026
  2. self-assigned this
    on Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugcross-platformgood first issuehelp wanted

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions