All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Anthony Krowiak" <akrowiak@linux.ibm.com>
Cc: "Heiko Carstens" <hca@linux.ibm.com>,
	"Alexander Gordeev" <agordeev@linux.ibm.com>,
	"Vasily Gorbik" <gor@linux.ibm.com>,
	kvm@vger.kernel.org, linux-s390@vger.kernel.org
Subject: Re: [PATCH v7 11/15] s390/vfio-ap: Transition device migration state to STOP
Date: Fri, 07 Aug 2026 22:54:01 +0000	[thread overview]
Message-ID: <20260807225402.D7DB01F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260807221834.562851-12-akrowiak@linux.ibm.com>

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

New issues:
- [High] Memory leak and silent transmission of stale device state due to missing buffer cleanup on FSM STOP transition.
- [High] Migration data file descriptors allow out-of-band manipulation of the active AP configuration.

Pre-existing issues:
- [Critical] Unprotected concurrent ioctls cause Use-After-Free, Double Free, and Memory Leaks during migration reads/writes.
- [High] RESUMING state machine is permanently broken by an incomplete reset.
--

--- Patch [11]: [PATCH v7 11/15] s390/vfio-ap: Transition device migration state to STOP ---
commit e7e2429c70da0ea0a7016c096567e78834fcc351
Author: Anthony Krowiak <akrowiak@linux.ibm.com>

    s390/vfio-ap: Transition device migration state to STOP
    
    This commit updates the VFIO AP migration state machine to handle
    transitions to the STOP state from RESUMING or STOP_COPY. It returns
    NULL instead of an error, stating that the VFIO migration framework
    will clean up file streams and associated data when releasing files.

> There  only thing to do at this point is return
> NULL to the caller since the file streams and associated data will get
> cleaned up as a result of the VFIO migration framework releasing the files.

[Severity: High]
Is this assumption about the file streams and associated data getting cleaned
up accurate?

Looking at the file release callback:

vfio_ap_release_mig_file() {
    ...
    vfio_device_put_registration(&matrix_mdev->vdev);
    return 0;
}

The callback only releases the VFIO registration reference and does not free
the state buffers like stop_copy_mig_file.ap_config or
resuming_mig_file.ap_config.

Could this lead to a memory leak and the silent transmission of stale device
state to the destination host on subsequent migration attempts, for instance
if we skip allocating and fetching a fresh configuration in
vfio_ap_stop_copy_read() because ap_config was leaked and left populated?

