Skip to content

fix: adjust dpms-state file permissions#1153

Merged
fly602 merged 1 commit into
linuxdeepin:masterfrom
fly602:master
Jun 18, 2026
Merged

fix: adjust dpms-state file permissions#1153
fly602 merged 1 commit into
linuxdeepin:masterfrom
fly602:master

Conversation

@fly602

@fly602 fly602 commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Change file permissions for /tmp/dpms-state from 0600 to 0644 in three locations

  1. keybinding1/utils.go: systemTurnOffScreen function
  2. session/power1/power_save_plan.go: restoreDpmsStateFile function
  3. session/power1/utils.go: setDPMSModeOff function This change is necessary because dde-lock also reads/writes this file, and the restrictive 0600 permissions caused permission errors leading to lock screen failure

Influence:

  1. Test lock screen functionality (dde-lock) to verify it can read/write dpms-state file without errors
  2. Verify power management functions (DPMS mode and state restoration) work correctly after permission change
  3. Test screen turn off/on sequences during power save operations
  4. Check file permissions are correctly set to 0644 and verify proper read/write access
  5. Test multiple users or processes accessing the dpms-state file simultaneously
  6. Verify no other components are affected by the permission change on / tmp/dpms-state

修复: 调整 dpms-state 文件权限

在三个位置将 /tmp/dpms-state 文件的权限从 0600 改为 0644

  1. keybinding1/utils.go: systemTurnOffScreen 函数
  2. session/power1/power_save_plan.go: restoreDpmsStateFile 函数
  3. session/power1/utils.go: setDPMSModeOff 函数 此更改是必要的,因为 dde-lock 也会读写此文件,而限制性的 0600 权限导致权
    限错误,造成锁屏失败

Influence:

  1. 测试锁屏功能 (dde-lock) 以确保其能够无错误地读写 dpms-state 文件
  2. 验证权限更改后电源管理功能(DPMS 模式和状态恢复)正常工作
  3. 测试省电计划期间的屏幕开关序列
  4. 检查文件权限是否正确设置为 0644 并验证适当的读写访问权限
  5. 测试多个用户或进程同时访问 dpms-state 文件
  6. 验证权限更改不会影响其他依赖于 /tmp/dpms-state 的组件

PMS: BUG-364835

Change file permissions for `/tmp/dpms-state` from 0600 to 0644 in three
locations
1. keybinding1/utils.go: systemTurnOffScreen function
2. session/power1/power_save_plan.go: restoreDpmsStateFile function
3. session/power1/utils.go: setDPMSModeOff function
This change is necessary because dde-lock also reads/writes this file,
and the restrictive 0600 permissions caused permission errors leading to
lock screen failure

Influence:
1. Test lock screen functionality (dde-lock) to verify it can read/write
dpms-state file without errors
2. Verify power management functions (DPMS mode and state restoration)
work correctly after permission change
3. Test screen turn off/on sequences during power save operations
4. Check file permissions are correctly set to 0644 and verify proper
read/write access
5. Test multiple users or processes accessing the dpms-state file
simultaneously
6. Verify no other components are affected by the permission change on /
tmp/dpms-state

修复: 调整 dpms-state 文件权限

在三个位置将 `/tmp/dpms-state` 文件的权限从 0600 改为 0644
1. keybinding1/utils.go: systemTurnOffScreen 函数
2. session/power1/power_save_plan.go: restoreDpmsStateFile 函数
3. session/power1/utils.go: setDPMSModeOff 函数
此更改是必要的,因为 dde-lock 也会读写此文件,而限制性的 0600 权限导致权
限错误,造成锁屏失败

Influence:
1. 测试锁屏功能 (dde-lock) 以确保其能够无错误地读写 dpms-state 文件
2. 验证权限更改后电源管理功能(DPMS 模式和状态恢复)正常工作
3. 测试省电计划期间的屏幕开关序列
4. 检查文件权限是否正确设置为 0644 并验证适当的读写访问权限
5. 测试多个用户或进程同时访问 dpms-state 文件
6. 验证权限更改不会影响其他依赖于 /tmp/dpms-state 的组件

PMS: BUG-364835

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @fly602, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:60分

■ 【总体评价】

代码通过修改权限解决了跨模块状态读取问题,但引入了严重的安全风险
逻辑正确但因在全局可写目录使用不安全权限引发安全漏洞,强制触发安全上限扣40分

■ 【详细分析】

  • 1.语法逻辑(完全正确)✓

