Linux SCSI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Benjamin Saiz" <benj.saiz@gmail.com>, linux-scsi@vger.kernel.org
Subject: Re: [PATCH RFC] scsi: aacraid: free aborted management FIBs on late completion
Date: Tue, 24 Nov 2026 11:18:43 +0000	[thread overview]
Message-ID: <sashiko-outbox-231035@kernel.org> (raw)
In-Reply-To: <20261124110444.3485-1-benj.saiz@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] scsi: aacraid: race on fib->done in aac_hba_send() and
  aac_intr_normal()
- [Medium] scsi: aacraid: double free on -ERESTARTSYS in
  aac_send_safw_bmic_cmd()
--

commit bdfff41a3eec2c252b35e5b8c632cb03cf063779
Author: Benjamin Saiz <benj.saiz@gmail.com>

scsi: aacraid: free aborted management FIBs on late completion

This commit calls aac_fib_free() in aac_intr_normal() when completing a
management FIB whose caller was interrupted and marked fib->done == 2.

> diff --git a/drivers/scsi/aacraid/dpcsup.c b/drivers/scsi/aacraid/dpcsup.c
> index 169c41d080a15..a965f18fe4849 100644
> --- a/drivers/scsi/aacraid/dpcsup.c
> +++ b/drivers/scsi/aacraid/dpcsup.c
> @@ -379,8 +379,10 @@ unsigned int aac_intr_normal(struct aac_dev *dev, u32 index, int isAif,
>  					mflags);
>  
>  				FIB_COUNTER_INCREMENT(aac_config.NativeRecved);
> -				if (completed)
> +				if (completed) {
>  					aac_fib_complete(fib);
> +					aac_fib_free(fib);
> +				}
>  			}
>  		} else {
>  			struct hw_fib *hwfib = fib->hw_fib_va;

[Severity: Medium]
Can freeing fib here cause a use-after-free and double free when a
synchronous native HBA command in aac_hba_send() is interrupted by a
signal?

In aac_hba_send(), fibptr->done = 2 is written outside fibptr->event_lock:

drivers/scsi/aacraid/commsup.c:aac_hba_send() {
    ...
		if (wait_for_completion_interruptible(&fibptr->event_wait))
			fibptr->done = 2;
		fibptr->flags &= ~(FIB_CONTEXT_FLAG_WAIT);

		spin_lock_irqsave(&fibptr->event_lock, flags);
		if ((fibptr->done == 0) || (fibptr->done == 2)) {
			fibptr->done = 2; /* Tell interrupt we aborted */
			spin_unlock_irqrestore(&fibptr->event_lock, flags);
			return -ERESTARTSYS;
		}
		spin_unlock_irqrestore(&fibptr->event_lock, flags);
    ...
}

If the completion interrupt arrives between the unlocked assignment and
spin_lock_irqsave(&fibptr->event_lock, flags), both sides free the FIB:

CPU0                                    CPU1 (<hardirq>)
aac_send_raw_srb()
  aac_hba_send()
    wait_for_completion_interruptible()
      /* interrupted by signal */
    fibptr->done = 2;
                                        aac_intr_normal()
                                          lock(&fib->event_lock)
                                          if (fib->done == 2) {
                                            fib->done = 1;
                                            completed = 1;
                                          }
                                          unlock(&fib->event_lock)
                                          aac_fib_complete(fib)
                                          aac_fib_free(fib)
    lock(&fibptr->event_lock)
    /* fibptr->done is 1, returns 0 */
    unlock(&fibptr->event_lock)
  /* reads freed srbfib */              <-- use-after-free
  if (rcode != -ERESTARTSYS)
    aac_fib_free(srbfib)                <-- double free

Because fibptr->done is now 1, the early return for done == 2 in
aac_fib_free() does not trigger, pushing srbfib onto dev->free_fib twice
and corrupting the management FIB free list into a cycle.

Also, unlike aac_fib_send(), neither aac_fib_alloc() nor aac_hba_send()
initializes fibptr->done = 0, so recycled FIBs enter aac_hba_send() with
fibptr->done == 1.  If aac_intr_normal() runs after
wait_for_completion_interruptible() is interrupted but before
fibptr->done = 2 is written, aac_intr_normal() sees fib->done != 2 and
does not free the FIB, while aac_hba_send() overwrites fibptr->done = 2
and returns -ERESTARTSYS, leaking the FIB.

> @@ -432,8 +434,10 @@ unsigned int aac_intr_normal(struct aac_dev *dev, u32 index, int isAif,
>  					mflags);
>  
>  				FIB_COUNTER_INCREMENT(aac_config.NormalRecved);
> -				if (completed)
> +				if (completed) {
>  					aac_fib_complete(fib);
> +					aac_fib_free(fib);
> +				}
>  			}
>  		}
>  

[Severity: Medium]
Does this double free fibptr when aac_fib_send() is interrupted by a
signal in aac_send_safw_bmic_cmd()?

Unlike ioctl_send_fib() and aac_send_raw_srb(), aac_send_safw_bmic_cmd()
translates -ERESTARTSYS to -ERESTART and unconditionally cleans up:

drivers/scsi/aacraid/aachba.c:aac_send_safw_bmic_cmd() {
    ...
	if (rcode == -ERESTARTSYS)
		rcode = -ERESTART;

	if (unlikely(rcode < 0))
		goto bmic_error;
    ...
bmic_error:
	dma_unmap_single(&dev->pdev->dev, addr, xfer_len, DMA_BIDIRECTIONAL);
fib_error:
	aac_fib_complete(fibptr);
	aac_fib_free(fibptr);
    ...
}

Previously, the early return for fibptr->done == 2 in aac_fib_complete()
and aac_fib_free() made those calls no-ops after an interrupted
aac_fib_send().  With this change, if aac_intr_normal() completes the
abandoned FIB before aac_send_safw_bmic_cmd() reaches aac_fib_free():

CPU0                                    CPU1 (<hardirq>)
aac_send_safw_bmic_cmd()
  aac_fib_send()
    /* interrupted, sets done = 2 */
    return -ERESTARTSYS;
                                        aac_intr_normal()
                                          if (fib->done == 2) {
                                            fib->done = 1;
                                            completed = 1;
                                          }
                                          aac_fib_complete(fib)
                                          aac_fib_free(fib)
  dma_unmap_single(...)
  aac_fib_complete(fibptr)
  aac_fib_free(fibptr)                  <-- double free

fibptr->done is already reset to 1, so aac_fib_free() does not return
early and pushes fibptr onto dev->free_fib a second time.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124110444.3485-1-benj.saiz@gmail.com?part=1

      reply	other threads:[~2026-11-24 11:18 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 11:04 [PATCH RFC] scsi: aacraid: free aborted management FIBs on late completion Benjamin Saiz
2026-11-24 11:18 ` sashiko-bot [this message]

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=sashiko-outbox-231035@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=benj.saiz@gmail.com \
    --cc=linux-scsi@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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