From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (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 82FF133F8A2 for ; Wed, 20 May 2026 15:57:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779292637; cv=none; b=q1Sva8bmwzt7dATMPT/vtM0yxm8XWWj91XAzL3cNrc9pyfLGfyqpoTpUzVOWiCcYg9bRT52/9Kr8f2NMU/B8iP2VV5vAB7ZWZtANsct/hY4pqgTgb8T+cXU2deq8OsM2AvNDzFDP39q6KRSG5mje7wqBfZGwFl2fozsGSfb26MM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779292637; c=relaxed/simple; bh=7NvgkVii9Mt+xIZwtiwwvrxtLwTlV0FgDfzptlWirWQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FmDQFljpuuUuv3NA24HKgBc5dvb4qX/mmUG+LefHBHzARywNPNHJG8PsqNZdxonKyPE6K4ZkSh4kyzQOrGB8ZzUdDw5wlogvLn0WjO4MsS4FO35BDQnSTQzGquuDXwsv3PtbN3hGuyDcLg+E97AOvtMT0nDe/uRMtdQ7yDqHLQs= 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=ntdMmOGp; arc=none smtp.client-ip=209.85.128.49 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="ntdMmOGp" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-488a8f97f6bso7689095e9.2 for ; Wed, 20 May 2026 08:57:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779292631; x=1779897431; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=oJt+1uhmRklXQWY1fAdB9UbwqmpgsjgJ14JFT2yZWDo=; b=ntdMmOGpOfWo8njdS7nH/M1gC2mxbaYawtNFFXpG3tTzJ9dpNQcRwW1DvIKFdeJY3S I9vB9Ni4o8wWYiEtjhzhGX42mR1u8uPUyHEUuXdZDsE1z4vbjPMG3NnLpPNCnDECXqH3 MQd4/IjnA0HocnJS9bLanIVui+Qbl0YAnYuSa7a9SiLR9M/R0mfnqW9dxHcMR9uNWtDm DlEhirnDo/LEFFSEXcUXpg28Xtm9xTbT6xfRYZEn2+4ZeQHwIukuMa+mKZ5cCE/ZwTqj oTbp73FsEdwzT2z9i0VhOVIG+uKIk/OkJDdmUGvN+UXH43W8LpCkOuEsx+JWngnqAtog TOTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779292631; x=1779897431; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=oJt+1uhmRklXQWY1fAdB9UbwqmpgsjgJ14JFT2yZWDo=; b=obWbz73GsVQWDz+GhLeuaBki8JnpPtabs5lPSPsiox5V4G+1i6WQB8SJmz+8+aW1Yp qMS8xqdX6z9hz/eM9cRCcrml9O7gzIDpSGJ74wHMIeARiTzXMytKVn7PmzDPsx1Pmm1z TixWHwf0rhQw6bqSXPhiNVcSa66J5P3+Px2RhjJdUGWYioChVvb7aZhg1U8VAyIOCbb6 ckKvw+veje6V2NrcMqod8Op5NMsQO8eW6MbIYTpnDsqGibwpS00C/HjI0MhKHqaHB90h +40sL7yKNWA2LOLq3lc6WZWUGn/JqVVsvQYCg6YMylcifigSXWs1HrRk6oKSi4n64kbB JZyA== X-Forwarded-Encrypted: i=1; AFNElJ+GuHGijj5l71BffJTOeC5pSRfD8ZwwJZZwDVNlfk86JDGKVH37IE6l6nJHobojRAuprCRgcX7TrhMqDDU=@vger.kernel.org X-Gm-Message-State: AOJu0YwaMD+GcMWuH6jmLtj6xz2GOCtjB9mCHcAmn7zUTW8QShisZEgZ YCNzDHy+rbRUWSjnct3nj+QSp4JjpXDhttCze2axG5T6nEsacZwoO5Dc X-Gm-Gg: Acq92OGiJuY2qxPyCeVXJGhmRx+ub6LFid9Zc14tMihdglbq4aU9ttsG125bwIUw8IT AGPGTq8DSC2ALIuUbt9aYJsNwxOaGuu1cxyVe80fQU73XzopFs1N1CifGvYoV0jiyx3R8xLqPv9 AoZd0+D6IEWQk57TJP4s4LrR84t7wRL776776DKoSgJgO5zcZxVh5OM+EBghmTqwRDGOoWlv1ey TECCql4B+YtOYjikKFbz1M41p1n3/XFIwxGKlXI61RCJGWqKUHf1cdlUR4hEWVC0Aea5A3SOjWj T4aLIvpY85xaMSClrTh/A7nC0OP1BSE8PiXW8gbCMtZveybzhIvEBHAGF9YQrTprQm+yljH/E/e iF85NIo1eoaD5tnXAOht8qU2E/9RrCfEzBv3M27PG39b1MCBen+n1oime3xufT0BDHYHh+uoFU7 UGyK/HY04Q8YagOjd8+TzEhaWPnj+Yib27sNnRt4PeVQFeZx8VdsNZ48RC78mXnw8wawHP2OI+i A== X-Received: by 2002:a05:600c:8b25:b0:490:6ab:406a with SMTP id 5b1f17b1804b1-49006ab40f1mr142371715e9.8.1779292630575; Wed, 20 May 2026 08:57:10 -0700 (PDT) Received: from menon.v.cablecom.net (84-74-0-139.dclient.hispeed.ch. [84.74.0.139]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48febe79ce3sm137216715e9.31.2026.05.20.08.57.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 May 2026 08:57:10 -0700 (PDT) From: Lothar Rubusch To: thorsten.blum@linux.dev, herbert@gondor.apana.org.au, davem@davemloft.net, nicolas.ferre@microchip.com, alexandre.belloni@bootlin.com, claudiu.beznea@tuxon.dev, tudor.ambarus@linaro.org, ardb@kernel.org, linusw@kernel.org Cc: linux-crypto@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, l.rubusch@gmail.com Subject: [PATCH v3 02/12] crypto: atmel-ecc - fix use after free situation Date: Wed, 20 May 2026 15:56:53 +0000 Message-Id: <20260520155703.23018-3-l.rubusch@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260520155703.23018-1-l.rubusch@gmail.com> References: <20260520155703.23018-1-l.rubusch@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Fixes the very likely race condition, having multiple of such devices attached (identified by sashiko feedback). The Scenario: Thread A (Device 1 Probe): Successfully adds i2c_priv to the global list (Line 324). The lock is released. Thread B (An active crypto request): Concurrently calls atmel_ecc_i2c_client_alloc(). It scans the global list, sees Device 1, and assigns a crypto job to it. Thread A: Moves to line 332. crypto_register_kpp() fails (e.g., out of memory or name clash). Thread A: Enters the error path. It removes Device 1 from the list and frees the i2c_priv memory. Thread B: Is still actively trying to talk to the I2C hardware using the i2c_priv pointer it grabbed in Step 2. The memory is now gone. Result: Kernel crash (Use-After-Free). Fixes: 11105693fa05 ("crypto: atmel-ecc - introduce Microchip / Atmel ECC driver") Signed-off-by: Lothar Rubusch --- drivers/crypto/atmel-ecc.c | 10 ++++++++++ drivers/crypto/atmel-i2c.h | 2 ++ 2 files changed, 12 insertions(+) diff --git a/drivers/crypto/atmel-ecc.c b/drivers/crypto/atmel-ecc.c index c9f798ebf44f..4c6860fc3dd9 100644 --- a/drivers/crypto/atmel-ecc.c +++ b/drivers/crypto/atmel-ecc.c @@ -218,6 +218,8 @@ static struct i2c_client *atmel_ecc_i2c_client_alloc(void) list_for_each_entry(i2c_priv, &atmel_i2c_mgmt.i2c_client_list, i2c_client_list_node) { + if (!i2c_priv->ready) + continue; tfm_cnt = atomic_read(&i2c_priv->tfm_count); if (tfm_cnt < min_tfm_cnt) { min_tfm_cnt = tfm_cnt; @@ -322,20 +324,24 @@ static int atmel_ecc_probe(struct i2c_client *client) return ret; i2c_priv = i2c_get_clientdata(client); + i2c_priv->ready = false; spin_lock(&atmel_i2c_mgmt.i2c_list_lock); list_add_tail(&i2c_priv->i2c_client_list_node, &atmel_i2c_mgmt.i2c_client_list); + i2c_priv->ready = true; spin_unlock(&atmel_i2c_mgmt.i2c_list_lock); ret = crypto_register_kpp(&atmel_ecdh_nist_p256); if (ret) { spin_lock(&atmel_i2c_mgmt.i2c_list_lock); list_del(&i2c_priv->i2c_client_list_node); + i2c_priv->ready = false; spin_unlock(&atmel_i2c_mgmt.i2c_list_lock); dev_err(&client->dev, "%s alg registration failed\n", atmel_ecdh_nist_p256.base.cra_driver_name); + return ret; } else { dev_info(&client->dev, "atmel ecc algorithms registered in /proc/crypto\n"); } @@ -347,6 +353,10 @@ static void atmel_ecc_remove(struct i2c_client *client) { struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client); + spin_lock(&atmel_i2c_mgmt.i2c_list_lock); + i2c_priv->ready = false; + spin_unlock(&atmel_i2c_mgmt.i2c_list_lock); + /* Return EBUSY if i2c client already allocated. */ if (atomic_read(&i2c_priv->tfm_count)) { /* diff --git a/drivers/crypto/atmel-i2c.h b/drivers/crypto/atmel-i2c.h index 72f04c15682f..e3b12030f9c4 100644 --- a/drivers/crypto/atmel-i2c.h +++ b/drivers/crypto/atmel-i2c.h @@ -129,6 +129,7 @@ struct atmel_ecc_driver_data { * @wake_token_sz : size in bytes of the wake_token * @tfm_count : number of active crypto transformations on i2c client * @hwrng : hold the hardware generated rng + * @ready : hw client is ready to use * * Reads and writes from/to the i2c client are sequential. The first byte * transmitted to the device is treated as the byte size. Any attempt to send @@ -145,6 +146,7 @@ struct atmel_i2c_client_priv { size_t wake_token_sz; atomic_t tfm_count ____cacheline_aligned; struct hwrng hwrng; + bool ready; }; /** -- 2.39.5