Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] idpf: psp: race with hard reset in idpf_psp_rx_spi_alloc()
- [Low] idpf: psp: wrong reset type in vport_id kernel-doc comment
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [Medium] idpf: memory leak of tx_tstamp_caps on RSS error paths in
  idpf_vport_alloc() [1]
--

commit 579b4b79a623f0ec9c780b3ed4bd922d42ad422e
Author: Stan Iliev <[email protected]>

idpf: psp: add support for psp rx key allocation

This commit adds virtchnl definitions and driver support for allocating
PSP V0 RX keys on demand via the rx_spi_alloc PSP device operation.

> diff --git a/drivers/net/ethernet/intel/idpf/idpf_lib.c 
> b/drivers/net/ethernet/intel/idpf/idpf_lib.c
> index 1385209519b25..062753a62f593 100644
> --- a/drivers/net/ethernet/intel/idpf/idpf_lib.c
> +++ b/drivers/net/ethernet/intel/idpf/idpf_lib.c
> @@ -1144,6 +1144,9 @@ static struct idpf_vport *idpf_vport_alloc(struct 
> idpf_adapter *adapter,
>       /* fill vport slot in the adapter struct */
>       adapter->vports[idx] = vport;
>       adapter->vport_ids[idx] = idpf_get_vport_id(vport);
> +     /* Copy for the PSP requests that can run while the vports are freed */
> +     if (IS_ENABLED(CONFIG_INET_PSP) && !idx)
> +             WRITE_ONCE(adapter->psp.vport_id, adapter->vport_ids[idx]);

[Severity: Medium]
This problem wasn't introduced by this patch, but does idpf_vport_alloc()
leak vport->tx_tstamp_caps [1] if RSS initialization fails before
adapter->vports[idx] is assigned?

Earlier in idpf_vport_alloc(), idpf_vport_init() calls
idpf_ptp_get_vport_tstamps_caps(), which allocates vport->tx_tstamp_caps
and populates its latches_free list.  If allocating rss_data->rss_key or
idpf_init_rss_lut() then fails, the error path jumps to
deinit_vport_queues or free_rss_key and frees vport without releasing
vport->tx_tstamp_caps, and the caller's cleanup via idpf_ptp_release()
also misses it because adapter->vports[idx] is still NULL:

drivers/net/ethernet/intel/idpf/idpf_lib.c:idpf_vport_alloc() {
    ...
        err = idpf_vport_init(vport, max_q);
        if (err)
                goto free_vport;
    ...
        rss_data->rss_key = kzalloc(rss_data->rss_key_size, GFP_KERNEL);
        if (!rss_data->rss_key)
                goto deinit_vport_queues;
    ...
        err = idpf_init_rss_lut(vport, rss_data);
        if (err)
                goto free_rss_key;

        /* fill vport slot in the adapter struct */
        adapter->vports[idx] = vport;
    ...
free_rss_key:
        kfree(rss_data->rss_key);
        rss_data->rss_key = NULL;
deinit_vport_queues:
        idpf_vport_deinit_queue_reg_chunks(adapter->vport_config[idx]);
free_vport:
        kfree(vport);
}

Calling idpf_ptp_release_vport_tstamp(vport) on the deinit_vport_queues
error path would avoid leaking the timestamp capability structure and its
latch entries.

> diff --git a/drivers/net/ethernet/intel/idpf/idpf_psp.c 
> b/drivers/net/ethernet/intel/idpf/idpf_psp.c
> index 67f1f1d7508cf..8ce390bcd6178 100644
> --- a/drivers/net/ethernet/intel/idpf/idpf_psp.c
> +++ b/drivers/net/ethernet/intel/idpf/idpf_psp.c

[ ... ]

> @@ -136,9 +139,49 @@ static int idpf_psp_set_config(struct psp_dev *psd, 
> struct psp_dev_config *conf,
>       return 0;
>  }
>  
> +/**
> + * idpf_psp_rx_spi_alloc - Allocate a PSP RX SPI and key
> + * @psd: PSP device
> + * @version: PSP version
> + * @key: filled with the allocated SPI and key
> + * @extack: netlink extended ack for error reporting
> + *
> + * Allocate one RX key with a synchronous VIRTCHNL2_OP_PSP_ALLOCATE_KEYS
> + * request.
> + *
> + * Return: 0 on success, negative error code on failure.
> + */
> +static int idpf_psp_rx_spi_alloc(struct psp_dev *psd, u32 version,
> +                              struct psp_key_parsed *key,
> +                              struct netlink_ext_ack *extack)
> +{
> +     struct idpf_adapter *adapter = idpf_netdev_to_adapter(psd->main_netdev);
> +     struct idpf_psp_rx_key rx_key;
> +     int err;
> +
> +     /* Only version 0 keys are allocated, see idpf_psp_register() */
> +     if (psp_key_size(version) != sizeof(rx_key.key)) {
> +             NL_SET_ERR_MSG_MOD(extack, "Unsupported PSP version");
> +             return -EOPNOTSUPP;
> +     }
> +
> +     err = idpf_send_allocate_psp_key(adapter, &rx_key);

[Severity: High]
Can idpf_psp_rx_spi_alloc() race with a hard reset and trigger a
use-after-free or NULL pointer dereference on the default mailbox queues?

idpf_psp_rx_spi_alloc() holds psd->lock, but does not acquire
adapter->vport_ctrl_lock or check idpf_is_reset_in_prog().  When a hard
reset runs, idpf_deinit_dflt_mbx() calls libie_ctlq_xn_deinit(), which
frees adapter->xnm, adapter->asq and adapter->arq before setting those
pointers to NULL:

CPU0                                  CPU1
idpf_init_hard_reset()                psp_nl_rx_assoc_doit()
  idpf_vport_ctrl_lock(adapter)         idpf_psp_rx_spi_alloc()
  idpf_vc_core_deinit(adapter)            idpf_send_allocate_psp_key()
  idpf_deinit_dflt_mbx(adapter)             idpf_send_mb_msg()
    libie_ctlq_xn_deinit(...)                 idpf_is_reset_detected()
      kfree(adapter->arq)                       readl(adapter->arq->...)
                                                  <-- use-after-free
    adapter->arq = NULL

If idpf_send_mb_msg() passes idpf_is_reset_detected() just before
idpf_deinit_dflt_mbx(), or during idpf_init_dflt_mbx() after adapter->arq
is assigned but before adapter->xnm is set, idpf_mb_clean() and
libie_ctlq_xn_send() can also dereference a freed or NULL adapter->asq or
adapter->xnm.

In addition, neither idpf_psp_rx_spi_alloc() nor
idpf_send_allocate_psp_key() checks idpf_is_psp_ena(adapter), so once
idpf_init_dflt_mbx() finishes during a reset, VIRTCHNL2_OP_PSP_ALLOCATE_KEYS
can be sent with a stale vport_id before vport recreation, or after
idpf_psp_init() has disabled PSP offload.

> diff --git a/drivers/net/ethernet/intel/idpf/idpf_psp_def.h 
> b/drivers/net/ethernet/intel/idpf/idpf_psp_def.h
> index 0829d0bbd92b2..f21bd7de5ae8c 100644
> --- a/drivers/net/ethernet/intel/idpf/idpf_psp_def.h
> +++ b/drivers/net/ethernet/intel/idpf/idpf_psp_def.h
> @@ -19,6 +32,10 @@
>   * @saidx_width: SA index width granted by VIRTCHNL2_OP_CRYPTO_GET_CAPS
>   * @psp_caps: PSP capabilities granted by VIRTCHNL2_OP_PSP_GET_CAPS, see
>   *         enum virtchnl2_psp_caps
> + * @vport_id: ID of adapter->vports[0] for the PSP requests. A copy, as the
> + *         PSP device stays registered while the vports are freed and
> + *         allocated again during a soft reset. Set in idpf_vport_alloc().

[Severity: Low]
This isn't a bug, but vports are freed and reallocated via
idpf_vport_alloc() during a hard reset rather than a soft reset
(idpf_initiate_soft_reset() only reallocates queue resources on the
existing vport).

[1] https://sashiko.dev/#/bug/linux-9b904fce-205d-411e-be8c-4b18ed5978ec

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=8

Reply via email to