Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp240040rwn; Wed, 7 Sep 2022 15:54:34 -0700 (PDT) X-Google-Smtp-Source: AA6agR6XlACHr6a+BM5lR7lp+2J0P2N1aYGkao0O5PM1qW17TJ8fuUqx7yRmxxA4LssIasJE2oum X-Received: by 2002:a17:907:7fa5:b0:730:5d54:4c24 with SMTP id qk37-20020a1709077fa500b007305d544c24mr3884478ejc.641.1662591274429; Wed, 07 Sep 2022 15:54:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662591274; cv=none; d=google.com; s=arc-20160816; b=J0bScJY2rb+MCiMo9eiwXzv6EMqiKMAZv2ZWpP04Q+08bhP88aFJdMEygEH36IS3Ps xGXayNL2OCAmCAYV0fm4S/rJvQQjjVfBJ+sZDtDAvgt+OHOxt6IIYnpEMcr4q/GEEkN1 JT6KFSBUEKP4b1B55vUnvpCyvmwdY9y1HwZt+WLkXZSsp6GIjKz2O1vcaJfxC5CqUYQW FZkCOccmgqqz9BVvYnNqJ1HVWGCAZncHBCdo1uWt0swOVQKzf75jzJ7WjTP2r8lJ1A03 G3LvtZRMkUFc41RIwKfQJ+hyL0W5bz0p5hU1Mg2QkA6tdOI0pSJ8Kj3kEZBpXIZmPez3 z2bg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=xJNlqeNd/xrr04Dfw5ABivBa4Akc5EG+Lwtk/YFuHVc=; b=Js3S/HSefyjSgdHhTckxZypQOcdE4OJ1SiQZdFsoXyGSinMsaOYeBZMVwgFTHhiZvP 1MmWKwMJSqmQKYymhYy8lSEDNh6x6VE0ZA3anhQmF16ENcZc0CznCJwRjS5L9bEJNAiX aRBGTMbgYdcBiNge5PgsD7hBPDvHKxnVNoTaXrErdWZEVK8DlbRU80/RxF5V4RDhcHce TLc3J8JR1Jp1M/MsfP4JjAwrgQFaNcRWxvMp96muNHQEumVghNEfybHWrsITPxmVmWqJ B1vfffK+2rzWPO6VcIlqF/wpuiJYcF01XekyxrlaDstozqXcUp3U3zeXMke32FsKK0XI CKEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=lYb1dK+7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ne8-20020a1709077b8800b007707c68226bsi733880ejc.297.2022.09.07.15.54.08; Wed, 07 Sep 2022 15:54:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=lYb1dK+7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229566AbiIGWoV (ORCPT + 99 others); Wed, 7 Sep 2022 18:44:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229477AbiIGWoT (ORCPT ); Wed, 7 Sep 2022 18:44:19 -0400 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC85D71726; Wed, 7 Sep 2022 15:44:18 -0700 (PDT) Received: by mail-ej1-x636.google.com with SMTP id lz22so12867255ejb.3; Wed, 07 Sep 2022 15:44:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date; bh=xJNlqeNd/xrr04Dfw5ABivBa4Akc5EG+Lwtk/YFuHVc=; b=lYb1dK+7UqpkUWaY+4E2VuAdHXPTP7pVSZx8UVTuB34kgV8YQqPI/sUojy9bPydShz ITmZwacH05i14Cec5Dmpe3qdSP4sbZ8yUPQAq/Lpk7biy1rV0kfwyvoaVwCheGe+P/7F kcBWwn774jQX9l8q7XzDMq/MstWQiY4a0NZMo0QCjQ1ZLADJcMOSj1OHCmtkNZvGbnMi DDe+P3x2Jfj9yqB3L0Q5Fw936CmXeH2/4i/0ojrSUeng25GhDen8lZ8nkZmVtMR06Tg5 1TtGhI/RefHuKq5MTBciDuWNBZfNT0qE8EWlRdbasdq/NKd/LoKqXeovcjsfgzJrnFEN 2s8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date; bh=xJNlqeNd/xrr04Dfw5ABivBa4Akc5EG+Lwtk/YFuHVc=; b=05w+aVdjz93VFBH6H5eQxau66/lgcTUa57Xfax1O09wucOq/uVd896vzpy72sxDSbB 1BgDzMDfUvMgNRmDvDBklvwBSSc4hRjIWWGknYu7G00s/V6hHmG/bPtLfds5xDrJQac7 fBDzyziPj2bqdaQtK/2vH20i5/udxBgojRmpQ7GWMKPrbQvDVIW/Owk5dMaX13Ne6pNd +4c5LXT3B2dQk16hQXZJQRoajqIHeiNVXXEdNby2bL0YblV7/XlGCk0aYZP8yZeQCLcg E/fcj453xR4juQeJlwz1G0Jv2gFIFxqvmjU1EtS1b1YpyfqPAFTa5PBpIMScic/pVCVz gP1w== X-Gm-Message-State: ACgBeo2lERGVIrzJjTh9SBzTeenLYCtZZgAR2tQ6a6//gp333wtCiJ9W eMyQyaMz6thjOnFHrmZFgD8= X-Received: by 2002:a17:907:a06b:b0:771:aa05:e108 with SMTP id ia11-20020a170907a06b00b00771aa05e108mr1743919ejc.167.1662590657195; Wed, 07 Sep 2022 15:44:17 -0700 (PDT) Received: from xws.localdomain ([37.120.217.162]) by smtp.gmail.com with ESMTPSA id x3-20020a170906148300b0073d83f80b05sm384829ejc.94.2022.09.07.15.44.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Sep 2022 15:44:16 -0700 (PDT) From: Maximilian Luz To: Sakari Ailus Cc: Bingbu Cao , Tianshu Qiu , Mauro Carvalho Chehab , Greg Kroah-Hartman , Tomi Valkeinen , Laurent Pinchart , Hans Verkuil , linux-media@vger.kernel.org, linux-staging@lists.linux.dev, linux-kernel@vger.kernel.org, Maximilian Luz , stable@vger.kernel.org Subject: [PATCH] ipu3-imgu: Fix NULL pointer dereference in imgu_subdev_set_selection() Date: Thu, 8 Sep 2022 00:44:09 +0200 Message-Id: <20220907224409.3187482-1-luzmaximilian@gmail.com> X-Mailer: git-send-email 2.37.3 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Calling v4l2_subdev_get_try_crop() and v4l2_subdev_get_try_compose() with a subdev state of NULL leads to a NULL pointer dereference. This can currently happen in imgu_subdev_set_selection() when the state passed in is NULL, as this method first gets pointers to both the "try" and "active" states and only then decides which to use. The same issue has been addressed for imgu_subdev_get_selection() with commit 30d03a0de650 ("ipu3-imgu: Fix NULL pointer dereference in active selection access"). However the issue still persists in imgu_subdev_set_selection(). Therefore, apply a similar fix as done in the aforementioned commit to imgu_subdev_set_selection(). To keep things a bit cleaner, introduce helper functions for "crop" and "compose" access and use them in both imgu_subdev_set_selection() and imgu_subdev_get_selection(). Fixes: 0d346d2a6f54 ("media: v4l2-subdev: add subdev-wide state struct") Cc: stable@vger.kernel.org # for v5.14 and later Signed-off-by: Maximilian Luz --- drivers/staging/media/ipu3/ipu3-v4l2.c | 57 +++++++++++++++----------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/drivers/staging/media/ipu3/ipu3-v4l2.c b/drivers/staging/media/ipu3/ipu3-v4l2.c index ce13e746c15f..e530767e80a5 100644 --- a/drivers/staging/media/ipu3/ipu3-v4l2.c +++ b/drivers/staging/media/ipu3/ipu3-v4l2.c @@ -188,6 +188,28 @@ static int imgu_subdev_set_fmt(struct v4l2_subdev *sd, return 0; } +static struct v4l2_rect * +imgu_subdev_get_crop(struct imgu_v4l2_subdev *sd, + struct v4l2_subdev_state *sd_state, unsigned int pad, + enum v4l2_subdev_format_whence which) +{ + if (which == V4L2_SUBDEV_FORMAT_TRY) + return v4l2_subdev_get_try_crop(&sd->subdev, sd_state, pad); + else + return &sd->rect.eff; +} + +static struct v4l2_rect * +imgu_subdev_get_compose(struct imgu_v4l2_subdev *sd, + struct v4l2_subdev_state *sd_state, unsigned int pad, + enum v4l2_subdev_format_whence which) +{ + if (which == V4L2_SUBDEV_FORMAT_TRY) + return v4l2_subdev_get_try_compose(&sd->subdev, sd_state, pad); + else + return &sd->rect.bds; +} + static int imgu_subdev_get_selection(struct v4l2_subdev *sd, struct v4l2_subdev_state *sd_state, struct v4l2_subdev_selection *sel) @@ -200,18 +222,12 @@ static int imgu_subdev_get_selection(struct v4l2_subdev *sd, switch (sel->target) { case V4L2_SEL_TGT_CROP: - if (sel->which == V4L2_SUBDEV_FORMAT_TRY) - sel->r = *v4l2_subdev_get_try_crop(sd, sd_state, - sel->pad); - else - sel->r = imgu_sd->rect.eff; + sel->r = *imgu_subdev_get_crop(imgu_sd, sd_state, sel->pad, + sel->which); return 0; case V4L2_SEL_TGT_COMPOSE: - if (sel->which == V4L2_SUBDEV_FORMAT_TRY) - sel->r = *v4l2_subdev_get_try_compose(sd, sd_state, - sel->pad); - else - sel->r = imgu_sd->rect.bds; + sel->r = *imgu_subdev_get_compose(imgu_sd, sd_state, sel->pad, + sel->which); return 0; default: return -EINVAL; @@ -223,10 +239,9 @@ static int imgu_subdev_set_selection(struct v4l2_subdev *sd, struct v4l2_subdev_selection *sel) { struct imgu_device *imgu = v4l2_get_subdevdata(sd); - struct imgu_v4l2_subdev *imgu_sd = container_of(sd, - struct imgu_v4l2_subdev, - subdev); - struct v4l2_rect *rect, *try_sel; + struct imgu_v4l2_subdev *imgu_sd = + container_of(sd, struct imgu_v4l2_subdev, subdev); + struct v4l2_rect *rect; dev_dbg(&imgu->pci_dev->dev, "set subdev %u sel which %u target 0x%4x rect [%ux%u]", @@ -238,22 +253,18 @@ static int imgu_subdev_set_selection(struct v4l2_subdev *sd, switch (sel->target) { case V4L2_SEL_TGT_CROP: - try_sel = v4l2_subdev_get_try_crop(sd, sd_state, sel->pad); - rect = &imgu_sd->rect.eff; + rect = imgu_subdev_get_crop(imgu_sd, sd_state, sel->pad, + sel->which); break; case V4L2_SEL_TGT_COMPOSE: - try_sel = v4l2_subdev_get_try_compose(sd, sd_state, sel->pad); - rect = &imgu_sd->rect.bds; + rect = imgu_subdev_get_compose(imgu_sd, sd_state, sel->pad, + sel->which); break; default: return -EINVAL; } - if (sel->which == V4L2_SUBDEV_FORMAT_TRY) - *try_sel = sel->r; - else - *rect = sel->r; - + *rect = sel->r; return 0; } -- 2.37.3