Subject: coreutils 8.32: install: fchmod fails with EBADF

From: Cameron Nemo <[email-masked]>
Date: Tue, 15 Sep 2020 03:31:43 +0200 (CEST)

Hello,

I am experiencing some curious behavior when trying to set the file mode using
the install command.

Version info: install (GNU coreutils) 8.32

Summary of behavior: fchmod to 1777 fails with EBADF

The log from strace shows the following:

    ...
    geteuid()                               = 0
    umask(000)                              = 022
    mkdirat(AT_FDCWD, "tmp", 01755)         = -1 EEXIST (File exists)
    openat(AT_FDCWD, "tmp", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_LARGEFILE|O_PATH|O_DIRECTORY) = 3
    fstat(3, {st_mode=S_IFDIR|S_ISVTX|0755, st_size=40, ...}) = 0
    fchmod(3, 01777)                        = -1 EBADF (Bad file descriptor)
    fcntl(3, F_GETFD)                       = 0
    fchmodat(AT_FDCWD, "/proc/self/fd/3", 01777) = 0
    close(3)                                = 0
    ...

For some odd reason, the call to fchmod is failing with EBADF.
This is causing the chmod() function to fall back to using an fd link in /proc.

Normally (i.e. when /proc is mounted) this poses no issues.
But I am trying to use the install command in a bootstrap/chroot environment,
and having to incorporate mount and unmount logic adds a significant amount of
non-idempodent complexity.

It seems like relying on the /proc link is not ideal,
and a bug is being hidden by such behavior.
Is there any chance that this can be resolved?

Regards,
--
Cameron Nemo