From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ej1-f44.google.com (mail-ej1-f44.google.com [209.85.218.44]) (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 62E1F397E81 for ; Fri, 17 Jul 2026 15:06:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784300790; cv=none; b=fzbGXDDfj/40iQxsTONqzauW8HwxtB7tPx9RMJIRltk2FrvoD8EP040PTLD2KywqLfiRpyFQUtqPP6mUOyIHyNQxD0SydtxxQxiGPonS+PMQeD4+lNTn7nUGw8ou2I6Iwc24M+w7k0hS0XsEeLLg7T/mTbMoERj9EmhkJmpyj/Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784300790; c=relaxed/simple; bh=t8k7v3qPLzWUekqdj32SHdLbPhJMgxMvTA4A8BZ+joo=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=j3Da+d/ixLg8DnsTXksBrzxq7MLCiw8xUneTC8bkSHLY37lFxDyXC1cCu1WYGEeNbKm+G3w9TOq8XdcFOvg7wCrHu6h/C6BcPmLIB+k8Bb+oqyIGeh9AZkbHeX6L17p/d7PK+Ikp+8tXxAoKIsxv1xLrrHlJIG097budunU8eQA= 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=gvPZ1luE; arc=none smtp.client-ip=209.85.218.44 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="gvPZ1luE" Received: by mail-ej1-f44.google.com with SMTP id a640c23a62f3a-c1712a04ddaso133768066b.2 for ; Fri, 17 Jul 2026 08:06:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784300786; x=1784905586; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=i1nXx/Bw8UKp/4mvBIV9E4y7f+3yDloK/9473BWu9mc=; b=gvPZ1luE7NEltsJGA2t+UN5rbWhr2g6RfvTqleCta3BdBCxzg9DuLjQU0rraf+/xXh 31bl1C2DBoJjktsxgJcv+lnNXCxDhgUO/QXOnW3K36K/ruWsv8BaA+wZt2C4sGBWmOr9 fMLxYcbpKCK+dhzBjrU+NAmSdZzMyo5KSb0cFd+nUbwSGeOYHH1Nt0n/6GiEvd9ODrC8 wUzbiY/PqvEXJaxOSvBHYIviAF+zwXoIxCGm0mvcYarJ2U25OYPNt80zkVe2XsHpVBzu FdO34HgAnsYFZlLEOht3kGc7cKbpp24fOMltGSz7NSP4TYeOURBeEgqjLea38xzYpTj7 Kmyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784300786; x=1784905586; h=content-transfer-encoding:mime-version: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=i1nXx/Bw8UKp/4mvBIV9E4y7f+3yDloK/9473BWu9mc=; b=XlipE0rLUXUY1eQqGnzXFd1AbIa7s1GHIBQsGNVAK+5tW47z3/e1K/4QlyC/n9nmp6 6Yt33j/f5ZAJQqU5DSvFoRKNTmxTWP1Jf4Wpf7f6OxLDDHYhL3EUrNgiTalZinCKSk0i tV5tmj9cMR7i0oLKUJsCUiw4hZkEYcgGUMtbK3qbhyh2sD4jcXYX04OKDWTLzPvU4XZA mkaZLu6vAo2jznlwYlJC/RnGIKo3NT0GlSZfTFNfxlqytMjxAa2MiB6IyGyZnU1juJhd iQ4XK2E13mhlIplLegLpel/MY8FeAX1Ut5j0WrJdVjSi9NXrYWpw0INbGWhhRqnxSf7K 98Nw== X-Forwarded-Encrypted: i=1; AHgh+RorZ7Qhj0D/F7RtKrrmIvnhVH35LeYXWopPEaF1o3h1aE8esap//KRNTZcYBxJX/VtnUrT+MkK24/3P47c=@vger.kernel.org X-Gm-Message-State: AOJu0Ywkgnszgcwn7h2Yl85xcqZ9et4Yaa98caN4BMC9tIVFaZYPNIlc HsS59vz1aSrbRW+RoZTkmXfyeo0aG9rU3OWUw0+xMo3bZnTi5U9xK45r X-Gm-Gg: AfdE7ckHfoeWOLJ2UPd+qdCXE9Rl0h/WSvwASLvpdq/wp14An6NvDcVHlZxAWG9eN9Z SJKTg5YoqFqvvNieOPYTFAWrILosQuoj9rD5wuGOJds3qcZqscmuZGU8lnYs/PWO8o0xEoWYjV4 wu9Oa/IszBn77V5011mB4ZuMMYvgBw63iX51J3syRz9691TPWaETK09qlisil/0sf9P3SP0OqnM i+urPFfkZCJSFeOJbt71nv9wtr7F7gYHPj3arssNJO7EEI0rB6hIY65UTd+EljoSHH3fQJll8vd LETILjdczaijxn1LSrOA1HcUX7aoX2l6ixhtpn8l+vGwbmubuZnnzhnnluqM0knTK6TFsKVUDfc NxhmZiAe19XCjc+HRemj/e4bOrj/rT4oFudcVMiUti/Vcpp1fPCRMYy4LAtKG5w/LWgio3R2JVW UE7cOnShZI9w/zH0WCA6EJ5PvoyC/NDJVlSUe85ciMFmJ4cP0PWxHLo9jLj2B6barQWg== X-Received: by 2002:a17:907:25c4:b0:c12:68f2:af4 with SMTP id a640c23a62f3a-c16b4866b46mr133720766b.54.1784300786177; Fri, 17 Jul 2026 08:06:26 -0700 (PDT) Received: from misharu.home (2a02-a463-a071-0-5347-776a-5ef9-a909.fixed6.kpn.net. [2a02:a463:a071:0:5347:776a:5ef9:a909]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-c1712a5fa2fsm89116066b.31.2026.07.17.08.06.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jul 2026 08:06:25 -0700 (PDT) From: Hari Mishal To: Amit Shah Cc: Arnd Bergmann , Greg Kroah-Hartman , virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, Hari Mishal Subject: [PATCH] virtio_console: take a kref in find_port_by_vq() to fix port UAF Date: Fri, 17 Jul 2026 17:06:22 +0200 Message-ID: <20260717150622.23636-1-harimishal1@gmail.com> X-Mailer: git-send-email 2.43.0 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 --- 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