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=ham 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 517D8C3A5A6 for ; Sun, 22 Sep 2019 18:53:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 239CE2190F for ; Sun, 22 Sep 2019 18:53:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1569178397; bh=DdnHqTTMKKfCpDwlPlBDOYddiSHmslJNpMh4FwJwhwI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=PByTIjzuUl+J4oYBZhv5b+mmX0OezDVTyfW+7DmCrgUAJKPDwEswrC90QVLvq58as g3LZXhhbVhGYzBqyjHs8e7LSUF/ktXY6B5883YWqQ/wmkXQjbG9bu7RlvrvMs+A4sf 7LWSippQLElRdSha+G3If1Di+CqemvTosx6jLdqA= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2393797AbfIVSxP (ORCPT ); Sun, 22 Sep 2019 14:53:15 -0400 Received: from mail.kernel.org ([198.145.29.99]:52106 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2393082AbfIVSwu (ORCPT ); Sun, 22 Sep 2019 14:52: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 6F80821479; Sun, 22 Sep 2019 18:52:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1569178369; bh=DdnHqTTMKKfCpDwlPlBDOYddiSHmslJNpMh4FwJwhwI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qVFAvgqcua45sSePESeoroPLAYwcPdJmW9IfHRbPMaGfT0t8+j8GRDcQNrK0kTrMN nPLhVkBO/bghJ/zuZnwCnYdSVsy4ASK7AReLkAGJw1xTJH6AlTOWynH7tKb7bVsIXX Ulf3xmlhI1M/2Wplo4Ltlo+V9tNhNWJaRcYvrym0= 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 5.2 120/185] media: ov9650: add a sanity check Date: Sun, 22 Sep 2019 14:48:18 -0400 Message-Id: <20190922184924.32534-120-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190922184924.32534-1-sashal@kernel.org> References: <20190922184924.32534-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 30ab2225fbd0c..b350f5c1a9890 100644 --- a/drivers/media/i2c/ov9650.c +++ b/drivers/media/i2c/ov9650.c @@ -703,6 +703,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