Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 B9B26439003 for ; Mon, 29 Jun 2026 16:07:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782749264; cv=none; b=OcGt/DmdlHjXhBEG1Gf/z9Jt5dP8tnWkrva6W1Wk330yvuRtm6l9RyQSFh7K2OMo+igd7larYZvJWxhmuxG6l/YQCCxgON8WAm6679jzbJ739URwotpsKVz2HDwDIL13FJ6RTdi3nLlyWxmK0NETjDGTKjVtCOOa2sa969zUwDI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782749264; c=relaxed/simple; bh=Y5sS/gzhsuQ/vBRSgQyhETeNqXu9nJ0bh5jxmit9nFc=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=HlsWut8g7Bqw8ie3olPYl+HTDnJfOjCW7f9eRO54BfwudhNYv3xkjETj21ulZGSxQXnQi/RElv0ktv0Kk57TzSITu2orUtT5ZT1Pbh8dWPtUdrABYVFCn3xEEgr4C3JtWxuP36xZif67wPRDlQYY5vnrsmIK2rpcH92VsqZF31s= 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=qQaXU9Yk; arc=none smtp.client-ip=209.85.128.52 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="qQaXU9Yk" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-49395888c7bso28977185e9.0 for ; Mon, 29 Jun 2026 09:07:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1782749261; x=1783354061; 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; bh=djAVDcWLQFHEcc6I7ketatyL6bgqYSNQerXOdvm3+eg=; b=qQaXU9YkR5LIf3j9ssKBGeia/k0wTmuW82UuYh6LbB+YuuF3s3k5oKqbEWuQYAYdM4 9AICQUJ9rRlq2tlLg2lYAG5lvg6mNaVAtuxyGOuX5aTFfCsv6gu+FBhC9Hf6iohldfQr tuVxd1oazuwLHM8URZiyADXRAfCIkDp0dEcoMf5iSsN2R+AJc0DekASFVrHFKeIDsK5B JnwIqtha1XmMslxAZet95JWx2OFGe2VF2ZC6g65l7YCsuwS0VWOvBywloveRARFaJ3X2 i+/jprkRX6ZSbfd83SrcZUQDcAArpY9k57quK83K135XJHpKe3HTmbUnM0aIme1fsDMU eroA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782749261; x=1783354061; 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; bh=djAVDcWLQFHEcc6I7ketatyL6bgqYSNQerXOdvm3+eg=; b=sVhxPt1SsvdHpfio2r3bN7QGpyB7kLp6OPBa903To4QdtkEylN7nKJ5GBOGoGAaYRJ KS7wsgWZhlGh9iXIPK1pc3kiSlQBq7dGAr6TVGyo2VdaKUAln+Z8LuNio+IV2Vgt/Jr2 uEYIFs5bBjJUt3aagSoB4KBOnRR7OF7I0seY2UnUssWUB8Pi0O3x4zHtf/QFQ5c1dXyA OWG6STPIk5i5V6kxDfHmH82/f+EA5sjfM8sQkNuUFw8Q5lEvsNFxaKcSyVBObs7QzeQt Uqgmk/S6X58t1/O7DmQjQAUuMcPHNTvW5AakX4iNiMnOaJ/HOqNtGGfc/+DDDfRIOq7F ASTg== X-Forwarded-Encrypted: i=1; AFNElJ/Dor9hkE6seZBcPgr+A2IoVqDBynSR52SAzVdYwaHAlFnj5qdWcxbHnKGtZCqU/q+5yNCTM/rmMoyfbRY=@vger.kernel.org X-Gm-Message-State: AOJu0Yyv0v45nypTXQvFse6qnrqjk2wxYZ/1b9m3GdiKrJ9dfFFr1mvG pKGhvWK1RI4z8fDsUXriifhXrSrSUQyu1lAsXpW2+vqqzuSmzB3giKNH X-Gm-Gg: AfdE7cnNXccU7gDWmN/9E5GctIj6z0LI4aCurrlbTZ0ijxGEVMABzHmIMk2gaNuNJ6N XW4lMTCxI4e8PMEjXavgEFuiXd2Orkjk9295HO+C0DoixIZ1ZSPX5QpN7eQger+NWD7dTGExxw3 7+fQ9Tdf6Htg34AXIGCDE1M5ABFOSLbVsnFX/I34UsuDzQg3ro+8zVECyXlOb0LAcsyI33akUVQ cSoQsXD9fSI3YhRaFa/FXqlF1QEd7owE77F9O+a3cmShNk1mG5+XDUyDtlX+mAxEZQ4bbtyNWUx ols+F+e+DqDV1Kor0wKj8xmUXhCTdrMQuchwOyESrK8LCpzUjl5zWJHeLvOd+r6T4YhDK1lfUuR GV3vY6UnTIC0Q7ArFGUW0sb0yg6omJ/Ftd345U62c5+sPW0SIcyJXpdeRn5wWiDhynnwUu7BgOd GSI4XZZJXSyznJWfp4tgVLQ4MMww== X-Received: by 2002:a05:600c:6308:b0:493:adc3:cced with SMTP id 5b1f17b1804b1-493b82c486fmr4665785e9.34.1782749260862; Mon, 29 Jun 2026 09:07:40 -0700 (PDT) Received: from Dev-Null-MSI ([2a0d:3344:52ac:a808:98a4:4381:be45:536f]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-474e31a7d82sm2571086f8f.1.2026.06.29.09.07.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jun 2026 09:07:40 -0700 (PDT) From: Yousef Alhouseen To: Scott Branden Cc: Broadcom internal kernel review list , Arnd Bergmann , Greg Kroah-Hartman , linux-kernel@vger.kernel.org, Yousef Alhouseen Subject: [PATCH] misc: bcm-vk: validate write size before allocation Date: Mon, 29 Jun 2026 18:06:05 +0200 Message-ID: <20260629160605.29412-1-alhouseenyousef@gmail.com> X-Mailer: git-send-email 2.54.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 bcm_vk_write() uses the user-supplied write count to size a flexible-array work entry and then copies count bytes into that array. The allocation expression is evaluated before any overflow check, so a very large count can wrap the allocation smaller than the subsequent copy. Reject empty writes, check the allocation arithmetic before kzalloc(), and initialize the __counted_by field before copying into to_v_msg[]. Signed-off-by: Yousef Alhouseen --- drivers/misc/bcm-vk/bcm_vk_msg.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/drivers/misc/bcm-vk/bcm_vk_msg.c b/drivers/misc/bcm-vk/bcm_vk_msg.c index 3916ec07ecad..3cd1e9d80c55 100644 --- a/drivers/misc/bcm-vk/bcm_vk_msg.c +++ b/drivers/misc/bcm-vk/bcm_vk_msg.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -1088,6 +1089,7 @@ ssize_t bcm_vk_write(struct file *p_file, u32 q_num; u32 msg_size; u32 msgq_size; + size_t entry_size; if (!bcm_vk_drv_access_ok(vk)) return -EPERM; @@ -1095,20 +1097,26 @@ ssize_t bcm_vk_write(struct file *p_file, dev_dbg(dev, "Msg count %zu\n", count); /* first, do sanity check where count should be multiple of basic blk */ - if (count & (VK_MSGQ_BLK_SIZE - 1)) { - dev_err(dev, "Failure with size %zu not multiple of %zu\n", + if (!count || count & (VK_MSGQ_BLK_SIZE - 1)) { + dev_err(dev, "Failure with size %zu not a positive multiple of %zu\n", count, VK_MSGQ_BLK_SIZE); rc = -EINVAL; goto write_err; } + if (check_add_overflow(sizeof(*entry), count, &entry_size) || + check_add_overflow(entry_size, vk->ib_sgl_size, &entry_size)) { + rc = -EOVERFLOW; + goto write_err; + } + /* allocate the work entry + buffer for size count and inband sgl */ - entry = kzalloc(sizeof(*entry) + count + vk->ib_sgl_size, - GFP_KERNEL); + entry = kzalloc(entry_size, GFP_KERNEL); if (!entry) { rc = -ENOMEM; goto write_err; } + entry->to_v_blks = count >> VK_MSGQ_BLK_SZ_SHIFT; /* now copy msg from user space, and then formulate the work entry */ if (copy_from_user(&entry->to_v_msg[0], buf, count)) { @@ -1116,7 +1124,6 @@ ssize_t bcm_vk_write(struct file *p_file, goto write_free_ent; } - entry->to_v_blks = count >> VK_MSGQ_BLK_SZ_SHIFT; entry->ctx = ctx; /* do a check on the blk size which could not exceed queue space */ @@ -1353,4 +1360,3 @@ void bcm_vk_msg_remove(struct bcm_vk *vk) bcm_vk_drain_all_pend(&vk->pdev->dev, &vk->to_v_msg_chan, NULL); bcm_vk_drain_all_pend(&vk->pdev->dev, &vk->to_h_msg_chan, NULL); } - -- 2.54.0