From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 91D1538C2C7 for ; Thu, 2 Apr 2026 08:41:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775119298; cv=none; b=Ry68cFjAXNEFY9dlJbY53QgA84uc3yMeiTQfGEaNhgkSugxRjg6g5TqzXaZ0G7eLqY7qb0U8zDk186XyJ2bTe6P+89bkYbc3c9kLTCtBkz3/AF4uGlpl3y38ZAY4DG3eNkTETbn/TEC8BzvAp7BE1VnLz+vWkWT4YR0FUkEb4vA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775119298; c=relaxed/simple; bh=ANqb5i1tpKDfgmY9Mdfmivntv7t3ODNAUIQtD3WHX88=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=FF7J+tKLNeQMUCDBIJD+HVLzgD3EBSaErett11N5rUMVWcM0fDcvwxMA5koSqZK+IwFS+wL9l+r7Wqy92d9rCwPeocWfLtv9gST4wHm4vzHatLjTMPobz/e1En9/L1q/hvSQmrzQmaZ9La9gQbRH4Kk7WMl8gom0DcLqeIZBXkU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=pa1jqXRy; arc=none smtp.client-ip=209.85.221.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="pa1jqXRy" Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-43d04fc3bf2so338659f8f.3 for ; Thu, 02 Apr 2026 01:41:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775119296; x=1775724096; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=5Eo+AVSMDc66CklTGyClArKPHGKdA4dAsAoQ34cPyIw=; b=pa1jqXRyZjRqcEgjVgS1yYQsCIZsyEEdtG1OY4F5NBNrTl4zI4kCZVV/ySsNIAUfh0 9qmKCn55kdg4b8rxcAp0zBrBrCodYjj9BbKZpNb8e7lLyeNMsZ3lwXsq5IBoD2NJNDeR EYQqhfn2SiPPEDFy9+/trjsYivnnWkeXcFeWLJoTwa9nLttUGcnCRuZyu9X2eSOI5yEC TVV17DvSqV1LdKVPOfsUrqPgV8JyA8LVEMqaXJXltxjWneStGMqSKj5CC2EwNJKBRZqz ZHnWw3+pgLGP8HZ6AfIG/0+JVLjL+mkQYn4VInj0bJLZf1/VNS9Te8Y39SdBHevgRx0E 8QqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775119296; x=1775724096; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5Eo+AVSMDc66CklTGyClArKPHGKdA4dAsAoQ34cPyIw=; b=chmHN2nlOLcTP33IircLX1lYTMDvvTa+XkP9Q29R7ECdCkuSRwdkYzfc9QOWoTVTpZ gqnohSyFLgxGfOB1Str+NV0FMYz99Y+XpnX6ayupWsCP2cwW6IlQKE5u89ZnJTYKpl0/ tB6Ty614/DVMcnUucANYUhVFKtQw+LG/szC23OzToFSSlwu1huA3HhdU4/z9bCyCtId8 0cC42CJb0B3FEG8sVyzqLjFde80hwxrUIrL25uscG96DwImSUeNwe/wf3v/Udp7eF4ao WPfXuICSbXT+vALDg1Iq9uVWlTZcQBuXF1wJqjphOgF5S2sxfCCya2VUIiVtnPfyNU+L BMSQ== X-Forwarded-Encrypted: i=1; AJvYcCUzriBuojiP3ELQHbujnOqcUcQ9aFYxkqSOoE/JMA8Cs1XeTeqQjzuZJCXlhGakZzfk4Gvmd3wDvoXKdNk=@vger.kernel.org X-Gm-Message-State: AOJu0Yz0f2ZlEIgMqWPTFViTbsahxqq532VSoGWh5oWMOUZA9yt0p3pm VVC2N1YwP01jeUJe9Z8rao6fsmjeKhY97/Eyw7vsW4fQLNFsyPJ8J/ea X-Gm-Gg: ATEYQzxx7zY3njQrCSHfgHnEvZEZ1HbXXqMsB7DmSsYz9D1AnCDaxygb+JMqGikhGU0 sl6+EaK/HC6MMAiUQGzWlnb2O/YYHCXHnB/bWpEGe1IEH4Et/hMTJmbtgw1l0gFDedQNw6TAvHY 0PiEkIzI4UXXjszCqpsc1zGWO+rJLF7WyN+pdZqEUjA2H+W6YVMJO9onsqhguxgaOoNkSy2FRdU t1yOmHWa1gZSkJH6GbpMQ9MhfsjxkOUcRrpRJN0OGmKQ095S3tQzNcDUFHa6vUogZNhQkopPYVv ur1mtTqU3p8wVDhicQ8rGb6ZCzjx/ORpSeVvNCJG0UHnUX9ZtnxRSeaKd/E0RcO6DHRG63Cz4tx IAVl+Gtr1B8m20qro4H578u0IK2eOWsS0t4ab+EJCky8zu0TmwO8iJdmdsEeuCKNfGHnHPBTkFh gOjpi5+bS+ogl2jbIu1f8= X-Received: by 2002:a05:6000:240b:b0:439:bcc2:bf0a with SMTP id ffacd0b85a97d-43d1f234bc9mr4271590f8f.23.1775119295773; Thu, 02 Apr 2026 01:41:35 -0700 (PDT) Received: from localhost ([196.207.164.177]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43d1e4d29bbsm6432694f8f.21.2026.04.02.01.41.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Apr 2026 01:41:34 -0700 (PDT) Date: Thu, 2 Apr 2026 11:41:30 +0300 From: Dan Carpenter To: Yuho Choi Cc: Andy Shevchenko , Hans de Goede , Mauro Carvalho Chehab , Sakari Ailus , Greg Kroah-Hartman , Peter Zijlstra , Kees Cook , Josh Poimboeuf , Thomas Andreatta , linux-media@vger.kernel.org, linux-staging@lists.linux.dev, linux-kernel@vger.kernel.org, Yuho Choi Subject: Re: [PATCH v3] media: atomisp: gc2235: fix UAF and memory leak Message-ID: References: <20260401163050.34830-1-yqc5929@psu.edu> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260401163050.34830-1-yqc5929@psu.edu> Please run your patches through checkpatch.pl. On Wed, Apr 01, 2026 at 12:30:50PM -0400, Yuho Choi wrote: > gc2235_probe() handles its error paths incorrectly. > > If media_entity_pads_init() fails, gc2235_remove() is called, which > tears down the subdev and frees dev, but then still falls through to > atomisp_register_i2c_module(). This results in use-after-free. > > If atomisp_register_i2c_module() fails, the media entity and control > handler are left initialized and dev is leaked. > > gc2235_remove() is the full teardown path for a successfully probed > device; it unconditionally assumes a fully-initialized device. > gc2235_probe() must unwind only the resources that were actually > initialized at the point of failure. The "must unwind only the resources that were actually initialized at the point of failure." phrasing is too strong. I was hoping you would review it and find an actual bug. I reviewed it myself and didn't find a bug beyond the leaks and use after frees mentioned in this commit message. As I wrote in my blog, leaks are one of the common bugs from this style of error handling because it is too complicated. > > Handle each failure path with explicit unwind labels that free only > what has been initialized. Return success only after the full probe > sequence completes. If I were determined to use a magical cleanup function to do the cleanups then I would reverse the gotos and direct returns. regards, dan carpenter diff --git a/drivers/staging/media/atomisp/i2c/atomisp-gc2235.c b/drivers/staging/media/atomisp/i2c/atomisp-gc2235.c index d3414312e1de..61fb82b26cc9 100644 --- a/drivers/staging/media/atomisp/i2c/atomisp-gc2235.c +++ b/drivers/staging/media/atomisp/i2c/atomisp-gc2235.c @@ -808,8 +808,11 @@ static int gc2235_probe(struct i2c_client *client) atomisp_bayer_order_grbg); ret = gc2235_s_config(&dev->sd, client->irq, gcpdev); - if (ret) - goto out_free; + if (ret) { + v4l2_device_unregister_subdev(&dev->sd); + kfree(dev); + return ret; + } dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; dev->pad.flags = MEDIA_PAD_FL_SOURCE; @@ -818,18 +821,16 @@ static int gc2235_probe(struct i2c_client *client) ret = v4l2_ctrl_handler_init(&dev->ctrl_handler, ARRAY_SIZE(gc2235_controls)); - if (ret) { - gc2235_remove(client); - return ret; - } + if (ret) + goto err_remove; for (i = 0; i < ARRAY_SIZE(gc2235_controls); i++) v4l2_ctrl_new_custom(&dev->ctrl_handler, &gc2235_controls[i], NULL); if (dev->ctrl_handler.error) { - gc2235_remove(client); - return dev->ctrl_handler.error; + ret = dev->ctrl_handler.error; + goto err_remove; } /* Use same lock for controls as for everything else. */ @@ -838,14 +839,16 @@ static int gc2235_probe(struct i2c_client *client) ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad); if (ret) - gc2235_remove(client); + goto err_remove; - return atomisp_register_i2c_module(&dev->sd, gcpdev); + ret = atomisp_register_i2c_module(&dev->sd, gcpdev); + if (ret) + goto err_remove; -out_free: - v4l2_device_unregister_subdev(&dev->sd); - kfree(dev); + return 0; +err_remove: + gc2235_remove(client); return ret; }