Add protection to disable Gitea when run as root #17168

Merged
lunny merged 17 commits from please-dont-run-as-root-i-beg-of-you into main 2021-10-07 08:52:09 +00:00
Contributor

This PR shouldn't be backported as it could potentially be breaking for users who are running Gitea as root.

This PR shouldn't be backported as it could potentially be breaking for users who are running Gitea as root.
wxiaoguang approved these changes 2021-09-28 03:50:12 +00:00
lafriks (Migrated from github.com) approved these changes 2021-09-28 09:31:12 +00:00
6543 (Migrated from github.com) approved these changes 2021-09-28 12:34:03 +00:00
6543 (Migrated from github.com) left a comment

🎉

:tada:
delvh reviewed 2021-09-28 13:58:05 +00:00
Contributor
	// The following is a purposefully undocumented option. Please do not run Gitea as root. It will only cause future headaches.
```suggestion // The following is a purposefully undocumented option. Please do not run Gitea as root. It will only cause future headaches. ```
@ -905,2 +906,4 @@
// 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.
Contributor

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

Can we guarantee for every supported OS that the root account will be called "root"?
@ -914,1 +923,4 @@
log.Critical("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.")
}
SSH.BuiltinServerUser = Cfg.Section("server").Key("BUILTIN_SSH_SERVER_USER").MustString(RunUser)
Contributor

What has the media player to do with the wording of this message? 😱

What has the media player to do with the wording of this message? :scream:
@ -914,2 +924,4 @@
}
SSH.BuiltinServerUser = Cfg.Section("server").Key("BUILTIN_SSH_SERVER_USER").MustString(RunUser)
Contributor

When we enter this if, I think we can shutdown Gitea immediately here as otherwise Gitea could still be run as root.

			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")
			os.Exit(1)
When we enter this if, I think we can shutdown Gitea immediately here as otherwise Gitea could still be run as root. ```suggestion 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") os.Exit(1) ```
delvh reviewed 2021-09-28 13:59:37 +00:00
Contributor
	// Please don't use root as a bandaid to "fix" something that is broken, instead the broken thing should instead be fixed properly.
```suggestion // Please don't use root as a bandaid to "fix" something that is broken, instead the broken thing should instead be fixed properly. ```
lafriks (Migrated from github.com) reviewed 2021-09-28 16:22:27 +00:00
@ -914,2 +924,4 @@
}
SSH.BuiltinServerUser = Cfg.Section("server").Key("BUILTIN_SSH_SERVER_USER").MustString(RunUser)
lafriks (Migrated from github.com) commented 2021-09-28 16:22:27 +00:00

log.Fatal does call os.Exit by design

log.Fatal does call os.Exit by design
techknowlogick reviewed 2021-09-28 16:22:31 +00:00
@ -914,1 +923,4 @@
log.Critical("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.")
}
SSH.BuiltinServerUser = Cfg.Section("server").Key("BUILTIN_SSH_SERVER_USER").MustString(RunUser)
Author
Contributor

They also disable running of root, and the messaging I used is similar to theirs.

They also disable running of root, and the messaging I used is similar to theirs.
techknowlogick reviewed 2021-09-28 16:23:50 +00:00
@ -914,2 +924,4 @@
}
SSH.BuiltinServerUser = Cfg.Section("server").Key("BUILTIN_SSH_SERVER_USER").MustString(RunUser)
Author
Contributor

I thought Fatal will exit Gitea with status 1 already?

Edit: The page didn't update w/ lafrik's post before I submitted mine ?

I thought `Fatal` will exit Gitea with status 1 already? Edit: The page didn't update w/ lafrik's post before I submitted mine ?
techknowlogick reviewed 2021-09-28 16:43:02 +00:00
@ -905,2 +906,4 @@
// 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.
Author
Contributor

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 ?

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 ?
delvh reviewed 2021-09-28 19:29:35 +00:00
@ -914,2 +924,4 @@
}
SSH.BuiltinServerUser = Cfg.Section("server").Key("BUILTIN_SSH_SERVER_USER").MustString(RunUser)
Contributor

Okay, I didn't know that. I did not expect a method called log.Fatal to automatically shutdown the program.
But I can see why it does that.

Okay, I didn't know that. I did not expect a method called `log.Fatal` to automatically shutdown the program. But I can see why it does that.
zeripath reviewed 2021-09-29 05:25:13 +00:00
@ -905,2 +906,4 @@
// 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.
Contributor

Part of me wonders if this should be log.Critical

Part of me wonders if this should be log.Critical
zeripath reviewed 2021-09-29 05:31:19 +00:00
@ -905,2 +906,4 @@
// 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.
Contributor

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

I'd stick a GOOS check here and exclude dozers from this check. Then just use os.GetUID() == 0 instead of checking username.
6543 (Migrated from github.com) reviewed 2021-10-05 23:28:58 +00:00
6543 (Migrated from github.com) commented 2021-10-05 23:28:57 +00:00
		log.Critical("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.")
```suggestion log.Critical("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 (Migrated from github.com) reviewed 2021-10-05 23:41:19 +00:00
@ -905,2 +906,4 @@
// 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.
6543 (Migrated from github.com) commented 2021-10-05 23:41:19 +00:00

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

we dont need to check goos on windows we will get a -1
6543 (Migrated from github.com) approved these changes 2021-10-05 23:41:53 +00:00
6543 (Migrated from github.com) approved these changes 2021-10-05 23:49:02 +00:00
This repo is archived. You cannot comment on pull requests.
No Milestone
No project
No Assignees
5 Participants
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: lunny/gitea#17168
No description provided.