news 2026/4/24 15:34:43

第6章:字符设备驱动的高级操作6:Capabilities and Restricted Operations

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
第6章:字符设备驱动的高级操作6:Capabilities and Restricted Operations

In continuation of the previous text第6章:字符设备驱动的高级操作5:Using the ioctl Argument​​​​​​​ let’s GO ahead.

Access to a device is controlled by the permissions on the device file(s), and the driver is not normally involved in permissions checking. There are situations, however, where any user is granted read/write permission on the device, but some control operations should still be denied. For example, not all users of a tape drive should be able to set its default block size, and a user who has been granted read/write access to a disk device should probably still be denied the ability to format it. In cases like these, the driver must perform additional checks to be sure that the user is capable of performing the requested operation.

对设备的访问是由设备文件的权限位控制的,驱动程序通常不参与权限检查。然而,在某些情况下,尽管所有用户都被授予了设备的读写权限,但某些控制操作仍然应该被禁止。例如,并非所有使用磁带驱动器的用户都应该能够设置其默认块大小;被授予磁盘设备读写权限的用户,通常也应当被禁止执行格式化操作。在类似这样的场景中,驱动程序必须执行额外的检查,以确保用户有权执行所请求的操作。

Unix systems have traditionally restricted privileged operations to the superuser account. This meant that privilege was an all-or-nothing thing—the superuser can do absolutely anything, but all other users are highly restricted. The Linux kernel provides a more flexible system called capabilities. A capability-based system leaves the all-or-nothing mode behind and breaks down privileged operations into separate subgroups. In this way, a particular user (or program) can be empowered to perform a specific privileged operation without giving away the ability to perform other, unrelated operations. The kernel uses capabilities exclusively for permissions management and exports two system calls capget and capset, to allow them to be managed from user space.

传统的 Unix 系统将特权操作限制为超级用户(root)才能执行。这意味着权限是 “全有或全无” 的 —— 超级用户可以做任何事,但其他所有用户都受到严格限制。Linux 内核提供了一种更灵活的机制,称为能力(capabilities)。基于能力的系统摆脱了 “全有或全无” 的模式,将特权操作分解为独立的子组。通过这种方式,特定的用户(或程序)可以被授权执行某一项特定的特权操作,而无需赋予其执行其他无关操作的权限。内核完全使用能力机制来进行权限管理,并导出了两个系统调用capgetcapset,允许从用户空间对能力进行管理。

The full set of capabilities can be found in . These are the only capabilities known to the system; it is not possible for driver authors or system administrators to define new ones without modifying the kernel source. A subset of those capabilities that might be of interest to device driver writers includes the following<linux/capability.h>:

完整的能力集定义在<linux/capability.h>头文件中。这些是系统已知的仅有的能力;驱动开发者或系统管理员如果不修改内核源码,就无法定义新的能力。设备驱动开发者可能关心的能力子集如下:

CapabilityDescription
CAP_DAC_OVERRIDEThe ability to override access restrictions (data access control, or DAC) on files and directories.
CAP_NET_ADMINThe ability to perform network administration tasks, including those that affect network interfaces.
CAP_SYS_MODULEThe ability to load or remove kernel modules.
CAP_SYS_RAWIOThe ability to perform "raw" I/O operations. Examples include accessing device ports or communicating directly with USB devices.
CAP_SYS_ADMINA catch-all capability that provides access to many system administration operations.
CAP_SYS_TTY_CONFIGThe ability to perform tty configuration tasks.
能力名称描述
CAP_DAC_OVERRIDE忽略文件和目录上的访问权限限制(数据访问控制,DAC)。
CAP_NET_ADMIN执行网络管理任务的权限,包括配置网络接口等操作。
CAP_SYS_MODULE加载或卸载内核模块的权限。
CAP_SYS_RAWIO执行 “原始” I/O 操作的权限。例如访问设备端口或直接与 USB 设备通信。
CAP_SYS_ADMIN一个通用的管理能力,涵盖了许多系统管理操作。
CAP_SYS_TTY_CONFIG执行 TTY 终端配置任务的权限。

Before performing a privileged operation, a device driver should check that the calling process has the appropriate capability; failure to do so could result user processes performing unauthorized operations with bad results on system stability or security. Capability checks are performed with the capable function (defined in ):

在执行特权操作之前,设备驱动程序应该检查调用进程是否拥有相应的能力;如果不这样做,可能会导致用户进程执行未授权的操作,从而对系统稳定性或安全性造成不良影响。能力检查通过capable函数完成(定义在<linux/sched.h>中):

int capable(int capability);

In the scull sample driver, any user is allowed to query the quantum and quantum set sizes. Only privileged users, however, may change those values, since inappropriate values could badly affect system performance. When needed, the scull implementation of ioctl checks a user’s privilege level as follows:

在 scull 示例驱动中,允许任何用户查询quantumquantum set的大小。但是,只有特权用户可以修改这些值,因为不恰当的数值可能严重影响系统性能。在需要时,scull 驱动的 ioctl 实现会按如下方式检查用户权限:

if (! capable (CAP_SYS_ADMIN)) return -EPERM;

In the absence of a more specific capability for this task, CAP_SYS_ADMIN was chosen for this test.

