From nobody Sat Jul 25 22:32:22 2026 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (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 CB4832417D9 for ; Sun, 12 Jul 2026 17:12:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783876325; cv=none; b=u0p/yq2YF5YBJqKpqCv62yXeEMDE8bINiS/BlRmS+CiMT9YB3l1yFTKqbzOdBXt1aTCzsZ5WXeyc2Gcjsurqmvjxx6EBgjTZJxyY/0OLH5Th3fXe8R3dB63qj/gi9Zz9dl0wH3D3ujnA2ZWXlIDteagRIaC4KH1f1Upw3pQJpAQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783876325; c=relaxed/simple; bh=BYuOZs3FhdP+WpUpATCq3M1/Cd7DWKOjgexkt1eckq8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AQ76gZQXpGf0EYH4sHmJykkh9bADFKxCjr3bCGJdBqMQWUDlOzjOD1v4Ys53jVLn4OmB2mLxYKzPo0nhUNbU7AF3+KFSJ8c1yn5BieVWuttBUAjeqnEVjCCxik1MYkapXMOtiIEQQJgpYsSNGbwmnf3mkSDkvbL6SeKMW0nu8Vs= 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=klnivRbQ; arc=none smtp.client-ip=209.85.214.171 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="klnivRbQ" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-2cace91f112so19507765ad.0 for ; Sun, 12 Jul 2026 10:12:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783876323; x=1784481123; 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:content-type; bh=5lXEqICBIqkji+uxAJTXRS2K+JcXjR+GmTpqueIXq4s=; b=klnivRbQX1y31CLPJ/kTCQLcOJm90m4ueyZA3MRzgopflHq102ozGt4uutwWYWEil2 wh5wPuJ6rkQPlbPxpSstfTJkJDnWWTIj0GXNg/Zd/TX3r/o+fMmFgt9zUFoV1q82dxA/ WgqQYkFdQ8/isuG8Zr4tKcttp77XEU174qpK7bSSQTT/nNU6KP7DPScrtxB17k+SE+qr Y4S3B8TC7B1QZHwJYeSXFzlc43PyQ/WrWaRiLovdklxhz5+W615GMYM3jHPjY87ZDV4v Eo9kgPZmBKMpvjc/0qsN0/3jeVjLUYaC7qKmobgHrguolhf64w+Sg7jnSjt22GCgdxbI CBqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783876323; x=1784481123; 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:content-type; bh=5lXEqICBIqkji+uxAJTXRS2K+JcXjR+GmTpqueIXq4s=; b=YmKftImk/ylNsdI4QubON70WyybM5guGNwOedTWRdpyxV1xBn5W2m5gmJ9yecB3Q9i qe5Cizyzpx706YLV4mZXEYSdr88ootLXaOrXryFcRXX7/Gu/rk+NC99F/oIuKEBGgEAn DCvnS2erOKyqHT+iuaqeACzy2vQpAgMf1V03uKRjBvlKALNFn9FzC50Yyw9wLvP8fAmK 33ERx2oc2Nrn8JIHug3Kzp+DuST/Lb54V8G4FRxaBof+xIwmO3l+J9DrKXJxZSxQusUJ d0PcQK8IfF5LDF/KXqnN2deUCtkm2jaN8yGOt1ZvSwsw3vF5chD9z2VVTqR865531RJk mOFA== X-Forwarded-Encrypted: i=1; AHgh+Rpjba7BgdQXPA2mSJyf+A1IJPgvtjMad5Lk6/lMz5Lx7VX9h5KfRkaqMKahBqugClpsShvLdcwTRqXKET4=@vger.kernel.org X-Gm-Message-State: AOJu0YyJ3tCZcy+OZx+f4ghBP/dg1EtyVdeK53gygBYCXsXRdAjRhTxZ 8KwyX97Jny1PmeCHmJpqRTPSccuSVV8oDD/D0FgUrENxDHxQfTIZAhzJ X-Gm-Gg: AfdE7cmhUUmfCd81f6JmmLwKMVlN8WE79sZkh/zwsY+t3C3useGXZTkyqyRZiQxl+k9 iNltR1y4dvNqwi/LcBRklwXyDgQhUPTI3N80LqrgVi+5llWI6qAy/ZReKaq0KjIQbmjUTYDsecl M3BM6e/LkHK972PUAfQ3skGhFENc9VIptfK7h45cgJa7/tNUYCzQ8etmaKVl64ZJXUljxfCR/Ay YgVRnQlpG4lsBkJFZU5rfpywXrmnhzUfMUUun0XkBfwypEQQnBjYTmqVgL9fAesK5SuSBjZlwao FIazGFJEaymiJQpsK4AjpYGZU+7dCuxUC8DHeiwqp9L6b/XSrjccnhXDusXndqyvHq8YfS40P3G GdNvLrnUoZ5UP9HTAcqTuYwBiU/CXspMDT4XbCew2nBqM/qlXmFXkeD6Ir75WHnDIM6YbPUmMCK Efma8/6Z3vJMgrngxu+KQODj6wKsYhPeijfCjREqzNo0lgkPJ/8M/d57MQj+JQoxUuo9qUwOPaQ DJYcUtNtSJu0X0+Jnbo/Q== X-Received: by 2002:a17:902:d546:b0:2c9:cf62:6f61 with SMTP id d9443c01a7336-2ce9eac212bmr66417335ad.17.1783876323131; Sun, 12 Jul 2026 10:12:03 -0700 (PDT) Received: from DESKTOP-UIAUP5R.localdomain ([116.37.10.184]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2ccc9bfe040sm86810655ad.31.2026.07.12.10.12.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jul 2026 10:12:02 -0700 (PDT) From: Jaewon Yang To: Peter Huewe , Jarkko Sakkinen Cc: Jason Gunthorpe , linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, security@kernel.org, linux-api@vger.kernel.org Subject: [PATCH v2] tpm: Make the TPM character devices non-seekable Date: Mon, 13 Jul 2026 02:11:47 +0900 Message-ID: <20260712171147.323213-1-yong010301@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The TPM character devices expose a sequential command/response interface, but their open handlers leave FMODE_PREAD and FMODE_PWRITE enabled. After a command leaves a response pending, pread(fd, buf, 16, 0x1400) passes 0x1400 as *off to tpm_common_read(). The transfer length is bounded by response_length, but the offset is used unchecked when forming data_buffer + *off. A sufficiently large offset therefore causes an out-of-bounds heap read through copy_to_user() and, if the copy succeeds, an out-of-bounds zero-write through the following memset(). Positional I/O does not provide coherent semantics for this interface. An arbitrary pread offset cannot represent how much of a response has been consumed sequentially. The write callback always stores a command at the start of data_buffer, while pwrite() does not update file->f_pos and can leave the sequential read cursor stale. Call nonseekable_open() from both open handlers. This removes FMODE_PREAD and FMODE_PWRITE, causing positional reads and writes to fail with -ESPIPE before reaching the TPM callbacks, and explicitly marks the files non-seekable. Normal read() and write() continue to use the existing sequential f_pos cursor, leaving the response state machine unchanged. Tested on Linux 6.12 with KASAN and a swtpm TPM2 device: - sequential partial reads returned the complete response; - pread() and preadv() with offset 0x1400 returned -ESPIPE; - pwrite() and pwritev() with offset zero returned -ESPIPE; - the pending response remained intact after the rejected operations; - a subsequent normal command/response cycle completed normally; and - no KASAN report was produced. Fixes: 9488585b21be ("tpm: add support for partial reads") Link: https://lore.kernel.org/all/20260710090217.191289-1-yong010301@gmail.= com/ Cc: stable@vger.kernel.org Signed-off-by: Jaewon Yang Acked-by: Jarkko Sakkinen --- Changes in v2: - replace the response-buffer bounds check with nonseekable_open(); - reject positional read and write at open time; - preserve the existing sequential read/write state machine. The alternative response_length rework proposed during review was tested and not taken: a read-until-EOF loop hangs because cleanup resets *off without clearing response_length. It also treats an arbitrary positional offset as the consumption cursor; for example, pread(fd, &c, 1, 99) on a 100-byte response can discard bytes 0 through 98 without returning them. drivers/char/tpm/tpm-dev.c | 2 +- drivers/char/tpm/tpmrm-dev.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/char/tpm/tpm-dev.c b/drivers/char/tpm/tpm-dev.c index 2779a8738..74488f0a7 100644 --- a/drivers/char/tpm/tpm-dev.c +++ b/drivers/char/tpm/tpm-dev.c @@ -36,7 +36,7 @@ static int tpm_open(struct inode *inode, struct file *fil= e) =20 tpm_common_open(file, chip, priv, NULL); =20 - return 0; + return nonseekable_open(inode, file); =20 out: clear_bit(0, &chip->is_open); diff --git a/drivers/char/tpm/tpmrm-dev.c b/drivers/char/tpm/tpmrm-dev.c index f48d4d9e1..19e8f2779 100644 --- a/drivers/char/tpm/tpmrm-dev.c +++ b/drivers/char/tpm/tpmrm-dev.c @@ -29,7 +29,7 @@ static int tpmrm_open(struct inode *inode, struct file *f= ile) =20 tpm_common_open(file, chip, &priv->priv, &priv->space); =20 - return 0; + return nonseekable_open(inode, file); } =20 static int tpmrm_release(struct inode *inode, struct file *file) --=20 2.43.0