GNU bug report logs - #11108
chmod: fix symlink race condition

Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.

Package: coreutils; Severity: wishlist; Reported by: Paul Eggert <eggert@HIDDEN>; Keywords: patch; merged with #18280, #32772; dated Wed, 28 Mar 2012 06:01:01 UTC; Maintainer for coreutils is bug-coreutils@HIDDEN.
Forcibly Merged 11108 18280 32772. Request was from Assaf Gordon <assafgordon@HIDDEN> to control <at> debbugs.gnu.org. Full text available.
Changed bug title to 'chmod: fix symlink race condition' from '[PATCH] chmod: fix symlink race condition' Request was from Assaf Gordon <assafgordon@HIDDEN> to control <at> debbugs.gnu.org. Full text available.
Severity set to 'wishlist' from 'normal' Request was from Assaf Gordon <assafgordon@HIDDEN> to control <at> debbugs.gnu.org. Full text available.
Forcibly Merged 11108 18280. Request was from Paul Eggert <eggert@HIDDEN> to control <at> debbugs.gnu.org. Full text available.

Message received at 11108 <at> debbugs.gnu.org:


Received: (at 11108) by debbugs.gnu.org; 28 Mar 2012 20:59:44 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Wed Mar 28 16:59:44 2012
Received: from localhost ([127.0.0.1]:43745 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.72)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1SCzy1-0002DZ-M0
	for submit <at> debbugs.gnu.org; Wed, 28 Mar 2012 16:59:43 -0400
Received: from smtp.cs.ucla.edu ([131.179.128.62]:50135)
	by debbugs.gnu.org with esmtp (Exim 4.72)
	(envelope-from <eggert@HIDDEN>) id 1SCzxl-0002D8-Dh
	for 11108 <at> debbugs.gnu.org; Wed, 28 Mar 2012 16:59:40 -0400
Received: from localhost (localhost.localdomain [127.0.0.1])
	by smtp.cs.ucla.edu (Postfix) with ESMTP id B0D8EA60001;
	Wed, 28 Mar 2012 13:28:01 -0700 (PDT)