由于没有更具体的能力适用于此任务,因此选择了CAP_SYS_ADMIN进行此项检查。

补充说明:

1. 为什么需要在驱动里做权限检查?

  • 设备文件权限太粗糙:设备文件通常只设置rwx(读 / 写 / 执行)三种权限。如果一个普通用户拥有设备的写权限,他就可以通过write写入数据。但是,修改硬件配置、格式化磁盘、重启设备这类高危操作,不能仅仅因为有写权限就允许执行。
  • 细粒度控制:驱动内的能力检查,可以实现 “允许读写数据,但禁止修改配置” 的精细化控制。

2. 什么是 Capabilities(能力)?

  • 打破 root 垄断:传统上只有 root (UID 0) 能做所有事。Capabilities 把 root 的超级权限拆分成几十个小权限(如CAP_SYS_BOOT只允许重启,CAP_NET_RAW只允许抓包)。
  • 最小权限原则:给程序分配刚好够用的权限,而不是直接给 root 权限,大大提升系统安全性。
  • 内核机制:内核在执行关键操作时,会检查当前进程是否具备对应的能力。

3. 核心函数:capable ()

  • 功能:检查当前发起调用的进程是否拥有指定的特权能力
  • 返回值
    • 非 0:拥有权限,可以执行操作。
    • 0:没有权限,驱动应拒绝请求。
  • 标准错误码:检查失败时,必须返回-EPERM(Operation not permitted),这是 Linux 标准约定。

4. 关于 CAP_SYS_ADMIN

  • 定位:它是一个 **“万能兜底”** 能力(catch-all)。
  • 含义:涵盖了各种杂项的系统管理操作。
  • 使用建议
    • 如果有更具体的能力(如CAP_NET_ADMIN管理网络),优先使用具体的
    • 只有当没有合适的专用能力时(如修改驱动内部配置参数),才使用CAP_SYS_ADMIN
    • 拥有此能力的进程,权限非常高,接近传统 root。

5. 驱动权限检查的典型流程

ioctl处理高危命令时,代码逻辑通常如下:

  1. 检查命令幻数、序号是否合法。
  2. 检查用户指针是否合法。
  3. 检查能力(Capabilities)
    // 示例:修改设备配置 case SCULL_IOCSQUANTUM: /* 检查是否有系统管理员权限 */ if (!capable(CAP_SYS_ADMIN)) return -EPERM; /* 权限不足 */ /* 执行设置操作... */ break;

6. 总结

  • 设备文件权限管的是 “能不能打开这个设备”。
  • Capabilities (能力)管的是 “打开后,能不能执行高危操作”。
  • 驱动实现ioctl时,凡是涉及修改系统配置、硬件参数、敏感数据的命令,必须加 capable () 检查,这是保证系统安全的核心环节。
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/24 15:34:33

三相风筒FU6812控制系统代码功能说明

无感FOC电机三相控制高速吹风筒方案 FU6812LFD2504S 电压AC220V 功率80W 最高转速20万RPM 方案优势&#xff1a;响应快、效率高、噪声低、成本低 控制方式&#xff1a;三相电机无感FOC 闭环方式&#xff1a;功率闭环&#xff0c;速度闭环 调速接口&#xff1a;按键调试 提供原理…

作者头像 李华
网站建设 2026/4/24 15:33:30

Rust的#[repr(C)]结构体布局与C语言互操作在系统编程中的精确控制

在系统编程领域&#xff0c;Rust与C语言的互操作能力至关重要。Rust通过#[repr(C)]属性提供了对结构体内存布局的精确控制&#xff0c;确保其与C语言兼容&#xff0c;从而无缝集成现有代码库或与硬件交互。本文将深入探讨#[repr(C)]的核心机制及其在系统编程中的实践价值。 内…

作者头像 李华
网站建设 2026/4/24 15:30:07

DLSS Swapper终极指南:3分钟免费解锁游戏画质新境界

DLSS Swapper终极指南&#xff1a;3分钟免费解锁游戏画质新境界 【免费下载链接】dlss-swapper 项目地址: https://gitcode.com/GitHub_Trending/dl/dlss-swapper 你是否曾因游戏画面模糊、帧率不稳而烦恼&#xff1f;当其他玩家享受4K高清画质时&#xff0c;你却只能忍…

作者头像 李华
网站建设 2026/4/24 15:27:20

终极赛博朋克2077存档编辑器:从新手到专家的完全指南

终极赛博朋克2077存档编辑器&#xff1a;从新手到专家的完全指南 【免费下载链接】CyberpunkSaveEditor A tool to edit Cyberpunk 2077 sav.dat files 项目地址: https://gitcode.com/gh_mirrors/cy/CyberpunkSaveEditor 赛博朋克2077存档编辑器是一个强大的开源工具&a…

作者头像 李华
网站建设 2026/4/24 15:27:17

Fourier变换及其应用(Brad G. Osgood)——第1章——Fourier级数(附录)

目录 1.8 附录&#xff1a;关于傅里叶级数收敛性的注解(Notes on the Convergence of Fourier Series) 1.8.1 借助Dirichlet核研究部分和:回到蜂鸣声问题(Sdudying partial sum via Dirichlet kernel:The buzz is back) 1.8.2 收敛速度和平滑度:Fourier系数有多大&#xff1…

作者头像 李华