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=-6.0 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS,USER_AGENT_GIT autolearn=unavailable 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 41FB0C04E87 for ; Wed, 15 May 2019 12:09:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1377920657 for ; Wed, 15 May 2019 12:09:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1557922161; bh=EMkLnOSZMMv4MZEje/xzNx+RTKH8T3X3TlZQylSguD4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=OWG0YiI9OcT2epxXBeQnjzuzY8QN6po9KGX0/XkRfOX1j8CJkSl9kKiG1TCj48LTh WxFfewOGJDLSBWkkf1yd8KafaAOe++L5qaxey1JbU8GV/Y4HxAQC/qwjbC41t1J+Q9 3BaXNjiYwfkG1Q33txTmnp/PpSyWwX+Tg/uNx/gU= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728695AbfEOLH4 (ORCPT ); Wed, 15 May 2019 07:07:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:39878 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728454AbfEOLHw (ORCPT ); Wed, 15 May 2019 07:07:52 -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 DC89720843; Wed, 15 May 2019 11:07:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1557918471; bh=EMkLnOSZMMv4MZEje/xzNx+RTKH8T3X3TlZQylSguD4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hQxKYp7mxch0BKRZIz/cvYifdoyljBIBFjUpz50Oi53IjwfmuqrWlzeuXlqZF7ssX y5iCzhDh3fKXfs1/bm3TGf3TRwfV2MjWULz8FF1pI7WSLX5hqJP/iWWfglgiKVPIRN A+riTtZEAvtXlLIYAnxYxczMbJKVn8QesKANNk1o= 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.4 143/266] scsi: libsas: fix a race condition when smp task timeout Date: Wed, 15 May 2019 12:54:10 +0200 Message-Id: <20190515090727.740821768@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190515090722.696531131@linuxfoundation.org> References: <20190515090722.696531131@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 @@ -47,17 +47,16 @@ static void smp_task_timedout(unsigned l 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); }