Skip to content

Commit 169ad00

Browse files
authored
Merge pull request omacom#8627 from mdisec/security/harden-cups-browsed
Harden CUPS printer discovery
2 parents f067277 + 74997fd commit 169ad00

11 files changed

Lines changed: 465 additions & 12 deletions

File tree

‎etc/cups/cups-browsed.conf‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Omarchy override of cups-browsed's shipped config. The only behavioural
2-
# change vs the upstream default (all-commented) is enabling auto-registration
3-
# of remote IPP printers discovered via Avahi/mDNS.
4-
CreateRemotePrinters Yes
1+
# Keep state away from /var/cache/cups, which is writable by the account CUPS
2+
# uses for print filters. cups-browsed is the only writer to this directory.
3+
CacheDir /var/cache/cups-browsed
4+
5+
# Auto-create queues only for modern driverless IPP printers. Remote queues
6+
# exported by another CUPS server can still be added manually when needed.
7+
CreateIPPPrinterQueues Driverless
8+
CreateRemoteCUPSPrinterQueues No

‎etc/cups/cups-files.conf‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#
2+
# File/directory/user/group configuration file for the CUPS scheduler.
3+
# See "man cups-files.conf" for a complete description of this file.
4+
#
5+
6+
# List of events that are considered fatal errors for the scheduler...
7+
#FatalErrors config
8+
9+
# Strip domain in local username?
10+
#StripUserDomain No
11+
12+
# Do we call fsync() after writing configuration or status files?
13+
#SyncOnClose No
14+
15+
# Default user and group for filters/backends/helper programs; this cannot be
16+
# any user or group that resolves to ID 0 for security reasons...
17+
User 209
18+
Group 209
19+
20+
# Administrator user group, used to match @SYSTEM in cupsd.conf policy rules...
21+
# This cannot contain the Group value for security reasons...
22+
SystemGroup cups-browsed sys root
23+
24+
25+
# Are Unix domain socket peer credentials used for authorization?
26+
PeerCred on
27+
28+
# User that is substituted for unauthenticated (remote) root accesses...
29+
#RemoteRoot remroot
30+
31+
# Do we allow file: device URIs other than to /dev/null?
32+
#FileDevice No
33+
34+
# Permissions for configuration and log files...
35+
#ConfigFilePerm 0640
36+
#LogFilePerm 0644
37+
38+
# Location of the file logging all access to the scheduler; may be the name
39+
# "syslog". If not an absolute path, the value of ServerRoot is used as the
40+
# root directory. Also see the "AccessLogLevel" directive in cupsd.conf.
41+
AccessLog /var/log/cups/access_log
42+
43+
# Location of cache files used by the scheduler...
44+
#CacheDir /var/cache/cups
45+
46+
# Location of data files used by the scheduler...
47+
#DataDir /usr/share/cups
48+
49+
# Location of the static web content served by the scheduler...
50+
#DocumentRoot /usr/share/cups/doc
51+
52+
# Location of the file logging all messages produced by the scheduler and any
53+
# helper programs; may be the name "syslog". If not an absolute path, the value
54+
# of ServerRoot is used as the root directory. Also see the "LogLevel"
55+
# directive in cupsd.conf.
56+
ErrorLog /var/log/cups/error_log
57+
58+
# Location of the file logging all pages printed by the scheduler and any
59+
# helper programs; may be the name "syslog". If not an absolute path, the value
60+
# of ServerRoot is used as the root directory. Also see the "PageLogFormat"
61+
# directive in cupsd.conf.
62+
PageLog /var/log/cups/page_log
63+
64+
# Location of the file listing all of the local printers...
65+
#Printcap /etc/printcap
66+
67+
# Format of the Printcap file...
68+
#PrintcapFormat bsd
69+
#PrintcapFormat plist
70+
#PrintcapFormat solaris
71+
72+
# Location of all spool files...
73+
#RequestRoot /var/spool/cups
74+
75+
# Location of helper programs...
76+
#ServerBin /usr/lib/cups
77+
78+
# SSL/TLS keychain for the scheduler...
79+
#ServerKeychain ssl
80+
81+
# Location of other configuration files...
82+
#ServerRoot /etc/cups
83+
84+
# Location of scheduler state files...
85+
#StateDir /run/cups
86+
87+
# Location of scheduler/helper temporary files. This directory is emptied on
88+
# scheduler startup and cannot be one of the standard (public) temporary
89+
# directory locations for security reasons...
90+
#TempDir /var/spool/cups/tmp
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[Service]
2+
User=cups-browsed
3+
Group=cups-browsed
4+
CacheDirectory=cups-browsed
5+
CacheDirectoryMode=0750
6+
UMask=0027
7+
NoNewPrivileges=yes
8+
ProtectSystem=strict
9+
ProtectHome=yes
10+
PrivateTmp=yes
11+
RestrictSUIDSGID=yes
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
u cups-browsed - "CUPS printer discovery" / -

