Skip to content

os.chmod is missing validation for dir_fd and follow_symlinks when path is a file descriptor #156264

Description

@KowalskiThomas

Bug report

Bug description:

The documentation for os.chmod says it is not correct to use dir_fd or follow_symlinks when path is an open file descriptor...

cpython/Modules/posixmodule.c

Lines 4092 to 4095 in f74cdf8

It is an error to use dir_fd or follow_symlinks when specifying path as
an open file descriptor.
dir_fd and follow_symlinks may not be implemented on your platform.
If they are unavailable, using them will raise a NotImplementedError.

... but os_chmod_impl never checks it.

When path->is_fd is true it takes the fchmod path and returns success, so both keywords are ignored altogether:

cpython/Modules/posixmodule.c

Lines 4146 to 4149 in f74cdf8

#ifdef HAVE_FCHMOD
if (path->is_fd) {
result = fchmod(path->fd, mode);
}

Other os utils (I checked os.stat, os.chown, and os.utime) reject this combination with dir_fd_and_fd_invalid / fd_and_follow_symlinks_invalid before the syscall. os.chown for example:

cpython/Modules/posixmodule.c

Lines 4548 to 4550 in f74cdf8

if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
fd_and_follow_symlinks_invalid("chown", path->is_fd, follow_symlinks))
return NULL;

(The Windows path also has the same problem with: path->is_fd going directly to win32_fchmod.)

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions