Skip to content

Commit f8ff938

Browse files
committed
Set secontext for bind volumes in selinux enabled distros
Fixes #1882 Signed-off-by: T K Chandra Hasan <t.k.chandra.hasan@ibm.com>
1 parent 1a72344 commit f8ff938

File tree

4 files changed

+101
-4
lines changed

4 files changed

+101
-4
lines changed

.github/workflows/test.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,12 @@ jobs:
405405
name: "vz"
406406
runs-on: macos-13
407407
timeout-minutes: 120
408+
strategy:
409+
fail-fast: false
410+
matrix:
411+
template:
412+
- experimental/vz.yaml
413+
- fedora.yaml
408414
steps:
409415
- uses: actions/checkout@v4
410416
with:
@@ -417,12 +423,14 @@ jobs:
417423
with:
418424
path: ~/Library/Caches/lima/download
419425
# hashFiles do not seem to support symlinks
420-
key: ${{ runner.os }}-${{ hashFiles('examples/experimental/vz.yaml') }}
426+
key: ${{ runner.os }}-${{ hashFiles('examples/*.yaml') }}
421427
- name: Make
422428
run: make
423429
- name: Install
424430
run: make install
425431
- name: Install test dependencies
426432
run: brew install qemu bash coreutils
427433
- name: Test
428-
run: ./hack/test-templates.sh templates/experimental/vz.yaml
434+
env:
435+
LIMACTL_CREATE_ARGS: "--vm-type vz --mount-type virtiofs --rosetta --network vzNAT"
436+
run: ./hack/test-templates.sh templates/${{ matrix.template }}

hack/test-selinux.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bash
2+
3+
set -eu -o pipefail
4+
5+
scriptdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
# shellcheck source=common.inc.sh
7+
source "${scriptdir}/common.inc.sh"
8+
9+
if [ "$#" -ne 1 ]; then
10+
ERROR "Usage: $0 NAME"
11+
exit 1
12+
fi
13+
14+
NAME="$1"
15+
expected="context=system_u:object_r:container_file_t:s0"
16+
#Skip Rosetta checks for x86 GHA mac runners
17+
if [ "$(arch)" == "arm64" ]; then
18+
INFO "Testing secontext is set for rosetta mounts"
19+
got=$(limactl shell "$NAME" mount | grep "rosetta" | awk '{print $6}')
20+
INFO "secontext rosetta: expected=${expected}, got=${got}"
21+
if [[ $got != *$expected* ]]; then
22+
ERROR "secontext for rosetta mount is not set or Invalid"
23+
exit 1
24+
fi
25+
fi
26+
INFO "Testing secontext is set for bind mounts"
27+
INFO "Checking in mounts"
28+
got=$(limactl shell "$NAME" mount | grep "$HOME" | awk '{print $6}')
29+
INFO "secontext ${HOME}: expected=${expected}, got=${got}"
30+
if [[ $got != *$expected* ]]; then
31+
ERROR "secontext for \"$HOME\" dir is not set or Invalid"
32+
exit 1
33+
fi
34+
got=$(limactl shell "$NAME" mount | grep "/tmp/lima" | awk '{print $6}')
35+
INFO "secontext /tmp/lima: expected=${expected}, got=${got}"
36+
if [[ $got != *$expected* ]]; then
37+
ERROR 'secontext for "/tmp/lima" dir is not set or Invalid'
38+
exit 1
39+
fi
40+
INFO "Checking in fstab file"
41+
expected='context="system_u:object_r:container_file_t:s0"'
42+
got=$(limactl shell "$NAME" cat /etc/fstab | grep "$HOME" | awk '{print $4}')
43+
INFO "secontext ${HOME}: expected=${expected}, got=${got}"
44+
if [[ $got != *$expected* ]]; then
45+
ERROR "secontext for \"$HOME\" dir is not set or Invalid"
46+
exit 1
47+
fi
48+
got=$(limactl shell "$NAME" cat /etc/fstab | grep "/tmp/lima" | awk '{print $4}')
49+
INFO "secontext /tmp/lima: expected=${expected}, got=${got}"
50+
if [[ $got != *$expected* ]]; then
51+
ERROR 'secontext for "/tmp/lima" dir is not set or Invalid'
52+
exit 1
53+
fi

hack/test-templates.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,15 @@ if [[ -n ${CHECKS["disk"]} ]]; then
109109
fi
110110

111111
set -x
112-
"${LIMACTL_CREATE[@]}" "$FILE"
112+
set +u
113+
if [ -z "${LIMACTL_CREATE_ARGS}" ]; then
114+
"${LIMACTL_CREATE[@]}" "$FILE"
115+
else
116+
# shellcheck disable=SC2086
117+
"${LIMACTL_CREATE[@]}" ${LIMACTL_CREATE_ARGS} "$FILE"
118+
fi
113119
set +x
120+
set -u
114121

115122
INFO "Starting \"$NAME\""
116123
set -x
@@ -223,7 +230,7 @@ if [[ -n ${CHECKS["port-forwards"]} ]]; then
223230
if [ "${NAME}" = "debian" ]; then
224231
limactl shell "$NAME" sudo apt-get install -y netcat-openbsd
225232
fi
226-
if [ "${NAME}" = "fedora" ]; then
233+
if [[ ${NAME} == *"fedora"* ]]; then
227234
limactl shell "$NAME" sudo dnf install -y nc
228235
fi
229236
if [ "${NAME}" = "opensuse" ]; then
@@ -389,6 +396,10 @@ if [[ -n ${CHECKS["snapshot-offline"]} ]]; then
389396
limactl start "$NAME"
390397
fi
391398

399+
if [[ $NAME == "fedora" && "$(limactl ls --json "$NAME" | jq -r .vmType)" == "vz" ]]; then
400+
"${scriptdir}"/test-selinux.sh "$NAME"
401+
fi
402+
392403
INFO "Stopping \"$NAME\""
393404
limactl stop "$NAME"
394405
sleep 3
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
set -eux -o pipefail
4+
5+
# Check if mount type is virtiofs and vm type as vz
6+
if ! [[ ${LIMA_CIDATA_VMTYPE} == "vz" && ${LIMA_CIDATA_MOUNTTYPE} == "virtiofs" ]]; then
7+
exit 0
8+
fi
9+
10+
# Update fstab entries and unmount/remount the volumes with secontext options
11+
# when selinux is enabled in kernel
12+
if [ -d /sys/fs/selinux ]; then
13+
# shellcheck disable=SC2013
14+
for line in $(grep -n virtiofs </etc/fstab | cut -d':' -f1); do
15+
OPTIONS=$(awk -v line="$line" 'NR==line {print $4}' /etc/fstab)
16+
if [[ ${OPTIONS} != *"context"* ]]; then
17+
sed -i -e "$line""s/comment=cloudconfig/comment=cloudconfig,context=\"system_u:object_r:container_file_t:s0\"/g" /etc/fstab
18+
TAG=$(awk -v line="$line" 'NR==line {print $1}' /etc/fstab)
19+
MOUNT_POINT=$(awk -v line="$line" 'NR==line {print $2}' /etc/fstab)
20+
OPTIONS=$(awk -v line="$line" 'NR==line {print $4}' /etc/fstab)
21+
umount "${TAG}"
22+
mount -t virtiofs "${TAG}" "${MOUNT_POINT}" -o "${OPTIONS}"
23+
fi
24+
done
25+
fi

0 commit comments

Comments
 (0)