Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp776309ybl; Wed, 11 Dec 2019 07:21:20 -0800 (PST) X-Google-Smtp-Source: APXvYqy1NNa6aGnCI5Xw89XZlPxWkL+MXX0DdrUkae+hXxklukPoDlkj1a3kbAb7JIoHX/ZTni/x X-Received: by 2002:aca:3a06:: with SMTP id h6mr3199806oia.137.1576077680196; Wed, 11 Dec 2019 07:21:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576077680; cv=none; d=google.com; s=arc-20160816; b=BiV161SE8xKNq+bp/JwKshD7/cKzjAnRPU4ZVT+TtltuFh+5Qe1Caf58QDFgx1hbrv SCgUuRcOUIDCV3jtO/W+Ze5vMZD5taYYQv49A4l4X4tIl9vAlHwZJRuqNwyH1ZtR6xGZ SSkVRya3SNc7oSxnEkBIWYAQTC3h/0UwiCqQX95TycCBMVDHaowBXJ2n1AUHhG4tuxEk 2W26VnNrM9o4A+CvzwDqjFcp9VOP88SfAqJCIniccn+0JttOaew5K8PG6CZlQUusndes mGKVloqXoQUxrCYf8zJuZsA6lQVp4vavr1neM+OQ3ZF4pVHrpkTV2Hx109+9T6xihNJX feKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=inOQLCf+2GELV9LwxHB1CtWECTXzT3JuvCRtdFXvIoc=; b=fwIejCLvGUbIs8QLNcrKHGZXuYUHTUqAUlT0mi031MYdYnvX7toCQDvH2YLL/kq0aE WkyqfKJsOZBQU5tgQ4Vs3S5MbSgSHVsV/ocXZui/c1CYD6gBd9tx6iiwNONahYZkzaIa T/cf6O57BKaOSHw0PDMJWCs8DmGPSL5PsVrgCGGvqTdCaRyxqN03TNF808Qdv1QNSsit 6veeS+SUKJYL1/WFhA15WwmdjxaTdyNfqX/Xg5VPP/maN6LzEEFlooTYU6RQBkWTy96S E9bbbfquGRPqFwyX1VBvD6dQR7bP/E3zpKoemXvvF8wXknoTHQWNCS19nI2+v9QwanLO pNTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=kFBaG3Wp; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o128si1301323oih.66.2019.12.11.07.21.07; Wed, 11 Dec 2019 07:21:20 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=kFBaG3Wp; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732220AbfLKPTa (ORCPT + 99 others); Wed, 11 Dec 2019 10:19:30 -0500 Received: from mail.kernel.org ([198.145.29.99]:48204 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732218AbfLKPT2 (ORCPT ); Wed, 11 Dec 2019 10:19:28 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7009F2073D; Wed, 11 Dec 2019 15:19:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1576077566; bh=Jr7OP2ExagPThx4x6gxb2VIob/AQyrcBJ5/rYVQqNVo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kFBaG3WpQ/Z/wH7t+x+gQ5aZuYEXYRRZgrxmlKB8zQSktFGq0C3XzhBcQ6X2nYC1u IXZTycgvjtmf0DGvYon+s/PF/feZUv9VgO+NXkBB7RdNh9E+0JoTEAuFOxXnY2/WqG 2YurgXqrDm4akcRI3kH+XfY9Md0gcOP9bKqwoTj8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Philipp Zabel , Hans Verkuil , Mauro Carvalho Chehab , Sasha Levin Subject: [PATCH 4.19 087/243] media: coda: fix memory corruption in case more than 32 instances are opened Date: Wed, 11 Dec 2019 16:04:09 +0100 Message-Id: <20191211150344.985875952@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191211150339.185439726@linuxfoundation.org> References: <20191211150339.185439726@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Philipp Zabel [ Upstream commit 649cfc2bdfeeb98ff7d8fdff0af3f8fb9c8da50f ] The ffz() return value is undefined if the instance mask does not contain any zeros. If it returned 32, the following set_bit would corrupt the debugfs_root pointer. Switch to IDA for context index allocation. This also removes the artificial 32 instance limit for all except CodaDx6. Signed-off-by: Philipp Zabel Signed-off-by: Hans Verkuil Signed-off-by: Mauro Carvalho Chehab Signed-off-by: Sasha Levin --- drivers/media/platform/coda/coda-common.c | 26 +++++++++-------------- drivers/media/platform/coda/coda.h | 3 ++- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/drivers/media/platform/coda/coda-common.c b/drivers/media/platform/coda/coda-common.c index 4b0220f40b425..fccc771d23a51 100644 --- a/drivers/media/platform/coda/coda-common.c +++ b/drivers/media/platform/coda/coda-common.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -2101,17 +2102,6 @@ int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq, return coda_queue_init(priv, dst_vq); } -static int coda_next_free_instance(struct coda_dev *dev) -{ - int idx = ffz(dev->instance_mask); - - if ((idx < 0) || - (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES)) - return -EBUSY; - - return idx; -} - /* * File operations */ @@ -2120,7 +2110,8 @@ static int coda_open(struct file *file) { struct video_device *vdev = video_devdata(file); struct coda_dev *dev = video_get_drvdata(vdev); - struct coda_ctx *ctx = NULL; + struct coda_ctx *ctx; + unsigned int max = ~0; char *name; int ret; int idx; @@ -2129,12 +2120,13 @@ static int coda_open(struct file *file) if (!ctx) return -ENOMEM; - idx = coda_next_free_instance(dev); + if (dev->devtype->product == CODA_DX6) + max = CODADX6_MAX_INSTANCES - 1; + idx = ida_alloc_max(&dev->ida, max, GFP_KERNEL); if (idx < 0) { ret = idx; goto err_coda_max; } - set_bit(idx, &dev->instance_mask); name = kasprintf(GFP_KERNEL, "context%d", idx); if (!name) { @@ -2243,8 +2235,8 @@ err_clk_per: err_pm_get: v4l2_fh_del(&ctx->fh); v4l2_fh_exit(&ctx->fh); - clear_bit(ctx->idx, &dev->instance_mask); err_coda_name_init: + ida_free(&dev->ida, ctx->idx); err_coda_max: kfree(ctx); return ret; @@ -2286,7 +2278,7 @@ static int coda_release(struct file *file) pm_runtime_put_sync(&dev->plat_dev->dev); v4l2_fh_del(&ctx->fh); v4l2_fh_exit(&ctx->fh); - clear_bit(ctx->idx, &dev->instance_mask); + ida_free(&dev->ida, ctx->idx); if (ctx->ops->release) ctx->ops->release(ctx); debugfs_remove_recursive(ctx->debugfs_entry); @@ -2747,6 +2739,7 @@ static int coda_probe(struct platform_device *pdev) mutex_init(&dev->dev_mutex); mutex_init(&dev->coda_mutex); + ida_init(&dev->ida); dev->debugfs_root = debugfs_create_dir("coda", NULL); if (!dev->debugfs_root) @@ -2834,6 +2827,7 @@ static int coda_remove(struct platform_device *pdev) coda_free_aux_buf(dev, &dev->tempbuf); coda_free_aux_buf(dev, &dev->workbuf); debugfs_remove_recursive(dev->debugfs_root); + ida_destroy(&dev->ida); return 0; } diff --git a/drivers/media/platform/coda/coda.h b/drivers/media/platform/coda/coda.h index 2469ca1dc5985..8df02c32781ee 100644 --- a/drivers/media/platform/coda/coda.h +++ b/drivers/media/platform/coda/coda.h @@ -16,6 +16,7 @@ #define __CODA_H__ #include +#include #include #include #include @@ -95,7 +96,7 @@ struct coda_dev { struct workqueue_struct *workqueue; struct v4l2_m2m_dev *m2m_dev; struct list_head instances; - unsigned long instance_mask; + struct ida ida; struct dentry *debugfs_root; }; -- 2.20.1