‎install/omarchy-base.packages‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ cliamp
1919
cups
2020
cups-browsed
2121
cups-filters
22-
cups-pdf
22+
cups-pk-helper
2323
ddcutil
2424
docker
2525
docker-buildx

‎install/post-install/pacman.sh‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
cp -f "$OMARCHY_PATH/default/pacman/pacman-${OMARCHY_MIRROR:-stable}.conf" /etc/pacman.conf
44
cp -f "$OMARCHY_PATH/default/pacman/mirrorlist-${OMARCHY_MIRROR:-stable}" /etc/pacman.d/mirrorlist
55

6-
# omarchy-settings skips this override until cups-browsed is actually present
7-
# to avoid pacman creating cups-browsed.conf.pacnew during ISO package install.
8-
if [[ -f $OMARCHY_PATH/etc-overrides/cups-cups-browsed.conf && -d /etc/cups ]]; then
6+
# omarchy-settings skips these overrides until CUPS is actually present to
7+
# avoid pacman creating .pacnew files during ISO package installation.
8+
if [[ -f $OMARCHY_PATH/etc-overrides/cups-cups-browsed.conf && -f /etc/cups/cups-files.conf ]]; then
9+
systemd-sysusers /etc/sysusers.d/omarchy-cups-browsed.conf
910
cp -f "$OMARCHY_PATH/etc-overrides/cups-cups-browsed.conf" /etc/cups/cups-browsed.conf
10-
rm -f /etc/cups/cups-browsed.conf.pacnew
11+
install -m 0640 -o root -g cups "$OMARCHY_PATH/etc-overrides/cups-cups-files.conf" /etc/cups/cups-files.conf
12+
rm -f /etc/cups/cups-browsed.conf.pacnew /etc/cups/cups-files.conf.pacnew
1113
fi
1214

1315
source "$OMARCHY_INSTALL/hardware/pacman.sh"

‎install/provisioning/setup-form.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Turkish|trq
7979
Ukrainian|ua'
8080

8181
OMARCHY_USERNAME_PATTERN='^[a-z_][a-z0-9_-]*[$]?$'
82-
OMARCHY_RESERVED_USERNAMES='^(root|bin|daemon|mail|ftp|http|nobody|dbus|systemd-coredump|systemd-network|systemd-oom|systemd-journal-remote|systemd-resolve|systemd-timesync|tss|uuidd|alpm|git|avahi|cups|lp|_talkd|polkitd|rtkit|qemu|brltty|gluster|rpc|libvirt-qemu|pcscd|nvidia-persistenced|sddm)$'
82+
OMARCHY_RESERVED_USERNAMES='^(root|bin|daemon|mail|ftp|http|nobody|dbus|systemd-coredump|systemd-network|systemd-oom|systemd-journal-remote|systemd-resolve|systemd-timesync|tss|uuidd|alpm|git|avahi|cups|cups-browsed|lp|_talkd|polkitd|rtkit|qemu|brltty|gluster|rpc|libvirt-qemu|pcscd|nvidia-persistenced|sddm)$'
8383
OMARCHY_HOSTNAME_PATTERN='^[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?$'
8484
OMARCHY_HOSTNAME_DEFAULT='omarchy'
8585

