From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.9 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS, T_DKIMWL_WL_HIGH,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A31BDC04AB1 for ; Thu, 9 May 2019 19:06:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6E75320656 for ; Thu, 9 May 2019 19:06:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1557428767; bh=2kJoZ+cjfSzkpzoetEwzeleoVs2fuDkITfPVv3PmI4M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=OPmgoiBPprZDRxeBp4M7/Ve3PSTN/dyS7m0EUCi3FHUaJjsh/ng+jH+D4SO2SyQ7R F20n8yBboPVyiSbcMGBTyoYuirnGvNzLAh8Lm3NcV7i+DirDPoXCKpQbtXNBpe1ySV vuZP3ESnR3gkzeRu5ABZJ4ueQLxgS0gvGiWz87gM= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727737AbfEISrK (ORCPT ); Thu, 9 May 2019 14:47:10 -0400 Received: from mail.kernel.org ([198.145.29.99]:39556 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727306AbfEISrG (ORCPT ); Thu, 9 May 2019 14:47:06 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 88133217F5; Thu, 9 May 2019 18:47:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1557427626; bh=2kJoZ+cjfSzkpzoetEwzeleoVs2fuDkITfPVv3PmI4M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BUo64gfq81yb3S/PhakiBB8yWGRnRASCr7Iqt0wrJuXQ5eNvUMWbCN4ADUa1wHvMJ 05UlK83eHgFb0W5RINaK+pCmUu+1SuZIf5x100R9PoSHRN5xBZy+XGkceGCjbp2PyA xM2RROEty7Dvkxv2RNqoLM9dWQxK519KELLqbxQg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, chenxiang , Jason Yan , John Garry , Johannes Thumshirn , Ewan Milne , Christoph Hellwig , Tomas Henzl , Dan Williams , Hannes Reinecke , "Martin K. Petersen" Subject: [PATCH 4.19 02/66] scsi: libsas: fix a race condition when smp task timeout Date: Thu, 9 May 2019 20:41:37 +0200 Message-Id: <20190509181301.995626693@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190509181301.719249738@linuxfoundation.org> References: <20190509181301.719249738@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jason Yan commit b90cd6f2b905905fb42671009dc0e27c310a16ae upstream. When the lldd is processing the complete sas task in interrupt and set the task stat as SAS_TASK_STATE_DONE, the smp timeout timer is able to be triggered at the same time. And smp_task_timedout() will complete the task wheter the SAS_TASK_STATE_DONE is set or not. Then the sas task may freed before lldd end the interrupt process. Thus a use-after-free will happen. Fix this by calling the complete() only when SAS_TASK_STATE_DONE is not set. And remove the check of the return value of the del_timer(). Once the LLDD sets DONE, it must call task->done(), which will call smp_task_done()->complete() and the task will be completed and freed correctly. Reported-by: chenxiang Signed-off-by: Jason Yan CC: John Garry CC: Johannes Thumshirn CC: Ewan Milne CC: Christoph Hellwig CC: Tomas Henzl CC: Dan Williams CC: Hannes Reinecke Reviewed-by: Hannes Reinecke Reviewed-by: John Garry Reviewed-by: Johannes Thumshirn Signed-off-by: Martin K. Petersen Cc: Guenter Roeck --- drivers/scsi/libsas/sas_expander.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) --- a/drivers/scsi/libsas/sas_expander.c +++ b/drivers/scsi/libsas/sas_expander.c @@ -48,17 +48,16 @@ static void smp_task_timedout(struct tim unsigned long flags; spin_lock_irqsave(&task->task_state_lock, flags); - if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) + if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { task->task_state_flags |= SAS_TASK_STATE_ABORTED; + complete(&task->slow_task->completion); + } spin_unlock_irqrestore(&task->task_state_lock, flags); - - complete(&task->slow_task->completion); } static void smp_task_done(struct sas_task *task) { - if (!del_timer(&task->slow_task->timer)) - return; + del_timer(&task->slow_task->timer); complete(&task->slow_task->completion); }