写保护虚拟页,捕获写入

发布于 2024-07-11 08:56:10 字数 255 浏览 5 评论 0原文

是否有一种方法可以捕获对写保护页面的写入?

我计划做一个类似自体的对象系统,您可以在其中复制对象来实例化它。 (因为与其他内容相比,这听起来简单而紧凑)显然,为此目的创建的对象应该以某种方式进行写保护。 我发现有一种方法可以在 ELF 的程序头中标记写保护的内容。 (RE、RWE、RW -标志)

这是否提供写保护? 我记得它应该会引发段错误,这是真的吗? 如何捕获对写保护页的写入。 这是实现我想要的效果的好方法吗?还有更好的方法吗?

Does there exist a way to catch a write into write-protected page?

I plan doing a self -like object system where you copy the object to instantiate it. (because it sounds simple and compact thing compared to the rest) Obviously, the objects created for this purpose ought be write-protected some way. I've seen there's a way to flag something write-protected from the program headers in the ELF. (RE, RWE, RW -flags)

Does this provide write-protection at all? I remember it should raise a segfault, is this true? How to catch the write into a write-protected page. Is this a good way to implement what I'm wanting for and is there a better way?

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

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

发布评论

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

评论(1

勿忘初心 2024-07-18 08:56:10

是的,您可以使用 mprotect

是的,写入受保护的内存会引发段错误。 您可以安装一个处理程序,例如在 C++ 中:

std::signal(SIGSEGV, my_segv_handler_func);

这是一种执行您想要的操作的合理方法,尽管您必须添加大量额外的管理工具才能使其工作。 例如,这种写入检测是在硬件中完成的,在 x86 架构上,页面大小为 4k。 因此,您可以一次保护 4k 的内容,并在 4k 边界上对齐——而不是通用的“从地址 X 开始并经过 N 字节”。 我相信你要么必须

  1. 有对象到页面的映射
    这样您就可以确定是否
    写入页面就是写入
    特定的受保护对象,或者
  2. 滚动您自己的 malloc
    在 4k 边界上分配,这将迫使您使用
    最小分配块大小为 4k

我不知道是否有更好的方法,但听起来很有趣。 :)

Yes, you can use mprotect.

Yes, a write to protected memory will raise a segfault. You can install a handler, e.g. in C++:

std::signal(SIGSEGV, my_segv_handler_func);

This is a plausible way of doing what you want, although you'd have to add a lot of extra management goo to make it work. For example, this sort of write-detection is done in hardware, and on x86 architectures you've got a page size of 4k. So you can protect things 4k at a time, aligned on 4k boundaries -- not a generic "start at address X and go N bytes". I believe you'd either have to

  1. have a mapping of objects to pages
    such that you can identify whether a
    write to a page is a write to a
    particular protected object, or
  2. roll your own malloc that always
    allocates on 4k boundaries, which would force you to use a
    minimum alloc'd block size of 4k

I don't know off the top of my head if there's a better way, but it sounds fun to play with. :)

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