Skip to content

Commit a464fa4

Browse files
committed
feat: Add security restriction configurations to all service files
- Use the deepin-daemon user to run services whenever possible, reducing privilege risks; - Add a D-Bus policy to allow the deepin-daemon user to invoke the HandleSystemEvent method; - Deny users other than root and deepin-daemon from calling HandleSystemEvent, strengthening access control; - Remove the unused update-metadata-info service. Task: https://pms.uniontech.com/task-view-385069.html
1 parent 2e44b10 commit a464fa4

17 files changed

Lines changed: 179 additions & 95 deletions

debian/lastore-daemon.postinst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ case "$1" in
1010
[ -e /lib/systemd/dbus-org.deepin.dde.Lastore1.service ] && rm /lib/systemd/system/dbus-org.deepin.dde.Lastore1.service || true
1111
fi
1212
systemctl daemon-reload || true
13-
/var/lib/lastore/scripts/update_metadata_info || true
1413
/var/lib/lastore/scripts/build_system_info || true
1514
# Fix the problem that the machine id is the same after the system is installed.
1615
if [ -f /etc/machine-id ] && grep -q "a5fa4f1b04514009830c73f3b1f1dd4c" /etc/machine-id; then

lib/systemd/system/lastore-abort-auto-download.service

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,20 @@ Description=System Update Auto Download Abort Service
33
Wants=lastore-daemon.service
44

55
[Service]
6+
CapabilityBoundingSet=
7+
ExecStart=/usr/bin/dbus-send --system --print-reply --dest=org.deepin.dde.Lastore1 /org/deepin/dde/Lastore1 org.deepin.dde.Lastore1.Manager.HandleSystemEvent string:AbortAutoDownload
8+
InaccessiblePaths=-/etc/shadow -/etc/pam.d/ -/etc/NetworkManager/system-connections/ -/etc/security/ -/etc/selinux/ -/etc/deepin-elf-verify/ -/etc/filearmor.d/ -/etc/crypttab -/etc/fstab -/sysroot/ostree/repo/ -/persistent/ostree/repo/
9+
MemoryDenyWriteExecute=true
10+
NoNewPrivileges=true
11+
PrivateDevices=true
12+
PrivateIPC=true
13+
PrivateTmp=true
14+
ProtectClock=true
15+
ProtectHome=true
16+
ProtectKernelModules=true
17+
ProtectKernelTunables=true
18+
ProtectProc=invisible
19+
ProtectSystem=strict
20+
RestrictSUIDSGID=true
621
Type=oneshot
7-
ExecStart=/usr/bin/dbus-send --system --print-reply --dest=org.deepin.dde.Lastore1 /org/deepin/dde/Lastore1 org.deepin.dde.Lastore1.Manager.HandleSystemEvent string:AbortAutoDownload
22+
User=deepin-daemon

lib/systemd/system/lastore-after-upgrade-check.service

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,26 @@ Description=generate config file for check system
33
Before=display-manager.service
44

55
[Service]
6-
Type=oneshot
6+
7+
# PrivateTmp=true is not set because it needs to use /tmp to save state
8+
9+
CapabilityBoundingSet=
710
ExecStart=/var/lib/lastore/scripts/gen_upgrade_check_config.sh
11+
InaccessiblePaths=-/etc/shadow -/etc/pam.d/ -/etc/NetworkManager/system-connections/ -/etc/security/ -/etc/selinux/ -/etc/deepin-elf-verify/ -/etc/filearmor.d/ -/etc/crypttab -/etc/fstab -/sysroot/ostree/repo/ -/persistent/ostree/repo/
12+
MemoryDenyWriteExecute=true
13+
NoNewPrivileges=true
14+
PrivateDevices=true
15+
PrivateIPC=true
16+
ProtectClock=true
17+
ProtectHome=true
18+
ProtectKernelModules=true
19+
ProtectKernelTunables=true
20+
ProtectProc=invisible
21+
ProtectSystem=strict
22+
ReadWritePaths=/tmp/
23+
RestrictSUIDSGID=true
24+
Type=oneshot
25+
User=deepin-daemon
826

927
[Install]
1028
WantedBy=multi-user.target

lib/systemd/system/lastore-auto-download.service

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,19 @@ Description=System Update Auto Download Service
33
Wants=lastore-daemon.service
44

