zig icon indicating copy to clipboard operation
zig copied to clipboard

std.os.linux: add ioctlFunc

Open marler8997 opened this issue 3 years ago • 0 comments

Adds ioctlFunc which returns a wrapper function whose parameter type is tied to the ioctl number/definition. This removes the need for using @ptrToInt for the ioctl argument. Instead, the argument will be properly declared as a pointer and is guaranteed to have the correct size and IO direction (aka mutability). These two guarantees prevent out-of-bound memory access caused from a mismatch in what the application passes to the ioctl argument.

Note that the guarantee of correct size/IO direction is provided by the kernel itself because these 2 properties are encoded into the ioctl number, so if they don't match then the kernel will reject the ioctl with ENOTTY rather than accessing memory out-of-bounds.

I'm not certain whether this belongs in std or not so feel free to close if not. I've created a gist that provides this as a module and has some background info on it: https://gist.github.com/marler8997/077a43569a2eda4dc1d4bdc3e44c2d46

TODO: we should double check that all ioctl numbers use this mechanism, I have a suspicion that there could be some legacy ioctl numbers that existed before this interface was created. In this case we should be able to add some hardcoded special casing for these.

marler8997 avatar Sep 07 '22 16:09 marler8997