• Will Deacon's avatar
    chardev: Avoid potential use-after-free in 'chrdev_open()' · ec576895
    Will Deacon authored
    commit 68faa679 upstream.
    
    'chrdev_open()' calls 'cdev_get()' to obtain a reference to the
    'struct cdev *' stashed in the 'i_cdev' field of the target inode
    structure. If the pointer is NULL, then it is initialised lazily by
    looking up the kobject in the 'cdev_map' and so the whole procedure is
    protected by the 'cdev_lock' spinlock to serialise initialisation of
    the shared pointer.
    
    Unfortunately, it is possible for the initialising thread to fail *after*
    installing the new pointer, for example if the subsequent '->open()' call
    on the file fails. In this case, 'cdev_put()' is called, the reference
    count on the kobject is dropped and, if nobody else has taken a reference,
    the release function is called which finally clears 'inode->i_cdev' from
    'cdev_purge()' before potentially freeing the object. The problem here
    is that a racing thread can happily take the 'cdev_lock' and see the
    non-NULL pointer in the inode, which...
    ec576895
char_dev.c 16.7 KB