An efficient unit test and fuzz tools for kernel/libc porting

41
An efficient unit test and fuzz tools for kernel/libc porting Bamvor Jian Zhang Huawei Oct, 6, 2016

Transcript of An efficient unit test and fuzz tools for kernel/libc porting

Page 1: An efficient unit test and fuzz tools for kernel/libc porting

Anefficientunittestandfuzztoolsforkernel/libcporting

BamvorJianZhang

Huawei

Oct,6,2016

Page 2: An efficient unit test and fuzz tools for kernel/libc porting

Selfintroduction●KerneldeveloperfromHuawei●Linarokernelworkinggroupassignee●Focusonmigrationof32-bitapplication●Interestedinmemorymanagement

Page 3: An efficient unit test and fuzz tools for kernel/libc porting

aarch64ILP32overview

Page 4: An efficient unit test and fuzz tools for kernel/libc porting

WhatisILP32?

Page 5: An efficient unit test and fuzz tools for kernel/libc porting

armarchitecture

*ThispictureisbelongtotheARMcompany

Page 6: An efficient unit test and fuzz tools for kernel/libc porting

Datamodel

Page 7: An efficient unit test and fuzz tools for kernel/libc porting

Migrate32-bitapplicationto64-bithardware

aarch32

kernel

aarch64_ILP32 aarch64_LP64

application

midware

libc

compat_syscall

application

midware

libc

syscall

application

midware

libc

Page 8: An efficient unit test and fuzz tools for kernel/libc porting

ILP32enablement

Page 9: An efficient unit test and fuzz tools for kernel/libc porting

WhyweneedunittestforILP32?

Page 10: An efficient unit test and fuzz tools for kernel/libc porting

Lotsofchoicestobemadeforanewapi

●Thedefinitionofbasictypeinuserspace(NOTthekernelpart!)

●Argumentpassing:one64-bitregisterortwo32-bitregisters

●Sanitizeregistercontents

Page 11: An efficient unit test and fuzz tools for kernel/libc porting

Thedefinitionofbasictypeinuserspace

#define__DEV_T_TYPE__UQUAD_TYPE#define__UID_T_TYPE__U32_TYPE#define__GID_T_TYPE__U32_TYPE#define__INO_T_TYPE__UQUAD_TYPE#define__INO64_T_TYPE__UQUAD_TYPE#define__MODE_T_TYPE__U32_TYPE#define__NLINK_T_TYPE__U32_TYPE#define__OFF_T_TYPE__SQUAD_TYPE#define__OFF64_T_TYPE__SQUAD_TYPE#define__PID_T_TYPE__S32_TYPE#define__RLIM_T_TYPE__UQUAD_TYPE#define__RLIM64_T_TYPE__UQUAD_TYPE#define__BLKCNT_T_TYPE__SQUAD_TYPE#define__BLKCNT64_T_TYPE__SQUAD_TYPE#define__FSBLKCNT_T_TYPE__UQUAD_TYPE#define__FSBLKCNT64_T_TYPE__UQUAD_TYPE#define__FSFILCNT_T_TYPE__UQUAD_TYPE#define__FSFILCNT64_T_TYPE__UQUAD_TYPE

Page 12: An efficient unit test and fuzz tools for kernel/libc porting

Thedefinitionofbasictypeinuserspace(Cont.)

#define__FSWORD_T_TYPE__SWORD_TYPE#define__ID_T_TYPE__U32_TYPE#define__CLOCK_T_TYPE__SLONGWORD_TYPE#define__TIME_T_TYPE__SLONGWORD_TYPE#define__USECONDS_T_TYPE__U32_TYPE#define__SUSECONDS_T_TYPE__SLONGWORD_TYPE#define__DADDR_T_TYPE__S32_TYPE#define__KEY_T_TYPE__S32_TYPE#define__CLOCKID_T_TYPE__S32_TYPE#define__TIMER_T_TYPEvoid*#define__BLKSIZE_T_TYPE__S32_TYPE#define__FSID_T_TYPEstruct{int__val[2];}/*ssize_tisalwayssingedlonginbothABIs.*/#define__SSIZE_T_TYPE__SLONGWORD_TYPE#define__SYSCALL_SLONG_TYPE__SLONGWORD_TYPE#define__SYSCALL_ULONG_TYPE__ULONGWORD_TYPE#define__CPU_MASK_TYPE__ULONGWORD_TYPE

Page 13: An efficient unit test and fuzz tools for kernel/libc porting

Fourbigchangesin3years

Page 14: An efficient unit test and fuzz tools for kernel/libc porting

VersionA

●Mostofsyscallsarecompatsyscalls●time_tandoff_tare32-bit

Page 15: An efficient unit test and fuzz tools for kernel/libc porting

VersionB

Similartox32(x86ILP32)

●Mostofsyscallsare64-bitsyscalls●time_tandoff_tare64-bit●Incompatiblewitharm32compat-ioctl

Page 16: An efficient unit test and fuzz tools for kernel/libc porting

VersionC

ComebacktoversionA

●Mostofsyscallsarecompatsyscalls●time_tandoff_tare32-bit●Pass64-bitvariablethroughone64-bitreg●Dothesign/zeroextensionwhenenteringkernel

Page 17: An efficient unit test and fuzz tools for kernel/libc porting

VersionD

●Morecompatsyscallscomparewithaarch32

●Pass64-bitvariablethroughtwo32-bitregs

●Clearthetop-halvesofofallthe64-bitregsofasyscallwhenenteringkernel

●time_tis32-bitandoff_tis64-bit

Page 18: An efficient unit test and fuzz tools for kernel/libc porting