> diff --git a/drivers/s390/crypto/vfio_ap_migration.c b/drivers/s390/crypto/vfio_ap_migration.c
> index 4dd7373c3d9d9..30160e3dddedd 100644
> --- a/drivers/s390/crypto/vfio_ap_migration.c
> +++ b/drivers/s390/crypto/vfio_ap_migration.c
> @@ -1511,12 +1511,17 @@ vfio_ap_transition_to_state(struct ap_matrix_mdev *matrix_mdev,
>  		return filp;
>  	}
>  
> +	/*
> +	 * Terminates the data transfer session of the vfio-ap device state
> +	 * between the source and target hosts. Since the vfio-ap device does
> +	 * not virtualize a DMA device, there is no internal device state to
> +	 * incorporate into the vfio-ap device on the target.
> +	 */
>  	if ((cur_state == VFIO_DEVICE_STATE_RESUMING &&
>  	     new_state == VFIO_DEVICE_STATE_STOP) ||
>  	    (cur_state == VFIO_DEVICE_STATE_STOP_COPY &&
>  	     new_state == VFIO_DEVICE_STATE_STOP)) {
> -		/* TODO */
> -		return ERR_PTR(-EOPNOTSUPP);
> +		return NULL;

[Severity: High]
Does returning NULL here successfully change the FSM state without clearing
the active filp pointers in mig_data?

If userspace keeps the migration file descriptor open (or duplicates it)
during an FSM transition to RUNNING, and then issues a write on the stale
file descriptor, could this allow out-of-band manipulation of the KVM guest's
AP configuration while the guest is actively running?

The file operations like validate_resuming_write_parms() appear to only check
if the incoming filp pointer matches the stored pointer:

validate_resuming_write_parms() {
    ...
    if (filp != resuming_mig_file.filp)
        return -ENXIO;
    ...
}

This happens without ensuring the FSM is actually in the RESUMING state,
potentially permitting writes during RUNNING.

[Severity: High]
This is a pre-existing issue, but does a reset or an aborted RESUMING phase
leave the RESUMING state machine permanently broken?

When vfio_ap_release_resuming_file() is called, it frees
resuming_mig_file.ap_config but fails to clear resuming_config_buf.filled:

vfio_ap_release_resuming_file() {
    kvfree(mig_data->resuming_mig_file.ap_config);
    mig_data->resuming_mig_file.ap_config = NULL;
    mig_data->resuming_mig_file.config_sz = 0;
    mig_data->resuming_mig_file.filp = NULL;
}

If userspace initiates a new RESUMING phase, validate_resuming_write_parms()
will evaluate this condition as true:

    if ((resuming_config_buf.filled && !resuming_mig_file.ap_config) ||
        (!resuming_config_buf.filled && resuming_mig_file.ap_config))
        return -EFAULT;

Would this unconditionally reject all subsequent writes with -EFAULT?

[Severity: Critical]
This is a pre-existing issue, but are the migration reads and writes protected
against concurrent ioctls?

For example, in vfio_ap_resuming_write(), mdevs_lock is dropped before
copying data from userspace and interacting with set_new_ap_configuration():

vfio_ap_resuming_write() {
    ...
    mutex_unlock(&matrix_dev->mdevs_lock);

    if (copy_from_user((char *)ap_config + write_pos, buf, write_len)) {
    ...
    ret = set_new_ap_configuration(matrix_mdev, ap_config, cfg_sz);
    ...
    if (!new_allocation)
        kvfree(ap_config);
    ...
}

If a concurrent VFIO_DEVICE_RESET ioctl occurs while the lock is dropped,
could set_new_ap_configuration() assign a locally allocated ap_config into
the now-NULL resuming_mig_file.ap_config?

Would the outer function then call kvfree(ap_config), leaving a dangling
pointer in the device state that could subsequently be used and double-freed?

Similarly, in vfio_ap_stop_copy_read(), the lock is dropped before
copy_to_user():

vfio_ap_stop_copy_read() {
    ...
    mutex_unlock(&matrix_dev->mdevs_lock);

    if (copy_to_user(buf, (char *)mig_file->ap_config + read_pos, len))
        return -EFAULT;
    ...
}

Could a concurrent reset free mig_file->ap_config before or during the copy,
leading to an information leak of freed kernel memory to userspace?

>  	}
>  
>  	if ((cur_state == VFIO_DEVICE_STATE_STOP &&

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260807221834.562851-1-akrowiak@linux.ibm.com?part=11

  reply	other threads:[~2026-08-07 22:54 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-07 22:18 [PATCH v7 00/15] s390/vfio-ap: Add live guest migration support Anthony Krowiak
2026-08-07 22:18 ` [PATCH v7 01/15] s390/vfio-ap: Provide function to get the number of queues assigned to mdev Anthony Krowiak
2026-08-07 22:27   ` sashiko-bot
2026-08-07 22:18 ` [PATCH v7 02/15] s390/vfio-ap: Data structures for facilitating vfio device migration Anthony Krowiak
2026-08-07 22:31   ` sashiko-bot
2026-08-07 22:18 ` [PATCH v7 03/15] s390/vfio-ap: Functions to initialize/release vfio device migration data Anthony Krowiak
2026-08-07 22:38   ` sashiko-bot
2026-08-07 22:18 ` [PATCH v7 04/15] s390/vfio-ap: Reset migration state in VFIO_DEVICE_RESET ioctl handler Anthony Krowiak
2026-08-07 22:54   ` sashiko-bot
2026-08-07 22:18 ` [PATCH v7 05/15] s390/vfio-ap: Callback to get/set vfio device mig state during guest migration Anthony Krowiak
2026-08-07 22:45   ` sashiko-bot
2026-08-07 22:18 ` [PATCH v7 06/15] s390/vfio-ap: Transition guest migration state from STOP to STOP_COPY Anthony Krowiak
2026-08-07 22:43   ` sashiko-bot
2026-08-07 22:18 ` [PATCH v7 07/15] s390/vfio-ap: File ops called to save the vfio device migration state Anthony Krowiak
2026-08-07 22:37   ` sashiko-bot
2026-08-07 22:18 ` [PATCH v7 08/15] s390/vfio-ap: Transition device migration state from STOP to RESUMING Anthony Krowiak
2026-08-07 22:41   ` sashiko-bot
2026-08-07 22:18 ` [PATCH v7 09/15] s390/vfio-ap: Add method to set a new guest AP configuration Anthony Krowiak
2026-08-07 22:40   ` sashiko-bot
2026-08-07 22:18 ` [PATCH v7 10/15] s390/vfio-ap: File ops called to resume the vfio device migration Anthony Krowiak
2026-08-07 22:30   ` sashiko-bot
2026-08-07 22:18 ` [PATCH v7 11/15] s390/vfio-ap: Transition device migration state to STOP Anthony Krowiak
2026-08-07 22:54   ` sashiko-bot [this message]
2026-08-07 22:18 ` [PATCH v7 12/15] s390/vfio-ap: Transition device migration state from STOP to RUNNING and vice versa Anthony Krowiak
2026-08-07 22:43   ` sashiko-bot
2026-08-07 22:18 ` [PATCH v7 13/15] s390/vfio-ap: Callback to get the size of data to be migrated during guest migration Anthony Krowiak
2026-08-07 22:37   ` sashiko-bot
2026-08-07 22:18 ` [PATCH v7 14/15] s390/vfio-ap: Add 'migratable' feature to sysfs 'features' attribute Anthony Krowiak
2026-08-07 22:36   ` sashiko-bot
2026-08-07 22:18 ` [PATCH v7 15/15] s390/vfio-ap: Add live guest migration chapter to vfio-ap.rst Anthony Krowiak
2026-08-07 22:32   ` sashiko-bot

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=20260807225402.D7DB01F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=agordeev@linux.ibm.com \
    --cc=akrowiak@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-s390@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.