by zX41ZdbW · 6y ago · 0 comments
Consider the following (from memory, untested):
word_t peek(word_t addr, int success) { word_t ret; errno = 0; access(addr, F_OK); if (success) success = (errno != EFAULT); if (errno != EFAULT) ret = *addr; return ret; }
I first learnt about this trick in http://www.gcu-squad.org/2005/08/non-ce-n-est-pas-sale%21/
In English: "syscalls cannot segfault, so all that accept an address (as parameter) have to check if it is valid and return EFAULT in case of error."
(Since C code actually calls libc not system calls directly, this rationale fails on calls that do something on pointers before calling the actual kernel system call.)
Also, given the time cost of making a system call, one has to has an approach finer than "pay a system call for each byte to write" if there are many.
Anyway, TL;DR : it's not a dirty hack, it has some general validity!