用户进程如何访问我的模块加载的字符设备

发布于 2024-12-08 07:45:56 字数 290 浏览 1 评论 0原文

我正在尝试动态地将系统调用加载到内核中(无需重新启动内核并对其进行编译),以尝试(一旦处于内核模式)写入用户进程的内存。

(我知道有一种方法可以使用 ptrace 接口执行此操作,但它不是一个选项。)

我知道执行此操作的唯一方法是加载模块。为了允许用户与其通信,我被告知使用字符设备(应该位于 /dev/ 中,对吗?)。我成功加载了一个。 我的问题是我不知道用户进程如何在没有系统调用的情况下访问它。 (我被告知使用 ioctl) 任何人都可以展示用户进程如何调用 ioctl 例如由我的模块加载的吗?

谢谢, 沙伊

I am trying to load into the kernel a system-call dynamically (without restarting the kernel and compailing it) in an attempt to (once in kernel mode) write to user process's memory.

(I know there is a way to do this with the ptrace interface but it is not an option.)

I know the only way to do this is to load a module. In order to allow the user communicating with it, i was told to use a character device (which is supposed to be in /dev/, right?). I loaded one successfully.
My problem is that i don't know how the user process access it without an system call.
(i was told to use ioctl)
Could anyone show how a user process can call ioctl for example that was loaded by my module?

Thanks,
Shai

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭⌒浅淡时光〆 2024-12-15 07:45:56

对于下面的答案,我假设您正在开发一个 Linux 模块。重新阅读你的问题后,我发现我可能误解了这个问题。


有几种与内核驱动程序通信的机制:

  • /proc 条目(又名 procfs
  • ioctl 通过设备接口
  • 直接通过设备接口

最常用的技术最后一个使用 read() 和/或 write() 系统调用来引起驱动程序操作。虽然这些系统调用通常传递纯数据,但没有什么可以阻止特定驱动程序传递元数据输入/输出接口。

另一方面,如果驱动程序已经具有有用的纯数据规则,而 read()write() 不适用于元数据,则 ioctl () 系统调用是一种通用的瑞士军刀,用于执行与文件相关的各种操作,例如加载或卸载磁带、弹出 DVD、查找网卡的以太网地址或查找找出发生了多少个磁盘驱动器错误。已经定义了如此多的ioctl操作代码,您可能可以找到一个合理的代码来重用您的目的。 ioctl 接口的一个很大的缺点是它最适合由自定义程序使用,因此通过连接到标准程序的管道。

/proc 接口结合了前两种技术的优点:它可以在标准实用程序中使用 stdin/stdout 约定,但它还通过常规驱动程序机制提供独立于任何 I/O 的设备驱动程序接口。例如,在 Linux 系统上尝试 cat /proc/net/tcp。它显示所有 TCP 连接的状态。

关于实现 procfs 功能的好文章位于 create_proc_entry ()此处详细介绍了 ioctl 的实现。元数据方法与任何其他设备驱动程序一样易于编码,尽管它可能会给经验丰富的实施者带来概念上的障碍。

For the answer below, I have assumed you to be developing a Linux module. Upon rereading your question, I see I could have misinterpreted the problem.


There are several mechanisms for communicating with a kernel driver:

  • a /proc entry (aka procfs)
  • ioctl through the device interface
  • directly through the device interface

The most common technique is the last which uses read() and/or write() system calls to cause driver action. While these system calls ordinarily pass pure data, there is nothing stopping a particular driver from instead passing metadata through the i/o interface.

On the other hand, if the driver already has a useful pure data discipline for which read() and write() are not good for metadata, then the ioctl() system call is a kind of general purpose Swiss army knife for doing all kinds of things related to a file, such as loading or unloading a magnetic tape, ejecting a DVD, finding a network card's Ethernet address, or finding out how many disk drive errors have occurred. There have been so many ioctl operation codes defined, you can probably find a reasonable one to reuse for your purposes. A big disadvantage of the ioctl interface is that it is best suited for use by a custom program, so it would be awkward or difficult to transfer data through an ioctl connected to a pipeline of standard programs.

The /proc interface combines the best of the previous two techniques: it is amenable to using stdin/stdout conventions with standard utility programs, but it also provides an interface to a device driver which is independent of any i/o through the regular driver mechanism. For example, try cat /proc/net/tcp on your Linux system. It shows the status of all TCP connections.

A good article on implementing the procfs feature is at create_proc_entry(). Implementing an ioctl is well covered here. The metadata approach is as easy to code as any other device driver, though it might present conceptual hurdles for an experienced implementer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文