Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750812AbeAOT6w (ORCPT + 1 other); Mon, 15 Jan 2018 14:58:52 -0500 Received: from mail-bl2nam02on0071.outbound.protection.outlook.com ([104.47.38.71]:38277 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750714AbeAOT6u (ORCPT ); Mon, 15 Jan 2018 14:58:50 -0500 From: "Madhani, Himanshu" To: Max Kellermann CC: Greg KH , linux-scsi , Dept-Eng QLA2xxx Upstream , "max.kellermann@gmail.com" , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH] drivers/scsi/qla2xxx: fix double free bug after firmware timeout Thread-Topic: [PATCH] drivers/scsi/qla2xxx: fix double free bug after firmware timeout Thread-Index: AQHTjiXzy1qsO0dDlkWHzWVZLq6UeqN1WjUA Date: Mon, 15 Jan 2018 19:58:47 +0000 Message-ID: <13B82B86-8810-4722-97F9-EC5B862ECC76@cavium.com> References: <151603716189.28707.13105191089826357709.stgit@rabbit.intern.cm-ag> In-Reply-To: <151603716189.28707.13105191089826357709.stgit@rabbit.intern.cm-ag> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Himanshu.Madhani@cavium.com; x-originating-ip: [173.186.134.106] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;SN4PR0701MB3629;7:tT01s/pUEWao4kRCyLfPVm1wGyY1P2M5J2DQJOEHhf5kqVnuiddwmXJt2CTP7C8QCxCxt41QlWzHJstPtRHMhKNPPCZIMelUY9tnYBzUmcP1pG4AC8ZzHYFeroEuS8hp862y9EYnv4dnC3EimGokGAbIRMS41F2KsVijVAli6P5bnzX8SACvQdFHA9uEL/9A4esVZOZZEy0mQXOSO2PtAc01QH4/WSiQqOVJChoeGjnugil9oNM5ypVpydpfBi+1 x-ms-exchange-antispam-srfa-diagnostics: SSOS; x-ms-office365-filtering-correlation-id: 293e8d68-8e86-4de4-a496-08d55c5263ee x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(4534125)(4602075)(4627221)(201703031133081)(201702281549075)(5600026)(4604075)(3008032)(2017052603307)(7153060)(7193020);SRVR:SN4PR0701MB3629; x-ms-traffictypediagnostic: SN4PR0701MB3629: x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(84791874153150)(17755550239193); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(6040470)(2401047)(5005006)(8121501046)(3231023)(944501161)(10201501046)(3002001)(93006095)(93001095)(6041268)(20161123558120)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123562045)(20161123564045)(6072148)(201708071742011);SRVR:SN4PR0701MB3629;BCL:0;PCL:0;RULEID:(100000803101)(100110400095);SRVR:SN4PR0701MB3629; x-forefront-prvs: 0553CBB77A x-forefront-antispam-report: SFV:NSPM;SFS:(10009020)(376002)(39380400002)(346002)(39860400002)(366004)(396003)(24454002)(189003)(199004)(8936002)(478600001)(3280700002)(6512007)(6436002)(6246003)(81166006)(6916009)(2950100002)(229853002)(25786009)(105586002)(106356001)(6306002)(81156014)(2900100001)(8676002)(86362001)(66066001)(2906002)(3660700001)(14454004)(39060400002)(6486002)(72206003)(966005)(4326008)(33656002)(83716003)(5660300001)(6116002)(3846002)(82746002)(5250100002)(7736002)(36756003)(6506007)(102836004)(53546011)(68736007)(54906003)(97736004)(53936002)(305945005)(59450400001)(99286004)(316002)(76176011);DIR:OUT;SFP:1101;SCL:1;SRVR:SN4PR0701MB3629;H:SN4PR0701MB3632.namprd07.prod.outlook.com;FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; x-microsoft-antispam-message-info: RX1eDkSikwi0j4LFqnTwnOFJCL5ZaYNOOpX0frtgnuXzN9p94vDDQTrQhnkoNGM9g50pNgSCfgoNG7FqY0QzBA== spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="us-ascii" Content-ID: <0D10ED0DEBA34247A4F3CF7DEC64569C@namprd07.prod.outlook.com> Content-Transfer-Encoding: 8BIT MIME-Version: 1.0 X-OriginatorOrg: cavium.com X-MS-Exchange-CrossTenant-Network-Message-Id: 293e8d68-8e86-4de4-a496-08d55c5263ee X-MS-Exchange-CrossTenant-originalarrivaltime: 15 Jan 2018 19:58:47.5044 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 711e4ccf-2e9b-4bcf-a551-4094005b6194 X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN4PR0701MB3629 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Return-Path: Hi Max, > On Jan 15, 2018, at 9:26 AM, Max Kellermann wrote: > > When the qla2xxx firmware is unavailable, eventually > qla2x00_sp_timeout() is reached, which calls the timeout function and > frees the srb_t instance. > > The timeout function always resolves to qla2x00_async_iocb_timeout(), > which invokes another callback function called "done". All of these > qla2x00_*_sp_done() callbacks also free the srb_t instance; after > returning to qla2x00_sp_timeout(), it is freed again. > > The fix is to remove the "sp->free(sp)" call from qla2x00_sp_timeout() > and add it to those code paths in qla2x00_async_iocb_timeout() which > do not already free the object. > > This is how it looks like with KASAN: > > BUG: KASAN: use-after-free in qla2x00_sp_timeout+0x228/0x250 > Read of size 8 at addr ffff88278147a590 by task swapper/2/0 > > Allocated by task 1502: > save_stack+0x33/0xa0 > kasan_kmalloc+0xa0/0xd0 > kmem_cache_alloc+0xb8/0x1c0 > mempool_alloc+0xd6/0x260 > qla24xx_async_gnl+0x3c5/0x1100 > > Freed by task 0: > save_stack+0x33/0xa0 > kasan_slab_free+0x72/0xc0 > kmem_cache_free+0x75/0x200 > qla24xx_async_gnl_sp_done+0x556/0x9e0 > qla2x00_async_iocb_timeout+0x1c7/0x420 > qla2x00_sp_timeout+0x16d/0x250 > call_timer_fn+0x36/0x200 > > The buggy address belongs to the object at ffff88278147a440 > which belongs to the cache qla2xxx_srbs of size 344 > The buggy address is located 336 bytes inside of > 344-byte region [ffff88278147a440, ffff88278147a598) > > Signed-off-by: Max Kellermann > --- > drivers/scsi/qla2xxx/qla_init.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) > > diff --git a/drivers/scsi/qla2xxx/qla_init.c b/drivers/scsi/qla2xxx/qla_init.c > index b5b48ddca962..801890564e00 100644 > --- a/drivers/scsi/qla2xxx/qla_init.c > +++ b/drivers/scsi/qla2xxx/qla_init.c > @@ -58,7 +58,6 @@ qla2x00_sp_timeout(unsigned long __data) > req->outstanding_cmds[sp->handle] = NULL; > iocb = &sp->u.iocb_cmd; > iocb->timeout(sp); > - sp->free(sp); > spin_unlock_irqrestore(&vha->hw->hardware_lock, flags); > } > > @@ -121,9 +120,11 @@ qla2x00_async_iocb_timeout(void *data) > ea.data[1] = lio->u.logio.data[1]; > ea.sp = sp; > qla24xx_handle_plogi_done_event(fcport->vha, &ea); > + sp->free(sp); > break; > case SRB_LOGOUT_CMD: > qlt_logo_completion_handler(fcport, QLA_FUNCTION_TIMEOUT); > + sp->free(sp); > break; > case SRB_CT_PTHRU_CMD: > case SRB_MB_IOCB: > We have patch to prevent this double free in 4.16/scsi-queue already. https://git.kernel.org/pub/scm/linux/kernel/git/mkp/scsi.git/commit/?h=4.16/scsi-queue&id=045d6ea200af794ba15515984cff63787a7fc3c0 Thanks, - Himanshu