This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ioctl mutable buffer copying problem
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: bbrazil, gk, pitrou
Priority: normal Keywords: patch

Created on 2010-09-03 16:54 by gk, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
ioctl_1024_mutable.patch bbrazil, 2010-09-04 15:42
Messages (5)
msg115467 - (view) Author: Grzegorz Kulewski (gk) Date: 2010-09-03 16:54
Hello,

It looks like something is broken in ioctl in 3.2 when the supplied (mutable) bytearray is exactly 1024 bytes long - the result is not copied into the buffer after the ioctl succedes:

def open_tuntap(type, name):
        TUN_TYPE = {
                'TUN' : 0x0001,
                'TAP' : 0x0002
        }

        TUNSETIFF = 0x400454ca

        dev = os.open('/dev/net/tun', os.O_RDWR)
        buf = bytearray(SET_LEN_HERE)
        name = name[:16]
        buf[:len(name)] = name
        buf[16:18] = TUN_TYPE[type].to_bytes(2, sys.byteorder)
        fcntl.ioctl(dev, TUNSETIFF, buf, True)
        print(buf)
        print(len(buf))

open_tuntap('TAP', b'madtun%d')

Now try it with SET_LEN_HERE = 1024, 1023, 1025 and any other values. For < 1024 it copies to the static buffer and back, for > 1024 it operates on the buffer itself (hopefully) and for 1024 it ignores the modified buffer completely. It's probably some corner case bug.

The example was tested under Linux 2.6.35.

Python 3.2a1+ (py3k:84054, Sep  3 2010, 01:45:35) 
[GCC 4.4.2] on linux2
msg115579 - (view) Author: Brian Brazil (bbrazil) * Date: 2010-09-04 15:42
The attached patch fixes this.
msg115580 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-09-04 15:47
Do you think there is a simple, generic way to test for this (see Lib/test/test_ioctl.py)?
msg115584 - (view) Author: Brian Brazil (bbrazil) * Date: 2010-09-04 16:10
I'd already had a look around, and my knowledge of ioctls is not sufficient to answer that question but I suspect the answer is no. 

Does someone know of a ioctl that works across platforms, doesn't require specific hardware or privileges and that does some form of read operation?
msg115788 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-09-07 16:36
Actually, writing a test ended up quite easy (by re-using an existing test in test_ioctl and simply enlarging its buffer). I've committed an updated patch in r84589 (3.x), r84590 (3.1) and r84591 (2.7).
Thank you!
History
Date User Action Args
2022-04-11 14:57:06adminsetgithub: 53967
2010-09-07 16:36:27pitrousetstatus: open -> closed
versions: + Python 3.1, Python 2.7
messages: + msg115788

resolution: fixed
stage: resolved
2010-09-04 16:10:01bbrazilsetmessages: + msg115584
2010-09-04 15:47:15pitrousetnosy: + pitrou
messages: + msg115580
2010-09-04 15:42:21bbrazilsetfiles: + ioctl_1024_mutable.patch

nosy: + bbrazil
messages: + msg115579

keywords: + patch
2010-09-03 16:54:11gkcreate