From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 78AFDC3A5A2 for ; Sun, 22 Sep 2019 19:10:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 467EF20882 for ; Sun, 22 Sep 2019 19:10:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1569179418; bh=7PwifRkhxZCltSJ3poUrNU33xnUhrZSpl8lRstMShQ4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=dRG0NMYGHYYPLGsxRydsdfL48CW3inqzorNTGEs9ASv8n/cZafAJIZBy8F9maPqBE C3BzQEq6JbZZwxpCONqLIIgtruoYgZj32OQkYG8tdf6yfti4k+w8uSKvKn88n5dB0J OWwPneguhYI6DLrpRfYoWw9oY0RaSQQG1wOPHQDE= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2394926AbfIVS6v (ORCPT ); Sun, 22 Sep 2019 14:58:51 -0400 Received: from mail.kernel.org ([198.145.29.99]:33642 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391264AbfIVS6u (ORCPT ); Sun, 22 Sep 2019 14:58:50 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A5D332190F; Sun, 22 Sep 2019 18:58:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1569178729; bh=7PwifRkhxZCltSJ3poUrNU33xnUhrZSpl8lRstMShQ4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HNg+4+ECh7/vyYuhdq3VRaJ6pv1sDsCxKRYkP/yTy0fTwXOXMMW/FgNv5Wy8vZf2v cSXaNz8KFpTy7A6AFYE/N8R/lYsAN+b3i4dGJLd7Hr5/XLzjsJf0TvaMDk01vNm60V pyhvH4sVpC1Z3okxbDgNSKA1/S9Kl+mmkyDqmXNQ= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Mauro Carvalho Chehab , Sylwester Nawrocki , Sasha Levin , linux-media@vger.kernel.org Subject: [PATCH AUTOSEL 4.14 59/89] media: ov9650: add a sanity check Date: Sun, 22 Sep 2019 14:56:47 -0400 Message-Id: <20190922185717.3412-59-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190922185717.3412-1-sashal@kernel.org> References: <20190922185717.3412-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org From: Mauro Carvalho Chehab [ Upstream commit 093347abc7a4e0490e3c962ecbde2dc272a8f708 ] As pointed by cppcheck: [drivers/media/i2c/ov9650.c:706]: (error) Shifting by a negative value is undefined behaviour [drivers/media/i2c/ov9650.c:707]: (error) Shifting by a negative value is undefined behaviour [drivers/media/i2c/ov9650.c:721]: (error) Shifting by a negative value is undefined behaviour Prevent mangling with gains with invalid values. As pointed by Sylvester, this should never happen in practice, as min value of V4L2_CID_GAIN control is 16 (gain is always >= 16 and m is always >= 0), but it is too hard for a static analyzer to get this, as the logic with validates control min/max is elsewhere inside V4L2 core. Reviewed-by: Sylwester Nawrocki Signed-off-by: Mauro Carvalho Chehab Signed-off-by: Sasha Levin --- drivers/media/i2c/ov9650.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/media/i2c/ov9650.c b/drivers/media/i2c/ov9650.c index 6ffb460e8589e..565903c3172d1 100644 --- a/drivers/media/i2c/ov9650.c +++ b/drivers/media/i2c/ov9650.c @@ -710,6 +710,11 @@ static int ov965x_set_gain(struct ov965x *ov965x, int auto_gain) for (m = 6; m >= 0; m--) if (gain >= (1 << m) * 16) break; + + /* Sanity check: don't adjust the gain with a negative value */ + if (m < 0) + return -EINVAL; + rgain = (gain - ((1 << m) * 16)) / (1 << m); rgain |= (((1 << m) - 1) << 4); -- 2.20.1