Skip to content

fix: enforce parent-dir permission check on sysopen O_CREAT#401

Draft
Koan-Bot wants to merge 1 commit into
cpan-authors:mainfrom
atoomic:koan.atoomic/fix-sysopen-parent-perm-check
Draft

fix: enforce parent-dir permission check on sysopen O_CREAT#401
Koan-Bot wants to merge 1 commit into
cpan-authors:mainfrom
atoomic:koan.atoomic/fix-sysopen-parent-perm-check

Conversation

@Koan-Bot
Copy link
Copy Markdown
Contributor

@Koan-Bot Koan-Bot commented May 15, 2026

What

Fix dead elsif branch in __sysopen that silently skipped parent-directory permission checks when creating new files via O_CREAT.

Why

The permission check at the end of __sysopen tested defined $mock_file->{'contents'} to distinguish new vs. existing files. But O_CREAT already set contents = '' earlier in the function, and the ENOENT guard returned early when contents was undef — so the elsif branch that checks parent-dir write+execute was unreachable. This meant sysopen($fh, '/protected/new', O_WRONLY|O_CREAT) would succeed in a read-only directory under set_user(), while the equivalent open($fh, '>', '/protected/new') correctly returned EACCES.

Issue #329 identified the root cause; issue #330 tracks the broader duplication.

How

Capture my $is_new = !defined $mock_file->{'contents'} before the O_CREAT block modifies contents. Use $is_new (not defined contents) to route the permission check: existing files check file-level perms, new files check parent-dir write+execute.

Testing

  • Added two subtests to t/perms.t: sysopen O_CREAT in read-only vs. writable parent dir, and sysopen on existing file with file-level perm check
  • Full test suite passes locally

Closes #329

🤖 Generated with Claude Code


Quality Report

Changes: 9 files changed, 118 insertions(+), 1176 deletions(-)

Code scan: clean

Tests: passed (OK)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

The `elsif` branch in `__sysopen`'s permission check that verified
parent-directory write+execute permission for new file creation was
unreachable dead code. O_CREAT set `contents = ''` before the check,
and the ENOENT guard returned early when contents was undef, so
`defined $mock_file->{'contents'}` was always true at the permission
check.

Fix: capture `$is_new` flag before O_CREAT modifies contents and use
it to route the permission check. Now `sysopen($fh, $path, O_WRONLY|O_CREAT)`
in a read-only directory correctly returns EACCES, matching `open()`
behavior.

Closes cpan-authors#329

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Dead elsif branch in __sysopen permission check silently bypasses parent-dir write-permission enforcement

1 participant