pax_global_header00006660000000000000000000000064141067711730014521gustar00rootroot0000000000000052 comment=9f7cd138a1ebc0684d43ef6046bf723978e8741f gosu-1.14/000077500000000000000000000000001410677117300124235ustar00rootroot00000000000000gosu-1.14/.dockerignore000077700000000000000000000000001410677117300170622.gitignoreustar00rootroot00000000000000gosu-1.14/.github/000077500000000000000000000000001410677117300137635ustar00rootroot00000000000000gosu-1.14/.github/workflows/000077500000000000000000000000001410677117300160205ustar00rootroot00000000000000gosu-1.14/.github/workflows/ci.yml000066400000000000000000000007721410677117300171440ustar00rootroot00000000000000name: CI on: pull_request: push: defaults: run: shell: 'bash -Eeuo pipefail -x {0}' jobs: test: name: Test runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - run: ./build.sh - run: ./test.sh gosu-amd64 - run: ./test.sh gosu-i386 - run: ./test.sh --debian gosu-amd64 - run: ./test.sh --debian gosu-i386 - run: docker build --pull --file hub/Dockerfile.alpine hub - run: docker build --pull --file hub/Dockerfile.debian hub gosu-1.14/.gitignore000066400000000000000000000000271410677117300144120ustar00rootroot00000000000000.git SHA256SUMS* gosu* gosu-1.14/Dockerfile000066400000000000000000000034551410677117300144240ustar00rootroot00000000000000FROM golang:1.16-alpine3.14 RUN apk add --no-cache file # disable CGO for ALL THE THINGS (to help ensure no libc) ENV CGO_ENABLED 0 WORKDIR /go/src/github.com/tianon/gosu COPY go.mod go.sum ./ RUN set -eux; \ go mod download; \ go mod verify ENV BUILD_FLAGS="-v -ldflags '-d -s -w'" COPY *.go ./ # gosu-$(dpkg --print-architecture) RUN set -eux; \ eval "GOARCH=amd64 go build $BUILD_FLAGS -o /go/bin/gosu-amd64"; \ file /go/bin/gosu-amd64; \ /go/bin/gosu-amd64 --version; \ /go/bin/gosu-amd64 nobody id; \ /go/bin/gosu-amd64 nobody ls -l /proc/self/fd RUN set -eux; \ eval "GOARCH=386 go build $BUILD_FLAGS -o /go/bin/gosu-i386"; \ file /go/bin/gosu-i386; \ /go/bin/gosu-i386 --version; \ /go/bin/gosu-i386 nobody id; \ /go/bin/gosu-i386 nobody ls -l /proc/self/fd RUN set -eux; \ eval "GOARCH=arm GOARM=5 go build $BUILD_FLAGS -o /go/bin/gosu-armel"; \ file /go/bin/gosu-armel RUN set -eux; \ eval "GOARCH=arm GOARM=6 go build $BUILD_FLAGS -o /go/bin/gosu-armhf"; \ file /go/bin/gosu-armhf # boo Raspberry Pi, making life hard (armhf-is-v7 vs armhf-is-v6 ...) #RUN set -eux; \ # eval "GOARCH=arm GOARM=7 go build $BUILD_FLAGS -o /go/bin/gosu-armhf"; \ # file /go/bin/gosu-armhf RUN set -eux; \ eval "GOARCH=arm64 go build $BUILD_FLAGS -o /go/bin/gosu-arm64"; \ file /go/bin/gosu-arm64 RUN set -eux; \ eval "GOARCH=mips64le go build $BUILD_FLAGS -o /go/bin/gosu-mips64el"; \ file /go/bin/gosu-mips64el RUN set -eux; \ eval "GOARCH=ppc64le go build $BUILD_FLAGS -o /go/bin/gosu-ppc64el"; \ file /go/bin/gosu-ppc64el RUN set -eux; \ eval "GOARCH=riscv64 go build $BUILD_FLAGS -o /go/bin/gosu-riscv64"; \ file /go/bin/gosu-riscv64 RUN set -eux; \ eval "GOARCH=s390x go build $BUILD_FLAGS -o /go/bin/gosu-s390x"; \ file /go/bin/gosu-s390x RUN set -eux; ls -lAFh /go/bin/gosu-*; file /go/bin/gosu-* gosu-1.14/Dockerfile.test-alpine000066400000000000000000000064761410677117300166560ustar00rootroot00000000000000FROM alpine:3.14 # add "nobody" to ALL groups (makes testing edge cases more interesting) RUN cut -d: -f1 /etc/group | xargs -n1 addgroup nobody RUN { \ echo '#!/bin/sh'; \ echo 'set -ex'; \ echo; \ echo 'spec="$1"; shift'; \ echo; \ echo 'expec="$1"; shift'; \ echo 'real="$(gosu "$spec" id -u):$(gosu "$spec" id -g):$(gosu "$spec" id -G)"'; \ echo '[ "$expec" = "$real" ]'; \ echo; \ echo 'expec="$1"; shift'; \ # have to "|| true" this one because of "id: unknown ID 1000" (rightfully) having a nonzero exit code echo 'real="$(gosu "$spec" id -un):$(gosu "$spec" id -gn):$(gosu "$spec" id -Gn)" || true'; \ echo '[ "$expec" = "$real" ]'; \ } > /usr/local/bin/gosu-t \ && chmod +x /usr/local/bin/gosu-t COPY gosu /usr/local/bin/ # adjust users so we can make sure the tests are interesting RUN chgrp nobody /usr/local/bin/gosu \ && chmod +s /usr/local/bin/gosu ENV GOSU_PLEASE_LET_ME_BE_COMPLETELY_INSECURE_I_GET_TO_KEEP_ALL_THE_PIECES="I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhäuser Gate. All those moments will be lost in time, like tears in rain. Time to die." USER nobody ENV HOME /omg/really/gosu/nowhere # now we should be nobody, ALL groups, and have a bogus useless HOME value RUN id RUN gosu-t 0 "0:0:$(id -G root)" "root:root:$(id -Gn root)" RUN gosu-t 0:0 '0:0:0' 'root:root:root' RUN gosu-t root "0:0:$(id -G root)" "root:root:$(id -Gn root)" RUN gosu-t 0:root '0:0:0' 'root:root:root' RUN gosu-t root:0 '0:0:0' 'root:root:root' RUN gosu-t root:root '0:0:0' 'root:root:root' RUN gosu-t 1000 "1000:$(id -g):$(id -g)" "1000:$(id -gn):$(id -gn)" RUN gosu-t 0:1000 '0:1000:1000' 'root:1000:1000' RUN gosu-t 1000:1000 '1000:1000:1000' '1000:1000:1000' RUN gosu-t root:1000 '0:1000:1000' 'root:1000:1000' RUN gosu-t 1000:root '1000:0:0' '1000:root:root' RUN gosu-t 1000:daemon "1000:$(id -g daemon):$(id -g daemon)" '1000:daemon:daemon' RUN gosu-t games "$(id -u games):$(id -g games):$(id -G games)" 'games:games:games users' RUN gosu-t games:daemon "$(id -u games):$(id -g daemon):$(id -g daemon)" 'games:daemon:daemon' RUN gosu-t 0: "0:0:$(id -G root)" "root:root:$(id -Gn root)" RUN gosu-t '' "$(id -u):$(id -g):$(id -G)" "$(id -un):$(id -gn):$(id -Gn)" RUN gosu-t ':0' "$(id -u):0:0" "$(id -un):root:root" RUN [ "$(gosu 0 env | grep '^HOME=')" = 'HOME=/root' ] RUN [ "$(gosu 0:0 env | grep '^HOME=')" = 'HOME=/root' ] RUN [ "$(gosu root env | grep '^HOME=')" = 'HOME=/root' ] RUN [ "$(gosu 0:root env | grep '^HOME=')" = 'HOME=/root' ] RUN [ "$(gosu root:0 env | grep '^HOME=')" = 'HOME=/root' ] RUN [ "$(gosu root:root env | grep '^HOME=')" = 'HOME=/root' ] RUN [ "$(gosu 0:1000 env | grep '^HOME=')" = 'HOME=/root' ] RUN [ "$(gosu root:1000 env | grep '^HOME=')" = 'HOME=/root' ] RUN [ "$(gosu 1000 env | grep '^HOME=')" = 'HOME=/' ] RUN [ "$(gosu 1000:0 env | grep '^HOME=')" = 'HOME=/' ] RUN [ "$(gosu 1000:root env | grep '^HOME=')" = 'HOME=/' ] RUN [ "$(gosu games env | grep '^HOME=')" = 'HOME=/usr/games' ] RUN [ "$(gosu games:daemon env | grep '^HOME=')" = 'HOME=/usr/games' ] # make sure we error out properly in unexpected cases like an invalid username RUN ! gosu bogus true RUN ! gosu 0day true RUN ! gosu 0:bogus true RUN ! gosu 0:0day true # something missing? some other functionality we could test easily? PR! :D gosu-1.14/Dockerfile.test-debian000066400000000000000000000066701410677117300166240ustar00rootroot00000000000000FROM debian:bullseye-slim # add "nobody" to ALL groups (makes testing edge cases more interesting) RUN cut -d: -f1 /etc/group | xargs -n1 -I'{}' usermod -aG '{}' nobody # emulate Alpine's "games" user (which is part of the "users" group) RUN usermod -aG users games RUN { \ echo '#!/bin/sh'; \ echo 'set -ex'; \ echo; \ echo 'spec="$1"; shift'; \ echo; \ echo 'expec="$1"; shift'; \ echo 'real="$(gosu "$spec" id -u):$(gosu "$spec" id -g):$(gosu "$spec" id -G)"'; \ echo '[ "$expec" = "$real" ]'; \ echo; \ echo 'expec="$1"; shift'; \ # have to "|| true" this one because of "id: unknown ID 1000" (rightfully) having a nonzero exit code echo 'real="$(gosu "$spec" id -un):$(gosu "$spec" id -gn):$(gosu "$spec" id -Gn)" || true'; \ echo '[ "$expec" = "$real" ]'; \ } > /usr/local/bin/gosu-t \ && chmod +x /usr/local/bin/gosu-t COPY gosu /usr/local/bin/ # adjust users so we can make sure the tests are interesting RUN chgrp nogroup /usr/local/bin/gosu \ && chmod +s /usr/local/bin/gosu ENV GOSU_PLEASE_LET_ME_BE_COMPLETELY_INSECURE_I_GET_TO_KEEP_ALL_THE_PIECES="I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhäuser Gate. All those moments will be lost in time, like tears in rain. Time to die." USER nobody ENV HOME /omg/really/gosu/nowhere # now we should be nobody, ALL groups, and have a bogus useless HOME value RUN id RUN gosu-t 0 "0:0:$(id -G root)" "root:root:$(id -Gn root)" RUN gosu-t 0:0 '0:0:0' 'root:root:root' RUN gosu-t root "0:0:$(id -G root)" "root:root:$(id -Gn root)" RUN gosu-t 0:root '0:0:0' 'root:root:root' RUN gosu-t root:0 '0:0:0' 'root:root:root' RUN gosu-t root:root '0:0:0' 'root:root:root' RUN gosu-t 1000 "1000:$(id -g):$(id -g)" "1000:$(id -gn):$(id -gn)" RUN gosu-t 0:1000 '0:1000:1000' 'root:1000:1000' RUN gosu-t 1000:1000 '1000:1000:1000' '1000:1000:1000' RUN gosu-t root:1000 '0:1000:1000' 'root:1000:1000' RUN gosu-t 1000:root '1000:0:0' '1000:root:root' RUN gosu-t 1000:daemon "1000:$(id -g daemon):$(id -g daemon)" '1000:daemon:daemon' RUN gosu-t games "$(id -u games):$(id -g games):$(id -G games)" 'games:games:games users' RUN gosu-t games:daemon "$(id -u games):$(id -g daemon):$(id -g daemon)" 'games:daemon:daemon' RUN gosu-t 0: "0:0:$(id -G root)" "root:root:$(id -Gn root)" RUN gosu-t '' "$(id -u):$(id -g):$(id -G)" "$(id -un):$(id -gn):$(id -Gn)" RUN gosu-t ':0' "$(id -u):0:0" "$(id -un):root:root" RUN [ "$(gosu 0 env | grep '^HOME=')" = 'HOME=/root' ] RUN [ "$(gosu 0:0 env | grep '^HOME=')" = 'HOME=/root' ] RUN [ "$(gosu root env | grep '^HOME=')" = 'HOME=/root' ] RUN [ "$(gosu 0:root env | grep '^HOME=')" = 'HOME=/root' ] RUN [ "$(gosu root:0 env | grep '^HOME=')" = 'HOME=/root' ] RUN [ "$(gosu root:root env | grep '^HOME=')" = 'HOME=/root' ] RUN [ "$(gosu 0:1000 env | grep '^HOME=')" = 'HOME=/root' ] RUN [ "$(gosu root:1000 env | grep '^HOME=')" = 'HOME=/root' ] RUN [ "$(gosu 1000 env | grep '^HOME=')" = 'HOME=/' ] RUN [ "$(gosu 1000:0 env | grep '^HOME=')" = 'HOME=/' ] RUN [ "$(gosu 1000:root env | grep '^HOME=')" = 'HOME=/' ] RUN [ "$(gosu games env | grep '^HOME=')" = 'HOME=/usr/games' ] RUN [ "$(gosu games:daemon env | grep '^HOME=')" = 'HOME=/usr/games' ] # make sure we error out properly in unexpected cases like an invalid username RUN ! gosu bogus true RUN ! gosu 0day true RUN ! gosu 0:bogus true RUN ! gosu 0:0day true # something missing? some other functionality we could test easily? PR! :D gosu-1.14/INSTALL.md000066400000000000000000000063571410677117300140660ustar00rootroot00000000000000# Installation We assume installation inside Docker (probably not the right tool for most use-cases outside Docker), and that you don't have either `wget` or `ca-certificates` already installed -- adjust (and version bump `GOSU_VERSION`) as necessary! ## `FROM debian` [Debian 9 ("Debian Stretch") or newer](https://packages.debian.org/gosu): ```dockerfile RUN set -eux; \ apt-get update; \ apt-get install -y gosu; \ rm -rf /var/lib/apt/lists/*; \ # verify that the binary works gosu nobody true ``` Older Debian releases (or newer `gosu` releases): ```dockerfile ENV GOSU_VERSION 1.14 RUN set -eux; \ # save list of currently installed packages for later so we can clean up savedAptMark="$(apt-mark showmanual)"; \ apt-get update; \ apt-get install -y --no-install-recommends ca-certificates wget; \ if ! command -v gpg; then \ apt-get install -y --no-install-recommends gnupg2 dirmngr; \ elif gpg --version | grep -q '^gpg (GnuPG) 1\.'; then \ # "This package provides support for HKPS keyservers." (GnuPG 1.x only) apt-get install -y --no-install-recommends gnupg-curl; \ fi; \ rm -rf /var/lib/apt/lists/*; \ \ dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')"; \ wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch"; \ wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc"; \ \ # verify the signature export GNUPGHOME="$(mktemp -d)"; \ gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4; \ gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu; \ command -v gpgconf && gpgconf --kill all || :; \ rm -rf "$GNUPGHOME" /usr/local/bin/gosu.asc; \ \ # clean up fetch dependencies apt-mark auto '.*' > /dev/null; \ [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; \ apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \ \ chmod +x /usr/local/bin/gosu; \ # verify that the binary works gosu --version; \ gosu nobody true ``` ## `FROM alpine` (3.7+) **Note:** when using Alpine, it's probably also worth checking out [`su-exec`](https://github.com/ncopa/su-exec) (`apk add --no-cache su-exec`) instead, which since version 0.2 is fully `gosu`-compatible in a fraction of the file size. ```dockerfile ENV GOSU_VERSION 1.14 RUN set -eux; \ \ apk add --no-cache --virtual .gosu-deps \ ca-certificates \ dpkg \ gnupg \ ; \ \ dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')"; \ wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch"; \ wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc"; \ \ # verify the signature export GNUPGHOME="$(mktemp -d)"; \ gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4; \ gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu; \ command -v gpgconf && gpgconf --kill all || :; \ rm -rf "$GNUPGHOME" /usr/local/bin/gosu.asc; \ \ # clean up fetch dependencies apk del --no-network .gosu-deps; \ \ chmod +x /usr/local/bin/gosu; \ # verify that the binary works gosu --version; \ gosu nobody true ``` gosu-1.14/LICENSE000066400000000000000000000261361410677117300134400ustar00rootroot00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. gosu-1.14/README.md000066400000000000000000000111141410677117300137000ustar00rootroot00000000000000# 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). ```console $ 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`](https://github.com/tianon/gosu/issues/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`](INSTALL.md). ## Why? ```console $ 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`](Dockerfile.test) for the "test suite" (and the associated [`test.sh`](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](https://stackoverflow.com/a/48105623) for a short explanation of why `sudo` does `fork`+`exec` instead of just `exec`.) ## Alternatives ### `su-exec` As mentioned in `INSTALL.md`, [`su-exec`](https://github.com/ncopa/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: ```console $ 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): ```console $ 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`) gosu-1.14/build.sh000077500000000000000000000004721410677117300140640ustar00rootroot00000000000000#!/bin/bash set -e cd "$(dirname "$(readlink -f "$BASH_SOURCE")")" set -x docker build --pull -t gosu . rm -f gosu* SHA256SUMS* docker run --rm gosu sh -c 'cd /go/bin && tar -c gosu*' | tar -xv sha256sum gosu* | tee SHA256SUMS file gosu* ls -lFh gosu* SHA256SUMS* "./gosu-$(dpkg --print-architecture)" --help gosu-1.14/go.mod000066400000000000000000000002341410677117300135300ustar00rootroot00000000000000module github.com/tianon/gosu go 1.16 require ( github.com/opencontainers/runc v1.0.1 golang.org/x/sys v0.0.0-20210817142637-7d9622a276b7 // indirect ) gosu-1.14/go.sum000066400000000000000000000150311410677117300135560ustar00rootroot00000000000000github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/bits-and-blooms/bitset v1.2.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA= github.com/checkpoint-restore/go-criu/v5 v5.0.0/go.mod h1:cfwC0EG7HMUenopBsUf9d89JlCLQIfgVcNsNN0t6T2M= github.com/cilium/ebpf v0.6.2/go.mod h1:4tRaxcgiL706VnOzHOdBlY8IEAIdxINsQBcU4xJJXRs= github.com/containerd/console v1.0.2/go.mod h1:ytZPjGgY2oeTkAONYafi2kSj0aYggsf8acV1PGKCbzQ= github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/cyphar/filepath-securejoin v0.2.2/go.mod h1:FpkQEhXnPnOthhzymB7CGsFk2G9VLXONKD9G7QGMM+4= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/moby/sys/mountinfo v0.4.1/go.mod h1:rEr8tzG/lsIZHBtN/JjGG+LMYx9eXgW2JI+6q0qou+A= github.com/mrunalp/fileutils v0.5.0/go.mod h1:M1WthSahJixYnrXQl/DFQuteStB1weuxD2QJNHXfbSQ= github.com/opencontainers/runc v1.0.1 h1:G18PGckGdAm3yVQRWDVQ1rLSLntiniKJ0cNRT2Tm5gs= github.com/opencontainers/runc v1.0.1/go.mod h1:aTaHFFwQXuA71CiyxOdFFIorAoemI04suvGRQFzWTD0= github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= github.com/opencontainers/selinux v1.8.2/go.mod h1:MUIHuUEvKB1wtJjQdOyYRgOnLD2xAPP8dBsCoU0KuF8= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/seccomp/libseccomp-golang v0.9.1/go.mod h1:GbW5+tmTXfcxTToHLXlScSlAvWlF4P2Ca7zGrPiEpWo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE= github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU= golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191115151921-52ab43148777/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210426230700-d19ff857e887/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210817142637-7d9622a276b7 h1:lQ8Btl/sJr2+f4ql7ffKUKfnV0BsgsICvm0oEeINAQY= golang.org/x/sys v0.0.0-20210817142637-7d9622a276b7/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gosu-1.14/hub/000077500000000000000000000000001410677117300132015ustar00rootroot00000000000000gosu-1.14/hub/Dockerfile.alpine000066400000000000000000000016321410677117300164440ustar00rootroot00000000000000FROM alpine:3.14 # https://github.com/tianon/gosu/releases ENV GOSU_VERSION 1.14 RUN set -eux; \ apk add --no-cache --virtual .fetch-deps dpkg gnupg; \ dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')"; \ wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc"; \ wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch"; \ GNUPGHOME="$(mktemp -d)"; export GNUPGHOME; \ gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4; \ gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu; \ gpgconf --kill all; \ rm -rf "$GNUPGHOME"; unset GNUPGHOME; \ apk del --no-network .fetch-deps; \ chmod +x /usr/local/bin/gosu; \ gosu --version; \ gosu nobody true; \ # hard link to / for ease of COPY --from ln -v /usr/local/bin/gosu / gosu-1.14/hub/Dockerfile.debian000066400000000000000000000022611410677117300164150ustar00rootroot00000000000000FROM debian:bullseye-slim # https://github.com/tianon/gosu/releases ENV GOSU_VERSION 1.13 RUN set -eux; \ savedAptMark="$(apt-mark showmanual)"; \ apt-get update; \ apt-get install -y --no-install-recommends ca-certificates dirmngr gnupg wget; \ rm -rf /var/lib/apt/lists/*; \ dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')"; \ wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc"; \ wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch"; \ GNUPGHOME="$(mktemp -d)"; export GNUPGHOME; \ gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4; \ gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu; \ gpgconf --kill all; \ rm -rf "$GNUPGHOME"; unset GNUPGHOME; \ apt-mark auto '.*' > /dev/null; \ [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark > /dev/null; \ apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \ chmod +x /usr/local/bin/gosu; \ gosu --version; \ gosu nobody true; \ # hard link to / for ease of COPY --from ln -v /usr/local/bin/gosu / gosu-1.14/hub/alpine.yml000066400000000000000000000012511410677117300151730ustar00rootroot00000000000000image: tianon/gosu:alpine manifests: - { image: tianon/gosu:alpine-amd64, platform: { os: linux, architecture: amd64 } } - { image: tianon/gosu:alpine-arm32v6, platform: { os: linux, architecture: arm, variant: v6 } } - { image: tianon/gosu:alpine-arm32v7, platform: { os: linux, architecture: arm, variant: v7 } } - { image: tianon/gosu:alpine-arm64v8, platform: { os: linux, architecture: arm64, variant: v8 } } - { image: tianon/gosu:alpine-i386, platform: { os: linux, architecture: 386 } } - { image: tianon/gosu:alpine-ppc64le, platform: { os: linux, architecture: ppc64le } } - { image: tianon/gosu:alpine-s390x, platform: { os: linux, architecture: s390x } } gosu-1.14/hub/build.sh000077500000000000000000000035431410677117300146440ustar00rootroot00000000000000#!/usr/bin/env bash set -Eeuo pipefail declare -A platforms=( [amd64]='linux/amd64' [arm32v5]='linux/arm/v5' [arm32v6]='linux/arm/v6' [arm32v7]='linux/arm/v7' [arm64v8]='linux/arm64/v8' [i386]='linux/386' [mips64le]='linux/mips64le' [ppc64le]='linux/ppc64le' [s390x]='linux/s390x' ) declare -A arches=( [alpine]='amd64 arm32v6 arm32v7 arm64v8 i386 ppc64le s390x' [debian]='amd64 arm32v5 arm32v7 arm64v8 i386 mips64le ppc64le s390x' ) preferredOrder=( alpine debian ) _platformToOCI() { local platform="$1"; shift local os="${platform%%/*}" platform="${platform#$os/}" local architecture="${platform%%/*}" platform="${platform#$architecture/}" local variant="$platform" [ "$architecture" != "$variant" ] || variant= echo "{ os: $os, architecture: $architecture${variant:+, variant: $variant} }" } declare -A latest=() for variant in "${preferredOrder[@]}"; do cat > "$variant.yml" <<-EOYAML image: tianon/gosu:$variant manifests: EOYAML for arch in ${arches[$variant]}; do platform="${platforms[$arch]}" docker build --pull --platform "$platform" --tag "tianon/gosu:$variant-$arch" - < "Dockerfile.$variant" : "${latest[$arch]:=$variant}" platform="$(_platformToOCI "$platform")" echo " - { image: tianon/gosu:$variant-$arch, platform: $platform }" >> "$variant.yml" done done cat > latest.yml <<-'EOYAML' image: tianon/gosu:latest manifests: EOYAML mapfile -d '' sorted < <(printf '%s\0' "${!latest[@]}" | sort -z) for arch in "${sorted[@]}"; do variant="${latest[$arch]}" docker tag "tianon/gosu:$variant-$arch" "tianon/gosu:$arch" platform="$(_platformToOCI "${platforms[$arch]}")" echo " - { image: tianon/gosu:$arch, platform: $platform }" >> latest.yml done echo echo '$ # now:' echo echo '$ docker push --all-tags tianon/gosu' for variant in "${preferredOrder[@]}" latest; do echo "\$ manifest-tool push from-spec $variant.yml" done gosu-1.14/hub/debian.yml000066400000000000000000000014051410677117300151460ustar00rootroot00000000000000image: tianon/gosu:debian manifests: - { image: tianon/gosu:debian-amd64, platform: { os: linux, architecture: amd64 } } - { image: tianon/gosu:debian-arm32v5, platform: { os: linux, architecture: arm, variant: v5 } } - { image: tianon/gosu:debian-arm32v7, platform: { os: linux, architecture: arm, variant: v7 } } - { image: tianon/gosu:debian-arm64v8, platform: { os: linux, architecture: arm64, variant: v8 } } - { image: tianon/gosu:debian-i386, platform: { os: linux, architecture: 386 } } - { image: tianon/gosu:debian-mips64le, platform: { os: linux, architecture: mips64le } } - { image: tianon/gosu:debian-ppc64le, platform: { os: linux, architecture: ppc64le } } - { image: tianon/gosu:debian-s390x, platform: { os: linux, architecture: s390x } } gosu-1.14/hub/latest.yml000066400000000000000000000014511410677117300152210ustar00rootroot00000000000000image: tianon/gosu:latest manifests: - { image: tianon/gosu:amd64, platform: { os: linux, architecture: amd64 } } - { image: tianon/gosu:arm32v5, platform: { os: linux, architecture: arm, variant: v5 } } - { image: tianon/gosu:arm32v6, platform: { os: linux, architecture: arm, variant: v6 } } - { image: tianon/gosu:arm32v7, platform: { os: linux, architecture: arm, variant: v7 } } - { image: tianon/gosu:arm64v8, platform: { os: linux, architecture: arm64, variant: v8 } } - { image: tianon/gosu:i386, platform: { os: linux, architecture: 386 } } - { image: tianon/gosu:mips64le, platform: { os: linux, architecture: mips64le } } - { image: tianon/gosu:ppc64le, platform: { os: linux, architecture: ppc64le } } - { image: tianon/gosu:s390x, platform: { os: linux, architecture: s390x } } gosu-1.14/main.go000066400000000000000000000047011410677117300137000ustar00rootroot00000000000000package main // import "github.com/tianon/gosu" import ( "bytes" "fmt" "log" "os" "os/exec" "path/filepath" "runtime" "strings" "syscall" "text/template" ) func init() { // make sure we only have one process and that it runs on the main thread (so that ideally, when we Exec, we keep our user switches and stuff) runtime.GOMAXPROCS(1) runtime.LockOSThread() } func version() string { return fmt.Sprintf(`%s (%s on %s/%s; %s)`, Version, runtime.Version(), runtime.GOOS, runtime.GOARCH, runtime.Compiler) } func usage() string { t := template.Must(template.New("usage").Parse(` Usage: {{ .Self }} user-spec command [args] eg: {{ .Self }} tianon bash {{ .Self }} nobody:root bash -c 'whoami && id' {{ .Self }} 1000:1 id {{ .Self }} version: {{ .Version }} {{ .Self }} license: Apache-2.0 (full text at https://github.com/tianon/gosu) `)) var b bytes.Buffer template.Must(t, t.Execute(&b, struct { Self string Version string }{ Self: filepath.Base(os.Args[0]), Version: version(), })) return strings.TrimSpace(b.String()) + "\n" } func main() { log.SetFlags(0) // no timestamps on our logs if ok := os.Getenv("GOSU_PLEASE_LET_ME_BE_COMPLETELY_INSECURE_I_GET_TO_KEEP_ALL_THE_PIECES"); ok != "I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhäuser Gate. All those moments will be lost in time, like tears in rain. Time to die." { if fi, err := os.Stat("/proc/self/exe"); err != nil { log.Fatalf("error: %v", err) } else if fi.Mode()&os.ModeSetuid != 0 { // ... oh no log.Fatalf("error: %q appears to be installed with the 'setuid' bit set, which is an *extremely* insecure and completely unsupported configuration! (what you want instead is likely 'sudo' or 'su')", os.Args[0]) } } if len(os.Args) >= 2 { switch os.Args[1] { case "--help", "-h", "-?": fmt.Println(usage()) os.Exit(0) case "--version", "-v": fmt.Println(version()) os.Exit(0) } } if len(os.Args) <= 2 { log.Println(usage()) os.Exit(1) } // clear HOME so that SetupUser will set it os.Unsetenv("HOME") if err := SetupUser(os.Args[1]); err != nil { log.Fatalf("error: failed switching to %q: %v", os.Args[1], err) } name, err := exec.LookPath(os.Args[2]) if err != nil { log.Fatalf("error: %v", err) } if err = syscall.Exec(name, os.Args[2:], os.Environ()); err != nil { log.Fatalf("error: exec failed: %v", err) } } gosu-1.14/setup-user.go000066400000000000000000000024221410677117300150660ustar00rootroot00000000000000package main import ( "os" "syscall" "github.com/opencontainers/runc/libcontainer/system" "github.com/opencontainers/runc/libcontainer/user" ) // this function comes from libcontainer/init_linux.go // we don't use that directly because we don't want the whole namespaces package imported here // (also, because we need minor modifications and it's not even exported) // SetupUser changes the groups, gid, and uid for the user inside the container func SetupUser(u string) error { // Set up defaults. defaultExecUser := user.ExecUser{ Uid: syscall.Getuid(), Gid: syscall.Getgid(), Home: "/", } passwdPath, err := user.GetPasswdPath() if err != nil { return err } groupPath, err := user.GetGroupPath() if err != nil { return err } execUser, err := user.GetExecUserPath(u, &defaultExecUser, passwdPath, groupPath) if err != nil { return err } if err := syscall.Setgroups(execUser.Sgids); err != nil { return err } if err := system.Setgid(execUser.Gid); err != nil { return err } if err := system.Setuid(execUser.Uid); err != nil { return err } // if we didn't get HOME already, set it based on the user's HOME if envHome := os.Getenv("HOME"); envHome == "" { if err := os.Setenv("HOME", execUser.Home); err != nil { return err } } return nil } gosu-1.14/sign.sh000077500000000000000000000004311410677117300137200ustar00rootroot00000000000000#!/bin/bash set -e cd "$(dirname "$(readlink -f "$BASH_SOURCE")")" set -x rm -f gosu*.asc SHA256SUMS.asc for f in gosu*; do gpg --output "$f.asc" --detach-sign "$f" done sha256sum gosu* > SHA256SUMS gpg --output SHA256SUMS.asc --detach-sign SHA256SUMS ls -lFh gosu* SHA256SUMS* gosu-1.14/test.sh000077500000000000000000000022231410677117300137400ustar00rootroot00000000000000#!/usr/bin/env bash set -Eeuo pipefail usage() { echo "usage: $0 [--platform] gosu-binary" echo " eg: $0 ./gosu-amd64" echo " $0 --debian ./gosu-amd64" } df='Dockerfile.test-alpine' case "${1:-}" in --alpine | --debian) df="Dockerfile.test-${1#--}" shift ;; esac gosu="${1:-}" shift || { usage >&2; exit 1; } [ -f "$gosu" ] || { usage >&2; exit 1; } trap '{ set +x; echo; echo FAILED; echo; } >&2' ERR set -x dir="$(mktemp -d -t gosu-test-XXXXXXXXXX)" base="$(basename "$dir")" img="gosu-test:$base" trap "rm -rf '$dir'" EXIT cp -T "$df" "$dir/Dockerfile" cp -T "$gosu" "$dir/gosu" docker build -t "$img" "$dir" rm -rf "$dir" trap - EXIT trap "docker rm -f '$base' > /dev/null; docker rmi -f '$img' > /dev/null" EXIT # using explicit "--init=false" in case dockerd is running with "--init" (because that will skew our process numbers) docker run -d --init=false --name "$base" "$img" gosu root sleep 1000 sleep 1 # give it plenty of time to get through "gosu" and into the "sleep" [ "$(docker top "$base" | wc -l)" = 2 ] # "docker top" should have only two lines # -- ps headers and a single line for the single process running in the container gosu-1.14/version.go000066400000000000000000000000451410677117300144360ustar00rootroot00000000000000package main const Version = "1.14"