CVE-2018-16976: Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition') (CWE-362)
value: Gitolite before 3.6.9 does not (in certain configurations involving @all or a regex) properly restrict access to a Git repository that is in the process of being migrated until the full set of migration steps has been completed. This can allow valid users to obtain unintended access.
CWE description: The product contains a code sequence that can run concurrently with other code, and the code sequence requires temporary, exclusive access to a shared resource, but a timing window exists in which the shared resource can be modified by another code sequence that is operating concurrently.
CVSS v3 base score: 8.1
Published: 2018-09-12T22:29Z
Repository: https://github.com/sitaramc/gitolite
Language: Other
Files changed: src/gitolite-shell, src/lib/Gitolite/Common.pm
Commit date: 2018-09-08 15:39:33 +0530
Commit message: prevent access to repos which are in the process of bring migrated

Björn Kautler pointed out that, when a repo is being migrated into
gitolite as per the documentation [1], there is a gap between the actual
move of the repo and the rest of the process where a user can gain read
or write access to the repo, which he would *not* have had after the
completion of the process.

My first thought was to document this, and advise people to use the
'writable' command to disable writes, but there is nothing as simple and
painless to prevent reads.  (On the plus side, this kind of racy read
access can only happen if the conf is using the "deny-rules" option to
restrict reads; without that, it makes no difference -- i.e., he gets no
access that he would not have got later anyway).

But eventually I realised that documentation was frustrating, for
various reasons, and that at least in this case there is a way to fix it
in the code -- just block all access to a repo that is in
~/repositories, but which does not yet have the update hook setup
correctly.  Plus, the code does not impact anything else, and is
basically just an extra check.

[1]: http://gitolite.com/gitolite/basic-admin/index.html#appendix-1-bringing-existing-repos-into-gitolite

Diff: diff --git a/src/gitolite-shell b/src/gitolite-shell
index 072e0ff..6c4c462 100755
--- a/src/gitolite-shell
+++ b/src/gitolite-shell
@@ -111,10 +111,17 @@ sub main {
     my ( $verb, $repo ) = parse_soc();    # returns only for git commands
     Gitolite::Conf::Load::sanity($repo, $REPONAME_PATT);
     $ENV{GL_REPO} = $repo;
     my $aa = ( $verb =~ 'upload' ? 'R' : 'W' );
 
+    # catch rare race when moving repos into gitolite control
+    _die "$aa any $repo $user DENIED by fallthru" .
+            "\n(or you mis-spelled the reponame)"
+        unless update_hook_present($repo);
+    # this error message is exactly the same as that from elsewhere in the
+    # code, for the usual reasons (avoid leaking information)
+
     # set up env vars from options set for this repo
     env_options($repo);
 
     # auto-create?
     if ( repo_missing($repo) and access( $repo, $user, '^C', 'any' ) !~ /DENIED/ ) {
diff --git a/src/lib/Gitolite/Common.pm b/src/lib/Gitolite/Common.pm
index 7a52f4b..3f47b37 100644
--- a/src/lib/Gitolite/Common.pm
+++ b/src/lib/Gitolite/Common.pm
@@ -17,10 +17,12 @@ package Gitolite::Common;
           t_start
           t_lap
 
           ssh_fingerprint_file
           ssh_fingerprint_line
+
+          update_hook_present
 );
 #>>>
 use Exporter 'import';
 use File::Path qw(mkpath);
 use File::Temp qw(tempfile);
@@ -233,18 +235,32 @@ sub cleanup_conf_line {
         my $cmd = 'find . ' . ($Gitolite::Rc::rc{REPO_SYMLINKS} || '') . ' -name "*.git" -prune';
         for my $repo (`$cmd`) {
             chomp($repo);
             $repo =~ s/\.git$//;
             $repo =~ s(^\./)();
-            push @phy_repos, $repo unless $repo =~ m(/$);
-                # tolerate bare repos within ~/repositories but silently ignore them
+            next if $repo =~ m(/$);
+                # tolerate non-bare repos within ~/repositories but silently ignore them
+            next unless update_hook_present($repo);
+                # ignore repos that don't yet have the update hook
+            push @phy_repos, $repo;
         }
         trace( 3, scalar(@phy_repos) . " physical repos found" );
         return sort_u( \@phy_repos );
     }
 }
 
+sub update_hook_present {
+    my $repo = shift;
+
+    return 1 unless -d "$ENV{GL_REPO_BASE}/$repo.git";  # non-existent repo is fine
+
+    my $x = readlink("$ENV{GL_REPO_BASE}/$repo.git/hooks/update");
+    return 1 if $x and $x eq "$ENV{GL_ADMIN_BASE}/hooks/common/update";
+
+    return 0;
+}
+
 # generate a timestamp
 sub gen_ts {
     my ( $s, $min, $h, $d, $m, $y ) = (localtime)[ 0 .. 5 ];
     $y += 1900; $m++;    # usual adjustments
     for ( $s, $min, $h, $d, $m ) {

Vulnerable code:             push @phy_repos, $repo unless $repo =~ m(/$);
                # tolerate bare repos within ~/repositories but silently ignore them
Fixed code:     # catch rare race when moving repos into gitolite control
    _die "$aa any $repo $user DENIED by fallthru" .
            "\n(or you mis-spelled the reponame)"
        unless update_hook_present($repo);
    # this error message is exactly the same as that from elsewhere in the
    # code, for the usual reasons (avoid leaking information)


          update_hook_present
            next if $repo =~ m(/$);
                # tolerate non-bare repos within ~/repositories but silently ignore them
            next unless update_hook_present($repo);
                # ignore repos that don't yet have the update hook
            push @phy_repos, $repo;
sub update_hook_present {
    my $repo = shift;

    return 1 unless -d "$ENV{GL_REPO_BASE}/$repo.git";  # non-existent repo is fine

    my $x = readlink("$ENV{GL_REPO_BASE}/$repo.git/hooks/update");
    return 1 if $x and $x eq "$ENV{GL_ADMIN_BASE}/hooks/common/update";

    return 0;
}