Skip to content

fix(security): replace shell command execution with direct exec calls#1099

Merged
mhduiy merged 1 commit intolinuxdeepin:masterfrom
mhduiy:sec
May 11, 2026
Merged

fix(security): replace shell command execution with direct exec calls#1099
mhduiy merged 1 commit intolinuxdeepin:masterfrom
mhduiy:sec

Conversation

@mhduiy
Copy link
Copy Markdown
Contributor

@mhduiy mhduiy commented May 8, 2026

  1. Remove doAction() helper in inputdevices1/utils.go and bin/user-config/config_datas.go that passed commands through /bin/sh
  2. Replace all exec.Command("/bin/sh", "-c", cmd) calls with exec.Command(prog, args...) using proper argument lists
  3. Fix command injection in checkProRunning() by validating serverName with regex and using pgrep instead of shell piped ps|awk
  4. Replace shell echo redirect with os.WriteFile in setSupportAcpiDeviceEnable()
  5. Convert lsblk invocation to use argument list instead of shell

Log: Eliminate command injection vectors by replacing shell command execution with direct exec.Command calls across multiple modules

fix(security): 替换 shell 命令执行为直接 exec 调用

  1. 移除 inputdevices1/utils.go 和 bin/user-config/config_datas.go 中 通过 /bin/sh 传递命令的 doAction() 辅助函数
  2. 将所有 exec.Command("/bin/sh", "-c", cmd) 调用替换为使用参数列表的 exec.Command(prog, args...)
  3. 修复 checkProRunning() 中的命令注入漏洞,使用正则验证 serverName 并用 pgrep 替代 shell 管道 ps|awk
  4. 将 setSupportAcpiDeviceEnable() 中的 shell echo 重定向替换为 os.WriteFile
  5. 将 lsblk 调用改为使用参数列表而非 shell 执行

Log: 通过在多个模块中将 shell 命令执行替换为直接 exec.Command 调用,消除命令注入风险
PMS: TASK-389293

1. Remove doAction() helper in inputdevices1/utils.go and
   bin/user-config/config_datas.go that passed commands through /bin/sh
2. Replace all exec.Command("/bin/sh", "-c", cmd) calls with
   exec.Command(prog, args...) using proper argument lists
3. Fix command injection in checkProRunning() by validating serverName
   with regex and using pgrep instead of shell piped ps|awk
4. Replace shell echo redirect with os.WriteFile in
   setSupportAcpiDeviceEnable()
5. Convert lsblk invocation to use argument list instead of shell

Log: Eliminate command injection vectors by replacing shell command execution with direct exec.Command calls across multiple modules

fix(security): 替换 shell 命令执行为直接 exec 调用

1. 移除 inputdevices1/utils.go 和 bin/user-config/config_datas.go 中
   通过 /bin/sh 传递命令的 doAction() 辅助函数
2. 将所有 exec.Command("/bin/sh", "-c", cmd) 调用替换为使用参数列表的
   exec.Command(prog, args...)
3. 修复 checkProRunning() 中的命令注入漏洞,使用正则验证 serverName
   并用 pgrep 替代 shell 管道 ps|awk
4. 将 setSupportAcpiDeviceEnable() 中的 shell echo 重定向替换为
   os.WriteFile
5. 将 lsblk 调用改为使用参数列表而非 shell 执行

Log: 通过在多个模块中将 shell 命令执行替换为直接 exec.Command 调用,消除命令注入风险
PMS: TASK-389293
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Sorry @mhduiy, 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

这份代码 diff 主要涉及对多个 Go 源文件的修改,核心改动在于移除了通过 /bin/sh -c 执行字符串命令的封装函数(如 doActionrunCommand),转而直接使用 exec.Command。同时,对版权年份进行了更新,并优化了部分系统调用。

以下是对语法逻辑、代码质量、代码性能和代码安全方面的详细审查意见:

1. 代码安全 - 显著提升