‎migrations/1787815267.sh‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
echo "Separate printer discovery from root and print-filter access"
2+
3+
machine_marker="${OMARCHY_CUPS_MIGRATION_MARKER:-/var/lib/omarchy/migrations/1787815267}"
4+
5+
[[ ! -e $machine_marker ]] || exit 0
6+
7+
# Existing releases allowed a desktop user or shared group named cups-browsed,
8+
# which systemd-sysusers would silently reuse for passwordless CUPS access.
9+
if omarchy-pkg-present cups; then
10+
cups_browsed_account=$(getent passwd cups-browsed || true)
11+
cups_browsed_group=$(getent group cups-browsed || true)
12+
13+
if [[ -n $cups_browsed_account || -n $cups_browsed_group ]]; then
14+
IFS=: read -r _ _ cups_browsed_uid cups_browsed_gid cups_browsed_description cups_browsed_home cups_browsed_shell <<<"$cups_browsed_account"
15+
IFS=: read -r _ _ cups_browsed_group_gid cups_browsed_group_members <<<"$cups_browsed_group"
16+
other_primary_user=$(getent passwd | awk -F: -v gid="$cups_browsed_gid" '$1 != "cups-browsed" && $4 == gid { print $1; exit }')
17+
18+
if [[ ! $cups_browsed_uid =~ ^[0-9]+$ || ! $cups_browsed_group_gid =~ ^[0-9]+$ ]] ||
19+
((cups_browsed_uid <= 0 || cups_browsed_uid >= 1000)) ||
20+
[[ $cups_browsed_gid != $cups_browsed_group_gid ]] ||
21+
[[ $cups_browsed_description != "CUPS printer discovery" || $cups_browsed_home != "/" || $cups_browsed_shell != "/usr/bin/nologin" ]] ||
22+
[[ -n $cups_browsed_group_members || -n $other_primary_user ]]; then
23+
echo "Cannot harden printer discovery: the existing cups-browsed user or group is not a dedicated system account." >&2
24+
false
25+
fi
26+
fi
27+
fi
28+
29+
# CUPS-PDF accepts a job-controlled post-processing command in a backend that
30+
# CUPS launches as root. Native application print-to-file support replaces it.
31+
omarchy-pkg-drop cups-pdf
32+
33+
# system-config-printer uses this helper to request printer administration
34+
# through Polkit now that the desktop user's wheel group is no longer @SYSTEM.
35+
if omarchy-pkg-present cups; then
36+
omarchy-pkg-add cups-pk-helper
37+
fi
38+
39+
# Stop the root-running daemon before changing the authorization it relies on.
40+
if systemctl is-active --quiet cups-browsed.service 2>/dev/null; then
41+
sudo systemctl stop cups-browsed.service
42+
fi
43+
44+
if omarchy-pkg-present cups; then
45+
sudo systemctl daemon-reload
46+
sudo systemctl try-reload-or-restart cups.service
47+
fi
48+
49+
# Resume on whether the unit is enabled, not on whether it was running when this
50+
# run started: an interrupted earlier run leaves it stopped, and a retry that
51+
# recomputed that would skip the restart and still write the marker below. A
52+
# masked or disabled unit reports not-enabled and is left alone.
53+
if systemctl is-enabled --quiet cups-browsed.service 2>/dev/null; then
54+
sudo systemctl restart cups-browsed.service
55+
fi
56+
57+
sudo install -Dm644 /dev/null "$machine_marker"

‎test/acceptance.d/system-test.sh‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,72 @@ verify_services() {
6464
pass "user audio services are running"
6565
}
6666

