Skip to content

Commit 512f0b7

Browse files
Jacopo MondiHans Verkuil
authored andcommitted
media: cx18: Fix invalid access to file *
Sice commit 7b9eb53 ("media: cx18: Access v4l2_fh from file") all ioctl handlers have been ported to operate on the file * first function argument. The cx18 DVB layer calls cx18_init_on_first_open() when the driver needs to start streaming. This function calls the s_input(), s_std() and s_frequency() ioctl handlers directly, but being called from the driver context, it doesn't have a valid file * to pass them. This causes the ioctl handlers to deference an invalid pointer. Fix this by moving the implementation of those ioctls to functions that take a cx18 pointer instead of a file pointer, and turn the V4L2 ioctl handlers into wrappers that get the cx18 from the file. When calling from cx18_init_on_first_open(), pass the cx18 pointer directly. This allows removing the fake fh in cx18_init_on_first_open(). The bug has been reported by Smatch: --> 1223 cx18_s_input(NULL, &fh, video_input); The patch adds a new dereference of "file" but some of the callers pass a NULL pointer. Reported-by: Dan Carpenter <dan.carpenter@linaro.org> Closes: https://lore.kernel.org/all/aKL4OMWsESUdX8KQ@stanley.mountain/ Fixes: 7b9eb53 ("media: cx18: Access v4l2_fh from file") Cc: stable@vger.kernel.org Reviewed-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com> Tested-by: Hans Verkuil <hverkuil+cisco@kernel.org> Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com> Signed-off-by: Hans Verkuil <hverkuil+cisco@kernel.org>
1 parent 3a86608 commit 512f0b7

3 files changed

Lines changed: 27 additions & 20 deletions

File tree

drivers/media/pci/cx18/cx18-driver.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,11 +1136,8 @@ int cx18_init_on_first_open(struct cx18 *cx)
11361136
int video_input;
11371137
int fw_retry_count = 3;
11381138
struct v4l2_frequency vf;
1139-
struct cx18_open_id fh;
11401139
v4l2_std_id std;
11411140

1142-
fh.cx = cx;
1143-
11441141
if (test_bit(CX18_F_I_FAILED, &cx->i_flags))
11451142
return -ENXIO;
11461143

@@ -1220,14 +1217,14 @@ int cx18_init_on_first_open(struct cx18 *cx)
12201217

12211218
video_input = cx->active_input;
12221219
cx->active_input++; /* Force update of input */
1223-
cx18_s_input(NULL, &fh, video_input);
1220+
cx18_do_s_input(cx, video_input);
12241221

12251222
/* Let the VIDIOC_S_STD ioctl do all the work, keeps the code
12261223
in one place. */
12271224
cx->std++; /* Force full standard initialization */
12281225
std = (cx->tuner_std == V4L2_STD_ALL) ? V4L2_STD_NTSC_M : cx->tuner_std;
1229-
cx18_s_std(NULL, &fh, std);
1230-
cx18_s_frequency(NULL, &fh, &vf);
1226+
cx18_do_s_std(cx, std);
1227+
cx18_do_s_frequency(cx, &vf);
12311228
return 0;
12321229
}
12331230

drivers/media/pci/cx18/cx18-ioctl.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -521,10 +521,8 @@ static int cx18_g_input(struct file *file, void *fh, unsigned int *i)
521521
return 0;
522522
}
523523

524-
int cx18_s_input(struct file *file, void *fh, unsigned int inp)
524+
int cx18_do_s_input(struct cx18 *cx, unsigned int inp)
525525
{
526-
struct cx18_open_id *id = file2id(file);
527-
struct cx18 *cx = id->cx;
528526
v4l2_std_id std = V4L2_STD_ALL;
529527
const struct cx18_card_video_input *card_input =
530528
cx->card->video_inputs + inp;
@@ -558,6 +556,11 @@ int cx18_s_input(struct file *file, void *fh, unsigned int inp)
558556
return 0;
559557
}
560558

559+
static int cx18_s_input(struct file *file, void *fh, unsigned int inp)
560+
{
561+
return cx18_do_s_input(file2id(file)->cx, inp);
562+
}
563+
561564
static int cx18_g_frequency(struct file *file, void *fh,
562565
struct v4l2_frequency *vf)
563566
{
@@ -570,11 +573,8 @@ static int cx18_g_frequency(struct file *file, void *fh,
570573
return 0;
571574
}
572575

573-
int cx18_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
576+
int cx18_do_s_frequency(struct cx18 *cx, const struct v4l2_frequency *vf)
574577
{
575-
struct cx18_open_id *id = file2id(file);
576-
struct cx18 *cx = id->cx;
577-
578578
if (vf->tuner != 0)
579579
return -EINVAL;
580580

@@ -585,6 +585,12 @@ int cx18_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *v
585585
return 0;
586586
}
587587

588+
static int cx18_s_frequency(struct file *file, void *fh,
589+
const struct v4l2_frequency *vf)
590+
{
591+
return cx18_do_s_frequency(file2id(file)->cx, vf);
592+
}
593+
588594
static int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
589595
{
590596
struct cx18 *cx = file2id(file)->cx;
@@ -593,11 +599,8 @@ static int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
593599
return 0;
594600
}
595601

596-
int cx18_s_std(struct file *file, void *fh, v4l2_std_id std)
602+
int cx18_do_s_std(struct cx18 *cx, v4l2_std_id std)
597603
{
598-
struct cx18_open_id *id = file2id(file);
599-
struct cx18 *cx = id->cx;
600-
601604
if ((std & V4L2_STD_ALL) == 0)
602605
return -EINVAL;
603606

@@ -642,6 +645,11 @@ int cx18_s_std(struct file *file, void *fh, v4l2_std_id std)
642645
return 0;
643646
}
644647

648+
static int cx18_s_std(struct file *file, void *fh, v4l2_std_id std)
649+
{
650+
return cx18_do_s_std(file2id(file)->cx, std);
651+
}
652+
645653
static int cx18_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
646654
{
647655
struct cx18_open_id *id = file2id(file);

drivers/media/pci/cx18/cx18-ioctl.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ u16 cx18_service2vbi(int type);
1212
void cx18_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal);
1313
u16 cx18_get_service_set(struct v4l2_sliced_vbi_format *fmt);
1414
void cx18_set_funcs(struct video_device *vdev);
15-
int cx18_s_std(struct file *file, void *fh, v4l2_std_id std);
16-
int cx18_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf);
17-
int cx18_s_input(struct file *file, void *fh, unsigned int inp);
15+
16+
struct cx18;
17+
int cx18_do_s_std(struct cx18 *cx, v4l2_std_id std);
18+
int cx18_do_s_frequency(struct cx18 *cx, const struct v4l2_frequency *vf);
19+
int cx18_do_s_input(struct cx18 *cx, unsigned int inp);

0 commit comments

Comments
 (0)