From patchwork Thu Dec 14 20:37:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Anderson X-Patchwork-Id: 13493614 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 000F8C4332F for ; Thu, 14 Dec 2023 20:38:15 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 4A31210E1E3; Thu, 14 Dec 2023 20:38:15 +0000 (UTC) Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by gabe.freedesktop.org (Postfix) with ESMTPS id A046910E1E3 for ; Thu, 14 Dec 2023 20:38:11 +0000 (UTC) Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1d045097b4cso51482865ad.0 for ; Thu, 14 Dec 2023 12:38:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1702586290; x=1703191090; darn=lists.freedesktop.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=CEbYOeExhWxrQcvIrSnW0dzfFwJIoL+EWRTjsEQRQR4=; b=AMeazQbdfWKWPlN8Wj2UDQoLgr59Y6PW13o7LAkbOqGufNIvxqb1i7cWQG/Q4T+un7 tIdRewu7c8Q3NTA2sBJBjzCbCsU3ed2XPj/DU9/W+3hxUOnlHCbQ9z2C3AoHxZ4tTgJK jax8CWwvN/5CpGHbgy1XNAyNVpGpmMSzj5aNs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702586290; x=1703191090; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=CEbYOeExhWxrQcvIrSnW0dzfFwJIoL+EWRTjsEQRQR4=; b=bj17bOID2NSsTFcjGdq/ZfskwL0vmCozrhBrTBqnK3JxmzgV+ANTzLBewqZY2gAoM7 yZ3ztCQpd36o2uVHrGbXt9tF4SFbARwKfgQ9jXHkNClU9YFZmDzkmZeY+YA2vS/D2Z1H 0g2jVa+y9iovKzUplVelaefDyK2Qgq7FT+eH28vrAdP0PIB/EakfX/vJyxLj3X3DMLcp /s0dq7hIExU1Dj+mwyWJY+ru5z+bajpCkgC6Zd1exX0OLxhbiaBWUlsNNLfU0epi/Gvh +pYac0NN72vioqNBqr6Yleg9hRomLrdEBFkAlBpaWCknx7N/O2e8Vk4bHiQVBUKv7UIE MuKg== X-Gm-Message-State: AOJu0YzrE6ZjCqwKOxaCwxeCTENoCr9v0o4ZUYhEhvUOmmjQKEhrg5B9 88dnp0roVDRHUpeNJIYkql2Zs+6GVzBD2m1jGwUZDti2 X-Google-Smtp-Source: AGHT+IGcoNWWmuS7W3F8Oa3UUyu1se2mbBEQgXS9YTTmt+8tUfR2PUem9ww3SQz2i2gofIRWMOARSg== X-Received: by 2002:a17:902:ec89:b0:1d0:c26d:e509 with SMTP id x9-20020a170902ec8900b001d0c26de509mr6417151plg.108.1702586290186; Thu, 14 Dec 2023 12:38:10 -0800 (PST) Received: from tictac2.mtv.corp.google.com ([2620:15c:9d:2:9d0c:9bd0:17f6:8236]) by smtp.gmail.com with ESMTPSA id y18-20020a170902b49200b001cfc2e0a82fsm12799081plr.26.2023.12.14.12.38.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 12:38:09 -0800 (PST) From: Douglas Anderson To: dri-devel@lists.freedesktop.org Subject: [PATCH v3 1/2] drm/bridge: parade-ps8640: Never store more than msg->size bytes in AUX xfer Date: Thu, 14 Dec 2023 12:37:51 -0800 Message-ID: <20231214123752.v3.1.I9d1afcaad76a3e2c0ca046dc4adbc2b632c22eda@changeid> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Maxime Ripard , Neil Armstrong , Philip Chen , Robert Foss , Andrzej Hajda , Thomas Zimmermann , Jonas Karlman , Sam Ravnborg , Douglas Anderson , Jernej Skrabec , Stephen Boyd , Laurent Pinchart , Guenter Roeck , linux-kernel@vger.kernel.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" While testing, I happened to notice a random crash that looked like: Kernel panic - not syncing: stack-protector: Kernel stack is corrupted in: drm_dp_dpcd_probe+0x120/0x120 Analysis of drm_dp_dpcd_probe() shows that we pass in a 1-byte buffer (allocated on the stack) to the aux->transfer() function. Presumably if the aux->transfer() writes more than one byte to this buffer then we're in a bad shape. Dropping into kgdb, I noticed that "aux->transfer" pointed at ps8640_aux_transfer(). Reading through ps8640_aux_transfer(), I can see that there are cases where it could write more bytes to msg->buffer than were specified by msg->size. This could happen if the hardware reported back something bogus to us. Let's fix this so we never write more than msg->size bytes. We'll still read all the bytes from the hardware just in case the hardware requires it since the aux transfer data comes through an auto-incrementing register. NOTE: I have no actual way to reproduce this issue but it seems likely this is what was happening in the crash I looked at. Fixes: 13afcdd7277e ("drm/bridge: parade-ps8640: Add support for AUX channel") Signed-off-by: Douglas Anderson Reviewed-by: Guenter Roeck Reviewed-by: Stephen Boyd --- Changes in v3: - Never return more than msg->size as the number of bytes we read. Changes in v2: - Still read all the bytes; just don't write them all to the buffer. drivers/gpu/drm/bridge/parade-ps8640.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/bridge/parade-ps8640.c b/drivers/gpu/drm/bridge/parade-ps8640.c index 8161b1a1a4b1..d264b80d909d 100644 --- a/drivers/gpu/drm/bridge/parade-ps8640.c +++ b/drivers/gpu/drm/bridge/parade-ps8640.c @@ -330,11 +330,12 @@ static ssize_t ps8640_aux_transfer_msg(struct drm_dp_aux *aux, return ret; } - buf[i] = data; + if (i < msg->size) + buf[i] = data; } } - return len; + return min(len, msg->size); } static ssize_t ps8640_aux_transfer(struct drm_dp_aux *aux, From patchwork Thu Dec 14 20:37:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Anderson X-Patchwork-Id: 13493615 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 870B5C4332F for ; Thu, 14 Dec 2023 20:38:21 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id ADD9F10E25E; Thu, 14 Dec 2023 20:38:20 +0000 (UTC) Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by gabe.freedesktop.org (Postfix) with ESMTPS id 4FE1E10E25E for ; Thu, 14 Dec 2023 20:38:13 +0000 (UTC) Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-6cecb004339so5095359b3a.3 for ; Thu, 14 Dec 2023 12:38:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1702586292; x=1703191092; darn=lists.freedesktop.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=RCuUsFQR6lp6/7dcHWOPjNe2VLvS+gov+CkK1fWJuUI=; b=YBsVD4O5IMzoM5Y2X+jIDLMmPq5WXmz0A8GZdJOpyLMGiBoyZX+t8lJOq3hHCpYwU9 aAHF4FCGZb3D2bfykcYo+y2FvvjcKYiF9mlYceFcNnV11TFcu6NV27i/Psg837pe6Jsl Ag90J1hkNf9Dye6u0qPlx7ZJJUstv1SJ2tL1Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702586292; x=1703191092; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RCuUsFQR6lp6/7dcHWOPjNe2VLvS+gov+CkK1fWJuUI=; b=CslwgVKcVrIptr+t0IT+u6LbAzCfKxYMjb2ChwHhsb+BH6GLBvblClZYU5u7jfIoey EJnGpnlBdOm6WH6m1pTgZY5mqOnGMXmoUqtTZ1Bw2Zir0cU6A/LfCGH9r+BL5342waqN XNdG6FHzQGkG7Q9OyKaduyqFleI5INbQz/aMEnLPQJBMGR1smNia4TrN4wrZllTEPVXB mRRO4fyDjW7w6nWPN3fn3VtDzMngVTmYsvtbmH+nyZRvNAdhB2Oh8u1kCWWTe5Equrje 8/CGP10wvie+9UVHCH1sXaRJTY35VMP5FqLTic7KAjHgOAk7iqh10IdWkKE0ZyndlXUU 17NA== X-Gm-Message-State: AOJu0Yx9tb5+pPjU/kuYJQoppfIs7KaCBHgq4YoNWgEOEwIzN2otoxCa 87EyWvClh8WsbVrZNnd5f8pjDckr0gglXzQcUsW4vNMc X-Google-Smtp-Source: AGHT+IEqBO+G2ecwBpTD5DnxvMh1rm2t6Nq3fzf3H6fhqeSmAfh6caWQ9m9jlmzPR4UB2NwbehmaMA== X-Received: by 2002:a05:6a21:3398:b0:18c:2287:29cf with SMTP id yy24-20020a056a21339800b0018c228729cfmr6588631pzb.40.1702586292209; Thu, 14 Dec 2023 12:38:12 -0800 (PST) Received: from tictac2.mtv.corp.google.com ([2620:15c:9d:2:9d0c:9bd0:17f6:8236]) by smtp.gmail.com with ESMTPSA id y18-20020a170902b49200b001cfc2e0a82fsm12799081plr.26.2023.12.14.12.38.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 12:38:11 -0800 (PST) From: Douglas Anderson To: dri-devel@lists.freedesktop.org Subject: [PATCH v3 2/2] drm/bridge: ti-sn65dsi86: Never store more than msg->size bytes in AUX xfer Date: Thu, 14 Dec 2023 12:37:52 -0800 Message-ID: <20231214123752.v3.2.I7b83c0f31aeedc6b1dc98c7c741d3e1f94f040f8@changeid> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog In-Reply-To: <20231214123752.v3.1.I9d1afcaad76a3e2c0ca046dc4adbc2b632c22eda@changeid> References: <20231214123752.v3.1.I9d1afcaad76a3e2c0ca046dc4adbc2b632c22eda@changeid> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Maxime Ripard , Neil Armstrong , Robert Foss , Andrzej Hajda , Thomas Zimmermann , Jonas Karlman , Sam Ravnborg , Douglas Anderson , Jernej Skrabec , Stephen Boyd , Laurent Pinchart , Guenter Roeck , linux-kernel@vger.kernel.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" For aux reads, the value `msg->size` indicates the size of the buffer provided by `msg->buffer`. We should never in any circumstances write more bytes to the buffer since it may overflow the buffer. In the ti-sn65dsi86 driver there is one code path that reads the transfer length from hardware. Even though it's never been seen to be a problem, we should make extra sure that the hardware isn't increasing the length since doing so would cause us to overrun the buffer. Fixes: 982f589bde7a ("drm/bridge: ti-sn65dsi86: Update reply on aux failures") Signed-off-by: Douglas Anderson Reviewed-by: Guenter Roeck Reviewed-by: Stephen Boyd --- (no changes since v2) Changes in v2: - Updated patch subject to match ps8640 patch. drivers/gpu/drm/bridge/ti-sn65dsi86.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/bridge/ti-sn65dsi86.c b/drivers/gpu/drm/bridge/ti-sn65dsi86.c index 9095d1453710..62cc3893dca5 100644 --- a/drivers/gpu/drm/bridge/ti-sn65dsi86.c +++ b/drivers/gpu/drm/bridge/ti-sn65dsi86.c @@ -527,6 +527,7 @@ static ssize_t ti_sn_aux_transfer(struct drm_dp_aux *aux, u32 request_val = AUX_CMD_REQ(msg->request); u8 *buf = msg->buffer; unsigned int len = msg->size; + unsigned int short_len; unsigned int val; int ret; u8 addr_len[SN_AUX_LENGTH_REG + 1 - SN_AUX_ADDR_19_16_REG]; @@ -600,7 +601,8 @@ static ssize_t ti_sn_aux_transfer(struct drm_dp_aux *aux, } if (val & AUX_IRQ_STATUS_AUX_SHORT) { - ret = regmap_read(pdata->regmap, SN_AUX_LENGTH_REG, &len); + ret = regmap_read(pdata->regmap, SN_AUX_LENGTH_REG, &short_len); + len = min(len, short_len); if (ret) goto exit; } else if (val & AUX_IRQ_STATUS_NAT_I2C_FAIL) {