From unknown Fri Aug 15 12:49:55 2025 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Mailer: MIME-tools 5.509 (Entity 5.509) Content-Type: text/plain; charset=utf-8 From: bug#45886 <45886@debbugs.gnu.org> To: bug#45886 <45886@debbugs.gnu.org> Subject: Status: mkdir -m argument does not work correctly, applies incorrect permissions Reply-To: bug#45886 <45886@debbugs.gnu.org> Date: Fri, 15 Aug 2025 19:49:55 +0000 retitle 45886 mkdir -m argument does not work correctly, applies incorrect = permissions reassign 45886 coreutils submitter 45886 Davin McCall severity 45886 normal thanks From debbugs-submit-bounces@debbugs.gnu.org Fri Jan 15 02:45:11 2021 Received: (at submit) by debbugs.gnu.org; 15 Jan 2021 07:45:11 +0000 Received: from localhost ([127.0.0.1]:39820 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1l0JnD-0005VE-HE for submit@debbugs.gnu.org; Fri, 15 Jan 2021 02:45:11 -0500 Received: from lists.gnu.org ([209.51.188.17]:60624) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1l0JnA-0005V3-OY for submit@debbugs.gnu.org; Fri, 15 Jan 2021 02:45:10 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:55776) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l0Jn8-0008OI-VO for bug-coreutils@gnu.org; Fri, 15 Jan 2021 02:45:08 -0500 Received: from davmac.org ([2001:41c9:1:420::104]:36398) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l0Jn5-0001CT-Sh for bug-coreutils@gnu.org; Fri, 15 Jan 2021 02:45:06 -0500 Received: from [192.168.1.50] (117-20-70-24.751446.bne.nbn.aussiebb.net [117.20.70.24]) (Authenticated sender: davmac) by davmac.org (Postfix) with ESMTPSA id 6AD75AFBCC for ; Fri, 15 Jan 2021 07:44:50 +0000 (GMT) To: bug-coreutils@gnu.org From: Davin McCall Subject: mkdir -m argument does not work correctly, applies incorrect permissions Message-ID: <4ea6d6a5-dbae-a6f0-e702-1ee1695985da@davmac.org> Date: Fri, 15 Jan 2021 17:45:29 +1000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.0 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="------------8480C768328E228D8230C266" Content-Language: en-US Received-SPF: pass client-ip=2001:41c9:1:420::104; envelope-from=davmac@davmac.org; helo=davmac.org X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, HTML_MESSAGE=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.4 (-) X-Debbugs-Envelope-To: submit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -2.4 (--) This is a multi-part message in MIME format. --------------8480C768328E228D8230C266 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Coreutils 8.32 (and possibly earlier) "mkdir -m ..." appears to ignore parts of the mode specification. POSIX suggests that with -m specified, the mode is "relative" to a=rwx. That implies a mode of "o-w" should yield a mode of "rwxrwxr-x", and a mode of "o+w" should yield "rwxrwxrwx". Instead: $ mkdir -m o-w t1 $ mkdir -m o+w t2 $ ls -l total 8 drwxr-xr-x 2 davmac users 4096 Jan 15 17:27 t1 drwxrwxrwx 2 davmac users 4096 Jan 15 17:27 t2 $ So, the "o+w" or "o-w" selects the "other" permissions but also the "group" permissions. This also happens with "g+w" and "g-w" (i.e. both "group" and "other" permissions get set or cleared). This implies it's not just a case of the initial mode not matching POSIX, but somehow "o" and "g" are being applied incorrectly. Setting POSIXLY_CORRECT makes no difference. --------------8480C768328E228D8230C266 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit

Coreutils 8.32 (and possibly earlier) "mkdir -m <mode> ..." appears to ignore parts of the mode specification. POSIX suggests that with -m specified, the mode is "relative" to a=rwx. That implies a mode of "o-w" should yield a mode of "rwxrwxr-x", and a mode of "o+w" should yield "rwxrwxrwx". Instead:

