From: Prasad J Pandit <pjp@fedoraproject.org>
Date: Sat, 9 Jan 2016 01:18:31 +0530
Subject: ide: ahci: reset ncq object to unused on error
Bug-Debian: https://bugs.debian.org/810527

When processing NCQ commands, ACHI device emulation prepares a
NCQ transfer object; To which an aio control block(aiocb) object
is assigned in 'execute_ncq_command'. In case, when the NCQ
command is invalid, the 'aiocb' object is not assigned, and NCQ
transfer object is left as 'used'. This leads to a use after
free kind of error in 'bdrv_aio_cancel_async' via 'ahci_reset_port'.
Reset NCQ transfer object to 'unused' to avoid it.

Reported-by: Qinghao Tang <luodalongde@gmail.com>
Signed-off-by: Prasad J Pandit <pjp@fedoraproject.org>
Reviewed-by: John Snow <jsnow@redhat.com>
Message-id: 1452282511-4116-1-git-send-email-ppandit@redhat.com
Signed-off-by: John Snow <jsnow@redhat.com>
---
 hw/ide/ahci.c | 1 +
 1 file changed, 1 insertion(+)

#diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c
#index dd1912e..17f1cbd 100644
#--- a/hw/ide/ahci.c
#+++ b/hw/ide/ahci.c
#@@ -910,6 +910,7 @@ static void ncq_err(NCQTransferState *ncq_tfs)
#     ide_state->error = ABRT_ERR;
#     ide_state->status = READY_STAT | ERR_STAT;
#     ncq_tfs->drive->port_regs.scr_err |= (1 << ncq_tfs->tag);
#+    ncq_tfs->used = 0;
# }
# 
# static void ncq_finish(NCQTransferState *ncq_tfs)
-- 

Mjt:

In 2.1, the code is different.  In particular, execute_ncq_command()
is part of process_ncq_command(), and there's no ncq_err() function
yet.  We do the "used = 0" assignment in the exact place where the
invalid NCQ command is detected.

diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c
index 604152a..1d4c60c 100644
--- a/hw/ide/ahci.c
+++ b/hw/ide/ahci.c
@@ -816,6 +816,7 @@ static void process_ncq_command(AHCIState *s, int port, uint8_t *cmd_fis,
         default:
             DPRINTF(port, "error: tried to process non-NCQ command as NCQ\n");
             qemu_sglist_destroy(&ncq_tfs->sglist);
+            ncq_tfs->used = 0;
             break;
     }
 }