55
[Service]
6+
CapabilityBoundingSet=
7+
ExecStart=/usr/bin/dbus-send --system --print-reply --dest=org.deepin.dde.Lastore1 /org/deepin/dde/Lastore1 org.deepin.dde.Lastore1.Manager.HandleSystemEvent string:AutoDownload
8+
InaccessiblePaths=-/etc/shadow -/etc/pam.d/ -/etc/NetworkManager/system-connections/ -/etc/security/ -/etc/selinux/ -/etc/deepin-elf-verify/ -/etc/filearmor.d/ -/etc/crypttab -/etc/fstab -/sysroot/ostree/repo/ -/persistent/ostree/repo/
9+
MemoryDenyWriteExecute=true
10+
NoNewPrivileges=true
11+
PrivateDevices=true
12+
PrivateIPC=true
13+
PrivateTmp=true
14+
ProtectClock=true
15+
ProtectHome=true
16+
ProtectKernelModules=true
17+
ProtectKernelTunables=true
18+
ProtectSystem=strict
19+
RestrictSUIDSGID=true
620
Type=oneshot
7-
ExecStart=/usr/bin/dbus-send --system --print-reply --dest=org.deepin.dde.Lastore1 /org/deepin/dde/Lastore1 org.deepin.dde.Lastore1.Manager.HandleSystemEvent string:AutoDownload
21+
User=deepin-daemon

lib/systemd/system/lastore-build-system-info.service

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,28 @@
22
Description=Build system info
33

44
[Service]
5-
Type=idle
6-
StartLimitInterval=10s
7-
StartLimitBurst=20
8-
RestartSec=5s
9-
Restart=on-failure
5+
6+
# MemoryDenyWriteExecute=true is not set because the script calls lastore-tools (a Go program compiled with -pie option) which would fail.
7+
8+
CapabilityBoundingSet=
109
ExecStart=/var/lib/lastore/scripts/build_system_info -now
10+
InaccessiblePaths=-/etc/shadow -/etc/pam.d/ -/etc/NetworkManager/system-connections/ -/etc/security/ -/etc/selinux/ -/etc/deepin-elf-verify/ -/etc/filearmor.d/ -/etc/crypttab -/etc/fstab -/sysroot/ostree/repo/ -/persistent/ostree/repo/
11+
NoNewPrivileges=true
12+
PrivateDevices=true
13+
PrivateIPC=true
14+
PrivateTmp=true
15+
ProtectClock=true
16+
ProtectHome=true
17+
ProtectKernelModules=true
18+
ProtectKernelTunables=true
19+
ProtectProc=invisible
20+
ProtectSystem=strict
21+
ReadWritePaths=/var/
22+
Restart=on-failure
23+
RestartSec=5s
24+
RestrictSUIDSGID=true
25+
StartLimitBurst=20
26+
StartLimitInterval=10s
27+
StateDirectory=lastore
28+
Type=idle
29+
User=deepin-daemon

lib/systemd/system/lastore-daemon.service

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,28 @@ Description=Deepin Lastore Daemon
33
After=display-manager.service
44

55
[Service]
6-
Type=dbus
6+
7+
# CapabilityBoundingSet= is not set because it would cause apt execution errors, such as setegid and other operations failing due to insufficient permissions.
8+
# InaccessiblePaths is not set because dpkg needs to be called to upgrade the system.
9+
# MemoryDenyWriteExecute=yes is not set because this is a Go program compiled with -pie option, which would prevent the process from starting.
10+
# PrivateDevices=true is not set because grub-mkconfig is executed when creating backup deployment.
11+
# PrivateTmp=true is not set because it relies on /tmp to record state.
12+
# ProtectHome=true is not set because sudo is needed to connect to the user Session Bus and send DDE message notifications.
13+
# ProtectKernelModules=true is not set because kernel packages need to be installed
14+
# ProtectProc=true is not set because it needs to access caller process environment variables.
15+
# ProtectSystem=strict is not set because dpkg needs to be called to upgrade the system.
16+
# RestrictSUIDSGID=true is not set because some packages may need to set SUID and SGID during installation.
17+
# StateDirectory=lastore is not set because it would conflict with the ownership of smartmirror-daemon and build-system-info services, which need to be owned by deepin-daemon, and enabling this would set the owner to root.
18+
719
BusName=org.deepin.dde.Lastore1
8-
ExecStart=/usr/libexec/lastore-daemon/lastore-daemon
9-
StandardOutput=null
10-
StandardError=null
11-
StateDirectory=lastore
1220
CacheDirectory=lastore
21+
ExecStart=/usr/libexec/lastore-daemon/lastore-daemon
22+
NoNewPrivileges=true
23+
PrivateIPC=true
24+
ProtectClock=true
25+
ProtectKernelTunables=true
1326
RuntimeDirectory=lastore
14-
RuntimeDirectoryMode=0750
27+
RuntimeDirectoryMode=0750
28+
StandardError=null
29+
StandardOutput=null
30+
Type=dbus

lib/systemd/system/lastore-smartmirror-daemon.service

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,31 @@ Wants=dbus.socket
55
After=dbus.socket
66

