3

I've been playing with a technique to transparently do data conversion on memory mapped data. The idea is basically to memory map a desired file, and create a second anonymous region that read/write protected. When the user goes to access to the anonymous region, the resulting segfault is caught, the data is converted and permissions are changed to allow the access to proceed.

It works great, but there's one little hitch. Passing the pointer without first touching the data to a system call like write() won't trigger the segfault handler, instead it just returns EFAULT as the handler isn't called to fix up permissions. Is there a way to cause system calls to use user-space handlers when there's a problem?

gct
  • 14,100
  • 15
  • 68
  • 107
  • 3
    There was once a [suggestion](https://lkml.org/lkml/1998/8/23/31) on the linux-kernel mailing list to generate SIGSEGV for syscalls to fix the inconsistency. There was even a [demonstration patch](https://lkml.org/lkml/1998/8/26/57). But some people were against it because EFAULT is traditional and some programs might actually be handling it, and converting them to use a signal handler instead would be difficult. That was 14 years ago. Maybe it's time to try again. – Alan Curry Jul 27 '12 at 19:09
  • Perhaps a patch that allows a user process to enable that behavior, while keeping the default to return EFAULT? – gct Jul 27 '12 at 19:27
  • Yeah, if it was enabled with a `prctl` maybe they'd accept it. Or if you don't need your translator to work on arbitrary kernels you could just hack yours. That 14-year-old patch probably won't port easily to a new kernel, and it was i386-only anyway, but it was small so maybe it wouldn't be too hard to do it again. – Alan Curry Jul 27 '12 at 19:33

1 Answers1

3

No there is no way to have syscalls like write make a SIGSEGV when the buffer is wrong. Return EFAULT is part of the semantics of that write(2) syscall.

You might do LD_PRELOAD things if only concerned by libc.so ; you could also use ptrace to catch syscalls à la strace.

See also this answer to a very similar question of yours.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547