审查意见: 这是本次改动最大的亮点,极大地提高了代码的安全性。

  • 命令注入风险消除
    • 旧代码:大量使用 exec.Command("/bin/sh", "-c", cmdString)。如果 cmdString 中的变量(如 user, dir, serverName)包含恶意构造的字符(例如 ; rm -rf /),将导致严重的命令注入漏洞。
    • 新代码:使用 exec.Command("program", "arg1", "arg2")。这种方式通过参数列表传递参数,由操作系统内核直接执行 execve 系统调用,完全避免了 Shell 解释器的参与,从而从根本上杜绝了命令注入的风险。
    • 具体案例
      • checkProRunning 函数:旧代码使用 ps ux | awk ... 拼接字符串,非常危险。新代码引入了正则校验 regexp.MustCompile(...) 来验证 serverName 的合法性,并改用 pgrep -f。这既防止了注入,也简化了逻辑。
      • changeDirOwner 函数:旧代码拼接 chown 命令字符串,新代码直接传递参数列表。
      • setSupportAcpiDeviceEnable 函数:旧代码通过 echo ... > ... 的 Shell 重定向方式写入文件,存在注入风险。新代码使用 Go 原生的 os.WriteFile,这是最安全、最纯粹的 Go 语言写法。

2. 代码质量 - 改进

审查意见: 代码更加符合 Go 语言的惯用法,可读性和可维护性增强。

  • 移除冗余封装
    • 删除了 doActionrunCommand 等通用封装函数。这些封装不仅增加了不必要的函数调用开销,还掩盖了具体的执行逻辑。直接使用 exec.Command 让调用处的意图更加清晰。
  • 错误处理
    • doAction 旧实现中,将命令执行的 stderr 输出转换为 error 字符串(errors.New(string(out))),这会丢失原始的错误类型和上下文。
    • 新代码直接返回 exec.Command(...).Run()error,保留了原始的 *exec.ExitError 类型,调用方可以更灵活地处理错误(例如判断退出状态码)。
  • 逻辑简化
    • setSupportAcpiDeviceEnable:从执行 Shell 命令写入文件改为使用标准库 os.WriteFile,减少了进程创建的开销,代码也更简洁。

3. 代码性能 - 优化

审查意见: 减少了系统资源消耗。

  • 减少进程创建
    • 旧代码每次执行命令都需要启动 /bin/sh 进程,再由 sh 启动目标程序(如 chown, lsblk),即"双进程"开销。
    • 新代码直接启动目标程序,减少了一个 Shell 进程的创建和销毁开销。
  • 避免管道开销
    • checkProRunning 旧代码使用了管道(ps | awk),涉及进程间通信。新代码使用 pgrep 单一进程完成工作,效率更高。

4. 语法逻辑 - 正确

审查意见: 语法修改正确,逻辑保持一致。

  • 参数拼接
    • changeDirOwneruser+":"+user 的拼接是正确的,对应 chown user:group 的格式。
    • applyOptions 中使用 args = append(args, "-option", opt) 正确地构建了参数列表,对应 setxkbmap -option opt1 -option opt2 的逻辑。
  • 正则表达式
    • checkProRunning 中的正则 ^[a-zA-Z0-9._@:/-]+$ 能够有效过滤掉大部分 Shell 元字符(如 ;, &, |, $, ` 等),逻辑上是合理的。

5. 具体改进建议

尽管整体改进很大,但仍有极少数细节可以进一步优化:

  1. setSupportAcpiDeviceEnable 的文件权限

    • 代码os.WriteFile(paths[0], []byte(paths[1]+"\n"), 0644)
    • 建议:写入 sysfs 文件(通常是 /sys/ 下的文件)时,权限通常不是由 0644 决定的,而是由内核控制。虽然这里传入 0644os.WriteFile 中是必须的,但建议确认 paths[0] 是否确实指向普通文件。如果是 sysfs 节点,确保程序有足够的权限(通常是 root)写入即可,权限掩码参数实际不起作用但也不会报错。如果是普通配置文件,0644 是合理的。
  2. checkProRunning 的正则严谨性

    • 代码regexp.MustCompile(^[a-zA-Z0-9._@:/-]+$)
    • 建议:目前的正则允许 /:,这对于进程名(通常是二进制文件名)来说可能过于宽泛,虽然 pgrep -f 匹配的是命令行全路径,但为了防御深度,建议确认 serverName 的实际输入格式。如果仅包含简单的服务名(如 dde-daemon),可以收紧正则,例如 ^[a-zA-Z0-9_-]+$
  3. 版权年份更新

    • 代码2018 - 2026
    • 建议:将年份更新到未来(2026)通常是为了减少维护频率,这属于项目管理决策,从代码角度看没有问题。

总结

这是一次非常高质量的代码重构。主要目标是提升安全性(消除命令注入),同时顺带提升了性能和代码质量。改动符合 Go 语言的最佳实践,建议合并。

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

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

@mhduiy mhduiy merged commit 99eafe7 into linuxdeepin:master May 11, 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