$ mkdir -m o-w t1
$ mkdir -m o+w t2
$ ls -l
total 8
drwxr-xr-x 2 davmac users 4096 Jan 15 17:27 t1
drwxrwxrwx 2 davmac users 4096 Jan 15 17:27 t2
$

So, the "o+w" or "o-w" selects the "other" permissions but also the "group" permissions. This also happens with "g+w" and "g-w" (i.e. both "group" and "other" permissions get set or cleared). This implies it's not just a case of the initial mode not matching POSIX, but somehow "o" and "g" are being applied incorrectly.

Setting POSIXLY_CORRECT makes no difference.


--------------8480C768328E228D8230C266-- From debbugs-submit-bounces@debbugs.gnu.org Fri Jan 15 06:03:18 2021 Received: (at 45886-done) by debbugs.gnu.org; 15 Jan 2021 11:03:18 +0000 Received: from localhost ([127.0.0.1]:39979 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1l0Msv-00048E-LX for submit@debbugs.gnu.org; Fri, 15 Jan 2021 06:03:18 -0500 Received: from zimbra.cs.ucla.edu ([131.179.128.68]:59284) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1l0Msr-00047o-Fz; Fri, 15 Jan 2021 06:03:14 -0500 Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id E646A160132; Fri, 15 Jan 2021 03:03:06 -0800 (PST) Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id 1kKLZdDoHCWL; Fri, 15 Jan 2021 03:03:04 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 3114D160138; Fri, 15 Jan 2021 03:03:04 -0800 (PST) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id Ev2V-MrVOMXR; Fri, 15 Jan 2021 03:03:04 -0800 (PST) Received: from [192.168.1.9] (cpe-23-243-218-95.socal.res.rr.com [23.243.218.95]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id 00819160132; Fri, 15 Jan 2021 03:03:03 -0800 (PST) Subject: Re: bug#45886: mkdir -m argument does not work correctly, applies incorrect permissions To: Davin McCall References: <4ea6d6a5-dbae-a6f0-e702-1ee1695985da@davmac.org> From: Paul Eggert Organization: UCLA Computer Science Department Message-ID: Date: Fri, 15 Jan 2021 03:03:03 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 MIME-Version: 1.0 In-Reply-To: <4ea6d6a5-dbae-a6f0-e702-1ee1695985da@davmac.org> Content-Type: multipart/mixed; boundary="------------DE8E9032DA0B4892713B7F9E" Content-Language: en-US X-Spam-Score: -2.5 (--) X-Debbugs-Envelope-To: 45886-done Cc: 14371@debbugs.gnu.org, 45886-done@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.5 (---) This is a multi-part message in MIME format. --------------DE8E9032DA0B4892713B7F9E Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Thanks for the bug report. I reproduced the problem and installed the attached patch to fix it. --------------DE8E9032DA0B4892713B7F9E Content-Type: text/x-patch; charset=UTF-8; name="0001-mkdir-fix-bug-when-m-s-more-generous-than-umask.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-mkdir-fix-bug-when-m-s-more-generous-than-umask.patch" >From b8375c422ffe0e018cbb4cad187d1e909195d263 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Fri, 15 Jan 2021 02:57:59 -0800 Subject: [PATCH] mkdir: fix bug when -m's more generous than umask Problem reported by David McCall (Bug#45886). I introduced this problem when fixing Bug#14371. * NEWS: Mention the fix. * src/mkdir.c (struct mkdir_options): New members umask_ancestor, umask_self, replacing umask_value. (make_ancestor): Use them when temporarily adjusting umask. (main): Set them, and set the umask to umask_self instead of leaving it alone. * tests/mkdir/perm.sh (tests): Add test case for bug. --- NEWS | 3 +++ src/mkdir.c | 30 ++++++++++++++++++------------ tests/mkdir/perm.sh | 1 + 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/NEWS b/NEWS index c2474fee3..a6ba96450 100644 --- a/NEWS +++ b/NEWS @@ -20,6 +20,9 @@ GNU coreutils NEWS -*- outline -*- ls no longer crashes when printing the SELinux context for unstatable files. [bug introduced in coreutils-6.9.91] + mkdir -m no longer mishandles modes more generous than the umask. + [bug introduced in coreutils-8.22] + nl now handles single character --section-delimiter arguments, by assuming a second ':' character has been specified, as specified by POSIX. [This bug was present in "the beginning".] diff --git a/src/mkdir.c b/src/mkdir.c index eccc9d382..b266cee8c 100644 --- a/src/mkdir.c +++ b/src/mkdir.c @@ -89,8 +89,11 @@ struct mkdir_options made. */ int (*make_ancestor_function) (char const *, char const *, void *); - /* Umask value in effect. */ - mode_t umask_value; + /* Umask value for when making an ancestor. */ + mode_t umask_ancestor; + + /* Umask value for when making the directory itself. */ + mode_t umask_self; /* Mode for directory itself. */ mode_t mode; @@ -130,20 +133,18 @@ make_ancestor (char const *dir, char const *component, void *options) error (0, errno, _("failed to set default creation context for %s"), quoteaf (dir)); - mode_t user_wx = S_IWUSR | S_IXUSR; - bool self_denying_umask = (o->umask_value & user_wx) != 0; - if (self_denying_umask) - umask (o->umask_value & ~user_wx); + if (o->umask_ancestor != o->umask_self) + umask (o->umask_ancestor); int r = mkdir (component, S_IRWXUGO); - if (self_denying_umask) + if (o->umask_ancestor != o->umask_self) { int mkdir_errno = errno; - umask (o->umask_value); + umask (o->umask_self); errno = mkdir_errno; } if (r == 0) { - r = (o->umask_value & S_IRUSR) != 0; + r = (o->umask_ancestor & S_IRUSR) != 0; announce_mkdir (dir, options); } return r; @@ -282,8 +283,7 @@ main (int argc, char **argv) if (options.make_ancestor_function || specified_mode) { mode_t umask_value = umask (0); - umask (umask_value); - options.umask_value = umask_value; + options.umask_ancestor = umask_value & ~(S_IWUSR | S_IXUSR); if (specified_mode) { @@ -293,10 +293,16 @@ main (int argc, char **argv) quote (specified_mode)); options.mode = mode_adjust (S_IRWXUGO, true, umask_value, change, &options.mode_bits); + options.umask_self = umask_value & ~options.mode; free (change); } else - options.mode = S_IRWXUGO; + { + options.mode = S_IRWXUGO; + options.umask_self = umask_value; + } + + umask (options.umask_self); } return savewd_process_files (argc - optind, argv + optind, diff --git a/tests/mkdir/perm.sh b/tests/mkdir/perm.sh index 4d36f19b5..083a47733 100755 --- a/tests/mkdir/perm.sh +++ b/tests/mkdir/perm.sh @@ -35,6 +35,7 @@ tests=' 050 : -m 312 : drwx-w-rwx : d-wx--x-w- : 160 : empty : drwx--xrwx : drw---xrwx : 160 : -m 743 : drwx--xrwx : drwxr---wx : + 022 : -m o-w : drwxr-xr-x : drwxrwxr-x : 027 : -m =+x : drwxr-x--- : d--x--x--- : 027 : -m =+X : drwxr-x--- : d--x--x--- : - : - : last : last : -- 2.27.0 --------------DE8E9032DA0B4892713B7F9E-- From unknown Fri Aug 15 12:49:55 2025 Received: (at fakecontrol) by fakecontrolmessage; To: internal_control@debbugs.gnu.org From: Debbugs Internal Request Subject: Internal Control Message-Id: bug archived. Date: Fri, 12 Feb 2021 12:24:06 +0000 User-Agent: Fakemail v42.6.9 # This is a fake control message. # # The action: # bug archived. thanks # This fakemail brought to you by your local debbugs # administrator