HowmanyissuesfoundbytrinitywhenLTPsyscallfailsare<20?

Page 19: An efficient unit test and fuzz tools for kernel/libc porting

0

Page 20: An efficient unit test and fuzz tools for kernel/libc porting

Compareexistingkernel/glibctesttools

Page 21: An efficient unit test and fuzz tools for kernel/libc porting

●Whethereasytoreproduceafailure●Whethersupportcoverage●Whethersupportlibctest●Whethergeneratefullrandomdatatobasicdatatype

Page 22: An efficient unit test and fuzz tools for kernel/libc porting

LTPandglibctestsuite●TheClassictestsuiteforkernelandglibc●Cons●Nofuzztest.Testmaypasswhilesomeissuesarehidden

Page 23: An efficient unit test and fuzz tools for kernel/libc porting

Trinity●Pros●Generatefuzzdatainasetofdatatype●Supportlotsofarchitecture

●Cons●Generaterandomaddressinsteadofbasicdatatypeformostofpointers

●TakestoolongtoproduceanissueandTakesmuchlongertore-produceandanalyzeit

●Donotsupportcoverage(?)

Page 25: An efficient unit test and fuzz tools for kernel/libc porting

Syzkaller(Cont.)●Pros●Canrecursivelyrandomizebasedatatype

●Cangeneratereadableshorttestcases●Candothecoverage

●Cons●DoesnottestClibrary

Page 26: An efficient unit test and fuzz tools for kernel/libc porting

AFLandTriforce●Pros:●BaseontheTriforceAFL●Donotneedthecoveragesupportinkernel

●Cons●Needspecialinstructioninqemu

Page 27: An efficient unit test and fuzz tools for kernel/libc porting

What'smissing?●Notestsuitecareabouttheportingoflibcandkernel

●Nofullunittestforsyscall

Page 28: An efficient unit test and fuzz tools for kernel/libc porting

Introducesyscallunittest

Page 29: An efficient unit test and fuzz tools for kernel/libc porting

Thetestflowofsyscallunittest

kernel

userspace

Dumpfunctionprototypefromkernel

Generatejprobehook

runtestcasewithmodified

trinityDumpfunctionprototype

fromuserspace

Generateparameterfuzzer

analysisresult

Page 30: An efficient unit test and fuzz tools for kernel/libc porting

Dumptheprototypeoffunctionandstruct

●Scriptbaseonabi-dumper●Generatethefuzzerfromjson.

Page 31: An efficient unit test and fuzz tools for kernel/libc porting

Thefuzzerforstructsinuserspace

structitimerspec*get_itimerspec(){structitimerspec*p=malloc(sizeof(structitimerspec));

p->it_interval.tv_sec=(unsignedlong)rand64();p->it_interval.tv_nsec=(unsignedlong)rand64();p->it_value.tv_sec=(unsignedlong)rand64();p->it_value.tv_nsec=(unsignedlong)rand64();

//printallthevalueofthisstructreturnp;}

Page 32: An efficient unit test and fuzz tools for kernel/libc porting

TheJprobehookinkernelmodule

longJC_SyS_getitimer(intwhich,structcompat_itimerval*it){printk("parametervalue:it<%u>,which<%u>",it,which);printk("it->it_interval.tv_sec<%u>,it->it_interval.tv_usec<%u>,it->it_value.tv_sec<%u>,it->it_value.tv_usec<%u>"it->it_interval.tv_sec,it->it_interval.tv_usec,it->it_value.tv_sec,it->it_value.tv_usec);jprobe_return();/*Alwaysendwithacalltojprobe_return().*/return0;}

staticstructjprobemy_jprobe={.entry=JC_SyS_getitimer,.kp={.symbol_name="compat_sys_getitimer",},};

Page 33: An efficient unit test and fuzz tools for kernel/libc porting

staticint__initjprobe_init(void){intret;

ret=register_jprobe(&my_jprobe);if(ret<0){printk(KERN_INFO"register_jprobefailed,returned%d\n",ret);return-1;}

return0;}

staticvoid__exitjprobe_exit(void){unregister_jprobe(&my_jprobe);printk(KERN_INFO"jprobeat%punregistered\n",my_jprobe.kp.addr);}

Page 34: An efficient unit test and fuzz tools for kernel/libc porting

Modifytrinity

●CallsyscallthroughClibrary●Addthemissingstructinsyscall●Addjprobehooksforcapturingtheargumentsofsyscall

●AddorChangesomeoutputmessageforscript

Page 35: An efficient unit test and fuzz tools for kernel/libc porting

Runit!

trinity/scripts/do_test_struct.sh

Page 36: An efficient unit test and fuzz tools for kernel/libc porting

Foundtwoissuesinaspecificversion●readahead●sync_file_range

Page 37: An efficient unit test and fuzz tools for kernel/libc porting

Thereturnvaluetestofsyscall●Randomreturnvaluethroughkretprobe

Page 38: An efficient unit test and fuzz tools for kernel/libc porting

TODOlist●Supportallthesyscallswhicharenotwrappedbylibc

●Fullautomationingeneratingthefuzzcode

Page 39: An efficient unit test and fuzz tools for kernel/libc porting

Whatisthefutureofsyscallunittest?ContributetoLTPand/orglibctestsuite?

Orkeepitasastandalonetestsuite?

Page 40: An efficient unit test and fuzz tools for kernel/libc porting

Codepublishedingithubhttps://github.com/bjzhang/trinity/tree/syscall_unittest

https://github.com/bjzhang/abi-dumper/tree/json_output

Page 41: An efficient unit test and fuzz tools for kernel/libc porting

Thanks