CVE-2018-16976 (published 2018-09-12T22:29Z)

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.

Severity: MEDIUM
CVSS v2 base score: 5.5 (AV:N/AC:L/Au:S/C:P/I:P/A:N)
CVSS v3 base score: 8.1, HIGH (CVSS:3.0/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:N)

Weakness information:
cwe_id: CWE-362
cwe_name: Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition')
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.
extended_description: 
            This can have security implications when the expected synchronization is in security-critical code, such as recording whether a user is authenticated or modifying important state information that should not be influenced by an outsider.
            A race condition occurs within concurrent environments, and is effectively a property of a code sequence. Depending on the context, a code sequence may be in the form of a function call, a small number of instructions, a series of program invocations, etc.
            A race condition violates these properties, which are closely related:
               
                  Exclusivity - the code sequence is given exclusive access to the shared resource, i.e., no other code sequence can modify properties of the shared resource before the original sequence has completed execution.
                  Atomicity - the code sequence is behaviorally atomic, i.e., no other thread or process can concurrently execute the same sequence of instructions (or a subset) against the same resource.
               
            A race condition exists when an "interfering code sequence" can still access the shared resource, violating exclusivity. Programmers may assume that certain code sequences execute too quickly to be affected by an interfering code sequence; when they are not, this violates atomicity. For example, the single "x++" statement may appear atomic at the code layer, but it is actually non-atomic at the instruction layer, since it involves a read (the original value of x), followed by a computation (x+1), followed by a write (save the result to x).
            The interfering code sequence could be "trusted" or "untrusted." A trusted interfering code sequence occurs within the product; it cannot be modified by the attacker, and it can only be invoked indirectly. An untrusted interfering code sequence can be authored directly by the attacker, and typically it is external to the vulnerable product.
         
         

Fix information:
repo_url: https://github.com/sitaramc/gitolite
commit_details: repo_url: https://github.com/sitaramc/gitolite
author: Sitaram Chamarty
author_date: 2018-09-08 15:38:41+05:30
msg: 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
repository: repo_url: https://github.com/sitaramc/gitolite
repo_name: sitaramc/gitolite
description: Hosting git repositories -- Gitolite allows you to setup git hosting on a central server, with very fine-grained access control and many (many!) more powerful features.
repo_language: Perl
owner: sitaramc
file_changes: filename: gitolite-shell
change_type: ModificationType.MODIFY
diff: @@ -113,6 +113,13 @@ sub main {
     $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);
 

programming_language: Perl
filename: Common.pm
change_type: ModificationType.MODIFY
diff: @@ -19,6 +19,8 @@ package Gitolite::Common;
 
           ssh_fingerprint_file
           ssh_fingerprint_line
+
+          update_hook_present
 );
 #>>>
 use Exporter 'import';
@@ -235,14 +237,28 @@ sub cleanup_conf_line {
             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 ];

programming_language: Perl