1. 程式人生 > >bug解決-核心C庫防寫(FORTIFY: write: prevented read past end of buffer)

bug解決-核心C庫防寫(FORTIFY: write: prevented read past end of buffer)

備註:展訊平臺

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。平時開發中注意
就行,當然也要糾正一下程式設計風格。