From patchwork Tue Sep 25 21:58:00 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sylwester Nawrocki X-Patchwork-Id: 1506571 Return-Path: X-Original-To: patchwork-linux-media@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id 417D23FC71 for ; Tue, 25 Sep 2012 21:58:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751195Ab2IYV6G (ORCPT ); Tue, 25 Sep 2012 17:58:06 -0400 Received: from mail-bk0-f46.google.com ([209.85.214.46]:41893 "EHLO mail-bk0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750898Ab2IYV6E (ORCPT ); Tue, 25 Sep 2012 17:58:04 -0400 Received: by bkcjk13 with SMTP id jk13so1534346bkc.19 for ; Tue, 25 Sep 2012 14:58:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=Cgj2GqV4LSW6HdHEC2KwczCc0Rgspf8cSWUmaqNs/1E=; b=qt7BGuIMHi79PvXF2Ry12zEX3h0Lwasxc9h+fPGoYl1WpVDfiU/DwOqwXrkF54s+5K 0EMdTSq9BbE7Rlphds45yHHJdNbv6bxa7pL85poPKC8KpW+n9fdneXyQoBtgGe9IHXx7 gVuCbNJJB/tK/hVf0F+snBd8GbBn05orPId5ET+ovo1+zPCeEc0BdnROMMCZ60fa0jXe JCtLJXDH5uj26zd0VLO+UtWBUjwIbROMn1z5itVYvFkn4BTt6hsHAZAZqKpmAXnVxfy+ SeOj9lcrs9D9A6lBEXTR09/rEAzXk7BCEK+VjiF0DuQ040b/DtUPFk5gHn1HCBWUvfGD KVHQ== Received: by 10.204.148.146 with SMTP id p18mr1591877bkv.51.1348610283040; Tue, 25 Sep 2012 14:58:03 -0700 (PDT) Received: from [192.168.1.110] (031011252076.warszawa.vectranet.pl. [31.11.252.76]) by mx.google.com with ESMTPS id e3sm697498bks.7.2012.09.25.14.58.01 (version=SSLv3 cipher=OTHER); Tue, 25 Sep 2012 14:58:01 -0700 (PDT) Message-ID: <506228E8.1040704@gmail.com> Date: Tue, 25 Sep 2012 23:58:00 +0200 From: Sylwester Nawrocki User-Agent: Mozilla/5.0 (X11; Linux i686; rv:12.0) Gecko/20120430 Thunderbird/12.0.1 MIME-Version: 1.0 To: Sachin Kamat CC: linux-media@vger.kernel.org, s.nawrocki@samsung.com, mchehab@infradead.org, patches@linaro.org Subject: Re: [PATCH] [media] s5k6aa: Fix possible NULL pointer dereference References: <1348298907-20791-1-git-send-email-sachin.kamat@linaro.org> In-Reply-To: <1348298907-20791-1-git-send-email-sachin.kamat@linaro.org> Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Hi Sachin, On 09/22/2012 09:28 AM, Sachin Kamat wrote: > It is previously assumed that 'rect' could be NULL. > Hence add a check to print the members of 'rect' only when it is not > NULL. > > Signed-off-by: Sachin Kamat > --- > drivers/media/i2c/s5k6aa.c | 5 +++-- > 1 files changed, 3 insertions(+), 2 deletions(-) > > diff --git a/drivers/media/i2c/s5k6aa.c b/drivers/media/i2c/s5k6aa.c > index 045ca7f..7531edb 100644 > --- a/drivers/media/i2c/s5k6aa.c > +++ b/drivers/media/i2c/s5k6aa.c > @@ -1177,8 +1177,9 @@ static int s5k6aa_get_crop(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh, > > mutex_unlock(&s5k6aa->lock); > > - v4l2_dbg(1, debug, sd, "Current crop rectangle: (%d,%d)/%dx%d\n", > - rect->left, rect->top, rect->width, rect->height); > + if (rect) > + v4l2_dbg(1, debug, sd, "Current crop rectangle: (%d,%d)/%dx%d\n", > + rect->left, rect->top, rect->width, rect->height); > > return 0; > } Thank you for the patch. I would attack this problem form slightly different angle though, i.e. I would make sure __s5k6aa_get_crop_rect() always returns valid pointer. There is similar issue in s5k6aa_set_crop(). Since crop->which is already validated in drivers/media/v4l2-core/v4l2-subdev.c and can have only values: V4L2_SUBDEV_FORMAT_TRY, V4L2_SUBDEV_FORMAT_ACTIVE it's safe to do something like: 8<------------------------------------------------------------------------- From 724aa5f1fefcaca2dee4f75ba960a1f620400e1a Mon Sep 17 00:00:00 2001 From: Sylwester Nawrocki Date: Tue, 25 Sep 2012 23:53:27 +0200 Subject: [PATCH] s5k6aa: Fix potential null pointer dereference Make sure __s5k6aa_get_crop_rect() always returns valid pointer, as it is assumed at the callers. crop->which is already validated when subdev set_crop and get_crop callbacks are called from within the v4l2-core. If it ever happens the crop operations are called directly for some reason in kernel space, with incorrect crop->which argument, just log it with WARN and return reference to the TRY crop. Reported-by: Sachin Kamat Signed-off-by: Sylwester Nawrocki Acked-by: Sachin Kamat --- drivers/media/i2c/s5k6aa.c | 11 ++++------- 1 files changed, 4 insertions(+), 7 deletions(-) diff --git a/drivers/media/i2c/s5k6aa.c b/drivers/media/i2c/s5k6aa.c index 045ca7f..57cd4fa 100644 --- a/drivers/media/i2c/s5k6aa.c +++ b/drivers/media/i2c/s5k6aa.c @@ -1061,10 +1061,9 @@ __s5k6aa_get_crop_rect(struct s5k6aa *s5k6aa, struct v4l2_subdev_fh *fh, { if (which == V4L2_SUBDEV_FORMAT_ACTIVE) return &s5k6aa->ccd_rect; - if (which == V4L2_SUBDEV_FORMAT_TRY) - return v4l2_subdev_get_try_crop(fh, 0); - return NULL; + WARN_ON(which != V4L2_SUBDEV_FORMAT_TRY); + return v4l2_subdev_get_try_crop(fh, 0); } static void s5k6aa_try_format(struct s5k6aa *s5k6aa, @@ -1169,12 +1168,10 @@ static int s5k6aa_get_crop(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh, struct v4l2_rect *rect; memset(crop->reserved, 0, sizeof(crop->reserved)); - mutex_lock(&s5k6aa->lock); + mutex_lock(&s5k6aa->lock); rect = __s5k6aa_get_crop_rect(s5k6aa, fh, crop->which); - if (rect) - crop->rect = *rect; - + crop->rect = *rect; mutex_unlock(&s5k6aa->lock); v4l2_dbg(1, debug, sd, "Current crop rectangle: (%d,%d)/%dx%d\n",