@@ -27,6 +27,11 @@ export P_CI_DIR="$PWD"
27
27
28
28
if [ -z " $DANGER_RUN_CI_ON_HOST " ]; then
29
29
echo " Creating $DOCKER_NAME_TAG container to run in"
30
+ LOCAL_UID=$( id -u)
31
+ LOCAL_GID=$( id -g)
32
+
33
+ # the name isn't important, so long as we use the same UID
34
+ LOCAL_USER=nonroot
30
35
${CI_RETRY_EXE} docker pull " $DOCKER_NAME_TAG "
31
36
32
37
if [ -n " ${RESTART_CI_DOCKER_BEFORE_RUN} " ] ; then
@@ -44,34 +49,47 @@ if [ -z "$DANGER_RUN_CI_ON_HOST" ]; then
44
49
--env-file /tmp/env \
45
50
--name $CONTAINER_NAME \
46
51
$DOCKER_NAME_TAG )
47
- export DOCKER_CI_CMD_PREFIX=" docker exec $DOCKER_ID "
52
+
53
+ # Create a non-root user inside the container which matches the local user.
54
+ #
55
+ # This prevents the root user in the container modifying the local file system permissions
56
+ # on the mounted directories
57
+ docker exec " $DOCKER_ID " useradd -u " $LOCAL_UID " -o -m " $LOCAL_USER "
58
+ docker exec " $DOCKER_ID " groupmod -o -g " $LOCAL_GID " " $LOCAL_USER "
59
+ docker exec " $DOCKER_ID " chown -R " $LOCAL_USER " :" $LOCAL_USER " " ${BASE_ROOT_DIR} "
60
+ export DOCKER_CI_CMD_PREFIX_ROOT=" docker exec -u 0 $DOCKER_ID "
61
+ export DOCKER_CI_CMD_PREFIX=" docker exec -u $LOCAL_UID $DOCKER_ID "
48
62
else
49
63
echo " Running on host system without docker wrapper"
50
64
fi
51
65
52
66
CI_EXEC () {
53
67
$DOCKER_CI_CMD_PREFIX bash -c " export PATH=$BASE_SCRATCH_DIR /bins/:\$ PATH && cd \" $P_CI_DIR \" && $* "
54
68
}
69
+ CI_EXEC_ROOT () {
70
+ $DOCKER_CI_CMD_PREFIX_ROOT bash -c " export PATH=$BASE_SCRATCH_DIR /bins/:\$ PATH && cd \" $P_CI_DIR \" && $* "
71
+ }
55
72
export -f CI_EXEC
73
+ export -f CI_EXEC_ROOT
56
74
57
75
if [ -n " $DPKG_ADD_ARCH " ]; then
58
- CI_EXEC dpkg --add-architecture " $DPKG_ADD_ARCH "
76
+ CI_EXEC_ROOT dpkg --add-architecture " $DPKG_ADD_ARCH "
59
77
fi
60
78
61
79
if [[ $DOCKER_NAME_TAG == * centos* ]]; then
62
- ${CI_RETRY_EXE} CI_EXEC dnf -y install epel-release
63
- ${CI_RETRY_EXE} CI_EXEC dnf -y --allowerasing install " $DOCKER_PACKAGES " " $PACKAGES "
80
+ ${CI_RETRY_EXE} CI_EXEC_ROOT dnf -y install epel-release
81
+ ${CI_RETRY_EXE} CI_EXEC_ROOT dnf -y --allowerasing install " $DOCKER_PACKAGES " " $PACKAGES "
64
82
elif [ " $CI_USE_APT_INSTALL " != " no" ]; then
65
83
if [[ " ${ADD_UNTRUSTED_BPFCC_PPA} " == " true" ]]; then
66
84
# Ubuntu 22.04 LTS and Debian 11 both have an outdated bpfcc-tools packages.
67
85
# The iovisor PPA is outdated as well. The next Ubuntu and Debian releases will contain updated
68
86
# packages. Meanwhile, use an untrusted PPA to install an up-to-date version of the bpfcc-tools
69
87
# package.
70
88
# TODO: drop this once we can use newer images in GCE
71
- CI_EXEC add-apt-repository ppa:hadret/bpfcc
89
+ CI_EXEC_ROOT add-apt-repository ppa:hadret/bpfcc
72
90
fi
73
- ${CI_RETRY_EXE} CI_EXEC apt-get update
74
- ${CI_RETRY_EXE} CI_EXEC apt-get install --no-install-recommends --no-upgrade -y " $PACKAGES " " $DOCKER_PACKAGES "
91
+ ${CI_RETRY_EXE} CI_EXEC_ROOT apt-get update
92
+ ${CI_RETRY_EXE} CI_EXEC_ROOT apt-get install --no-install-recommends --no-upgrade -y " $PACKAGES " " $DOCKER_PACKAGES "
75
93
fi
76
94
77
95
if [ -n " $PIP_PACKAGES " ]; then
@@ -126,7 +144,7 @@ if [[ "${RUN_TIDY}" == "true" ]]; then
126
144
CI_EXEC " mkdir -p ${DIR_IWYU} /build/"
127
145
CI_EXEC " git clone --depth=1 https://github.com/include-what-you-use/include-what-you-use -b clang_14 ${DIR_IWYU} /include-what-you-use"
128
146
CI_EXEC " cd ${DIR_IWYU} /build && cmake -G 'Unix Makefiles' -DCMAKE_PREFIX_PATH=/usr/lib/llvm-14 ../include-what-you-use"
129
- CI_EXEC " cd ${DIR_IWYU} /build && make install $MAKEJOBS "
147
+ CI_EXEC_ROOT " cd ${DIR_IWYU} /build && make install $MAKEJOBS "
130
148
fi
131
149
fi
132
150
0 commit comments