From patchwork Thu Mar 18 13:43:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnd Bergmann X-Patchwork-Id: 404030 Delivered-To: patch@linaro.org Received: by 2002:a02:8562:0:0:0:0:0 with SMTP id g89csp492735jai; Thu, 18 Mar 2021 06:45:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJweBEQ/MfgicLgw74FhUCmNe0S9u1h6Tyc2h9lQwtoIJxNndhquQTTedjYEbtc52KI3OpFZ X-Received: by 2002:a05:6402:1cc1:: with SMTP id ds1mr3674534edb.135.1616075105213; Thu, 18 Mar 2021 06:45:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616075105; cv=none; d=google.com; s=arc-20160816; b=FakWDstIhzLXInDr8mfClETGIyzsEi7AA0feHsTQPKL/m9QwNYNGxqKD8Ov+yr6OMw F9HjKl1ovjnFpEvD0jjVl1rsCqCbGYRgx9uznyZjyBPHdl9t5PKud125taxUHiZnfQfT sx4uTR4zKpfXsYIlmX3h2OH+3x4CpC+ikyxxlO8dJTRWQRiiEIoUFPL3fYnxlANCp4Pn QVbLESITq6UN7m24NRtlD7U4GQEErYmqRXFBGeAk6hvQHPA2lXyYWNeoQzU+Ictypavp o2XbYDf4ejdS4EbL7tTF1FxBUJuj9nypv5MMOMwvXuHIUwF1tvdtuun/hTcjXU289Qb9 IGjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=2S0YxWdclkpya/0jIwRq1V6gulXACr//YFIjwWitPS4=; b=WuVfqtnMVuAU6hJRGoEm74UjmGzwIi0PNLefK/FnYvLbdr3s0WGuiH/Ajwvng3sxOi i6fvj3LP7zCKscXpP2H619ykPsuSd4ooW3Ks5OklLnUaEbe9kzrlv1zILzD0T6Q5cW5W xRDGvKTEjozxG0eX60H2NpQJq8Y8k0INZ+Ni+GHO4LOSrt8uegcoVdykAIfHLELZkgjH VhSLd0bcg0FMyVXClS78HQPJe7QduAxk/YwwOlkJFJ4bxzHGF0v3hIRH7y5UOfA+A8xC AUP4MaatWBnslYOxJF6pwBnq34eYHcMXMfCSbta+EZ2VXDErMZtz7fNe6MKwQoKCCdvi RVMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=tvzLhSlO; spf=pass (google.com: domain of linux-media-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-media-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cw22si1542409edb.429.2021.03.18.06.45.05; Thu, 18 Mar 2021 06:45:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-media-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=tvzLhSlO; spf=pass (google.com: domain of linux-media-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-media-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230182AbhCRNob (ORCPT + 3 others); Thu, 18 Mar 2021 09:44:31 -0400 Received: from mail.kernel.org ([198.145.29.99]:34904 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230010AbhCRNn7 (ORCPT ); Thu, 18 Mar 2021 09:43:59 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8902264E4D; Thu, 18 Mar 2021 13:43:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1616075039; bh=M/awrcCSuKIqOO2dmf7RKrAzqBZawv3ByeWFBOHNV9Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tvzLhSlOEy7dCNqLkoFPSJ7DAlxsG5RAo0ns+QYpq+UrW9JrAUln88DcFs/NC0OJs T+GBiXt5nHG8vVrImz57sLy1Krri1JXQIwe+PeXmVHvx3XaM85RFib2ALg12XqaaFB fuy9UpJ2ezFNdlKGINaY3k8T2r/k07cicOzR/N3jzogfGsrrnkNeAVCKGZin7M19MH OiE/XtNe3ry+YfI1fjH37CnNaOD5Z4MLNGTwp3GAKm8d7xwpjUQiZ4sXCtLDYS1pHx 3c6BtbyNjxYQBUEr38ghw3WHyIrRrBW3zT3g4bV/xfNTBCHbeZj1QiaCbBBo5m4roi eUPIBahJZqtMQ== From: Arnd Bergmann To: Mauro Carvalho Chehab Cc: Dmitry Vyukov , Arnd Bergmann , syzbot+142888ffec98ab194028@syzkaller.appspotmail.com, Hans Verkuil , Laurent Pinchart , Sakari Ailus , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] media: v4l2-core: explicitly clear ioctl input data Date: Thu, 18 Mar 2021 14:43:19 +0100 Message-Id: <20210318134334.2933141-2-arnd@kernel.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210318134334.2933141-1-arnd@kernel.org> References: <20210318134334.2933141-1-arnd@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org From: Arnd Bergmann As seen from a recent syzbot bug report, mistakes in the compat ioctl implementation can lead to uninitialized kernel stack data getting used as input for driver ioctl handlers. The reported bug is now fixed, but it's possible that other related bugs are still present or get added in the future. As the drivers need to check user input already, the possible impact is fairly low, but it might still cause an information leak. To be on the safe side, always clear the entire ioctl buffer before calling the conversion handler functions that are meant to initialize them. Signed-off-by: Arnd Bergmann --- drivers/media/v4l2-core/v4l2-ioctl.c | 51 ++++++++++++++++------------ 1 file changed, 29 insertions(+), 22 deletions(-) -- 2.29.2 Reviewed-by: Laurent Pinchart diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c index 2b1bb68dc27f..6cec92d0972c 100644 --- a/drivers/media/v4l2-core/v4l2-ioctl.c +++ b/drivers/media/v4l2-core/v4l2-ioctl.c @@ -3164,12 +3164,23 @@ static int video_get_user(void __user *arg, void *parg, if (cmd == real_cmd) { if (copy_from_user(parg, (void __user *)arg, n)) - err = -EFAULT; - } else if (in_compat_syscall()) { - err = v4l2_compat_get_user(arg, parg, cmd); - } else { - switch (cmd) { + return -EFAULT; + + /* zero out anything we don't copy from userspace */ + if (n < _IOC_SIZE(real_cmd)) + memset((u8 *)parg + n, 0, _IOC_SIZE(real_cmd) - n); + + return 0; + } + + /* zero out whole buffer first to deal with missing emulation */ + memset(parg, 0, _IOC_SIZE(real_cmd)); + + if (in_compat_syscall()) + return v4l2_compat_get_user(arg, parg, cmd); + #if !defined(CONFIG_64BIT) && defined(CONFIG_COMPAT_32BIT_TIME) + switch (cmd) { case VIDIOC_QUERYBUF_TIME32: case VIDIOC_QBUF_TIME32: case VIDIOC_DQBUF_TIME32: @@ -3182,28 +3193,24 @@ static int video_get_user(void __user *arg, void *parg, *vb = (struct v4l2_buffer) { .index = vb32.index, - .type = vb32.type, - .bytesused = vb32.bytesused, - .flags = vb32.flags, - .field = vb32.field, - .timestamp.tv_sec = vb32.timestamp.tv_sec, - .timestamp.tv_usec = vb32.timestamp.tv_usec, - .timecode = vb32.timecode, - .sequence = vb32.sequence, - .memory = vb32.memory, - .m.userptr = vb32.m.userptr, - .length = vb32.length, - .request_fd = vb32.request_fd, + .type = vb32.type, + .bytesused = vb32.bytesused, + .flags = vb32.flags, + .field = vb32.field, + .timestamp.tv_sec = vb32.timestamp.tv_sec, + .timestamp.tv_usec = vb32.timestamp.tv_usec, + .timecode = vb32.timecode, + .sequence = vb32.sequence, + .memory = vb32.memory, + .m.userptr = vb32.m.userptr, + .length = vb32.length, + .request_fd = vb32.request_fd, }; break; } -#endif - } } +#endif - /* zero out anything we don't copy from userspace */ - if (!err && n < _IOC_SIZE(real_cmd)) - memset((u8 *)parg + n, 0, _IOC_SIZE(real_cmd) - n); return err; }