67+
verify_printing_security() {
68+
local cups_browsed_pid lpinfo_output printer_name printer_process printer_tmp
69+
70+
! pacman -Q cups-pdf >/dev/null 2>&1 || fail "CUPS-PDF is absent"
71+
pass "the root CUPS-PDF backend is not installed"
72+
73+
getent passwd cups-browsed >/dev/null || fail "the cups-browsed service account exists"
74+
[[ $(systemctl show -P User cups-browsed.service) == "cups-browsed" ]] ||
75+
fail "cups-browsed runs as its service account"
76+
[[ $(systemctl show -P Group cups-browsed.service) == "cups-browsed" ]] ||
77+
fail "cups-browsed runs as its service group"
78+
systemctl is-active --quiet cups-browsed.service || fail "cups-browsed is running"
79+
80+
cups_browsed_pid=$(systemctl show -P MainPID cups-browsed.service)
81+
[[ -r /proc/$cups_browsed_pid/status ]] || fail "cups-browsed has a readable process status"
82+
[[ $(awk '/^Uid:/{print $2}' "/proc/$cups_browsed_pid/status") != 0 ]] ||
83+
fail "cups-browsed does not run with root UID"
84+
[[ $(awk '/^CapEff:/{print $2}' "/proc/$cups_browsed_pid/status") == "0000000000000000" ]] ||
85+
fail "cups-browsed has no effective Linux capabilities"
86+
87+
[[ $(stat -c '%a %U:%G' /var/cache/cups-browsed) == "750 cups-browsed:cups-browsed" ]] ||
88+
fail "cups-browsed has an isolated cache" "$(stat -c '%a %U:%G' /var/cache/cups-browsed)"
89+
[[ " $(id -nG cups-browsed) " != *" cups "* ]] ||
90+
fail "cups-browsed is separate from the print-filter group"
91+
92+
if lpinfo_output=$(LC_ALL=C timeout 10 lpinfo -v </dev/null 2>&1); then
93+
fail "the desktop user cannot administer CUPS without authentication"
94+
elif [[ $lpinfo_output != *"Forbidden"* ]]; then
95+
fail "CUPS explicitly denies unauthenticated desktop administration" "$lpinfo_output"
96+
fi
97+
98+
pass "CUPS discovery is isolated from root, filters, and passwordless desktop administration"
99+
100+
# A live driverless printer proves the non-root daemon can still discover and
101+
# create queues without the CAP_NET_BIND_SERVICE Ubuntu carries downstream.
102+
printer_name="OmarchyAcceptancePrinter"
103+
printer_tmp=$(mktemp -d)
104+
printf '#!/bin/bash\nexit 0\n' >"$printer_tmp/command"
105+
chmod 0700 "$printer_tmp/command"
106+
mkdir -m 0700 "$printer_tmp/spool"
107+
108+
ippeveprinter -p 18631 -d "$printer_tmp/spool" -c "$printer_tmp/command" "$printer_name" \
109+
>"$printer_tmp/ippeveprinter.log" 2>&1 &
110+
printer_process=$!
111+
112+
printing_test_cleanup() {
113+
kill "$printer_process" >/dev/null 2>&1 || true
114+
wait "$printer_process" >/dev/null 2>&1 || true
115+
rm -rf "$printer_tmp"
116+
}
117+
trap printing_test_cleanup EXIT
118+
119+
for _ in {1..30}; do
120+
lpstat -v "$printer_name" 2>/dev/null | grep -q "implicitclass://$printer_name/" && break
121+
sleep 1
122+
done
123+
124+
lpstat -v "$printer_name" 2>/dev/null | grep -q "implicitclass://$printer_name/" ||
125+
fail "non-root cups-browsed discovers a driverless IPP printer" "$(<"$printer_tmp/ippeveprinter.log")"
126+
127+
printing_test_cleanup
128+
trap - EXIT
129+
130+
pass "non-root cups-browsed still creates driverless IPP queues without capabilities"
131+
}
132+
67133
verify_runtime_tools() {
68134
# Docker access is intentionally NOT granted to the desktop user: the docker
69135
# group is root-equivalent, so a rogue process running as the user could
@@ -107,7 +173,7 @@ verify_user_setup() {
107173
pass "Omarchy user state and shell configuration exist"
108174
}
109175

110-
for check in verify_core_packages verify_defaults verify_services verify_runtime_tools verify_user_setup; do
176+
for check in verify_core_packages verify_defaults verify_services verify_printing_security verify_runtime_tools verify_user_setup; do
111177
if ! ("$check"); then
112178
status=1
113179
fi

0 commit comments

Comments
 (0)