mlibc icon indicating copy to clipboard operation
mlibc copied to clipboard

sysdeps/managarm: ioctl will crash rather than EFAULT if given a bad buffer

Open ArsenArsen opened this issue 3 years ago • 10 comments

Test code: struct.unpack('HHHH', fcntl.ioctl(0, termios.TIOCGWINSZ, 0)), working: struct.unpack('HHHH', fcntl.ioctl(0, termios.TIOCGWINSZ, b"aaaaaaaa"))

ioctl in userspace won't check the buffer address, and hence will not return EFAULT and will crash instead.

ArsenArsen avatar Feb 10 '22 22:02 ArsenArsen

What's the issue here? Are there programs that rely on EFAULT to detect if a memory range is readable/writable?

avdgrinten avatar Feb 11 '22 05:02 avdgrinten

I haven't ran into that specifically, but simply the behavior is inaccurate, libc syscall wrappers should never cause a program to crash like this

ArsenArsen avatar Feb 11 '22 07:02 ArsenArsen

I think in general we should factor out the errno translation code, and just provide a general function that does the translation instead of open coding it in every sysdep.

Geertiebear avatar Feb 11 '22 10:02 Geertiebear

I agree but that would also not fix this, this happens because ioctl is populated in mlibc rather than posix-server

ArsenArsen avatar Feb 11 '22 11:02 ArsenArsen

How should ioctl in userspace check if the buffer is valid?

Geertiebear avatar Feb 11 '22 11:02 Geertiebear

If I knew that I'd have submitted a PR :P

ArsenArsen avatar Feb 11 '22 11:02 ArsenArsen

I'm not sure if we should make an effort to return EFAULT instead of raising a segfault. Relying on EFAULT sounds very strange.

avdgrinten avatar Feb 11 '22 11:02 avdgrinten

It's more of a human reliance than a programmatic one, e.g. I discovered this while poking around with ioctls to test SIGWINCH-related changes. I can't think of a very clean way to make a memory access not fail with a synchronous signal, though.

ArsenArsen avatar Feb 11 '22 12:02 ArsenArsen

Does glibc or musl do this?

Geertiebear avatar Feb 11 '22 12:02 Geertiebear

the kernel does for both (since linux will just produce an EFAULT, I'm also pretty sure this doesn't happen with the linux sysdeps for that reason, hence specifically mentioning sysdeps/managarm in the subject)

ArsenArsen avatar Feb 11 '22 12:02 ArsenArsen