修改内核中的只读区内容

研究到 apparmor 内核源码,其中涉及到只读变量 __lsm_ro_after_init,研究怎么修改只读区实现部分功能,这里记录一下。

思路上很简单,修改的时候禁用写保护,或者修改pte写保护属性。

实现上第一种更简单,所以采用第一种。

主要函数为 disable_write_protection,修改cr0的写保护位即可。


inline void mywrite_cr0(unsigned long cr0) {
  asm volatile("mov %0,%%cr0" : "+r"(cr0), "+m"(__force_order));
}

void enable_write_protection(void) {
  unsigned long cr0 = read_cr0();
  set_bit(16, &cr0);
  mywrite_cr0(cr0);
}

void disable_write_protection(void) {
  unsigned long cr0 = read_cr0();
  clear_bit(16, &cr0);
  mywrite_cr0(cr0);
}

static int hook_apparmor_func(void *arg)
{
    union security_list_options *tmp = arg;

    disable_write_protection();
    tmp->file_permission = some_func;
    enable_write_protection();
    return 0;
}

static int recover_apparmor_func(void *arg)
{
    union security_list_options *tmp = arg;

    disable_write_protection();
    tmp->file_permission = orig_apparmor_file_permission ;
    enable_write_protection();
    return 0;
}

void hook_apparmor(void)
{
    struct security_hook_list *P;
    struct hlist_head *head = &(orig_security_hook_heads->file_permission);
        hlist_for_each_entry(P, head, list) {
        if(strcmp(P->lsm, "apparmor") == 0){
            orig_apparmor_file_permission = P->hook.file_permission;
            stop_machine(hook_apparmor_func, (void*)&(P->hook), 0);
        }
   }
}

void recover_apparmor(void)
{
    struct security_hook_list *P;
    struct hlist_head *head = &(orig_security_hook_heads->file_permission);
        hlist_for_each_entry(P, head, list) {
        if(strcmp(P->lsm, "apparmor") == 0){
            stop_machine(recover_apparmor_func, (void*)&(P->hook), 0);
        }
   }
}

Original: https://www.cnblogs.com/RayWHL/p/16263555.html
Author: RayWHL
Title: 修改内核中的只读区内容

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/607415/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 带你了解我们的“彩虹运维技术栈社区”

    关于我们 彩虹象征着美好、童话、幻想,所要追求的成功,必历经挫折之后才能达到目标。我们每个人都需经历学习的枯燥、工作和生活的辛苦,不经历风雨又怎能见彩虹? TTR,全称Taste …

    Linux 2023年6月7日
    090
  • supervisord 进程管理利器

    Supervisor概述 ​ supervisor是一个 Client/Server模式的系统,允许用户在类unix操作系统上监视和控制多个进程,或者可以说是多个程序。superv…

    Linux 2023年5月27日
    0106
  • redis如何设置密码

    密码设置这里简单介绍一下redis如何设置密码redis密码设置有两种方式,一种需要重启redis服务,一种不需要重启redis服务。 首先,介绍一下需要重启redis服务的设置方…

    Linux 2023年5月28日
    0101
  • Linux安装mysql8.0

    MySQL 配置MySQL8安装源 sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3….

    Linux 2023年6月6日
    0103
  • Redis 分布式锁的实现

    0X00 测试环境 CentOS 6.6 + Redis 3.2.10 + PHP 7.0.7(+ phpredis 4.1.0) [root@localhost ~]# cat …

    Linux 2023年5月28日
    0112
  • ceph存储集群搭建以及介绍

    转载至https://blog.csdn.net/weixin_44989941/article/details/123370410 Original: https://www.c…

    Linux 2023年6月14日
    095
  • VMware Workstation 16.2 Pro for Linux SLIC 2.6 & Unlocker

    macOS Unlocker,支持 macOS Monterey 请访问原文链接:VMware Workstation 16 Pro Unlocker & OEM BIOS…

    Linux 2023年5月27日
    096
  • 节约内存:Instagram的Redis实践(转)

    1.M emcached 内存Key-Value Cache Redis 内存数据库 四,节约内存:Instagram的Redis实践 Instagram可以说是网拍App的始祖级…

    Linux 2023年5月28日
    0103
  • grep

    grep 基本匹配: grep a*re hello.txt –* …

    Linux 2023年6月11日
    088
  • 从 Hadoop 到云原生, 大数据平台如何做存算分离

    Hadoop 的诞生改变了企业对数据的存储、处理和分析的过程,加速了大数据的发展,受到广泛的应用,给整个行业带来了变革意义的改变;随着云计算时代的到来, 存算分离的架构受到青睐,企…

    Linux 2023年6月14日
    062
  • 创建右键nushell打开UTF-8(似乎需要win10 1803版本以上,admin的不需要)

    在环境变量中添加名为LESSCHARSET,值为utf-8的环境变量(解决git log中文乱码) 在桌面新建nushell.reg文件,里面写如下代码: Windows Regi…

    Linux 2023年5月28日
    099
  • 【原创】Linux虚拟化KVM-Qemu分析(七)之timer虚拟化

    背景 Read the fucking source code! –By 鲁迅 A picture is worth a thousand words. –…

    Linux 2023年6月8日
    0112
  • supervisord 进程管理利器

    Supervisor概述 ​ supervisor是一个 Client/Server模式的系统,允许用户在类unix操作系统上监视和控制多个进程,或者可以说是多个程序。superv…

    Linux 2023年6月14日
    0115
  • Nginx服务的搭建与配置

    Nginx服务的搭建与配置 一、关闭防火墙并安装epel源 1、关闭selinux ①修改selinux的配置文件 [root@localhost ~]# vim /etc/sel…

    Linux 2023年6月7日
    0122
  • JuiceFS 缓存预热详解

    缓存预热是一个比较常见的概念,相信很多小伙伴都有所了解。对于 JuiceFS 来说,缓存预热就是将需要操作的数据预先从对象存储拉取到本地,从而获得与使用本地存储类似的性能表现。 缓…

    Linux 2023年6月14日
    089
  • 根据温度、气压计算海拔高度

    基本概念 标准大气压:表示气压的单位,习惯上常用水银柱高度。例如,一个标准大气压等于760毫米高的水银柱的重量,它相当于一平方厘米面积上承受1.0336公斤重的大气压力。由于各国所…

    Linux 2023年6月8日
    0103
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球