From: Kees Cook <kees@kernel.org>
To: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Cc: Marc Kleine-Budde <mkl@pengutronix.de>,
linux-can@vger.kernel.org,
Thomas Kopp <thomas.kopp@microchip.com>,
Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>,
"Gustavo A . R . Silva" <gustavoars@kernel.org>,
netdev@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: Re: [PATCH 1/2] can: peak_canfd: decorate pciefd_board.can with __counted_by()
Date: Mon, 10 Jun 2024 11:34:40 -0700 [thread overview]
Message-ID: <202406101128.C7BEF1F@keescook> (raw)
In-Reply-To: <20240609045419.240265-2-mailhol.vincent@wanadoo.fr>
On Sun, Jun 09, 2024 at 01:54:18PM +0900, Vincent Mailhol wrote:
> A new __counted_by() attribute was introduced in [1]. It makes the
> compiler's sanitizer aware of the actual size of a flexible array
> member, allowing for additional runtime checks.
>
> Move the end of line comments to the previous line to make room and
> apply the __counted_by() attribute to the can flexible array member of
> struct pciefd_board.
>
> [1] commit dd06e72e68bc ("Compiler Attributes: Add __counted_by macro")
> Link: https://git.kernel.org/torvalds/c/dd06e72e68bc
>
> CC: Kees Cook <kees@kernel.org>
> Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
> ---
> drivers/net/can/peak_canfd/peak_pciefd_main.c | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/net/can/peak_canfd/peak_pciefd_main.c b/drivers/net/can/peak_canfd/peak_pciefd_main.c
> index 1df3c4b54f03..636102103a88 100644
> --- a/drivers/net/can/peak_canfd/peak_pciefd_main.c
> +++ b/drivers/net/can/peak_canfd/peak_pciefd_main.c
> @@ -190,8 +190,10 @@ struct pciefd_board {
> void __iomem *reg_base;
> struct pci_dev *pci_dev;
> int can_count;
> - spinlock_t cmd_lock; /* 64-bits cmds must be atomic */
> - struct pciefd_can *can[]; /* array of network devices */
> + /* 64-bits cmds must be atomic */
> + spinlock_t cmd_lock;
> + /* array of network devices */
> + struct pciefd_can *can[] __counted_by(can_count);
> };
>
> /* supported device ids. */
You'll need to adjust the code logic that manipulates "can_count", as
accesses to "can" will trap when they're seen as out of bounds. For
example:
pciefd = devm_kzalloc(&pdev->dev, struct_size(pciefd, can, can_count),
GFP_KERNEL);
...
/* pciefd->can_count is "0" now */
while (pciefd->can_count < can_count) {
...
pciefd_can_probe(pciefd);
/* which does: */
pciefd->can[pciefd->can_count] = priv; /// HERE
...
pciefd->can_count++;
}
The access at "HERE" above will trap: "can" is believed to have
"can_count" elements (0 on the first time through the loop).
This needs to be adjusted to increment "can_count" first.
Perhaps:
diff --git a/drivers/net/can/peak_canfd/peak_pciefd_main.c b/drivers/net/can/peak_canfd/peak_pciefd_main.c
index 1df3c4b54f03..df8304b2d291 100644
--- a/drivers/net/can/peak_canfd/peak_pciefd_main.c
+++ b/drivers/net/can/peak_canfd/peak_pciefd_main.c
@@ -676,7 +676,8 @@ static int pciefd_can_probe(struct pciefd_board *pciefd)
spin_lock_init(&priv->tx_lock);
/* save the object address in the board structure */
- pciefd->can[pciefd->can_count] = priv;
+ pciefd->can_count++;
+ pciefd->can[pciefd->can_count - 1] = priv;
dev_info(&pciefd->pci_dev->dev, "%s at reg_base=0x%p irq=%d\n",
ndev->name, priv->reg_base, ndev->irq);
@@ -800,8 +801,6 @@ static int peak_pciefd_probe(struct pci_dev *pdev,
err = pciefd_can_probe(pciefd);
if (err)
goto err_free_canfd;
-
- pciefd->can_count++;
}
/* set system timestamps counter in RST mode */
--
Kees Cook
next prev parent reply other threads:[~2024-06-10 18:34 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-09 4:54 [PATCH 0/2] can: treewide: decorate flexible array members with __counted_by() Vincent Mailhol
2024-06-09 4:54 ` [PATCH 1/2] can: peak_canfd: decorate pciefd_board.can " Vincent Mailhol
2024-06-10 18:34 ` Kees Cook [this message]
2024-06-09 4:54 ` [PATCH 2/2] can: mcp251xfd: decorate mcp251xfd_rx_ring.obj " Vincent Mailhol
2024-06-10 18:44 ` Kees Cook
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=202406101128.C7BEF1F@keescook \
--to=kees@kernel.org \
--cc=gustavoars@kernel.org \
--cc=linux-can@vger.kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=mailhol.vincent@wanadoo.fr \
--cc=manivannan.sadhasivam@linaro.org \
--cc=mkl@pengutronix.de \
--cc=netdev@vger.kernel.org \
--cc=thomas.kopp@microchip.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).