Skip to content

Commit 3c375a8

Browse files
committed
Unify and fix rootless key setup
For some reason, ssh-keygen is unable to write to /root even as root on AlmaLinux 8: # id uid=0(root) gid=0(root) groups=0(root) context=system_u:system_r:initrc_t:s0 # id -Z ls -ld /root # ssh-keygen -t ecdsa -N "" -f /root/rootless.key || cat /var/log/audit/audit.log Saving key "/root/rootless.key" failed: Permission denied The audit.log shows: > type=AVC msg=audit(1744834995.352:546): avc: denied { dac_override } for pid=13471 comm="ssh-keygen" capability=1 scontext=system_u:system_r:ssh_keygen_t:s0 tcontext=system_u:system_r:ssh_keygen_t:s0 tclass=capability permissive=0 > type=SYSCALL msg=audit(1744834995.352:546): arch=c000003e syscall=257 success=no exit=-13 a0=ffffff9c a1=5641c7587520 a2=241 a3=180 items=0 ppid=4978 pid=13471 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=4294967295 comm="ssh-keygen" exe="/usr/bin/ssh-keygen" subj=system_u:system_r:ssh_keygen_t:s0 key=(null)␝ARCH=x86_64 SYSCALL=openat AUID="unset" UID="root" GID="root" EUID="root" SUID="root" FSUID="root" EGID="root" SGID="root" FSGID="root" A workaround is to use /root/.ssh directory instead of just /root. While at it, let's unify rootless user and key setup into a single place. Signed-off-by: Kir Kolyshkin <[email protected]> (cherry picked from commit 87ae2f8) Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 5275ebb commit 3c375a8

File tree

5 files changed

+21
-25
lines changed

5 files changed

+21
-25
lines changed

.cirrus.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,8 @@ task:
7171
git checkout $BATS_VERSION
7272
./install.sh /usr/local
7373
cd -
74-
# Add a user for rootless tests
75-
useradd -u2000 -m -d/home/rootless -s/bin/bash rootless
76-
# Allow root and rootless itself to execute `ssh rootless@localhost` in tests/rootless.sh
77-
ssh-keygen -t ecdsa -N "" -f /root/rootless.key
78-
mkdir -m 0700 -p /home/rootless/.ssh
79-
cp /root/rootless.key /home/rootless/.ssh/id_ecdsa
80-
cat /root/rootless.key.pub >> /home/rootless/.ssh/authorized_keys
81-
chown -R rootless.rootless /home/rootless
74+
# Setup rootless tests.
75+
/home/runc/script/setup_rootless.sh
8276
# set PATH
8377
echo 'export PATH=/usr/local/go/bin:/usr/local/bin:$PATH' >> /root/.bashrc
8478
# Setup ssh localhost for terminal emulation (script -e did not work)

.github/workflows/test.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,7 @@ jobs:
159159
- name: add rootless user
160160
if: matrix.rootless == 'rootless'
161161
run: |
162-
sudo useradd -u2000 -m -d/home/rootless -s/bin/bash rootless
163-
# Allow root and rootless itself to execute `ssh rootless@localhost` in tests/rootless.sh
164-
ssh-keygen -t ecdsa -N "" -f $HOME/rootless.key
165-
sudo mkdir -m 0700 -p /home/rootless/.ssh
166-
sudo cp $HOME/rootless.key /home/rootless/.ssh/id_ecdsa
167-
sudo cp $HOME/rootless.key.pub /home/rootless/.ssh/authorized_keys
168-
sudo chown -R rootless.rootless /home/rootless
162+
./script/setup_rootless.sh
169163
sudo chmod a+X $HOME # for Ubuntu 22.04 and later
170164
171165
- name: integration test (fs driver)

script/setup_host_fedora.sh

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,8 @@ dnf clean all
1212
# To avoid "avc: denied { nosuid_transition }" from SELinux as we run tests on /tmp.
1313
mount -o remount,suid /tmp
1414

15-
# Add a user for rootless tests
16-
useradd -u2000 -m -d/home/rootless -s/bin/bash rootless
17-
18-
# Allow root and rootless itself to execute `ssh rootless@localhost` in tests/rootless.sh
19-
ssh-keygen -t ecdsa -N "" -f /root/rootless.key
20-
mkdir -m 0700 /home/rootless/.ssh
21-
cp /root/rootless.key /home/rootless/.ssh/id_ecdsa
22-
cat /root/rootless.key.pub >>/home/rootless/.ssh/authorized_keys
23-
chown -R rootless.rootless /home/rootless
15+
# Setup rootless user.
16+
"$(dirname "${BASH_SOURCE[0]}")"/setup_rootless.sh
2417

2518
# Delegate cgroup v2 controllers to rootless user via --systemd-cgroup
2619
mkdir -p /etc/systemd/system/[email protected]

script/setup_rootless.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
set -eux -o pipefail
3+
4+
# Add a user for rootless tests.
5+
sudo useradd -u2000 -m -d/home/rootless -s/bin/bash rootless
6+
7+
# Allow both the current user and rootless itself to use
8+
# ssh rootless@localhost in tests/rootless.sh.
9+
# shellcheck disable=SC2174 # Silence "-m only applies to the deepest directory".
10+
mkdir -p -m 0700 "$HOME/.ssh"
11+
ssh-keygen -t ecdsa -N "" -f "$HOME/.ssh/rootless.key"
12+
sudo mkdir -p -m 0700 /home/rootless/.ssh
13+
sudo cp "$HOME/.ssh/rootless.key" /home/rootless/.ssh/id_ecdsa
14+
sudo cp "$HOME/.ssh/rootless.key.pub" /home/rootless/.ssh/authorized_keys
15+
sudo chown -R rootless.rootless /home/rootless

tests/rootless.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ for enabled_features in $features_powerset; do
185185
# We use `ssh rootless@localhost` instead of `sudo -u rootless` for creating systemd user session.
186186
# Alternatively we could use `machinectl shell`, but it is known not to work well on SELinux-enabled hosts as of April 2020:
187187
# https://bugzilla.redhat.com/show_bug.cgi?id=1788616
188-
ssh -t -t -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i "$HOME/rootless.key" rootless@localhost -- PATH="$PATH" RUNC_USE_SYSTEMD="$RUNC_USE_SYSTEMD" bats -t "$ROOT/tests/integration$ROOTLESS_TESTPATH"
188+
ssh -t -t -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i "$HOME/.ssh/rootless.key" rootless@localhost -- PATH="$PATH" RUNC_USE_SYSTEMD="$RUNC_USE_SYSTEMD" bats -t "$ROOT/tests/integration$ROOTLESS_TESTPATH"
189189
else
190190
sudo -HE -u rootless PATH="$PATH" "$(which bats)" -t "$ROOT/tests/integration$ROOTLESS_TESTPATH"
191191
fi

0 commit comments

Comments
 (0)