Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 09CB2C44529 for ; Tue, 21 Jul 2026 10:56:41 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wm88v-0007Sp-Dm; Tue, 21 Jul 2026 06:56:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wm88t-0007RW-Gr for qemu-trivial@nongnu.org; Tue, 21 Jul 2026 06:56:07 -0400 Received: from mx0b-0031df01.pphosted.com ([205.220.180.131]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wm88r-0005BZ-FN for qemu-trivial@nongnu.org; Tue, 21 Jul 2026 06:56:07 -0400 Received: from pps.filterd (m0279870.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 66LARS1D1455582 for ; Tue, 21 Jul 2026 10:56:03 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= mhydVc2NwLlNOLaFqB5Z9f4lqxRRFuE0Eh/pblaYSfI=; b=bPIWu+iDHZRwKd7Q hRdbfn9kCXZFPYo/fc4mS2gaze11Rqga0a9d5IErcj53WJUh83QHg6MPl5udaCIC T2zCpbOofDyljxCh6dfBAgS4CEYueK+0Rv2HORpn1CtmLI+Co2Wr3Xvj3Iv1B0NW ymzC/cr9n2kgyfJJLDkw9IsO5bd+LmIEZZF61EDKJDUqvlYMWd5h98KF/ehUPimd 4vaCt4aozp9H1yuMhDkd9jZ1NLhNASgXCkLyDyCMjzNBnf+UT6QTQJbqeTg6+Wca lZ8ZD17yHT0jDPZOOcShkg+2uhZibr+0hv5EV4DoVAsTls+KTxv1ai5fT6UK/dMF SnnUlw== Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4fj1duhtxm-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Tue, 21 Jul 2026 10:56:03 +0000 (GMT) Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-526da7e3c9dso7110851cf.1 for ; Tue, 21 Jul 2026 03:56:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1784631363; x=1785236163; darn=nongnu.org; h=content-transfer-encoding:content-type:in-reply-to:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to:content-type; bh=mhydVc2NwLlNOLaFqB5Z9f4lqxRRFuE0Eh/pblaYSfI=; b=jjlA1b6Aa7Z+mQD4A+CfVj4XLcF+VRzHHpsZBvvV2Kd85y7bovlWSS8FBLWXOAVIGr rvSzy8J6GU244ig5iGo7SHSzxf/verj0nS2sk9M9MurVyP9V1kAm+Aw9JTs7IQqHIIgn WyOF6OWUz8BH4Wgocbm8H52kVz/VZuPAaKBhTwLVO+O+xNIaITIvCBGRByV6A/pPp7rh oe0PjgBYlv/P+gGMY52oGngcZ6ZUhu405bNbBcuQJD4rd0LkfvF1uXrweJBcd7nll8WV 4TYnMhZ34PhHNjnbZNtR2lm2KiC1HsTdN2eqHGqWNbV5MtkMEdHlg7dnQNUOTJaV88RS O05w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784631363; x=1785236163; h=content-transfer-encoding:content-type:in-reply-to:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=mhydVc2NwLlNOLaFqB5Z9f4lqxRRFuE0Eh/pblaYSfI=; b=HWQTfY9EhwmNnfURD10SGeHWLho4ATGAz1tOJYB4D6bPhI8RMAdoLKUh+48/vAk6Ca DvKOfmhfmoVtXZAM5hjL8/pwyhqbt9M6QMups8PXsXwEnPvQ5xUghwkalLhtk7lL69WU ngBlfeQKE3ugsfirMvjreoNaS/BClOuBK9PVEaon1kHAhq9MWwH4yN9GclI3wABnI65u fQwpE9U3r6zHT7JRrmNDK5BS8EsZttC/3b+OvWelSEJsAOqTg/GNCtpVMPcu8srtlVAm FAbYnLfhcfX/em1nnXnwZT7Z3HHdjeKuqm0P2MijKy056pzaAHwMgyIXRJ5NNMlPjEAC +KSg== X-Forwarded-Encrypted: i=1; AHgh+Rpy+eF9sQfOYvxin5+FZfbJ5At2+DD+CF7ZBrF+NGN/wBx0DbKGkbT5/0EH7GZoy9bBK6jVXNyN4Qi8QGk=@nongnu.org X-Gm-Message-State: AOJu0YwXnzglgk5yoOxGT1QatEn2iQvHBFC6GNdPSjgaNH5V8q8ss24r POeGkFjv8wcDXtdvd4CdV3oDnUnC1OZ+EHr6lFVn0ZaEkT5bMxWAgom6Lp7V4YMvqpFEKvSPTb+ 75Mru89Xnoh2fgB+cPdnWluYD2pVfNBECGfgVGkIHhN1dsO5xyDgb5I2FYpik X-Gm-Gg: AfdE7cnLwuWBxaBhooCHJ3gEdp7xPijXCOHucoPZRjBSmJ2whP1eQCtPYRb8Bax/kod IFInHslVHXoyT2P6d8hf4MwwHu68GtvJWT5PbV8IGQ8aIv7UZGKvbFS8UU1pqmpldhUfR9KX1G8 ApRDrW8KbDZjZZvTiCvjmnwRlOsOaIv4UBs5pGUAkOQzhPHtG4jEoY8BeFCZWJmdOEDTAOpqhWw brPDDw2qMbtcGIF/x+8+amUdG7cRVcorzuQxfe6wMhe+1k2UnPIHsdZkMJTgRbNs7kDe4tcza6a sfhI7HqeI7q20iNya9M5LevRLY57zpZ0CITVfSUmDIdPsypEfBYNJPv/YSWxciMNu3zcZBIqUqV ZaeLhpkbt1JlBQ9zr8xYch6h4sjU/l/1HNsIpARfVoVDhOB0evs8= X-Received: by 2002:a05:622a:991:b0:51c:640:231f with SMTP id d75a77b69052e-5213daa1d60mr180897411cf.45.1784631362592; Tue, 21 Jul 2026 03:56:02 -0700 (PDT) X-Received: by 2002:a05:622a:991:b0:51c:640:231f with SMTP id d75a77b69052e-5213daa1d60mr180897151cf.45.1784631362103; Tue, 21 Jul 2026 03:56:02 -0700 (PDT) Received: from [192.168.69.219] (88-187-86-199.subs.proxad.net. [88.187.86.199]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-495653bfabesm67335225e9.10.2026.07.21.03.56.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 21 Jul 2026 03:56:01 -0700 (PDT) Message-ID: <500f7d75-f8a5-4152-bf5b-17cf047ad24a@oss.qualcomm.com> Date: Tue, 21 Jul 2026 12:55:59 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] hw/ide/core: Fix possible crash via NULL pointer in ide_cancel_dma_sync() Content-Language: en-US To: Thomas Huth , Mark Cave-Ayland , qemu-devel@nongnu.org, John Snow Cc: Alexander Bulekov , qemu-block@nongnu.org, qemu-stable@nongnu.org, qemu-trivial@nongnu.org References: <20260721070216.82984-1-thuth@redhat.com> <3f8027d5-f7db-4fb5-8386-1d5f93974a10@nutanix.com> <563e1a3e-1420-4f6b-9244-9ad4360c207f@oss.qualcomm.com> <06ede776-5505-4154-a4d4-a74b77d5cb90@redhat.com> From: =?UTF-8?Q?Philippe_Mathieu-Daud=C3=A9?= In-Reply-To: <06ede776-5505-4154-a4d4-a74b77d5cb90@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Proofpoint-ORIG-GUID: DtcK9FL2FDwH1AmDdHiZZqAnOPpboZ7F X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNzIxMDExNSBTYWx0ZWRfX12b9y9mrT0nE JP3uBWM5OaPh9BSSYlmIYg2gI4kOPq8lTo6FPvnW1q03d/FRhJEYsnp0MOtLs4ynNMsxbH34EKG Wn1T7FBc9HRc/CMmvznw3yDRs4MqJhsOet5itJIE9vPcPmw+O4WT/eZtiTn4pB8CfxuTwVZpJn5 FNHIfUuglN3tCMYrD0k8LHwEeS+BXtKWznjN21jptGGUjYMwNCJduKNhoNWcWq2ixWGIeAchZ7y nksL5um81Dzi0R3sLAcFvXFq/iaYHKgUj+EwNHragI/4kIpMludxg0MMbv55K/cOHkf3igaU+VD Tnln8DvWP/5YF5z2irNRn40ZDjSCzDS56Xd0bhL7FyhVBmTO4WuGD8dA2Q1Jb3ODoAkTk2rXRm/ gHdETYCwlgFgYgQuvQXz73z1FdT0xaOW2twO6qKJvwTomUBciqQBMCda60RKWurFFfY4drkUWin VlOVs4e4ZuWAtO9//1A== X-Authority-Analysis: v=2.4 cv=I9JVgtgg c=1 sm=1 tr=0 ts=6a5f5043 cx=c_pps a=WeENfcodrlLV9YRTxbY/uA==:117 a=4s3hRJSeHn4rkQlkrse1kQ==:17 a=IkcTkHD0fZMA:10 a=RAioF0-LDSMA:10 a=M51BFTxLslgA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=gowsoOTTUOVcmtlkKump:22 a=RpNjiQI2AAAA:8 a=p0WdMEafAAAA:8 a=20KFwNOVAAAA:8 a=xo6y5mzXw5BTvs1Glo0A:9 a=3ZKOabzyN94A:10 a=QEXdDO2ut3YA:10 a=kacYvNCVWA4VmyqE58fU:22 X-Proofpoint-GUID: DtcK9FL2FDwH1AmDdHiZZqAnOPpboZ7F X-Proofpoint-Spam-Info: AW1haW4tMjYwNzIxMDExNSBTYWx0ZWRfX6KxSKqnOoqP1 Lxwy40YrTYnUqM3vigeT7A9f+TgkA4Qmf964H+tn0ddWNPT7Kea4/EYDvILQRbq3STp3COz45BE IX1b64eUViYJksR1Q5MBa82+hFCzdxw= X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.134,FMLib:17.12.100.49 definitions=2026-07-21_01,2026-07-20_03,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 adultscore=0 spamscore=0 clxscore=1015 suspectscore=0 lowpriorityscore=0 phishscore=0 priorityscore=1501 impostorscore=0 malwarescore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2606150000 definitions=main-2607210115 Received-SPF: pass client-ip=205.220.180.131; envelope-from=philmd@oss.qualcomm.com; helo=mx0b-0031df01.pphosted.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-trivial@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-trivial-bounces+qemu-trivial=archiver.kernel.org@nongnu.org Sender: qemu-trivial-bounces+qemu-trivial=archiver.kernel.org@nongnu.org On 21/7/26 12:21, Thomas Huth wrote: > On 21/07/2026 11.58, Philippe Mathieu-Daudé wrote: >> On 21/7/26 11:51, Philippe Mathieu-Daudé wrote: >>> On 21/7/26 10:29, Mark Cave-Ayland wrote: >>>> On 21/07/2026 08:02, Thomas Huth wrote: >>>> >>>>> From: Thomas Huth >>>>> >>>>> ide_cancel_dma_sync() is called with a "IDEState *s" for one of the >>>>> two IDE drives on a bus (primary or secondary drive) to cancel all >>>>> pending DMA transfers on the drive. The code then checks >>>>> s->bus->dma->aiocb to see whether there is any IO in flight on the >>>>> *bus* and then calls blk_drain(s->blk) to wait for its completion. >>>>> However, s->bus->dma->aiocb might belong to the other drive on the >>>>> bus, and if there is no disk attached to the current drive, s->blk >>>>> is NULL. Since blk_drain() does not check its parameter for a NULL >>>>> pointer, QEMU can crash in such a case. >>>>> >>>>> To fix the problem, we have to check that "blk" is not NULL before >>>>> calling blk_drain(). And we have to call blk_drain() for both drives, >>>>> otherwise the assert(s->bus->dma->aiocb == NULL) statement after >>>>> the blk_drain() might trigger if the IO in flight belongs to the >>>>> the other drive. >>>>> >>>>> Resolves: https://urldefense.proofpoint.com/v2/url? >>>>> u=https-3A__gitlab.com_qemu-2Dproject_qemu_-2D_work-5Fitems_905&d=DwIDAg&c=s883GpUCOChKOHiocYtGcg&r=c23RpsaH4D2MKyD3EPJTDa0BAxz6tV8aUJqVSoytEiY&m=BbZFP82dEZMyWzN5GnJ9XaxwmyrmYGbqWbEBNSUM_afB5JRRF3InIaZAr5UpUUh2&s=rCgSz13wVfk11Z-Uw5WTQViAQKkSvwpQqf_dJX1tiro&e= >>>>> Reported-by: Alexander Bulekov >>>>> Resolves: https://urldefense.proofpoint.com/v2/url? >>>>> u=https-3A__gitlab.com_qemu-2Dproject_qemu_-2D_work-5Fitems_4052&d=DwIDAg&c=s883GpUCOChKOHiocYtGcg&r=c23RpsaH4D2MKyD3EPJTDa0BAxz6tV8aUJqVSoytEiY&m=BbZFP82dEZMyWzN5GnJ9XaxwmyrmYGbqWbEBNSUM_afB5JRRF3InIaZAr5UpUUh2&s=B-AkqMAsQwA0LzU5ptwPJ3ZIBAA8ncf5ZbQg8GTwaME&e= >>>>> Reported-by: dong ling >>>>> Signed-off-by: Thomas Huth >>>>> --- >>>>>   v2: Drain both drives >>>>> >>>>>   hw/ide/core.c | 9 ++++++++- >>>>>   1 file changed, 8 insertions(+), 1 deletion(-) >>>>> >>>>> diff --git a/hw/ide/core.c b/hw/ide/core.c >>>>> index f78b00220b8..f43ab95b07b 100644 >>>>> --- a/hw/ide/core.c >>>>> +++ b/hw/ide/core.c >>>>> @@ -741,10 +741,17 @@ void ide_cancel_dma_sync(IDEState *s) >>>>>        * In the future we'll be able to safely cancel the I/O if the >>>>>        * whole DMA operation will be submitted to disk with a single >>>>>        * aio operation with preadv/pwritev. >>>>> +     * >>>>> +     * Note: s->bus->dma->aiocb might belong to the adjacent >>>>> IDEState, >>>>> +     * so we have to drain both drives to get it cleared. >>>>>        */ >>>>>       if (s->bus->dma->aiocb) { >>>>>           trace_ide_cancel_dma_sync_remaining(); >>>>> -        blk_drain(s->blk); >>>>> +        for (int i = 0; i < 2; i++) { >>>>> +            if (s->bus->ifs[i].blk) { >>>>> +                blk_drain(s->bus->ifs[i].blk); >>>>> +            } >>>>> +        } >>>>>           assert(s->bus->dma->aiocb == NULL); >>>>>       } >>>>>   } >>>> >>>> Doesn't ide_bus_active_if() do the right thing here for in-flight >>>> requests? >>> >>> bmdma_cmd_writeb() already calls ide_bus_active_if(). >>> >>> Is the issue in the caller, bmdma_cmd_writeb(), instead of >>> here in ide_cancel_dma_sync()? >> >> i.e.: >> >> -- >8 -- >> diff --git a/hw/ide/pci.c b/hw/ide/pci.c >> index 7ce1ae67ab2..e7c6af9a49e 100644 >> --- a/hw/ide/pci.c >> +++ b/hw/ide/pci.c >> @@ -384,7 +384,7 @@ void bmdma_cmd_writeb(BMDMAState *bm, uint32_t val) >>       /* Ignore writes to SSBM if it keeps the old value */ >>       if ((val & BM_CMD_START) != (bm->cmd & BM_CMD_START)) { >>           if (!(val & BM_CMD_START)) { >> -            ide_cancel_dma_sync(ide_bus_active_if(bm->bus)); >> +            ide_cancel_dma_sync(bmdma_active_if(bm)); >>               bm->status &= ~BM_STATUS_DMAING; >>           } else { >>               bm->cur_addr = bm->addr; > No, I don't think this will fix all cases. While https://gitlab.com/ > qemu-project/qemu/-/work_items/905 has a call trace with > bmdma_cmd_writeb() in it, https://gitlab.com/qemu-project/qemu/-/ > work_items/4052 is slightly different: The backtrace shows > ide_bus_perform_srst() ->  ide_perform_srst() -> ide_cancel_dma_sync() > here instead. I'd say let's play safe and always drain both interfaces. > WDYT? I dunno I'm kind of lost. Can't blk_bs() handle NULL blk? -- >8 -- diff --git a/block/block-backend.c b/block/block-backend.c index 37ba7e9fc40..b5d84c9789a 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -760,7 +760,7 @@ BlockBackend *blk_by_name(const char *name) BlockDriverState *blk_bs(BlockBackend *blk) { IO_CODE(); - return blk->root ? blk->root->bs : NULL; + return (blk && blk->root) ? blk->root->bs : NULL; } ---