45 lines
5.2 KiB
Markdown
45 lines
5.2 KiB
Markdown
+++
|
|
title = "[solved] Gitea 1.16.[678] error: fatal: unsafe repository is owned by someone else"
|
|
date = 2022-05-15
|
|
description = "If Gitea runs as user git, calls a patched Git version and a parent directory of the git repositories is owned by a user other than git, it will fail."
|
|
[taxonomies]
|
|
tags = ['gna', 'gitea', 'upgrade', 'troubleshoot', 'problem']
|
|
|
|
[extra]
|
|
author = 'dachary'
|
|
+++
|
|
|
|
April 12, 2022 version [git v2.35.2](https://lore.kernel.org/git/xmqqv8veb5i6.fsf@gitster.g/) was released and addresses a security issue [CVE-2022-24765](https://github.com/git-for-windows/git/security/advisories/GHSA-vw2c-22j4-2fh2). It was backported to 2.30.3, v2.31.2, v2.32.1, v2.33.2, and v2.34.2 and published by distributions such as [Debian GNU/Linux](https://security-tracker.debian.org/tracker/CVE-2022-24765), [Alpine](https://www.alpinelinux.org/releases/).
|
|
|
|
**If Gitea runs as user `foo`, calls a patched Git version and a parent directory of the git repositories is owned by a user other than `foo`, it will fail** with a message such as:
|
|
|
|
```
|
|
Failed to open repository: Git/Data Error: exit status 128 - fatal: unsafe repository ('/data/git/repositories/git/data.git' is owned by someone else)
|
|
```
|
|
|
|
This started to show in the past few weeks to [users running the Gitea binary on Windows](https://github.com/go-gitea/gitea/issues/19455) who also independently installed git v2.36. And then to people running [Gitea from snap](https://github.com/go-gitea/gitea/issues/19455#issuecomment-1106331149), on [a Synology NAS](https://github.com/go-gitea/gitea/issues/19455#issuecomment-1106312061) and then people running from [Gitea docker images](https://github.com/go-gitea/gitea/blob/main/Dockerfile#L2) which is based on [Alpine](https://www.alpinelinux.org/releases/).
|
|
|
|
### Workarounds
|
|
|
|
* If using [Gitea docker images](https://hub.docker.com/r/gitea/gitea):
|
|
* upgrade to [Gitea >=1.16.9](https://github.com/go-gitea/gitea/pull/19876) or 1.17, both have git >=2.36
|
|
* `git config --global --replace-all safe.directory '*'`
|
|
* If the Gitea binary was installed independently of git, upgrade git to a version that is [greater or equal to 2.36](https://git-scm.com/docs/git-config#Documentation/git-config.txt-safedirectory) and disable the security check entirely with:
|
|
* impersonate the [user dedicated to Gitea](https://docs.gitea.io/en-us/install-from-binary/#recommended-server-configuration) (usually git)
|
|
* `git config --global --replace-all safe.directory '*'`
|
|
|
|
### Bug fix
|
|
|
|
The [bug fix](https://github.com/go-gitea/gitea/pull/19870) is for Gitea to ensure `git config --global --replace-all safe.directory '*'` is set on its [dedicated user](https://docs.gitea.io/en-us/install-from-binary/#recommended-server-configuration) when it initializes. It is effective on the condition that the git CLI version is [greater or equal to 2.36](https://git-scm.com/docs/git-config#Documentation/git-config.txt-safedirectory).
|
|
|
|
### Bug fix rationale
|
|
|
|
It is safe to [disable the security check in Gitea](https://lab.forgefriends.org/forgefriends/forgefriends/-/merge_requests/50/diffs). It is not vulnerable to **[CVE-2022-24765](https://github.com/git-for-windows/git/security/advisories/GHSA-vw2c-22j4-2fh2)** because it calls the git CLI [after changing its working directory](https://github.com/go-gitea/gitea/blob/main/modules/git/command.go#L160) to be the git repository targeted by the command (for instance [diff](https://github.com/go-gitea/gitea/blob/main/modules/git/diff.go#L38-L45)) or a temporary directory. Therefore **it will not explore the parent directories looking for a git configuration file**.
|
|
|
|
The security check is triggered because the repository is owned by an unexpected user (root instead of git typically) and **not because a parent directory is owned by an unexpected user**. This, in itself, is a problem worth investigating but it is unrelated and was revealed by the newer security check of git even though it does not match the threat described in **[CVE-2022-24765](https://github.com/git-for-windows/git/security/advisories/GHSA-vw2c-22j4-2fh2)**.
|
|
|
|
It appears non trivial to enforce a consistent ownership of files and directories, either within docker or outside docker when networked file systems are involved. The Gitea server was not troubled by this inconsistency so far because the permissions allow it to write and read where expected, regardless of the owner. It is not worth looking into but it is ancient and unrelated.
|
|
|
|
Gitea runs under a dedicated user, either when installed [from binary](https://docs.gitea.io/en-us/install-from-binary/#recommended-server-configuration) or from [docker](https://docs.gitea.io/en-us/install-with-docker/) and [modifies the global git configuration](https://github.com/go-gitea/gitea/blob/main/modules/git/git.go#L196-L207) depending on the git version at initialization time. Fixing the problem can therefore be done by [disabling the security check in the global git config file at initialization time](https://lab.forgefriends.org/forgefriends/forgefriends/-/merge_requests/50/diffs#bcd72ff867cbd1ddd5b6518c3a05b5f1a6021286_209_209). It also requires a minimum version of git 2.36 to be installed, which is the case for Gitea docker images with [versions >= 1.16.9](https://github.com/go-gitea/gitea/pull/19876).
|
|
|