Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ acceptance-tests/os-conf-release
**/*.log

ci/docker/VMware-ovftool-*.bundle
tmp/
24 changes: 18 additions & 6 deletions stemcell_builder/stages/bosh_monit/assets/monit-access-helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,24 @@
monit_isolation_classid=2958295041

permit_monit_access() {
net_cls_location="$(cat /proc/self/mounts | grep ^cgroup | grep net_cls | awk '{ print $2 }' )"
net_cls_subproc="$(grep net_cls /proc/self/cgroup | awk -F ":" '{ print $3 }' )"
monit_access_cgroup="${net_cls_location}/${net_cls_subproc}/monit-api-access"
if grep -q '^0::' /proc/self/cgroup 2>/dev/null; then
# cgroupv2 (unified hierarchy)
# Create a sub-cgroup under the current process's cgroup and move into it.
# The iptables rules match on this cgroup path.
cgroup_mount="$(awk '$3 == "cgroup2" { print $2 }' /proc/self/mounts)"
current_cgroup="$(grep '^0::' /proc/self/cgroup | cut -d: -f3)"
monit_access_cgroup="${cgroup_mount}${current_cgroup}/monit-api-access"

mkdir -p "${monit_access_cgroup}"
echo "${monit_isolation_classid}" > "${monit_access_cgroup}/net_cls.classid"
mkdir -p "${monit_access_cgroup}"
echo $$ > "${monit_access_cgroup}/cgroup.procs"
else
# cgroupv1 - use net_cls classid
net_cls_location="$(cat /proc/self/mounts | grep ^cgroup | grep net_cls | awk '{ print $2 }')"
net_cls_subproc="$(grep net_cls /proc/self/cgroup | awk -F ":" '{ print $3 }')"
monit_access_cgroup="${net_cls_location}/${net_cls_subproc}/monit-api-access"

echo $$ > "${monit_access_cgroup}/tasks"
mkdir -p "${monit_access_cgroup}"
echo "${monit_isolation_classid}" > "${monit_access_cgroup}/net_cls.classid"
echo $$ > "${monit_access_cgroup}/tasks"
fi
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@

source /var/vcap/bosh/etc/monit-access-helper.sh

if iptables -t mangle -C POSTROUTING -d 127.0.0.1 -p tcp --dport 2822 \
-m cgroup \! --cgroup "${monit_isolation_classid}" -j DROP
then
/bin/true
if grep -q '^0::' /proc/self/cgroup 2>/dev/null; then
# cgroupv2: dynamically determine the cgroup path for this process.
# The agent calls permit_monit_access() to join the monit-api-access sub-cgroup.
current_cgroup="$(grep '^0::' /proc/self/cgroup | cut -d: -f3)"
cgroup_match="--path ${current_cgroup}/monit-api-access"
else
# cgroupv1: use the classid from monit-access-helper.sh
cgroup_match="--cgroup ${monit_isolation_classid}"
fi

# Add iptables rules if they don't already exist.
# The DROP rule blocks traffic to monit (port 2822) from processes outside the monit cgroup.
# The ESTABLISHED,RELATED rule ensures existing connections aren't broken.
if ! iptables -t mangle -C POSTROUTING -d 127.0.0.1 -p tcp --dport 2822 \
-m cgroup ! ${cgroup_match} -j DROP 2>/dev/null; then
iptables -t mangle -I POSTROUTING -d 127.0.0.1 -p tcp --dport 2822 \
-m cgroup \! --cgroup "${monit_isolation_classid}" -j DROP
-m cgroup ! ${cgroup_match} -j DROP
iptables -t mangle -I POSTROUTING -d 127.0.0.1 -p tcp --dport 2822 \
-m state --state ESTABLISHED,RELATED -j ACCEPT
-m state --state ESTABLISHED,RELATED -j ACCEPT
fi
11 changes: 11 additions & 0 deletions stemcell_builder/stages/bosh_systemd/apply.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ source $base_dir/lib/prelude_bosh.bash
run_in_chroot $chroot "
echo 'RemoveIPC=no' >> /etc/systemd/logind.conf
"

# Prevent systemd-binfmt from running in containers.
# When running in a privileged container (e.g., Docker CPI on Apple Silicon),
# this service clears the host's binfmt_misc registrations (including Rosetta),
# causing "exec format error" for all subsequent x86_64 processes.
mkdir -p $chroot/etc/systemd/system/systemd-binfmt.service.d

cat > $chroot/etc/systemd/system/systemd-binfmt.service.d/skip-in-container.conf <<EOF
[Unit]
ConditionVirtualization=!container
EOF
Loading