X-Virus-Scanned: amavisd-new at smtp.cs.ucla.edu
Received: from smtp.cs.ucla.edu ([127.0.0.1])
	by localhost (smtp.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id C2HcvVMKMQxF; Wed, 28 Mar 2012 13:28:01 -0700 (PDT)
Received: from penguin.cs.ucla.edu (Penguin.CS.UCLA.EDU [131.179.64.200])
	by smtp.cs.ucla.edu (Postfix) with ESMTPSA id 64D2739E800F;
	Wed, 28 Mar 2012 13:28:01 -0700 (PDT)
Message-ID: <4F737451.2090001@HIDDEN>
Date: Wed, 28 Mar 2012 13:28:01 -0700
From: Paul Eggert <eggert@HIDDEN>
Organization: UCLA Computer Science Department
User-Agent: Mozilla/5.0 (X11; Linux x86_64;
	rv:10.0.1) Gecko/20120209 Thunderbird/10.0.1
MIME-Version: 1.0
To: Jim Meyering <jim@HIDDEN>
Subject: Re: bug#11108: [PATCH] chmod: fix symlink race condition
References: <4F72A17F.6010308@HIDDEN> <87zkb1uqhq.fsf@HIDDEN>
	<4F735451.6000603@HIDDEN> <87fwcsqyav.fsf@HIDDEN>
In-Reply-To: <87fwcsqyav.fsf@HIDDEN>
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
X-Spam-Score: -1.9 (-)
X-Debbugs-Envelope-To: 11108
Cc: 11108 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.13
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <http://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <http://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <http://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Sender: debbugs-submit-bounces <at> debbugs.gnu.org
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
X-Spam-Score: -1.9 (-)

On 03/28/2012 01:13 PM, Jim Meyering wrote:
>     $ ./chmod u+w f
>     ./chmod: changing permissions of 'f': Operation not supported

Yeouch.  I undid the change for now.
Hmm, why did "make check" work for me?
I'll have to investigate later, alas.




Information forwarded to bug-coreutils@HIDDEN:
bug#11108; Package coreutils. Full text available.

Message received at 11108 <at> debbugs.gnu.org:


Received: (at 11108) by debbugs.gnu.org; 28 Mar 2012 20:45:12 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Wed Mar 28 16:45:12 2012
Received: from localhost ([127.0.0.1]:43724 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.72)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1SCzjz-0001rv-IG
	for submit <at> debbugs.gnu.org; Wed, 28 Mar 2012 16:45:12 -0400
Received: from mx.meyering.net ([88.168.87.75]:38428)
	by debbugs.gnu.org with esmtp (Exim 4.72)
	(envelope-from <jim@HIDDEN>) id 1SCzjR-0001qa-5q
	for 11108 <at> debbugs.gnu.org; Wed, 28 Mar 2012 16:45:10 -0400
Received: from rho.meyering.net (localhost.localdomain [127.0.0.1])
	by rho.meyering.net (Acme Bit-Twister) with ESMTP id 7F4B660081;
	Wed, 28 Mar 2012 22:13:12 +0200 (CEST)
From: Jim Meyering <jim@HIDDEN>
To: Paul Eggert <eggert@HIDDEN>
Subject: Re: bug#11108: [PATCH] chmod: fix symlink race condition
In-Reply-To: <4F735451.6000603@HIDDEN> (Paul Eggert's message of "Wed, 28
	Mar 2012 11:11:29 -0700")
References: <4F72A17F.6010308@HIDDEN> <87zkb1uqhq.fsf@HIDDEN>
	<4F735451.6000603@HIDDEN>
Date: Wed, 28 Mar 2012 22:13:12 +0200
Message-ID: <87fwcsqyav.fsf@HIDDEN>
Lines: 29
MIME-Version: 1.0
Content-Type: text/plain
X-Spam-Score: -1.9 (-)
X-Debbugs-Envelope-To: 11108
Cc: 11108 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.13
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <http://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <http://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <http://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Sender: debbugs-submit-bounces <at> debbugs.gnu.org
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
X-Spam-Score: -1.9 (-)

Paul Eggert wrote:
> On 03/28/2012 12:36 AM, Jim Meyering wrote:
>> I presume you'll update NEWS, too, where you can say
>> [bug introduced in the beginning]
>
> Thanks, good point.  I did that in the version I just committed
> to the master.

Rats:

    $ ./chmod u+w f
    ./chmod: changing permissions of 'f': Operation not supported

That fix introduces chmod failures on several important systems,
including my Fedora 17 desktop ;-)

I confess that I had not tested it, and had missed or forgotten
this part of the GNU/Linux/fchmodat documentation:

       AT_SYMLINK_NOFOLLOW
              If pathname is a symbolic link, do not dereference  it:  instead
              operate  on  the link itself.  This flag is not currently imple-
              mented.

The nixos/hydra build server is reporting failures, too:

  http://hydra.nixos.org/build/2341393
  http://hydra.nixos.org/build/2341397
  http://hydra.nixos.org/build/2341395




Information forwarded to bug-coreutils@HIDDEN:
bug#11108; Package coreutils. Full text available.

Message received at 11108 <at> debbugs.gnu.org:


Received: (at 11108) by debbugs.gnu.org; 28 Mar 2012 20:04:32 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Wed Mar 28 16:04:32 2012
Received: from localhost ([127.0.0.1]:43699 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.72)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1SCz6c-0000sL-Cy
	for submit <at> debbugs.gnu.org; Wed, 28 Mar 2012 16:04:31 -0400
Received: from mx.meyering.net ([88.168.87.75]:38312)
	by debbugs.gnu.org with esmtp (Exim 4.72)
	(envelope-from <jim@HIDDEN>) id 1SCz63-0000rQ-7a
	for 11108 <at> debbugs.gnu.org; Wed, 28 Mar 2012 16:04:28 -0400
Received: from rho.meyering.net (localhost.localdomain [127.0.0.1])
	by rho.meyering.net (Acme Bit-Twister) with ESMTP id D72F660064;
	Wed, 28 Mar 2012 21:32:29 +0200 (CEST)
From: Jim Meyering <jim@HIDDEN>
To: Paul Eggert <eggert@HIDDEN>
Subject: Re: bug#11108: [PATCH] chmod: fix symlink race condition
In-Reply-To: <4F735451.6000603@HIDDEN> (Paul Eggert's message of "Wed, 28
	Mar 2012 11:11:29 -0700")
References: <4F72A17F.6010308@HIDDEN> <87zkb1uqhq.fsf@HIDDEN>
	<4F735451.6000603@HIDDEN>
Date: Wed, 28 Mar 2012 21:32:29 +0200
Message-ID: <87wr64r06q.fsf@HIDDEN>
Lines: 33
MIME-Version: 1.0
Content-Type: text/plain
X-Spam-Score: -1.9 (-)
X-Debbugs-Envelope-To: 11108
Cc: 11108 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.13
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <http://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <http://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <http://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Sender: debbugs-submit-bounces <at> debbugs.gnu.org
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
X-Spam-Score: -1.9 (-)

Paul Eggert wrote:
> On 03/28/2012 12:36 AM, Jim Meyering wrote:
>> I presume you'll update NEWS, too, where you can say
>> [bug introduced in the beginning]
>
> Thanks, good point.  I did that in the version I just committed
> to the master.
>
>> I note also that this doesn't protect anyone who is using
>> a system that lacks both fchmodat and lchmod.
>
> Right; I put that in the NEWS entry.
>
> There are still problems, in the sense that the attacker
> can use a hard link to target any visible file on the same filesystem,
> by using hard links; but this problem is unavoidable.
>
>> we'd have to openat each file to get a file descriptor,
>> then fstat that FD to verify it's the same dev/ino as
>> found by the fts-run stat call, and only then, call fchmod.
>
> This might be useful to close other (more-subtle) races
> involving things like hard-link manipulation and chmod +X,
> where the new mode depends on the old.  A general problem
> with using 'open' for this sort of thing, though,
> is that 'open' can have side effects on devices.  I wish
> there was a variant of 'open' guaranteed to never
> hang and never have side effects; then we could play this
> sort of game more reliably.

Oops.  I should not have suggested using open, since it cannot
work in general: it would fail for any file that is neither
readable nor writable.




Information forwarded to bug-coreutils@HIDDEN:
bug#11108; Package coreutils. Full text available.

Message received at 11108 <at> debbugs.gnu.org:


Received: (at 11108) by debbugs.gnu.org; 28 Mar 2012 18:43:32 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Wed Mar 28 14:43:32 2012
Received: from localhost ([127.0.0.1]:43640 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.72)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1SCxqD-0007NH-AP
	for submit <at> debbugs.gnu.org; Wed, 28 Mar 2012 14:43:31 -0400
Received: from smtp.cs.ucla.edu ([131.179.128.62]:50322)
	by debbugs.gnu.org with esmtp (Exim 4.72)
	(envelope-from <eggert@HIDDEN>) id 1SCxpd-0007Ls-FO
	for 11108 <at> debbugs.gnu.org; Wed, 28 Mar 2012 14:43:27 -0400
Received: from localhost (localhost.localdomain [127.0.0.1])
	by smtp.cs.ucla.edu (Postfix) with ESMTP id 4CF7AA60002;
	Wed, 28 Mar 2012 11:11:30 -0700 (PDT)
X-Virus-Scanned: amavisd-new at smtp.cs.ucla.edu
Received: from smtp.cs.ucla.edu ([127.0.0.1])
	by localhost (smtp.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id cwzZxM059W2J; Wed, 28 Mar 2012 11:11:29 -0700 (PDT)
Received: from penguin.cs.ucla.edu (Penguin.CS.UCLA.EDU [131.179.64.200])
	by smtp.cs.ucla.edu (Postfix) with ESMTPSA id A959FA60001;
	Wed, 28 Mar 2012 11:11:29 -0700 (PDT)
Message-ID: <4F735451.6000603@HIDDEN>
Date: Wed, 28 Mar 2012 11:11:29 -0700
From: Paul Eggert <eggert@HIDDEN>
Organization: UCLA Computer Science Department
User-Agent: Mozilla/5.0 (X11; Linux x86_64;
	rv:10.0.1) Gecko/20120209 Thunderbird/10.0.1
MIME-Version: 1.0
To: Jim Meyering <jim@HIDDEN>
Subject: Re: bug#11108: [PATCH] chmod: fix symlink race condition
References: <4F72A17F.6010308@HIDDEN> <87zkb1uqhq.fsf@HIDDEN>
In-Reply-To: <87zkb1uqhq.fsf@HIDDEN>
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
X-Spam-Score: -1.9 (-)
X-Debbugs-Envelope-To: 11108
Cc: 11108 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.13
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <http://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <http://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <http://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Sender: debbugs-submit-bounces <at> debbugs.gnu.org
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
X-Spam-Score: -1.9 (-)

On 03/28/2012 12:36 AM, Jim Meyering wrote:
> I presume you'll update NEWS, too, where you can say
> [bug introduced in the beginning]

Thanks, good point.  I did that in the version I just committed
to the master.

> I note also that this doesn't protect anyone who is using
> a system that lacks both fchmodat and lchmod.

Right; I put that in the NEWS entry.

There are still problems, in the sense that the attacker
can use a hard link to target any visible file on the same filesystem,
by using hard links; but this problem is unavoidable.

> we'd have to openat each file to get a file descriptor,
> then fstat that FD to verify it's the same dev/ino as
> found by the fts-run stat call, and only then, call fchmod.

This might be useful to close other (more-subtle) races
involving things like hard-link manipulation and chmod +X,
where the new mode depends on the old.  A general problem
with using 'open' for this sort of thing, though,
is that 'open' can have side effects on devices.  I wish
there was a variant of 'open' guaranteed to never
hang and never have side effects; then we could play this
sort of game more reliably.




Information forwarded to bug-coreutils@HIDDEN:
bug#11108; Package coreutils. Full text available.

Message received at 11108 <at> debbugs.gnu.org:


Received: (at 11108) by debbugs.gnu.org; 28 Mar 2012 08:07:58 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Wed Mar 28 04:07:58 2012
Received: from localhost ([127.0.0.1]:42199 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.72)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1SCnvC-0004o6-7w
	for submit <at> debbugs.gnu.org; Wed, 28 Mar 2012 04:07:58 -0400
Received: from mx.meyering.net ([88.168.87.75]:36306)
	by debbugs.gnu.org with esmtp (Exim 4.72)
	(envelope-from <jim@HIDDEN>) id 1SCnud-0004nE-Gx
	for 11108 <at> debbugs.gnu.org; Wed, 28 Mar 2012 04:07:56 -0400
Received: from rho.meyering.net (localhost.localdomain [127.0.0.1])
	by rho.meyering.net (Acme Bit-Twister) with ESMTP id 8CF6F60062;
	Wed, 28 Mar 2012 09:36:01 +0200 (CEST)
From: Jim Meyering <jim@HIDDEN>
To: Paul Eggert <eggert@HIDDEN>
Subject: Re: bug#11108: [PATCH] chmod: fix symlink race condition
In-Reply-To: <4F72A17F.6010308@HIDDEN> (Paul Eggert's message of "Tue, 27
	Mar 2012 22:28:31 -0700")
References: <4F72A17F.6010308@HIDDEN>
Date: Wed, 28 Mar 2012 09:36:01 +0200
Message-ID: <87zkb1uqhq.fsf@HIDDEN>
Lines: 54
MIME-Version: 1.0
Content-Type: text/plain
X-Spam-Score: -1.9 (-)
X-Debbugs-Envelope-To: 11108
Cc: 11108 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.13
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <http://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <http://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <http://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Sender: debbugs-submit-bounces <at> debbugs.gnu.org
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
X-Spam-Score: -1.9 (-)

Paul Eggert wrote:
> This fixes what I hope is an obvious race condition
> that can occur if some other process substitutes a
> symlink for a non-symlink while chmod is running.

Good catch.  I'll bet that's exploitable by anyone who
can convince root to run "chmod -r ... DIR" on files they own.

The chmodat-introducing commit was v5.92-656-gc97a36e,
but the preceding use of chmod was just as vulnerable.
If you reference a commit in your log, please use "git describe"
output, not the bare-8-byte-SHA1 like we've done in the past.
While "git describe" output is not converted to a clickable link
by a released gitk, with the one in upcoming git-1.7.10, it is.

I presume you'll update NEWS, too, where you can say
[bug introduced in the beginning]
I've confirmed that the very first version of chmod.c has the same
problem: it calls stat, then calls chmod whenever !S_ISLNK.

I note also that this doesn't protect anyone who is using
a system that lacks both fchmodat and lchmod.
For that, we'd have to openat each file to get a file descriptor,
then fstat that FD to verify it's the same dev/ino as
found by the fts-run stat call, and only then, call fchmod.

> =====
> * src/chmod.c (process_file): Don't follow symlink if we
> think the file is not a symlink.
> ---
>  src/chmod.c |   10 +++++++++-
>  1 files changed, 9 insertions(+), 1 deletions(-)
>
> diff --git a/src/chmod.c b/src/chmod.c
> index aa4ac77..2e1f1c7 100644
> --- a/src/chmod.c
> +++ b/src/chmod.c
> @@ -268,7 +268,15 @@ process_file (FTS *fts, FTSENT *ent)
>
>        if (! S_ISLNK (old_mode))
>          {
> -          if (chmodat (fts->fts_cwd_fd, file, new_mode) == 0)
> +          /* Use any native support for AT_SYMLINK_NOFOLLOW, to avoid
> +             following a symlink if there is a race.  */
> +          #if HAVE_FCHMODAT || HAVE_LCHMOD
> +          int follow_flag = AT_SYMLINK_NOFOLLOW;
> +          #else
> +          int follow_flag = 0;
> +          #endif
> +
> +          if (fchmodat (fts->fts_cwd_fd, file, new_mode, follow_flag) == 0)
>              chmod_succeeded = true;
>            else
>              {




Information forwarded to bug-coreutils@HIDDEN:
bug#11108; Package coreutils. Full text available.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 28 Mar 2012 06:00:47 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Wed Mar 28 02:00:47 2012
Received: from localhost ([127.0.0.1]:42121 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.72)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1SClw6-0001sq-DU
	for submit <at> debbugs.gnu.org; Wed, 28 Mar 2012 02:00:47 -0400
Received: from eggs.gnu.org ([208.118.235.92]:48947)
	by debbugs.gnu.org with esmtp (Exim 4.72)
	(envelope-from <eggert@HIDDEN>) id 1SClvX-0001s7-RX
	for submit <at> debbugs.gnu.org; Wed, 28 Mar 2012 02:00:44 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <eggert@HIDDEN>) id 1SClRC-0002OP-Rc
	for submit <at> debbugs.gnu.org; Wed, 28 Mar 2012 01:28:52 -0400
X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on eggs.gnu.org
X-Spam-Level: 
X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI
	autolearn=unavailable version=3.3.2
Received: from lists.gnu.org ([208.118.235.17]:59250)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <eggert@HIDDEN>) id 1SClRC-0002OK-OW
	for submit <at> debbugs.gnu.org; Wed, 28 Mar 2012 01:28:50 -0400
Received: from eggs.gnu.org ([208.118.235.92]:37007)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <eggert@HIDDEN>) id 1SClRB-0000b3-37
	for bug-coreutils@HIDDEN; Wed, 28 Mar 2012 01:28:50 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <eggert@HIDDEN>) id 1SClR9-0002Nw-9K
	for bug-coreutils@HIDDEN; Wed, 28 Mar 2012 01:28:48 -0400
Received: from smtp.cs.ucla.edu ([131.179.128.62]:49915)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <eggert@HIDDEN>) id 1SClR9-0002Ni-2s
	for bug-coreutils@HIDDEN; Wed, 28 Mar 2012 01:28:47 -0400
Received: from localhost (localhost.localdomain [127.0.0.1])
	by smtp.cs.ucla.edu (Postfix) with ESMTP id C4EB139E800F
	for <bug-coreutils@HIDDEN>; Tue, 27 Mar 2012 22:28:44 -0700 (PDT)
X-Virus-Scanned: amavisd-new at smtp.cs.ucla.edu
Received: from smtp.cs.ucla.edu ([127.0.0.1])
	by localhost (smtp.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id LTxIpqXyKRXU for <bug-coreutils@HIDDEN>;
	Tue, 27 Mar 2012 22:28:43 -0700 (PDT)
Received: from [192.168.1.10] (pool-71-189-109-235.lsanca.fios.verizon.net
	[71.189.109.235])
	by smtp.cs.ucla.edu (Postfix) with ESMTPSA id 98F2539E800A
	for <bug-coreutils@HIDDEN>; Tue, 27 Mar 2012 22:28:43 -0700 (PDT)
Message-ID: <4F72A17F.6010308@HIDDEN>
Date: Tue, 27 Mar 2012 22:28:31 -0700
From: Paul Eggert <eggert@HIDDEN>
Organization: UCLA Computer Science Department
User-Agent: Mozilla/5.0 (X11; Linux i686;
	rv:11.0) Gecko/20120310 Thunderbird/11.0
MIME-Version: 1.0
To: bug-coreutils@HIDDEN
Subject: [PATCH] chmod: fix symlink race condition
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3)
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3)
X-Received-From: 208.118.235.17
X-Spam-Score: -1.2 (-)
X-Debbugs-Envelope-To: submit
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.13
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <http://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <http://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <http://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Sender: debbugs-submit-bounces <at> debbugs.gnu.org
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
X-Spam-Score: -6.2 (------)