代码仅将八进制权限字面量从 0600 修改为 0644,符合 Go 语言语法规范,未引入任何编译错误或逻辑异常

建议:无需修改语法

  • 2.代码质量(一般)✕

代码在三个不同文件中硬编码了 "/tmp/dpms-state" 路径,存在魔法字符串和代码重复问题,且使用 /tmp 目录作为系统状态持久化路径不符合 UOS 系统规范
潜在问题:硬编码路径导致维护困难;/tmp 目录在系统重启后可能被清理,导致状态丢失逻辑不可靠
建议:将文件路径提取为全局常量;将状态文件迁移至 XDG_RUNTIME_DIR 或 /var/lib/ 下的专用目录

  • 3.代码性能(无性能问题)✓

修改仅涉及文件权限参数传递,不涉及算法变更或额外系统调用开销

建议:无需优化性能

  • 4.代码安全(存在 2 个安全漏洞(中危1个,低危1个))✕

漏洞对比统计:新增漏洞 2 个,减少漏洞 0 个,持平 0 个
在全局可写的 /tmp 目录下操作文件且放宽权限,存在被恶意利用的风险

  • 安全漏洞1(【中危】):符号链接攻击/竞态条件 在 keybinding1/utils.gosession/power1/power_save_plan.gosession/power1/utils.goSafeWriteFile 调用中,在全局可写的 /tmp 目录下操作文件,若 SafeWriteFile 内部未严格校验并消除符号链接,攻击者可预先创建 /tmp/dpms-state 软链接指向 /etc/sudoers 等关键系统文件,触发写入操作将导致系统文件被覆盖,引发拒绝服务或本地权限提升 ——非常重要

  • 安全漏洞2(【低危】):敏感状态信息暴露 在上述三个函数中,将文件权限从 0600 改为 0644,导致系统上所有用户均可读取该 DPMS 状态文件,可能被本地恶意软件用于侧信道探测用户屏幕活动状态或作为定时任务触发的判断依据 ——非常重要

  • 建议:废弃 /tmp 目录,改用 os.Getenv("XDG_RUNTIME_DIR") 获取用户级安全运行时目录;若确需跨进程共享,将权限设置为 0640 并确保访问进程处于同一用户组;使用 os.OpenFile 配合 O_NOFOLLOW 标志位防止符号链接跟随

■ 【改进建议代码示例】

--- a/keybinding1/utils.go
+++ b/keybinding1/utils.go
@@ -1,6 +1,8 @@
 package keybinding1
 
 import (
+	"os"
+	"path/filepath"
 	"..."
 )
 
@@ -331,7 +333,12 @@ func (m *Manager) systemTurnOffScreen() {
 		m.setWmBlackScreenActive(false)
 	}
 	undoPrepareSuspend()
-	fileutil.SafeWriteFile("/tmp/dpms-state", []byte("1"), 0644)
+
+	stateDir := os.Getenv("XDG_RUNTIME_DIR")
+	if stateDir == "" {
+		logger.Warning("XDG_RUNTIME_DIR is not set")
+		return
+	}
+	fileutil.SafeWriteFile(filepath.Join(stateDir, "dpms-state"), []byte("1"), 0640)
 }
 
 func isDpmsOff() bool {
--- a/session/power1/power_save_plan.go
+++ b/session/power1/power_save_plan.go
@@ -1068,7 +1068,7 @@ func (ps *powerSavePlan) restoreDpmsStateFile() {
 	}
 
 	if string(v) == "1" {
-		err = fileutil.SafeWriteFile("/tmp/dpms-state", []byte("0"), 0644)
+		err = fileutil.SafeWriteFile(filepath.Join(os.Getenv("XDG_RUNTIME_DIR"), "dpms-state"), []byte("0"), 0640)
 		if err != nil {
 			logger.Warning("write dpms state:", err)
 		}
--- a/session/power1/utils.go
+++ b/session/power1/utils.go
@@ -210,7 +210,7 @@ func (m *Manager) setDPMSModeOff() {
 	} else {
 		callSetScreenState(true)
 	}
-	fileutil.SafeWriteFile("/tmp/dpms-state", []byte("1"), 0644)
+	fileutil.SafeWriteFile(filepath.Join(os.Getenv("XDG_RUNTIME_DIR"), "dpms-state"), []byte("1"), 0640)
 }
 
 const (

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: fly602, mhduiy

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@fly602 fly602 merged commit fdd5c71 into linuxdeepin:master Jun 18, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants