Skip to content

Commit d371d31

Browse files
committed
AppArmor debugging for BIND
1 parent f484a73 commit d371d31

File tree

1 file changed

+80
-9
lines changed

1 file changed

+80
-9
lines changed

ext/standard/tests/dns/bind-start.sh

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,52 @@ else
100100
exit 1
101101
fi
102102

103+
# Enhanced AppArmor handling
103104
if [[ -f /etc/apparmor.d/usr.sbin.named ]]; then
104-
echo "Debug: AppArmor profile detected, setting to complain mode..."
105-
aa-complain /usr/sbin/named || echo "Failed to set AppArmor to complain mode"
105+
echo "Debug: AppArmor profile detected, attempting comprehensive bypass..."
106+
107+
# Install apparmor-utils if not present
108+
if ! command -v aa-complain >/dev/null 2>&1; then
109+
echo "Debug: Installing apparmor-utils..."
110+
apt-get update -qq
111+
apt-get install -y apparmor-utils
112+
fi
113+
114+
# Check initial status
115+
echo "Debug: Initial AppArmor status for named:"
116+
aa-status 2>/dev/null | grep named || echo "No named profile in initial aa-status"
117+
118+
# Try complain mode first
119+
echo "Debug: Setting to complain mode..."
120+
aa-complain /usr/sbin/named 2>/dev/null || echo "Failed to set AppArmor to complain mode"
121+
122+
# Check what mode it's actually in
123+
echo "Debug: AppArmor profile mode after complain:"
124+
cat /sys/kernel/security/apparmor/profiles 2>/dev/null | grep named || echo "No named in profiles"
125+
126+
# Try to completely disable it
127+
echo "Debug: Attempting to disable AppArmor profile completely..."
128+
aa-disable /usr/sbin/named 2>/dev/null || echo "Failed to disable AppArmor profile"
129+
130+
# Alternative disable method
131+
echo "Debug: Trying alternative disable method..."
132+
ln -sf /etc/apparmor.d/usr.sbin.named /etc/apparmor.d/disable/ 2>/dev/null || echo "Symlink method failed"
133+
134+
# Unload from kernel
135+
if command -v apparmor_parser >/dev/null 2>&1; then
136+
echo "Debug: Unloading profile from kernel..."
137+
apparmor_parser -R /etc/apparmor.d/usr.sbin.named 2>/dev/null || echo "Failed to unload profile"
138+
fi
139+
140+
# Final status check
141+
echo "Debug: Final AppArmor status:"
142+
aa-status 2>/dev/null | grep named || echo "No named profile found (good!)"
143+
106144
elif [ -d /etc/apparmor.d/ ]; then
107-
ls /etc/apparmor.d/
145+
echo "Debug: AppArmor directory exists but no named profile found:"
146+
ls /etc/apparmor.d/ | grep -i named || echo "No named-related profiles"
108147
else
109-
echo "No apparmor.d"
148+
echo "Debug: No AppArmor directory found"
110149
fi
111150

112151
echo "Debug: Generated named.conf contents:"
@@ -147,6 +186,17 @@ else
147186
exit 1
148187
fi
149188

189+
# Set up permissions for bind user
190+
echo "Debug: Setting up permissions for bind user..."
191+
if id bind >/dev/null 2>&1; then
192+
chown bind:bind "$NAMED_CONF" "$ZONES_DIR"/*.zone 2>/dev/null || echo "Failed to chown to bind user"
193+
chmod 644 "$NAMED_CONF" "$ZONES_DIR"/*.zone
194+
echo "Debug: File permissions after chown:"
195+
ls -la "$NAMED_CONF" "$ZONES_DIR"/*.zone
196+
else
197+
echo "Debug: bind user does not exist, keeping current permissions"
198+
fi
199+
150200
# Check IPv4/IPv6 configuration with fallbacks
151201
echo "Debug: Network configuration check:"
152202
echo "Debug: localhost resolution:"
@@ -183,21 +233,38 @@ else
183233
echo "Debug: systemd-resolved status:"
184234
systemctl is-active systemd-resolved 2>/dev/null || echo "systemd-resolved not active"
185235

236+
# Monitor AppArmor denials in background
237+
echo "Debug: Starting AppArmor denial monitoring..."
238+
(timeout 15 tail -f /var/log/syslog 2>/dev/null | grep "apparmor.*DENIED" | head -10 &) || echo "Could not start syslog monitoring"
239+
240+
# Try different user approaches
241+
NAMED_USER="$(whoami)"
242+
if id bind >/dev/null 2>&1; then
243+
echo "Debug: Trying with bind user instead of root..."
244+
NAMED_USER="bind"
245+
fi
246+
186247
# Run named and capture both stdout and stderr separately
187-
echo "Debug: Starting named..."
188-
if named -c "$NAMED_CONF" -p 53 -u "$(whoami)" > "$LOG_FILE" 2>&1; then
248+
echo "Debug: Starting named as user: $NAMED_USER..."
249+
if named -c "$NAMED_CONF" -p 53 -u "$NAMED_USER" > "$LOG_FILE" 2>&1; then
189250
echo "Debug: named command succeeded"
190251
else
191252
NAMED_EXIT_CODE=$?
192253
echo "Debug: named command failed with exit code: $NAMED_EXIT_CODE"
193254
echo "Debug: Log file contents:"
194255
cat "$LOG_FILE" 2>/dev/null || echo "No log file found"
195256

257+
# Show any AppArmor denials
258+
echo "Debug: Checking for AppArmor denials:"
259+
grep "apparmor.*DENIED.*named" /var/log/syslog 2>/dev/null | tail -10 || echo "No AppArmor denials found in syslog"
260+
261+
# Show general AppArmor messages
262+
echo "Debug: Recent AppArmor messages for named:"
263+
grep "apparmor.*named" /var/log/syslog 2>/dev/null | tail -10 || echo "No AppArmor messages found"
264+
196265
# Try to run named with more verbose output
197266
echo "Debug: Trying to run named in foreground for better error output:"
198-
named -c "$NAMED_CONF" -p 53 -u "$(whoami)" -g -d 1 || true
199-
200-
cat /var/log/syslog | grep apparmor | grep named
267+
timeout 5 named -c "$NAMED_CONF" -p 53 -u "$NAMED_USER" -g -d 1 || echo "Foreground attempt timed out or failed"
201268

202269
exit $NAMED_EXIT_CODE
203270
fi
@@ -234,5 +301,9 @@ else
234301
echo "No log file found at $LOG_FILE"
235302
fi
236303

304+
# Final AppArmor check
305+
echo "Debug: Final AppArmor denial check:"
306+
grep "apparmor.*DENIED.*named" /var/log/syslog 2>/dev/null | tail -5 || echo "No final AppArmor denials found"
307+
237308
exit 1
238309
fi

0 commit comments

Comments
 (0)