Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add protection to disable Gitea when run as root #17168

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ steps:
- git update-ref refs/heads/tag_test ${DRONE_COMMIT_SHA}

- name: unit-test
image: golang:1.17
image: gitea/test_env:linux-amd64 # https://gitea.com/gitea/test-env
user: gitea
commands:
- make unit-test-coverage test-check
environment:
Expand All @@ -220,7 +221,8 @@ steps:

- name: unit-test-gogit
pull: always
image: golang:1.17
image: gitea/test_env:linux-amd64 # https://gitea.com/gitea/test-env
user: gitea
commands:
- make unit-test-coverage test-check
environment:
Expand All @@ -232,6 +234,7 @@ steps:

- name: test-mysql
image: gitea/test_env:linux-amd64 # https://gitea.com/gitea/test-env
user: gitea
commands:
- make test-mysql-migration integration-test-coverage
environment:
Expand All @@ -246,6 +249,7 @@ steps:

- name: test-mysql8
image: gitea/test_env:linux-amd64 # https://gitea.com/gitea/test-env
user: gitea
commands:
- timeout -s ABRT 40m make test-mysql8-migration test-mysql8
environment:
Expand All @@ -259,6 +263,7 @@ steps:

- name: test-mssql
image: gitea/test_env:linux-amd64 # https://gitea.com/gitea/test-env
user: gitea
commands:
- make test-mssql-migration test-mssql
environment:
Expand Down Expand Up @@ -345,7 +350,8 @@ steps:

- name: build
pull: always
image: golang:1.17
image: gitea/test_env:linux-arm64 # https://gitea.com/gitea/test-env
user: gitea
commands:
- make backend
environment:
Expand All @@ -355,6 +361,7 @@ steps:

- name: test-sqlite
image: gitea/test_env:linux-arm64 # https://gitea.com/gitea/test-env
user: gitea
commands:
- timeout -s ABRT 40m make test-sqlite-migration test-sqlite
environment:
Expand All @@ -368,6 +375,7 @@ steps:

- name: test-pgsql
image: gitea/test_env:linux-arm64 # https://gitea.com/gitea/test-env
user: gitea
commands:
- timeout -s ABRT 40m make test-pgsql-migration test-pgsql
environment:
Expand Down
11 changes: 11 additions & 0 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,9 @@ func NewContext() {
}

RunUser = Cfg.Section("").Key("RUN_USER").MustString(user.CurrentUsername())
// The following is a purposefully undocumented option. Please do not run Gitea as root. It will only cause future headaches.
// Please don't use root as a bandaid to "fix" something that is broken, instead the broken thing should instead be fixed properly.
unsafeAllowRunAsRoot := Cfg.Section("").Key("I_AM_BEING_UNSAFE_RUNNING_AS_ROOT").MustBool(false)
RunMode = Cfg.Section("").Key("RUN_MODE").MustString("prod")
// Does not check run user when the install lock is off.
if InstallLock {
Expand All @@ -907,6 +910,14 @@ func NewContext() {
}
}

if RunUser == "root" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we guarantee for every supported OS that the root account will be called "root"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't entirely (Windows is the case where it is certain to not be "root"), however windows is out of scope of this PR. This purpose of this PR wasn't to be exhaustive of all the possibilities, just to prevent me from being lazy and running things as root where I could mess up file permissions 😆

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd stick a GOOS check here and exclude dozers from this check.
Then just use os.GetUID() == 0 instead of checking username.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we dont need to check goos on windows we will get a -1

if !unsafeAllowRunAsRoot {
// Special thanks to VLC which inspired the wording of this messaging.
6543 marked this conversation as resolved.
Show resolved Hide resolved
log.Fatal("Gitea is not supposed to be run as root. Sorry. If you need to use privileged TCP ports please instead use setcap and the `cap_net_bind_service` permission")
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
}
log.Warn("You are running Gitea using the root user, and have purposely chosen to skip built-in protections around this. You have been warned against this.")
6543 marked this conversation as resolved.
Show resolved Hide resolved
6543 marked this conversation as resolved.
Show resolved Hide resolved
}

SSH.BuiltinServerUser = Cfg.Section("server").Key("BUILTIN_SSH_SERVER_USER").MustString(RunUser)

newRepository()
Expand Down