Skip to content

Commit 8f1788f

Browse files
committed
sshd: generate a custom sshd_config
Instead of relying on the host's sshd configuration, generate a custom sshd_config to make sure that all the options required by virtme-ng are enabled. Moreover, move all files generated by virtme-ng to the cache directory to prevent the risk of overwriting files on the host. Signed-off-by: Andrea Righi <arighi@nvidia.com>
1 parent f53c692 commit 8f1788f

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

virtme/guest/virtme-sshd-script

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,43 @@ if [ -z "${virtme_ssh_user}" ]; then
77
exit 1
88
fi
99

10+
mkdir -p /run/sshd
11+
rm -f /var/run/nologin
12+
1013
SSH_HOME=$(getent passwd "${virtme_ssh_user}" | cut -d: -f6)
1114
if [ ! -e "${SSH_HOME}" ]; then
1215
# Setup an arbitrary ssh location, just to be able to start sshd.
13-
SSH_HOME=/run/ssh
16+
SSH_HOME=/run/sshd
1417
fi
1518

16-
# Update authorized_keys by adding the user's public keys, but only if the
17-
# changes are confined to the guest (no modifications made to the host).
18-
#
19-
# Overwriting authorized_keys is considered safe only when the guest rootfs
20-
# is mounted as read-only, with an overlayfs on top to handle writes within
21-
# the guest environment (e.g. `--rw` or `--rwdir` not specified as argument).
22-
OVERLAYFS="794c7630" # OVERLAYFS_SUPER_MAGIC in include/uapi/linux/magic.h
23-
SSH_AUTH_KEYS="${SSH_HOME}/.ssh/authorized_keys"
24-
if [ "$(stat -f -c "%t" "${SSH_AUTH_KEYS}")" = "${OVERLAYFS}" ]; then
25-
cat "${SSH_HOME}"/.ssh/id_*.pub >> "${SSH_AUTH_KEYS}" 2>/dev/null
26-
chown "${virtme_ssh_user}" "${SSH_AUTH_KEYS}" 2>/dev/null
27-
fi
19+
# Generate authorized_keys in the virtme-ng cache directory and add all
20+
# user's public keys.
21+
CACHE_DIR=${SSH_HOME}/.cache/virtme-ng/.ssh
22+
SSH_AUTH_KEYS="${CACHE_DIR}/authorized_keys"
23+
cat "${SSH_HOME}"/.ssh/id_*.pub >> "${SSH_AUTH_KEYS}" 2>/dev/null
24+
chown "${virtme_ssh_user}" "${SSH_AUTH_KEYS}" 2>/dev/null
25+
chmod 600 "${SSH_AUTH_KEYS}" 2>/dev/null
2826

2927
# Generate ssh host keys (if they don't exist already).
30-
CACHE_DIR=${SSH_HOME}/.cache/virtme-ng/.ssh
3128
mkdir -p "${CACHE_DIR}/etc/ssh"
3229
ssh-keygen -A -f "${CACHE_DIR}"
33-
ARGS=()
30+
31+
# Generate a minimal sshd config.
32+
SSH_CONFIG=/run/sshd/sshd_config
33+
cat << EOF > "${SSH_CONFIG}"
34+
# This file is automatically generated by virtme-ng.
35+
Port 22
36+
PermitRootLogin yes
37+
AuthorizedKeysFile ${SSH_AUTH_KEYS}
38+
PubkeyAuthentication yes
39+
UsePAM yes
40+
PrintMotd no
41+
EOF
42+
43+
# Start sshd.
44+
ARGS=(-f "${SSH_CONFIG}")
3445
for key in "${CACHE_DIR}"/etc/ssh/ssh_host_*_key; do
3546
ARGS+=(-h "${key}")
3647
done
3748

38-
# Start sshd.
39-
mkdir -p /run/sshd
40-
rm -f /var/run/nologin
41-
/usr/sbin/sshd "${ARGS[@]}"
49+
/usr/sbin/sshd -f "${SSH_CONFIG}" "${ARGS[@]}"

0 commit comments

Comments
 (0)