I've just noticed some interesting behavior with
chmod when unsetting the setgid bit:
$ mkdir test $ chmod 2755 test $ stat -c '%a %n' test 2755 test # as expected $ chmod 0755 test $ stat -c '%a %n' test 2755 test # what? see below $ chmod 00755 test $ stat -c '%a %n' test 755 test # double what?!
Attempting to unset the setgid bit with
chmod 0755 doesn't work, which is surprising. However, the man page indicates that this is the intended behavior:
SETUID AND SETGID BITS
chmod clears the set-group-ID bit of a regular file if the file's group ID does not match the user's effective group ID or one of the user's supplementary group IDs, unless the user has appropriate privileges. Additional restrictions may cause the set-user-ID and set-group-ID bits of MODE or RFILE to be ignored. This behavior depends on the policy and functionality of the underlying chmod system call. When in doubt, check the underlying system behavior.
chmod preserves a directory's set-user-ID and set-group-ID bits unless you explicitly specify otherwise. You can set or clear the bits with symbolic modes like u+s and g-s, and you can set (but not clear) the bits with a numeric mode.
So it seems
chmod 0755 isn't meant to unset the setgid bit. Why, then, however, does
chmod 00755 unset it?
chmod doesn't seem to have any use for five digits of a numeric mode. Again from the man page:
A numeric mode is from one to four octal digits (0-7), derived by adding up the bits with values 4, 2, and 1. Omitted digits are assumed to be leading zeroes.
What's going on here? Why would
chmod decide to ignore a single leading 0? Why does it not ignore two leading 0's?
(Debian Stretch 9.1, with chmod (GNU Coreutils) 8.6)