bug解决-内核C库写保护(FORTIFY: write: prevented read past end of buffer)

来源:互联网 发布:收款收据打印软件 编辑:程序博客网 时间:2024/06/04 18:03

备注:展讯平台

1、问题描述

  昨天同事问我一个问题,报的是一个native crash问题,问题log如下所示:

01-05 00:01:12.600 2794 6237 F libc : Fatal signal 6 (SIGABRT), code -6 in tid 6237 (Binder:2794_2)
01-05 00:01:12.601 189 189 W : debuggerd: handling request: pid=2794 uid=1047 gid=1005 tid=6237
01-05 00:01:12.663 6239 6239 F DEBUG : * *
01-05 00:01:12.664 6239 6239 F DEBUG : Native Crash TIME: 2674549
01-05 00:01:12.664 6239 6239 F DEBUG : * *
01-05 00:01:12.664 6239 6239 F DEBUG : Build fingerprint: ‘Condor/SP530/SP530:7.0/NRD90M/SP530_V4.0_20170607_CTS:user/release-keys’
01-05 00:01:12.664 6239 6239 F DEBUG : Revision: ‘0’
01-05 00:01:12.664 6239 6239 F DEBUG : ABI: ‘arm’
01-05 00:01:12.665 6239 6239 F DEBUG : pid: 2794, tid: 6237, name: Binder:2794_2 >>> /system/bin/cameraserver <<<
01-05 00:01:12.665 6239 6239 F DEBUG : signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr ——–
01-05 00:01:12.668 6239 6239 F DEBUG : Abort message: ‘FORTIFY: write: prevented read past end of buffer’
01-05 00:01:12.669 6239 6239 F DEBUG : r0 00000000 r1 0000185d r2 00000006 r3 00000008
01-05 00:01:12.669 6239 6239 F DEBUG : r4 ab310978 r5 00000006 r6 ab310920 r7 0000010c
01-05 00:01:12.669 6239 6239 F DEBUG : r8 00000000 r9 00000000 sl 00000001 fp 00000001
01-05 00:01:12.669 6239 6239 F DEBUG : ip 00000002 sp ab310750 lr ae7c2597 pc ae7c4df4 cpsr 20070010
01-05 00:01:12.682 6239 6239 F DEBUG :
01-05 00:01:12.682 6239 6239 F DEBUG : backtrace:
01-05 00:01:12.683 6239 6239 F DEBUG : #00 pc 00049df4 /system/lib/libc.so (tgkill+12)
01-05 00:01:12.683 6239 6239 F DEBUG : #01 pc 00047593 /system/lib/libc.so (pthread_kill+34)
01-05 00:01:12.683 6239 6239 F DEBUG : #02 pc 0001d855 /system/lib/libc.so (raise+10)
01-05 00:01:12.683 6239 6239 F DEBUG : #03 pc 000193a1 /system/lib/libc.so (__libc_android_abort+34)
01-05 00:01:12.683 6239 6239 F DEBUG : #04 pc 00017014 /system/lib/libc.so (abort+4)
01-05 00:01:12.683 6239 6239 F DEBUG : #05 pc 0001b84f /system/lib/libc.so (__libc_fatal+22)
01-05 00:01:12.683 6239 6239 F DEBUG : #06 pc 0001b82f /system/lib/libc.so (__fortify_chk_fail+26)
01-05 00:01:12.683 6239 6239 F DEBUG : #07 pc 0004fd81 /system/lib/libc.so (__write_chk+36)
01-05 00:01:12.683 6239 6239 F DEBUG : #08 pc 00040195 /system/lib/libcamsensor.so

由上面的log可以发现明显的错误log是‘FORTIFY: write: prevented read past end of buffer’ ,可以发现是在调用C库write函数时出现了问题。C库一般不会出现问题,所以应该是传入的参数有问题。结合本地代码和symbol文件,定位是在下面代码中出现了问题。

    fd = open("/sys/bus/platform/drivers/HardwareInfo/HardwareInfo/main_camera", O_RDWR);    if (fd >= 0) {        write(fd,"", 20);        close(fd);    } else {        CMR_LOGE("Hardwareinfo open file error:%s \n",strerror(errno));    }

问题就发生在write(fd,"", 20); 这里由于写缓冲中只有一个字节,而传给kernel需要写入数据的长度是20个字节,那么
kernel在写入20个字节时在读缓冲时,发生异常(因为只有一个字节的缓冲)。

解决办法就是,要么把写缓冲搞大一点,要么把后面的写字节数改成1,当然具体问题具体分析。

2、总结

当然C库中不一定只有write函数会出现这样的异常,memcpy,memset等等都会出现这样的错误log。平时开发中注意
就行,当然也要纠正一下编程风格。

原创粉丝点击