This fixes what I hope is an obvious race condition
that can occur if some other process substitutes a
symlink for a non-symlink while chmod is running.

=====
* src/chmod.c (process_file): Don't follow symlink if we
think the file is not a symlink.
---
 src/chmod.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)

diff --git a/src/chmod.c b/src/chmod.c
index aa4ac77..2e1f1c7 100644
--- a/src/chmod.c
+++ b/src/chmod.c
@@ -268,7 +268,15 @@ process_file (FTS *fts, FTSENT *ent)
 
       if (! S_ISLNK (old_mode))
         {
-          if (chmodat (fts->fts_cwd_fd, file, new_mode) == 0)
+          /* Use any native support for AT_SYMLINK_NOFOLLOW, to avoid
+             following a symlink if there is a race.  */
+          #if HAVE_FCHMODAT || HAVE_LCHMOD
+          int follow_flag = AT_SYMLINK_NOFOLLOW;
+          #else
+          int follow_flag = 0;
+          #endif
+
+          if (fchmodat (fts->fts_cwd_fd, file, new_mode, follow_flag) == 0)
             chmod_succeeded = true;
           else
             {
-- 
1.7.6.5





Acknowledgement sent to Paul Eggert <eggert@HIDDEN>:
New bug report received and forwarded. Copy sent to bug-coreutils@HIDDEN. Full text available.
Report forwarded to bug-coreutils@HIDDEN:
bug#11108; Package coreutils. Full text available.
Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.
Last modified: Tue, 30 Oct 2018 04:30:02 UTC

GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997 nCipher Corporation Ltd, 1994-97 Ian Jackson.