From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ej1-f45.google.com (mail-ej1-f45.google.com [209.85.218.45]) (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 8317B390CB8 for ; Wed, 15 Jul 2026 14:42:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784126581; cv=none; b=VGPWvIZ6nU7rQM99wJVx5jZnjuRUilV/DFaPGGTFRaQj73dXTChxgYFuiNft91W7cu7ivMHbQJccSujVTzOtutD/dRiuzvqsJeuzEow7he47R59UpukKA95KYiHl55Z7g1njNA16RUnvdcJvGx+gXkSF1EFK0IaPegs7m0Y+Nb0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784126581; c=relaxed/simple; bh=GiLQK2P9mz7B1AOLw3zNKl/JkrN5OSok2azGQMRbFRw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OHaHqfm48XtYn55JB5v8Ji+ZLQgNRCmCohCNUzv9VQS7tkm/Iq+lfLop23XHLc9wdT5i989OaLV7IvzKlb7XE40Iem7oz/4pww9AUoLGla33SapPxlvMtqRjDBUAPBtiX1SkdtG9dFRsF9DPFMsTvgHdFAfDlwNzzQ3ukMYzVXE= 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=jHxlM8y2; arc=none smtp.client-ip=209.85.218.45 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="jHxlM8y2" Received: by mail-ej1-f45.google.com with SMTP id a640c23a62f3a-c15e592da74so681677566b.1 for ; Wed, 15 Jul 2026 07:42:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784126578; x=1784731378; 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=kSbTXzpQvZ2uVegQIg8zTuF9MAlBVbm4zZyffa3w32E=; b=jHxlM8y2S8S6GQ/HEN9GIkoz2K66RxUcmDhZ95QDjWZlyPTB8648vswy3TCQcZUecy JCY/WHpRvKkRuVYGY64q69/O22vZ3QGX8Axs+9QpO0OWzMDt3K2GozgYCfLWWTE7IbuR LHiDadM6E2onOo7VWh/PkVBJegMQY6UCkzQg87umZTcNQr69QZwBeIeise5x3gzr2Y7e UMgBb3EMeY+3OAJiJgEzaMkQjht9j6E9JgF/kwxR91ENwHVdiqVzsbjRZVm4T6/OyQSu y2cCgtJAxaADVT36HMyha9KNiAX8dQwq20xi1rJP9S1QF/dhCxWzPHMPCJiO/GO8H0LN Z9ZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784126578; x=1784731378; 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=kSbTXzpQvZ2uVegQIg8zTuF9MAlBVbm4zZyffa3w32E=; b=PnQOrAClmOCUxQgE6WsTnEE7aFGoXKSiB/N6dvSQyRDv8fcyzadYr5azfVnNzQ0Ted 8XrJHpXwom6oRSk/nDu+MetZjRUbB0ZSe78JeofsXA4Zym+4wRLALZ3p4knWBMwwtwAc VX+FDT0AF2kuj+3Ba0Dl1d4xuQldKVg+1kL55o+15YhTVkdYTnC9mMjGlp5iVF0Ie2n1 iwTVSwGquL65N+sWnjNxJ+3rSUdOqDy9S6uvZThEx2eoycFsRFpBUFN2BPt4KI5FhuaW wRNV+U7iVU8PZGlTPa4ulv4SknrgNz55K8SNLRcsJa2fLQJR7QCltEcTlppuyExay+uz tiSw== X-Forwarded-Encrypted: i=1; AHgh+Rq4tE8VMXYP1eUj2H33EqNBK1plHhTkifGPaBOf5Rc2m6nErAdddXGREJSDo1Jg7uNPaf5InCu79FI51C0=@vger.kernel.org X-Gm-Message-State: AOJu0YyN467kLUOOzVZ5Xq1IoElLMRH2T0BLKTfQoOdMmNDx07vmdHlq WZWetxfovzkPzpXjQrGhWSQc451QcIArmKDaPWs7bLfe+5TvhU+w6+5T X-Gm-Gg: AfdE7cno++bElyL5KUuS8OqMh6uRSfNG1YJFJ4T7KblqQUMmPz3C51lvn3hXVPJkwhm YOErheHjDNu47V05jkTyfGCBEBegxY8JaRG2oH1kbabs85zuCM+XzIDAo5dlp1/WgTKygQitYAx PEYpGCCnkAU/5QQVymRo5Av/0+YQPNgKxHiV6aswKAVviIGtot5G5oU9ZIFDMrfrhyI6fPybl4b Y/UlRfBChkSatg7jyLhqN2lNuWqWfKVEx70D+nA9WxuvaxVjygxFSaVnkrwqtUe0wemm6ucXVCK oXx+dmLAG3RRQl6m0YPTrYtTVNtc8wvkBUGPEIcg7ofeTyNzGgtgdaEqGEMMkZmwRo47JIEfo6L 4KW8hw1rsqa1AoEC4XhKyjB3ZjFL9ipM0V8MZoZ/VGtl6J8BvlWBjYXgNuafimGqvN/OUEacTbf AhGpMgjP67yLOXuxLbQbW318hnokaHnULT0PE3FTBOgAUWNDc/AbeRzQf05psKq/mqFQO+1sZp7 TGE X-Received: by 2002:a17:906:4792:b0:c15:dbc7:a6c1 with SMTP id a640c23a62f3a-c1678fb5229mr227335666b.0.1784126577566; Wed, 15 Jul 2026 07:42:57 -0700 (PDT) Received: from misharu.home (2a02-a463-a071-0-8f3f-3afb-28f6-19c0.fixed6.kpn.net. [2a02:a463:a071:0:8f3f:3afb:28f6:19c0]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-c1687125db5sm50849066b.12.2026.07.15.07.42.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jul 2026 07:42:56 -0700 (PDT) From: Hari Mishal To: Amit Shah , Arnd Bergmann , Greg Kroah-Hartman Cc: virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, Hari Mishal Subject: [PATCH v2 4/4] virtio_console: take a kref in find_port_by_vq() to fix port UAF Date: Wed, 15 Jul 2026 16:42:43 +0200 Message-ID: <20260715144254.26156-1-harimishal1@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260715142337.22811-5-harimishal1@gmail.com> References: <20260715142337.22811-5-harimishal1@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 find_port_by_vq() returns a raw struct port pointer without taking a reference on it, unlike find_port_by_devt_in_portdev() which does. find_port_by_vq()'s only two callers, in_intr() and out_intr(), run as virtqueue interrupt callbacks, entirely independent of and possibly concurrently with unplug_port(), which itself runs from a workqueue when the host sends a VIRTIO_CONSOLE_PORT_REMOVE control message. unplug_port() removes the port from portdev->ports under ports_lock, then later drops its last reference with kref_put(), freeing it via remove_port(). find_port_by_vq() also walks portdev->ports under ports_lock, so if it finds the port still on the list, the list removal, and therefore the eventual kref_put(), has not happened yet, and taking a reference at that point is always safe. Without doing so, in_intr()/out_intr() can be left holding a pointer to a port that unplug_port() frees on another core before they are done using it. Both triggers are host-controlled as the host decides when to send the PORT_REMOVE control message and when to kick the port's data vq. So a malicious backend could race the two on purpose, without any guest side cooperation. The freed object is a generic kmalloc allocation containing a wait_queue_head_t, which in_intr()/out_intr() pass to wake_up_interruptible() after touching the stale pointer. wake_up_interruptible() invokes a function pointer read out of the wait queue's entries. If the freed slab slot is reclaimed with attacker influenced content before that call, then this is an arbitrary function call primitive rather than just undefined behaviour. Take a reference in find_port_by_vq() while still holding ports_lock, matching find_port_by_devt_in_portdev(), and release it in in_intr() and out_intr() once they are done with the port. Signed-off-by: Hari Mishal --- v2: reworded the comment above find_port_by_vq() for clarity, no functional change since v1. drivers/char/virtio_console.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/drivers/char/virtio_console.c b/drivers/char/virtio_console.c index faef362dae85..1b63afe24e29 100644 --- a/drivers/char/virtio_console.c +++ b/drivers/char/virtio_console.c @@ -304,6 +304,12 @@ static struct port *find_port_by_id(struct ports_device *portdev, u32 id) return port; } +/* + * Finds a port by the virtqueue and returns a pointer to struct port + * with the reference count incremented. + * + * Callers MUST decrement it when finished. + */ static struct port *find_port_by_vq(struct ports_device *portdev, struct virtqueue *vq) { @@ -312,8 +318,10 @@ static struct port *find_port_by_vq(struct ports_device *portdev, spin_lock_irqsave(&portdev->ports_lock, flags); list_for_each_entry(port, &portdev->ports, list) - if (port->in_vq == vq || port->out_vq == vq) + if (port->in_vq == vq || port->out_vq == vq) { + kref_get(&port->kref); goto out; + } port = NULL; out: spin_unlock_irqrestore(&portdev->ports_lock, flags); @@ -1706,6 +1714,7 @@ static void out_intr(struct virtqueue *vq) } wake_up_interruptible(&port->waitqueue); + kref_put(&port->kref, remove_port); } static void in_intr(struct virtqueue *vq) @@ -1723,6 +1732,7 @@ static void in_intr(struct virtqueue *vq) if (!port->portdev) { /* Port is being unplugged, ignore further data. */ spin_unlock_irqrestore(&port->inbuf_lock, flags); + kref_put(&port->kref, remove_port); return; } port->inbuf = get_inbuf(port); @@ -1756,6 +1766,8 @@ static void in_intr(struct virtqueue *vq) if (is_console_port(port) && hvc_poll(port->cons.hvc)) hvc_kick(); + + kref_put(&port->kref, remove_port); } static void control_intr(struct virtqueue *vq) -- 2.43.0