-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Detect buffer overflow in fcntl.fcntl() and fcntl.ioctl() #132915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
SystemError is raised when buffer overflow is detected. The stack and memory can already be corrupted, so treat this error as fatal.
This is one of rare cases when a Python code can trigger a SystemError. But these functions are as unsafe as ctypes, so I think that it is appropriate use of SystemError. |
Do you have examples? Before you, nobody requested this feature, so I'm a little surprised that this feature is really needed. |
The third argument of We cannot do anything with this. Unlike See also #95380. It is more dangerous when the size exceeds 1024 and we read/write directly from/to the bytes-like object, but I hope that the user will be more accurate there. |
…-132919) SystemError is raised when buffer overflow is detected. The stack and memory can already be corrupted, so treat this error as fatal.
Fix the warning: Modules/fcntlmodule.c:27:36: warning: initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute (9 chars into 8 available) [-Wunterminated-string-initialization] static const char guard[GUARDSZ] = "\x00\xfa\x69\xc4\x67\xa3\x6c\x58";
fcntl()
andioctl()
take an argument which can be a pointer to a buffer of unspecified length, depending on operation. They can also write in that buffer, depending on operation. A temporary buffer of size 1024 is used, so a chance of directly overflowing the bytes-like object provided by user is small, but if its size than necessary, the user will get truncated data in best case, and in worst case it will cause the C stack corruption.We cannot prevent this, unless we limit the set of supported operations to a small set of allowed operations. This is not practical, because
fcntl()
andioctl()
exist to support operations not explicitly supported by Python. But we can detect a buffer overflow, and raise an exception. It may be too late, if the stack or memory are corrupted, but it is better than silently ignore error.Linked PRs
The text was updated successfully, but these errors were encountered: