• Stars
    star
    4,679
  • Rank 9,042 (Top 0.2 %)
  • Language
    Shell
  • License
    Apache License 2.0
  • Created over 10 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple Go-based setuid+setgid+setgroups+exec

gosu

This is a simple tool grown out of the simple fact that su and sudo have very strange and often annoying TTY and signal-forwarding behavior. They're also somewhat complex to setup and use (especially in the case of sudo), which allows for a great deal of expressivity, but falls flat if all you need is "run this specific application as this specific user and get out of the pipeline".

The core of how gosu works is stolen directly from how Docker/libcontainer itself starts an application inside a container (and in fact, is using the /etc/passwd processing code directly from libcontainer's codebase).

$ gosu
Usage: ./gosu user-spec command [args]
   eg: ./gosu tianon bash
       ./gosu nobody:root bash -c 'whoami && id'
       ./gosu 1000:1 id

./gosu version: 1.1 (go1.3.1 on linux/amd64; gc)

Once the user/group is processed, we switch to that user, then we exec the specified process and gosu itself is no longer resident or involved in the process lifecycle at all. This avoids all the issues of signal passing and TTY, and punts them to the process invoking gosu and the process being invoked by gosu, where they belong.

Warning

The core use case for gosu is to step down from root to a non-privileged user during container startup (specifically in the ENTRYPOINT, usually).

Uses of gosu beyond that could very well suffer from vulnerabilities such as CVE-2016-2779 (from which the Docker use case naturally shields us); see tianon/gosu#37 for some discussion around this point.

Installation

High-level steps:

  1. download gosu-$(dpkg --print-architecture | awk -F- '{ print $NF }') as gosu
  2. download gosu-$(dpkg --print-architecture | awk -F- '{ print $NF }').asc as gosu.asc
  3. fetch my public key (to verify your download): gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4
  4. gpg --batch --verify gosu.asc gosu
  5. chmod +x gosu

For explicit Dockerfile instructions, see INSTALL.md.

Why?

$ docker run -it --rm ubuntu:trusty su -c 'exec ps aux'
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  46636  2688 ?        Ss+  02:22   0:00 su -c exec ps a
root         6  0.0  0.0  15576  2220 ?        Rs   02:22   0:00 ps aux
$ docker run -it --rm ubuntu:trusty sudo ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  3.0  0.0  46020  3144 ?        Ss+  02:22   0:00 sudo ps aux
root         7  0.0  0.0  15576  2172 ?        R+   02:22   0:00 ps aux
$ docker run -it --rm -v $PWD/gosu-amd64:/usr/local/bin/gosu:ro ubuntu:trusty gosu root ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0   7140   768 ?        Rs+  02:22   0:00 ps aux

Additionally, due to the fact that gosu is using Docker's own code for processing these user:group, it has exact 1:1 parity with Docker's own --user flag.

If you're curious about the edge cases that gosu handles, see Dockerfile.test for the "test suite" (and the associated test.sh script that wraps this up for testing arbitrary binaries).

(Note that sudo has different goals from this project, and it is not intended to be a sudo replacement; for example, see this Stack Overflow answer for a short explanation of why sudo does fork+exec instead of just exec.)

Alternatives

su-exec

As mentioned in INSTALL.md, su-exec is a very minimal re-write of gosu in C, making for a much smaller binary, and is available in the main Alpine package repository.

chroot

With the --userspec flag, chroot can provide similar benefits/behavior:

$ docker run -it --rm ubuntu:trusty chroot --userspec=nobody / ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
nobody       1  5.0  0.0   7136   756 ?        Rs+  17:04   0:00 ps aux

setpriv

Available in newer util-linux (>= 2.32.1-0.2, in Debian; https://manpages.debian.org/buster/util-linux/setpriv.1.en.html):

$ docker run -it --rm buildpack-deps:buster-scm setpriv --reuid=nobody --regid=nogroup --init-groups ps faux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
nobody       1  5.0  0.0   9592  1252 pts/0    RNs+ 23:21   0:00 ps faux

Others

I'm not terribly familiar with them, but a few other alternatives I'm aware of include:

  • chpst (part of runit)

More Repositories

1

dockerfiles

A semi-random collection of odds and ends.
Dockerfile
1,042
star
2

docker-postgres-upgrade

a PoC for using "pg_upgrade" inside Docker -- learn from it, adapt it for your needs; don't expect it to work as-is!
Shell
1,039
star
3

docker-brew-ubuntu-core

DEPRECATED; see https://github.com/tianon/docker-brew-ubuntu-core/pull/248
Shell
586
star
4

cgroupfs-mount

Simple (outdated) scripts to mount the cgroupfs (v1) hierarchy, especially structured for Debian packaging
Shell
193
star
5

rawdns

a direct, raw DNS interface to the Docker API
Go
185
star
6

docker-qemu

Dockerization of supported QEMU releases
Shell
120
star
7

docker-brew-debian

DEPRECATED in favor of https://github.com/debuerreotype/docker-debian-artifacts
Shell
114
star
8

gosleep

"sleep(1)", but with Go duration parsing OR explicit time to sleep until and a progress bar
Go
95
star
9

docker-bash

Many versions of Bash, Dockerized
Shell
58
star
10

boot2docker-debian

a newly re-imagined boot2docker, based on Debian for stability and maintainability
Shell
55
star
11

docker-overlay

Gentoo overlay for Docker
Shell
51
star
12

docker-syncthing

Dockerization of supported versions of the popular syncthing utility (https://syncthing.net)
Shell
38
star
13

docker-bin

A small collection of useful scripts (lives in my PATH)
Shell
33
star
14

pgp-happy-eyeballs

DEPRECATED; like "happy eyeballs" (ipv4/ipv6), but for flaky PGP gossip servers
Go
33
star
15

squignix

NGINX, configured to act like Squid
Dockerfile
28
star
16

docker-dind-cluster

simple docker-compose setup for running a quick Docker-in-Docker Swarm cluster for testing
19
star
17

debian-golang-pty

https://anonscm.debian.org/cgit/pkg-go/packages/golang-pty.git
Go
17
star
18

gentoo-overlay

Shell
15
star
19

debian-golang-dbus

https://anonscm.debian.org/cgit/pkg-go/packages/golang-dbus.git
Go
15
star
20

jenkins-groovy

Just a collection of my Jenkins Groovy nonsense (especially Job DSL scripts)
Groovy
13
star
21

vim-docker

An automatic mirror of Vim Dockerfile syntax files (see README)
11
star
22

home

dotfiles
Shell
10
star
23

linux-rtlwifi-8188ce

A mirror of the "official" Realtek kernel module for the 8188CE (and others) - note that this driver is really no better or worse than the one directly in-kernel, since the driver can't fix horrid hardware. This also probably doesn't compile properly on newer kernels.
C
9
star
24

docker-brew-ubuntu

OLD SOURCE OF STACKBREW IMAGES
Shell
8
star
25

mirror-refind

Mirror of https://git.code.sf.net/p/refind/code (https://sourceforge.net/p/refind/code)
C
8
star
26

docker-brew-ubuntu-debootstrap

DEPRECATED
Shell
8
star
27

debian-security-tracker-mirror

DEPRECATED in favor of https://salsa.debian.org/security-tracker-team/security-tracker/tree/master/data
Shell
8
star
28

docker-gentoo-stage3

Shell
7
star
29

debian-moby

Debian packaging for Docker bits (Docker CLI, Engine, containerd, etc.)
Makefile
7
star
30

docker-moosefs

Dockerization of supported versions of MooseFS (https://moosefs.com/) + a Dockerized Docker volume driver
Go
7
star
31

bash

Mirror of https://git.savannah.gnu.org/cgit/bash.git
C
5
star
32

go-progress

simple Go progress bar inspired by PyPi's "progress" package
Go
4
star
33

musl

Mirror of http://git.musl-libc.org/cgit/musl
C
4
star
34

docker-brew-cirros

Imports of the CirrOS tarballs for use in Docker
Shell
4
star
35

go-aptsources

WIP package to deal with apt sources, especially generating "sources.list"; see also https://pault.ag/go/debian
Go
4
star
36

debian-bin

A collection of scripts/utilities to help with various Debian packaging prep and building
Shell
4
star
37

uscan-helper

https://wiki.debian.org/debian/watch
Go
4
star
38

pinkpogo-scripts

Useful scripts for Debian on my PogoPlug which may or may not be pinkpogo-specific - use at your own risk.
Shell
4
star
39

debian-golang-gocapability

https://anonscm.debian.org/cgit/pkg-go/packages/golang-gocapability-dev.git
Go
3
star
40

dockerhub-public-proxy

A simple proxy for public Docker Hub resources to improve their explicit cacheability
Perl
3
star
41

gdbuild

"docker build" for Debian packages
Go
3
star
42

debian-docker

https://anonscm.debian.org/cgit/docker/docker.io.git
3
star
43

abstract-sockets

An attempt to document the types of applications which might be using abstract sockets by default. PLEASE READ THE README (esp. the bit about responsible disclosure).
3
star
44

docker-tini-tags

Mapping Docker releases to tini commits (https://github.com/tianon/docker-tini-tags/tags)
2
star
45

docker-machine-driver-triton

Go
2
star
46

docker-elk-stack

2
star
47

debian-rawdns

https://anonscm.debian.org/cgit/docker/rawdns.git
Go
2
star
48

one

The "One" True Way (this was a misguided experiment -- don't use it!)
Shell
2
star
49

tianon.github.io

Tianon's Ramblings ✿
CSS
2
star
50

backups3

(abandon-ware; school project) Performs backups to Amazon's S3 service.
Perl
2
star
51

debian-golang-go-systemd

https://anonscm.debian.org/cgit/pkg-go/packages/golang-go-systemd.git
Go
2
star
52

cirros

(DEPRECATED) Mirror of https://git.launchpad.net/cirros
2
star
53

2016-container-summit-vegas

My talk "slides" from Container Summit 2016 in Las Vegas, NV
C
2
star
54

debian-runc

https://anonscm.debian.org/cgit/pkg-go/packages/runc.git
1
star
55

rouge

(abandon-ware) A remote server incremental backup system (using git) for performing backups of many computers easily and centrally.
Perl
1
star
56

foobar

DO NOT USE
Shell
1
star
57

munin-plugins

A collection of the public Munin plugins I've written or adapted in some way.
Perl
1
star
58

docker-dart-sample

Dockerfile
1
star
59

docker-prometheus-example

1
star
60

docker-deb-vendored

a mockup of Debian packaging which vendors _all_ components of Docker including Go itself (using multiorig tarballs)
1
star
61

containerd-registry

A basic OCI registry built on top of containerd's image/content store
Go
1
star
62

dtodo

rough "TODO" list generator for in-progress Debian packages
Go
1
star
63

docker-brew-alpine-multiarch

PROOF OF CONCEPT ONLY -- this is a PoC to show off what an official-images multiarch Alpine repo might look like
Shell
1
star
64

docker-runc-tags

Mapping Docker releases to runc commits (https://github.com/tianon/docker-runc-tags/tags)
1
star
65

docker-libnetwork-tags

Mapping Docker releases to libnetwork commits (https://github.com/tianon/docker-libnetwork-tags/tags)
1
star
66

aufs-util

(DEPRECATED) Mirror of https://git.code.sf.net/p/aufs/aufs-util
1
star
67

docker-museum

Shell
1
star
68

debian-containerd

https://anonscm.debian.org/cgit/pkg-go/packages/containerd.git
Go
1
star
69

docker-containerd-tags

Mapping Docker releases to containerd commits (https://github.com/tianon/docker-containerd-tags/tags)
1
star
70

bashbrew-tianon

Shell
1
star