From 6dc156be3bcc8cd45bafdcb22244471614628e4e Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sun, 6 Dec 2026 21:16:47 +1100 Subject: [PATCH] drm/apple: v26 link: fix READY/callback race, pump busy-spin, and NULL buffer deref Fix four transport bugs in the J613/25G83 DCPLink RPC implementation: 1. Handshake READY-to-callback race and pre-alloc receive handling: dcp_v26_link_receive() previously set ready_result = 0 and completed link->ready without setting link->enabled = true under link->lock. Any firmware callback arriving before dcp_v26_link_start() woke and acquired link->lock re-entered the !link->enabled path, overwrote ready_result with -EPROTO, dropped the callback, and killed the link. Conversely, a malformed ready message did not fail the link, allowing a subsequent READY message to overwrite -EPROTO with 0. Also ignore messages arriving while link->rpc is NULL, and publish/clear link->rpc under link->lock. 2. Pump failure orphan / 5-second CPU busy-spin: When link_callback() failed in dcp_v26_link_pump(), the event was freed without removing the callback entry or failing the link. Any later dcp_v26_link_call() saw event_count == 0 and callback_count > 0, causing link_submit() to return -EBUSY and retry in a tight busy-spin for the entire 5-second call timeout. Fail the link when dcp_v26_link_pump() fails a callback, and only retry -EBUSY when event_count is non-zero. 3. NULL input/output buffer dereference in dcp_v26_link_call(): Reject non-zero input_size/output_size with a NULL pointer before copying into or out of the RPC packet. 4. Overflow-safe bounds check and checked message encoding in link_submit(): Avoid offset + size addition overflow and validate apple_dcp_link_rpc_message() / apple_dcp_link_stream_message() return values before mutating link->rpc or link->calls[nested]. Signed-off-by: Ryan Murray --- drivers/gpu/drm/apple/iomfb_v26_6_link.c | 71 ++++++--- drivers/gpu/drm/apple/iomfb_v26_6_link_test.c | 135 ++++++++++++++++++ 2 files changed, 187 insertions(+), 19 deletions(-) diff --git a/drivers/gpu/drm/apple/iomfb_v26_6_link.c b/drivers/gpu/drm/apple/iomfb_v26_6_link.c index 7301f13911121..b46eefb36a0ef 100644 --- a/drivers/gpu/drm/apple/iomfb_v26_6_link.c +++ b/drivers/gpu/drm/apple/iomfb_v26_6_link.c @@ -190,22 +190,32 @@ void dcp_v26_link_set_idle_work(struct dcp_v26_link *link, struct work_struct *w int dcp_v26_link_start(struct dcp_v26_link *link) { struct apple_dcp_link_init_desc *desc; + dma_addr_t rpc_dva = 0; + void *rpc; u64 message; int ret; - link->rpc = link_alloc_rpc(link, &link->rpc_dva); - if (!link->rpc) + rpc = link_alloc_rpc(link, &rpc_dva); + if (!rpc) return -ENOMEM; - memset(link->rpc, 0, APPLE_DCP_LINK_RPC_MEMORY_SIZE); - desc = link->rpc; + memset(rpc, 0, APPLE_DCP_LINK_RPC_MEMORY_SIZE); + desc = rpc; apple_dcp_link_init_desc_encode(desc, 0, false); /* This firmware reads the version field as the RPC alignment. */ desc->version = cpu_to_le32(DCP_V26_LINK_ALIGNMENT); - ret = apple_dcp_link_init_message(link->rpc_dva, &message); - if (ret) - goto err_free_rpc; + ret = apple_dcp_link_init_message(rpc_dva, &message); + if (ret) { + link_free_rpc(link, rpc, rpc_dva); + return ret; + } + mutex_lock(&link->lock); + link->rpc = rpc; + link->rpc_dva = rpc_dva; + link->enabled = false; link->ready_result = -ETIMEDOUT; + mutex_unlock(&link->lock); + ret = link_start_ep(link); if (ret) goto err_free_rpc; @@ -219,11 +229,14 @@ int dcp_v26_link_start(struct dcp_v26_link *link) } mutex_lock(&link->lock); - ret = link->failed ? -EIO : link->ready_result; - if (ret) - link_fail(link); + if (link->ready_result == -EPROTO) + ret = -EPROTO; else - link->enabled = true; + ret = link->failed ? -EIO : link->ready_result; + if (ret) { + link->enabled = false; + link_fail(link); + } mutex_unlock(&link->lock); if (!ret) dev_info(link->dev, "DCPLink ready, RPC alignment %d\n", DCP_V26_LINK_ALIGNMENT); @@ -231,14 +244,18 @@ int dcp_v26_link_start(struct dcp_v26_link *link) err_fail: mutex_lock(&link->lock); + link->enabled = false; link_fail(link); mutex_unlock(&link->lock); return ret; err_free_rpc: - link_free_rpc(link, link->rpc, link->rpc_dva); + mutex_lock(&link->lock); link->rpc = NULL; link->rpc_dva = 0; + link->enabled = false; + mutex_unlock(&link->lock); + link_free_rpc(link, rpc, rpc_dva); return ret; } @@ -253,16 +270,21 @@ void dcp_v26_link_receive(struct dcp_v26_link *link, u64 message) bool nested; mutex_lock(&link->lock); - if (link->failed) + if (link->failed || !link->rpc) goto out; if (!link->enabled) { /* Firmware-side INIT needs no answer; READY ends the handshake. */ if ((message & 3) == APPLE_DCP_LINK_MSG_FIRMWARE_INIT) goto out; - link->ready_result = message == DCP_V26_LINK_READY_MSG ? 0 : -EPROTO; - if (link->ready_result) + if (message == DCP_V26_LINK_READY_MSG) { + link->ready_result = 0; + link->enabled = true; + } else { dev_err(link->dev, "unexpected DCPLink ready message %#llx\n", message); + link->ready_result = -EPROTO; + link_fail(link); + } mutex_unlock(&link->lock); complete(&link->ready); return; @@ -358,10 +380,15 @@ static int link_submit(struct dcp_v26_link *link, const struct apple_dcp_link_rp ret = apple_dcp_link_stream_layout(APPLE_DCP_LINK_SIDE_AP, 0, remote, &layout); if (ret) goto out; - if (offset + size > layout.capacity) { + if (offset > layout.capacity || size > layout.capacity - offset) { ret = -EMSGSIZE; goto out; } + ret = apple_dcp_link_rpc_message(offset, size, false, &message); + if (!ret) + ret = apple_dcp_link_stream_message(message, 0, remote, &message); + if (ret) + goto out; memcpy(link->rpc + layout.local_offset + offset, rpc, size); memset(link->rpc + layout.local_offset + offset + sizeof(*rpc) + input_size, 0, output_size); @@ -370,8 +397,6 @@ static int link_submit(struct dcp_v26_link *link, const struct apple_dcp_link_rp link->calls[nested].offset = offset; link->calls[nested].remote = remote; link->calls[nested].active = true; - apple_dcp_link_rpc_message(offset, size, false, &message); - apple_dcp_link_stream_message(message, 0, remote, &message); dma_wmb(); ret = link_send_message(link, message); if (ret) { @@ -486,6 +511,11 @@ int dcp_v26_link_pump(struct dcp_v26_link *link, unsigned long timeout, return PTR_ERR(e); ret = link_callback(link, e, callback, cookie, NULL); kfree(e); + if (ret) { + mutex_lock(&link->lock); + link_fail(link); + mutex_unlock(&link->lock); + } return ret; } @@ -507,6 +537,8 @@ int dcp_v26_link_call(struct dcp_v26_link *link, u32 tag, const void *input, bool nested, replied = false; int ret; + if ((input_size && !input) || (output_size && !output)) + return -EINVAL; ret = apple_dcp_link_rpc_payload_size(input_size, output_size, &size); if (ret || size > APPLE_DCP_LINK_STREAM_BUFFER_SIZE) return -EMSGSIZE; @@ -551,7 +583,8 @@ int dcp_v26_link_call(struct dcp_v26_link *link, u32 tag, const void *input, mutex_unlock(&link->lock); ret = link_submit(link, packet, size, nested, parent); /* A callback arrived between the drain and the submit. */ - if (ret == -EBUSY && !nested && time_before(jiffies, deadline)) + if (ret == -EBUSY && !nested && READ_ONCE(link->event_count) && + time_before(jiffies, deadline)) goto retry; kfree(packet); if (ret) diff --git a/drivers/gpu/drm/apple/iomfb_v26_6_link_test.c b/drivers/gpu/drm/apple/iomfb_v26_6_link_test.c index 9757c6cdfe7da..fd016d3f25dd8 100644 --- a/drivers/gpu/drm/apple/iomfb_v26_6_link_test.c +++ b/drivers/gpu/drm/apple/iomfb_v26_6_link_test.c @@ -25,10 +25,19 @@ struct v26_link_test_ctx { unsigned int alloc_count; unsigned int free_count; bool send_bad_ready; + bool send_ready_and_cb; + bool send_bad_then_ready; + u64 ready_cb_msg; + void *ready_cb_out; }; static struct v26_link_test_ctx *v26_test_ctx; +static u64 v26_link_test_queue_cb(struct kunit *test, struct dcp_v26_link *link, + u32 tag, u8 stream, bool remote, u32 offset, + u32 in_val, u32 in_size, u32 out_size, + void **out_slot); + static void v26_link_test_cleanup_alloc(void *data) { struct v26_link_test_ctx *ctx = data; @@ -92,6 +101,29 @@ static int v26_stub_send_message(struct dcp_v26_link *link, u64 message) return 0; } + if (ctx->send_ready_and_cb && (message & 0xff) == APPLE_DCP_LINK_MSG_INIT) { + /* + * Deliver READY immediately followed by a valid firmware + * callback before dcp_v26_link_start() wakes and takes lock. + */ + dcp_v26_link_receive(link, DCP_V26_LINK_READY_MSG); + ctx->ready_cb_msg = v26_link_test_queue_cb(ctx->test, link, + DCP_V26_TAG('D', 1), + 0, false, 0, 1, 4, 4, + &ctx->ready_cb_out); + return 0; + } + + if (ctx->send_bad_then_ready && (message & 0xff) == APPLE_DCP_LINK_MSG_INIT) { + /* + * Bad handshake message followed by READY before start() wakes: + * the protocol error must stick rather than being overwritten. + */ + dcp_v26_link_receive(link, 0xbad00001ULL); + dcp_v26_link_receive(link, DCP_V26_LINK_READY_MSG); + return 0; + } + if ((message & 3) == APPLE_DCP_LINK_MSG_RPC && !(message & BIT_ULL(6))) { struct apple_dcp_link_stream_layout layout; u8 stream = apple_dcp_link_message_stream(message); @@ -338,12 +370,115 @@ static void dcp_v26_link_set_idle_work_null_test(struct kunit *test) dcp_v26_link_reset(&ctx->link); } +static void dcp_v26_link_ready_callback_race_test(struct kunit *test) +{ + struct v26_link_test_ctx *ctx = + v26_link_test_init_ctx(test, "v26-link-ready-race", false); + + ctx->alloc_dva = 0x100000; + ctx->send_ready_and_cb = true; + + KUNIT_ASSERT_EQ(test, dcp_v26_link_start(&ctx->link), 0); + KUNIT_EXPECT_FALSE(test, dcp_v26_link_failed(&ctx->link)); + KUNIT_EXPECT_TRUE(test, ctx->link.enabled); + KUNIT_EXPECT_EQ(test, ctx->link.callback_count, 1U); + KUNIT_EXPECT_EQ(test, ctx->link.event_count, 1U); + + KUNIT_EXPECT_EQ(test, dcp_v26_link_pump(&ctx->link, 0, v26_fifo_cb, ctx), 0); + KUNIT_EXPECT_FALSE(test, dcp_v26_link_failed(&ctx->link)); + KUNIT_EXPECT_EQ(test, ctx->link.callback_count, 0U); + KUNIT_ASSERT_NOT_NULL(test, ctx->ready_cb_out); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(ctx->ready_cb_out), 0x11223344U); + KUNIT_ASSERT_GE(test, ctx->sent_count, 2U); + KUNIT_EXPECT_EQ(test, ctx->sent[1], + apple_dcp_link_rpc_reply(ctx->ready_cb_msg)); +} + +static void dcp_v26_link_bad_ready_sticks_test(struct kunit *test) +{ + struct v26_link_test_ctx *ctx; + + /* Spurious READY before RPC memory exists must not arm or complete start(). */ + ctx = v26_link_test_init_ctx(test, "v26-link-prealloc-rx", false); + dcp_v26_link_receive(&ctx->link, DCP_V26_LINK_READY_MSG); + KUNIT_EXPECT_FALSE(test, ctx->link.enabled); + KUNIT_EXPECT_FALSE(test, completion_done(&ctx->link.ready)); + + /* Bad READY followed by valid READY before start() wakes must stay failed. */ + ctx = v26_link_test_init_ctx(test, "v26-link-bad-then-ready", false); + ctx->alloc_dva = 0x100000; + ctx->send_bad_then_ready = true; + KUNIT_EXPECT_EQ(test, dcp_v26_link_start(&ctx->link), -EPROTO); + KUNIT_EXPECT_TRUE(test, dcp_v26_link_failed(&ctx->link)); + KUNIT_EXPECT_FALSE(test, ctx->link.enabled); +} + +static void dcp_v26_link_pump_failure_no_spin_test(struct kunit *test) +{ + struct v26_link_test_ctx *ctx = + v26_link_test_init_ctx(test, "v26-link-pump-fail", true); + unsigned long start; + + /* Unknown tag makes v26_fifo_cb return -EINVAL; pump must fail the link. */ + v26_link_test_queue_cb(test, &ctx->link, DCP_V26_TAG('D', 99), + 0, false, 0, 0, 0, 4, NULL); + KUNIT_EXPECT_EQ(test, dcp_v26_link_pump(&ctx->link, 0, v26_fifo_cb, ctx), + -EINVAL); + KUNIT_EXPECT_TRUE(test, dcp_v26_link_failed(&ctx->link)); + + /* A subsequent outer call must return promptly instead of busy-spinning 5s. */ + start = jiffies; + KUNIT_EXPECT_EQ(test, + dcp_v26_link_call(&ctx->link, DCP_V26_TAG('A', 1), + NULL, 0, NULL, 0, 0, v26_fifo_cb, ctx), + -EIO); + KUNIT_EXPECT_LT(test, jiffies_to_msecs(jiffies - start), 1000U); + + /* + * Even if an orphaned callback slot exists with no queued events, an + * outer call must not spin until the 5-second call deadline. + */ + dcp_v26_link_reset(&ctx->link); + ctx->link.callback_count = 1; + start = jiffies; + KUNIT_EXPECT_EQ(test, + dcp_v26_link_call(&ctx->link, DCP_V26_TAG('A', 1), + NULL, 0, NULL, 0, 0, v26_fifo_cb, ctx), + -EBUSY); + KUNIT_EXPECT_LT(test, jiffies_to_msecs(jiffies - start), 1000U); + dcp_v26_link_reset(&ctx->link); +} + +static void dcp_v26_link_call_null_buf_test(struct kunit *test) +{ + struct v26_link_test_ctx *ctx = + v26_link_test_init_ctx(test, "v26-link-null-buf", true); + __le32 in_val = cpu_to_le32(0x12345678U); + + KUNIT_EXPECT_EQ(test, + dcp_v26_link_call(&ctx->link, DCP_V26_TAG('A', 1), + NULL, sizeof(in_val), NULL, 0, 0, + v26_fifo_cb, ctx), + -EINVAL); + KUNIT_EXPECT_EQ(test, + dcp_v26_link_call(&ctx->link, DCP_V26_TAG('A', 1), + &in_val, sizeof(in_val), NULL, sizeof(u32), 0, + v26_fifo_cb, ctx), + -EINVAL); + KUNIT_EXPECT_EQ(test, ctx->sent_count, 0U); + KUNIT_EXPECT_FALSE(test, dcp_v26_link_failed(&ctx->link)); +} + static struct kunit_case dcp_v26_link_cases[] = { KUNIT_CASE(dcp_v26_link_multi_callback_fifo_test), KUNIT_CASE(dcp_v26_link_nested_call_side_test), KUNIT_CASE(dcp_v26_link_start_cleanup_test), KUNIT_CASE(dcp_v26_link_callback_depth_limit_test), KUNIT_CASE(dcp_v26_link_set_idle_work_null_test), + KUNIT_CASE(dcp_v26_link_ready_callback_race_test), + KUNIT_CASE(dcp_v26_link_bad_ready_sticks_test), + KUNIT_CASE(dcp_v26_link_pump_failure_no_spin_test), + KUNIT_CASE(dcp_v26_link_call_null_buf_test), {} };