]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - releases/5.15.154/media-xc4000-fix-atomicity-violation-in-xc4000_get_f.patch
Linux 5.15.154
[thirdparty/kernel/stable-queue.git] / releases / 5.15.154 / media-xc4000-fix-atomicity-violation-in-xc4000_get_f.patch
1 From 2df58315090ce55b04d533cdf4d239acb122e6ef Mon Sep 17 00:00:00 2001
2 From: Sasha Levin <sashal@kernel.org>
3 Date: Fri, 22 Dec 2023 13:50:30 +0800
4 Subject: media: xc4000: Fix atomicity violation in xc4000_get_frequency
5
6 From: Gui-Dong Han <2045gemini@gmail.com>
7
8 [ Upstream commit 36d503ad547d1c75758a6fcdbec2806f1b6aeb41 ]
9
10 In xc4000_get_frequency():
11 *freq = priv->freq_hz + priv->freq_offset;
12 The code accesses priv->freq_hz and priv->freq_offset without holding any
13 lock.
14
15 In xc4000_set_params():
16 // Code that updates priv->freq_hz and priv->freq_offset
17 ...
18
19 xc4000_get_frequency() and xc4000_set_params() may execute concurrently,
20 risking inconsistent reads of priv->freq_hz and priv->freq_offset. Since
21 these related data may update during reading, it can result in incorrect
22 frequency calculation, leading to atomicity violations.
23
24 This possible bug is found by an experimental static analysis tool
25 developed by our team, BassCheck[1]. This tool analyzes the locking APIs
26 to extract function pairs that can be concurrently executed, and then
27 analyzes the instructions in the paired functions to identify possible
28 concurrency bugs including data races and atomicity violations. The above
29 possible bug is reported when our tool analyzes the source code of
30 Linux 6.2.
31
32 To address this issue, it is proposed to add a mutex lock pair in
33 xc4000_get_frequency() to ensure atomicity. With this patch applied, our
34 tool no longer reports the possible bug, with the kernel configuration
35 allyesconfig for x86_64. Due to the lack of associated hardware, we cannot
36 test the patch in runtime testing, and just verify it according to the
37 code logic.
38
39 [1] https://sites.google.com/view/basscheck/
40
41 Fixes: 4c07e32884ab ("[media] xc4000: Fix get_frequency()")
42 Cc: stable@vger.kernel.org
43 Reported-by: BassCheck <bass@buaa.edu.cn>
44 Signed-off-by: Gui-Dong Han <2045gemini@gmail.com>
45 Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
46 Signed-off-by: Sasha Levin <sashal@kernel.org>
47 ---
48 drivers/media/tuners/xc4000.c | 4 ++--
49 1 file changed, 2 insertions(+), 2 deletions(-)
50
51 diff --git a/drivers/media/tuners/xc4000.c b/drivers/media/tuners/xc4000.c
52 index ef9af052007cb..849df4d1c573c 100644
53 --- a/drivers/media/tuners/xc4000.c
54 +++ b/drivers/media/tuners/xc4000.c
55 @@ -1517,10 +1517,10 @@ static int xc4000_get_frequency(struct dvb_frontend *fe, u32 *freq)
56 {
57 struct xc4000_priv *priv = fe->tuner_priv;
58
59 + mutex_lock(&priv->lock);
60 *freq = priv->freq_hz + priv->freq_offset;
61
62 if (debug) {
63 - mutex_lock(&priv->lock);
64 if ((priv->cur_fw.type
65 & (BASE | FM | DTV6 | DTV7 | DTV78 | DTV8)) == BASE) {
66 u16 snr = 0;
67 @@ -1531,8 +1531,8 @@ static int xc4000_get_frequency(struct dvb_frontend *fe, u32 *freq)
68 return 0;
69 }
70 }
71 - mutex_unlock(&priv->lock);
72 }
73 + mutex_unlock(&priv->lock);
74
75 dprintk(1, "%s()\n", __func__);
76
77 --
78 2.43.0
79