77
[Service]
8-
Type=dbus
8+
# Cannot set PrivateNetwork=yes because network access is required.
9+
# Cannot set MemoryDenyWriteExecute=yes because this is a Go program compiled with -pie option, which would prevent the process from starting.
910
BusName=org.deepin.dde.Lastore1.Smartmirror
10-
User=deepin-daemon
11+
CapabilityBoundingSet=
1112
ExecStart=/usr/libexec/lastore-daemon/lastore-smartmirror-daemon
12-
StandardOutput=null
13-
StandardError=journal
14-
15-
ProtectSystem=strict
16-
StateDirectory=lastore
17-
InaccessiblePaths=/etc/shadow
18-
InaccessiblePaths=-/etc/NetworkManager/system-connections
19-
InaccessiblePaths=-/etc/pam.d
20-
InaccessiblePaths=-/usr/share/uadp/
21-
13+
InaccessiblePaths=-/etc/shadow -/etc/pam.d/ -/etc/NetworkManager/system-connections/ -/etc/security/ -/etc/selinux/ -/etc/deepin-elf-verify/ -/etc/filearmor.d/ -/etc/crypttab -/etc/fstab -/sysroot/ostree/repo/ -/persistent/ostree/repo/
14+
LockPersonality=yes
2215
NoNewPrivileges=yes
23-
ProtectHome=yes
24-
ProtectKernelTunables=yes
25-
ProtectKernelModules=yes
26-
ProtectControlGroups=yes
16+
PrivateDevices=yes
17+
PrivateIPC=true
2718
PrivateMounts=yes
2819
PrivateTmp=yes
29-
PrivateDevices=yes
30-
# 需要联网
31-
#PrivateNetwork=yes
3220
PrivateUsers=yes
21+
ProtectClock=true
22+
ProtectControlGroups=yes
23+
ProtectHome=yes
24+
ProtectKernelModules=yes
25+
ProtectKernelTunables=yes
26+
ProtectSystem=strict
27+
RemoveIPC=yes
3328
RestrictNamespaces=yes
34-
LockPersonality=yes
3529
RestrictRealtime=yes
36-
RemoveIPC=yes
37-
# 和golang -pie参数冲突,导致进程无法启动
38-
#MemoryDenyWriteExecute=yes
39-
#MemoryLimit=100M
30+
RestrictSUIDSGID=true
31+
StandardError=journal
32+
StandardOutput=null
33+
StateDirectory=lastore
34+
Type=dbus
35+
User=deepin-daemon

lib/systemd/system/lastore-update-metadata-info.service

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/lastore-daemon/manager_unit.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ type systemdEventType string
3838
const (
3939
AutoCheck systemdEventType = "AutoCheck"
4040
AutoClean systemdEventType = "AutoClean"
41-
UpdateInfosChanged systemdEventType = "UpdateInfosChanged"
4241
OsVersionChanged systemdEventType = "OsVersionChanged"
4342
InitIdleDownload systemdEventType = "InitIdleDownload"
4443
AutoDownload systemdEventType = "AutoDownload"
@@ -335,16 +334,6 @@ func (m *Manager) getNextUpdateDelay() time.Duration {
335334
}
336335

337336
func (m *Manager) delHandleSystemEvent(sender dbus.Sender, eventType string) error {
338-
uid, err := m.service.GetConnUID(string(sender))
339-
if err != nil {
340-
logger.Warning(err)
341-
return dbusutil.ToError(err)
342-
}
343-
if uid != 0 && systemdEventType(eventType) != OsVersionChanged {
344-
err = fmt.Errorf("%q is not allowed to trigger system event", uid)
345-
logger.Warning(err)
346-
return dbusutil.ToError(err)
347-
}
348337
m.service.DelayAutoQuit()
349338
typ := systemdEventType(eventType)
350339
switch typ {
@@ -363,9 +352,6 @@ func (m *Manager) delHandleSystemEvent(sender dbus.Sender, eventType string) err
363352
logger.Warning(err)
364353
}
365354
}()
366-
// case UpdateInfosChanged:
367-
// logger.Info("UpdateInfos Changed")
368-
// m.handleUpdateInfosChanged()
369355
case OsVersionChanged:
370356
go updateplatform.UpdateTokenConfigFile(m.config.IncludeDiskInfo)
371357
case InitIdleDownload:

src/lastore-smartmirror-daemon/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package main
66

77
import (
8+
"os"
9+
810
"github.com/linuxdeepin/lastore-daemon/src/internal/system"
911
)
1012

@@ -33,5 +35,8 @@ func (c *config) setEnable(enable bool) error {
3335
}
3436

3537
func (c *config) save() error {
38+
if err := os.Remove(c.filePath); err != nil && !os.IsNotExist(err) {
39+
logger.Warning("remove config file failed:", err)
40+
}
3641
return system.EncodeJson(c.filePath, c)
3742
}

0 commit comments

Comments
 (0)