Skip to content

Commit bbe1351

Browse files
committed
Set secontext for bind volumes in selinux enabled distros
Fixes #1882 Signed-off-by: T K Chandra Hasan <[email protected]>
1 parent df53f57 commit bbe1351

File tree

4 files changed

+103
-5
lines changed

4 files changed

+103
-5
lines changed

Diff for: .github/workflows/test.yml

+14-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ on:
77
- 'release/**'
88
pull_request:
99

10+
env:
11+
LIMACTL_CREATE_ARGS: ""
12+
1013
jobs:
1114
lints:
1215
name: "Lints"
@@ -399,6 +402,12 @@ jobs:
399402
name: "vz"
400403
runs-on: macos-13
401404
timeout-minutes: 120
405+
strategy:
406+
fail-fast: false
407+
matrix:
408+
template:
409+
- experimental/vz.yaml
410+
- fedora.yaml
402411
steps:
403412
- uses: actions/checkout@v4
404413
with:
@@ -411,14 +420,16 @@ jobs:
411420
with:
412421
path: ~/Library/Caches/lima/download
413422
# hashFiles do not seem to support symlinks
414-
key: ${{ runner.os }}-${{ hashFiles('examples/experimental/vz.yaml') }}
423+
key: ${{ runner.os }}-${{ hashFiles('examples/*.yaml') }}
415424
- name: Make
416425
run: make
417426
- name: Install
418427
run: make install
419428
- name: Install test dependencies
420-
run: brew install bash coreutils
429+
run: brew install bash coreutils jq
421430
- name: Uninstall qemu
422431
run: brew uninstall --ignore-dependencies --force qemu
423432
- name: Test
424-
run: ./hack/test-templates.sh templates/experimental/vz.yaml
433+
env:
434+
LIMACTL_CREATE_ARGS: "--vm-type vz --mount-type virtiofs --rosetta --network vzNAT"
435+
run: ./hack/test-templates.sh templates/${{ matrix.template }}

Diff for: hack/test-selinux.sh

+53
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 [[ "$(uname)" == "Darwin" && "$(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

Diff for: hack/test-templates.sh

+11-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,12 @@ if [[ -n ${CHECKS["disk"]} ]]; then
109109
fi
110110

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

115120
INFO "Starting \"$NAME\""
@@ -223,7 +228,7 @@ if [[ -n ${CHECKS["port-forwards"]} ]]; then
223228
if [ "${NAME}" = "debian" ]; then
224229
limactl shell "$NAME" sudo apt-get install -y netcat-openbsd
225230
fi
226-
if [ "${NAME}" = "fedora" ]; then
231+
if [ "${NAME}" == "fedora" ]; then
227232
limactl shell "$NAME" sudo dnf install -y nc
228233
fi
229234
if [ "${NAME}" = "opensuse" ]; then
@@ -389,6 +394,10 @@ if [[ -n ${CHECKS["snapshot-offline"]} ]]; then
389394
limactl start "$NAME"
390395
fi
391396

397+
if [[ $NAME == "fedora" && "$(limactl ls --json "$NAME" | jq -r .vmType)" == "vz" ]]; then
398+
"${scriptdir}"/test-selinux.sh "$NAME"
399+
fi
400+
392401
INFO "Stopping \"$NAME\""
393402
limactl stop "$NAME"
394403
sleep 3

Diff for: pkg/cidata/cidata.TEMPLATE.d/boot/05-lima-mounts.sh

+25
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)