linux/drivers/media
Daniel Mentz a1dfb4c48c media: v4l2-compat-ioctl32.c: refactor compat ioctl32 logic
The 32-bit compat v4l2 ioctl handling is implemented based on its 64-bit
equivalent. It converts 32-bit data structures into its 64-bit
equivalents and needs to provide the data to the 64-bit ioctl in user
space memory which is commonly allocated using
compat_alloc_user_space().

However, due to how that function is implemented, it can only be called
a single time for every syscall invocation.

Supposedly to avoid this limitation, the existing code uses a mix of
memory from the kernel stack and memory allocated through
compat_alloc_user_space().

Under normal circumstances, this would not work, because the 64-bit
ioctl expects all pointers to point to user space memory. As a
workaround, set_fs(KERNEL_DS) is called to temporarily disable this
extra safety check and allow kernel pointers. However, this might
introduce a security vulnerability: The result of the 32-bit to 64-bit
conversion is writeable by user space because the output buffer has been
allocated via compat_alloc_user_space(). A malicious user space process
could then manipulate pointers inside this output buffer, and due to the
previous set_fs(KERNEL_DS) call, functions like get_user() or put_user()
no longer prevent kernel memory access.

The new approach is to pre-calculate the total amount of user space
memory that is needed, allocate it using compat_alloc_user_space() and
then divide up the allocated memory to accommodate all data structures
that need to be converted.

An alternative approach would have been to retain the union type karg
that they allocated on the kernel stack in do_video_ioctl(), copy all
data from user space into karg and then back to user space. However, we
decided against this approach because it does not align with other
compat syscall implementations. Instead, we tried to replicate the
get_user/put_user pairs as found in other places in the kernel:

    if (get_user(clipcount, &up->clipcount) ||
        put_user(clipcount, &kp->clipcount)) return -EFAULT;

Notes from hans.verkuil@cisco.com:

This patch was taken from:
    97b733953c

Clearly nobody could be bothered to upstream this patch or at minimum
tell us :-( We only heard about this a week ago.

This patch was rebased and cleaned up. Compared to the original I
also swapped the order of the convert_in_user arguments so that they
matched copy_in_user. It was hard to review otherwise. I also replaced
the ALLOC_USER_SPACE/ALLOC_AND_GET by a normal function.

Fixes: 6b5a9492ca ("v4l: introduce string control support.")

Signed-off-by: Daniel Mentz <danielmentz@google.com>
Co-developed-by: Hans Verkuil <hans.verkuil@cisco.com>
Acked-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Cc: <stable@vger.kernel.org>      # for v4.15 and up
Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com>
2018-01-30 07:40:41 -05:00
..
cec media: cec: move cec autorepeat handling to rc-core 2017-12-14 10:35:30 -05:00
common media: drivers/media/common/videobuf2: rename from videobuf 2018-01-23 08:05:02 -05:00
dvb-core media: dvb_demux: improve debug messages 2018-01-29 07:49:24 -05:00
dvb-frontends media: ts2020: avoid integer overflows on 32 bit machines 2018-01-29 07:46:46 -05:00
firewire media: replace all <spaces><tab> occurrences 2018-01-04 13:15:05 -05:00
i2c media: i2c: ov7740: use gpio/consumer.h instead of gpio.h 2018-01-23 08:13:02 -05:00
mmc media: move dvb kAPI headers to include/media 2017-12-28 13:16:01 -05:00
pci media: intel-ipu3: cio2: fixup off-by-one bug in cio2_vb2_buf_init 2018-01-23 08:08:44 -05:00
platform media: vivid: fix module load error when enabling fb and no_error_inj=1 2018-01-30 07:18:22 -05:00
radio media: replace all <spaces><tab> occurrences 2018-01-04 13:15:05 -05:00
rc media: rc: do not remove first bit if leader pulse is present 2018-01-23 07:32:38 -05:00
spi
tuners media: replace all <spaces><tab> occurrences 2018-01-04 13:15:05 -05:00
usb media: cxusb, dib0700: ignore XC2028_I2C_FLUSH 2018-01-29 07:47:47 -05:00
v4l2-core media: v4l2-compat-ioctl32.c: refactor compat ioctl32 logic 2018-01-30 07:40:41 -05:00
Kconfig media: fix usage of whitespaces and on indentation 2018-01-04 13:12:01 -05:00
Makefile License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
media-device.c media: drop use of MEDIA_API_VERSION 2017-08-08 06:03:15 -04:00
media-devnode.c media: utilize new cdev_device_add helper function 2017-03-21 06:44:33 +01:00
media-entity.c [media] media: Check for active and has_no_links overrun 2017-10-04 17:16:14 -03:00