Linux C/C++内存越界定位: 利用mprotect使程序在crash在第一现场

Linux C/C++内存越界定位: 利用mprotect使程序在crash在第一现场

https://blog.csdn.net/thisinnocence/article/details/80025064

 

通过反汇编定位段错误
https://blog.csdn.net/ringrang/article/details/60596846


嵌入式设备使用GDB及coredump文件查找崩溃问题
https://blog.csdn.net/anychenp/article/details/79636556


利用反汇编帮助查找段错误
https://blog.csdn.net/langzijing/article/details/78541166


反汇编定位代码崩溃位置_4
https://blog.csdn.net/lhl1158612009/article/details/77897568

 

对于大型Linux C/C++程序,内存越界和野指针类问题往往比较难定位。有的由于内存被非法改写造成了业务功能问题,有的则直接导致了程序crash,而且还经常不是第一现场。针对这种问题,可以采取的解决方法有:

利用valgrind工具来排查,会影响程序性能;
使用Address Sanitizer工具排查;
如果是固定的内存被破坏,可以利用gdb watch来抓取第一现场的调用栈;
可以利用Git二分回退代码库的commit点,缩减代码范围进行code review;
利用mprotect来进行保护对应内存,被非法改写时crash掉程序,分析coredump;
这里用一个小例子介绍下mprotect用法。根据mprotect的官方文档说明,使用mprotect这里最重要的一点是被保护的内存是按页对齐的,范围也是按页来的。这是因为Linux管理进程地址空间是一VMA(Virtual Memory Area)为单位来管理进程虚拟地址空间的,而VMA必须是page size大小的整数倍,可以看这篇文章 How The Kernel Manages Your Memory.
对于按页对齐申请内存,可以看这篇适配malloc申请按页对齐的内存。
也可以使用 posix_memalign来申请,如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>

int *result = 0;

void add(int a, int b)
{
    *result = a + b;
}

void subtract(int a, int b)
{
    *result = a - b;
}

int main()
{
    int ret;
    int pagesize;

    // 获取操作系统一个页的大小, 一般是 4KB == 4096
    pagesize = sysconf(_SC_PAGE_SIZE);
    printf("pagesize is: %d Byte\n", pagesize);
    if (pagesize == -1) {
        perror("sysconf");
        return -1;
    }

    // 按页对齐来申请一页内存, result会是一个可以被页(0x1000 == 4096)整除的地址
    ret = posix_memalign((void**)&result, pagesize, pagesize);
    printf("posix_memalign mem %p\n", result);
    if (ret != 0) {
        // posix_memalign 返回失败不会设置系统的errno, 不能用perror输出错误
        printf("posix_memalign fail, ret %u\n", ret);
        return -1;
    }

    add(1, 1); // 结果写入 *result
    printf("the result is %d\n", *result);

    // 保护result指向的内存, 权限设为只读
    ret = mprotect(result, pagesize, PROT_READ);
    if (ret == -1) {
        perror("mprotect");
        return -1;
    }

    subtract(1, 1); // 结果写入 *result, 但是 *result 只读, 引发segment fault
    printf("the result is %d\n", *result);

    free(result);
    return 0;
}


运行定位如下, 执行ulimit -c unlimited打开生成coredump,执行过程如下

root@ubuntu:/media/psf/Home/iLearn/learn_mprotect# ulimit -c unlimited
root@ubuntu:/media/psf/Home/iLearn/learn_mprotect# gcc -g main.c
root@ubuntu:/media/psf/Home/iLearn/learn_mprotect# ./a.out
pagesize is: 4096 Byte
posix_memalign mem 0x1b2f000
the result is 2
Segmentation fault (core dumped)
root@ubuntu:/media/psf/Home/iLearn/learn_mprotect# ls
a.out  core  main.c
root@ubuntu:/media/psf/Home/iLearn/learn_mprotect# gdb a.out core
GNU gdb (Ubuntu 7.7-0ubuntu3.1) 7.7
Reading symbols from a.out...done.
[New LWP 20389]
Core was generated by `./a.out'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000004006e5 in subtract (a=1, b=1) at main.c:15
15        *result = a - b;
(gdb) bt
#0  0x00000000004006e5 in subtract (a=1, b=1) at main.c:15
#1  0x00000000004007f2 in main () at main.c:50

这样由于野指针或越界导致的内存被非法改写就可以crash到第一现场了,通过coredump就可以很容易找到问题点 ?
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值