From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 77DCACA40; Tue, 16 Jul 2024 15:34:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721144077; cv=none; b=W2IvUpYtt63mauPy8f+0NqBEp7DdspbWrwLf/E1ODlZjc5/50sYbSaVHXbAicqXp7ssBjvRfNejTgvoYJH/ud0F6ccosM4Cg5zt5H34acYTtHNn0sYXbKB8d9vWhAajzfirl4+enbLG6vTRQRvSgL56raEhURDPT6O9c8uvu3QQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721144077; c=relaxed/simple; bh=Nlk7dzb+VAUlDzB7PLoqsomt7kFhs0gsE47O1p5vlA0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UwMkocnoCCURGHEaKizk5Xckd1TbTHTpUjQ1o85+xGLNF7Brvx2f7u2m/leSO8w4ShWwFTHbedaQD93gOgrvTFILB8KIbiTpm5lh9L5Sv43kOolUN+iT2GOBSTLeaAuxULFimWpZz02doezAuZI6KKxWxKyyWIu0ovngdMgj3uQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=HXIrhk79; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="HXIrhk79" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 98726C4AF0B; Tue, 16 Jul 2024 15:34:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1721144077; bh=Nlk7dzb+VAUlDzB7PLoqsomt7kFhs0gsE47O1p5vlA0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HXIrhk79xpL5REW225Vl1G6y3e6h0cvqgBFbfpsKZvIe4PLfKZWy6mJnAQBo9WrPf /3cAq7jN8ZZhzpF8bM5wMfSsUN0LEQ9+9DBp/XwkxONWJVxBfHHgOAh+UuwQH9HdUE l2Foa34N6SFPrZo7dhwrdaSvos8XuiCxrlT3Be7I= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Erick Archer , Kees Cook , Dmitry Torokhov , Sasha Levin Subject: [PATCH 4.19 08/66] Input: ff-core - prefer struct_size over open coded arithmetic Date: Tue, 16 Jul 2024 17:30:43 +0200 Message-ID: <20240716152738.484854383@linuxfoundation.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240716152738.161055634@linuxfoundation.org> References: <20240716152738.161055634@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Erick Archer [ Upstream commit a08b8f8557ad88ffdff8905e5da972afe52e3307 ] This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1][2]. As the "ff" variable is a pointer to "struct ff_device" and this structure ends in a flexible array: struct ff_device { [...] struct file *effect_owners[] __counted_by(max_effects); }; the preferred way in the kernel is to use the struct_size() helper to do the arithmetic instead of the calculation "size + count * size" in the kzalloc() function. The struct_size() helper returns SIZE_MAX on overflow. So, refactor the comparison to take advantage of this. This way, the code is more readable and safer. This code was detected with the help of Coccinelle, and audited and modified manually. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1] Link: https://github.com/KSPP/linux/issues/160 [2] Signed-off-by: Erick Archer Reviewed-by: Kees Cook Link: https://lore.kernel.org/r/AS8PR02MB72371E646714BAE2E51A6A378B152@AS8PR02MB7237.eurprd02.prod.outlook.com Signed-off-by: Dmitry Torokhov Signed-off-by: Sasha Levin --- drivers/input/ff-core.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/input/ff-core.c b/drivers/input/ff-core.c index 66a46c84e28f5..7d83de2c536dd 100644 --- a/drivers/input/ff-core.c +++ b/drivers/input/ff-core.c @@ -24,8 +24,10 @@ /* #define DEBUG */ #include +#include #include #include +#include #include #include @@ -330,9 +332,8 @@ int input_ff_create(struct input_dev *dev, unsigned int max_effects) return -EINVAL; } - ff_dev_size = sizeof(struct ff_device) + - max_effects * sizeof(struct file *); - if (ff_dev_size < max_effects) /* overflow */ + ff_dev_size = struct_size(ff, effect_owners, max_effects); + if (ff_dev_size == SIZE_MAX) /* overflow */ return -EINVAL; ff = kzalloc(ff_dev_size, GFP_KERNEL); -- 2.43.0