Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp2615460pxb; Sun, 17 Oct 2021 20:35:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxhqODAdLbQSO0V3+Re8xUf9nuBfJM1MNyS6BjMG1zRgO9y1aS5VYSUSE2sEFcToeRdulxW X-Received: by 2002:a63:e516:: with SMTP id r22mr21698303pgh.197.1634528135163; Sun, 17 Oct 2021 20:35:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634528135; cv=none; d=google.com; s=arc-20160816; b=wKdGTxyR0zibqqmb8dp71BsE1HPOAdZWRC4K5JUSnEW6pXnhK5o6OPDEP3txZI/reX L+kJFPFFrjzvozD3rELaO4vB1AZleKfReisR8h7FD8adDzVnaXGU2jlJAVGysUwmF6qi EC8KojToA19F6wk39sc9/iifvqI2yStrvCSqQOY21Pc8zJLdpcxcCHFArY+2zsZRp/3u YwSZjyBueikHeHiqT3gqZ5fp9E8rnbvoUxrojqGfP/0SB7R6/DF3m9s1fCxPZbslWQAU aMjHNZ21FtOsL7ma1iY5f1tmfFoB3vf6A+o6+TXPdlNf+ToZ1Q7zLUNdCsWR3fTRWQUt OrkA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from :dkim-signature; bh=r9fwNM9GKQH17yC9Q3NgykdrcmxUpMayXjBnqsUJA/0=; b=BizACBISxFLaHxZtN8fip4KEPMDspLZW0s+wrJcSr0X0Plvreae2xzkYlRCg/ppG4o CJT5dUYuULG+sKJkMIoLrp35e+rKW1GZP45+9fs4cSMglZLSN2qG3ca/r5znmIEz3htS kbDJPMlT7FPt2W3EXjNUQ/VmZTDYLyUp5RiyfUvrb3ecIJZDv5TdtYodEbyL4FHYnphV yMcWsXsbIqJ2974D8lx1kIZVzBFRIvleUCZqJJm/RXnlgF1yp6pkgQOXTzmQmBGffdXA MUp6QcPRNl5hieVEtUV7m/u78QELr2fdjeqEcAYM4s1q7uVfu+3mQ/ojKh20FiLjmVXx mCHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=eMBXODue; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f10si10171347pjp.94.2021.10.17.20.35.22; Sun, 17 Oct 2021 20:35:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=eMBXODue; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244307AbhJPL2o (ORCPT + 98 others); Sat, 16 Oct 2021 07:28:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229848AbhJPL2o (ORCPT ); Sat, 16 Oct 2021 07:28:44 -0400 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 712BEC061570; Sat, 16 Oct 2021 04:26:36 -0700 (PDT) Received: by mail-pj1-x102b.google.com with SMTP id oa4so9034324pjb.2; Sat, 16 Oct 2021 04:26:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id; bh=r9fwNM9GKQH17yC9Q3NgykdrcmxUpMayXjBnqsUJA/0=; b=eMBXODueWZCF1lIKJ7Hd0flKHLkiZO3LfXKwhNIfdNKmmDZ1FbLrKs6Np+LC4PXdZZ 4bOG39GvqJOnvlkUcPdrduDx8AlDVCExczFaQIXPCkeo1e87pTmCuy2IYfe8KR6wdw4j x7WVK9016VeDuYZjWDqcpTAqqZNOGl+JgnIH4D4u3m6m33SJ//lfoxpLUMDPp0TShl/c et7qB3yezm3S8QumdhCPi4H97M55n+N2W7ybK1MAgfZVHsgdP0HkAQLVeHhRzk1WmigX 2uKw4GXQx6pFbiPk9SmBdep5VTcU79uOXq2IGhQ9duydZb0K1ul//wkhRG1F9YacQZOq 3Q3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=r9fwNM9GKQH17yC9Q3NgykdrcmxUpMayXjBnqsUJA/0=; b=f+/iR5flfut0N2xZvYBaicQGiYrz63xsouTH64w16jAj51eWnZ+dnN57J+kDqCIymv Mvs0ap37+eIcKJodMIb5UbdyuSh6XUh8tk1lSgVrq5e/5rC52+ts979Sk2nL8XHdpKkL ezzhVum0zEsUfKv6MQWjh6fQhNEoWrH/qAgBxWgDPPEXEaiDJp/kijWFQn51xkFxogpY I5Yiff+qmKFWjDU4P2A12X7D8mEWhkGG8CgVbUHH8ZNnBL1GfFD/ajlN8E06ih1vQi0f r4qNzm27RWu6t5sj9MB0+96Z2SEpuhpkCOykWPdMlOmKWYKlgzQfhLnz+Bifh3MxkeNq eaRQ== X-Gm-Message-State: AOAM533h/q9IjBwtBB4KvRcVlrCAT5ly0I6g3yLfN5HvhcEJBTjPRZ59 MyyCHSXzfqQDNNaN0KbIi2HBT8vTnKTg X-Received: by 2002:a17:90b:4b85:: with SMTP id lr5mr20087134pjb.66.1634383595956; Sat, 16 Oct 2021 04:26:35 -0700 (PDT) Received: from vultr.guest ([107.191.53.97]) by smtp.gmail.com with ESMTPSA id h4sm7096943pgn.6.2021.10.16.04.26.33 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 16 Oct 2021 04:26:35 -0700 (PDT) From: Zheyu Ma To: maximlevitsky@gmail.com, oakad@yahoo.com, ulf.hansson@linaro.org Cc: linux-mmc@vger.kernel.org, linux-kernel@vger.kernel.org, Zheyu Ma Subject: [PATCH] memstick: r592: Fix a UAF bug when removing the driver Date: Sat, 16 Oct 2021 11:26:21 +0000 Message-Id: <1634383581-11055-1-git-send-email-zheyuma97@gmail.com> X-Mailer: git-send-email 2.7.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In r592_remove(), the driver will free dma after freeing the host, which may cause a UAF bug. The following log reveals it: [ 45.361796 ] BUG: KASAN: use-after-free in r592_remove+0x269/0x350 [r592] [ 45.364286 ] Call Trace: [ 45.364472 ] dump_stack_lvl+0xa8/0xd1 [ 45.364751 ] print_address_description+0x87/0x3b0 [ 45.365137 ] kasan_report+0x172/0x1c0 [ 45.365415 ] ? r592_remove+0x269/0x350 [r592] [ 45.365834 ] ? r592_remove+0x269/0x350 [r592] [ 45.366168 ] __asan_report_load8_noabort+0x14/0x20 [ 45.366531 ] r592_remove+0x269/0x350 [r592] [ 45.378785 ] [ 45.378903 ] Allocated by task 4674: [ 45.379162 ] ____kasan_kmalloc+0xb5/0xe0 [ 45.379455 ] __kasan_kmalloc+0x9/0x10 [ 45.379730 ] __kmalloc+0x150/0x280 [ 45.379984 ] memstick_alloc_host+0x2a/0x190 [ 45.380664 ] [ 45.380781 ] Freed by task 5509: [ 45.381014 ] kasan_set_track+0x3d/0x70 [ 45.381293 ] kasan_set_free_info+0x23/0x40 [ 45.381635 ] ____kasan_slab_free+0x10b/0x140 [ 45.381950 ] __kasan_slab_free+0x11/0x20 [ 45.382241 ] slab_free_freelist_hook+0x81/0x150 [ 45.382575 ] kfree+0x13e/0x290 [ 45.382805 ] memstick_free+0x1c/0x20 [ 45.383070 ] device_release+0x9c/0x1d0 [ 45.383349 ] kobject_put+0x2ef/0x4c0 [ 45.383616 ] put_device+0x1f/0x30 [ 45.383865 ] memstick_free_host+0x24/0x30 [ 45.384162 ] r592_remove+0x242/0x350 [r592] [ 45.384473 ] pci_device_remove+0xa9/0x250 Signed-off-by: Zheyu Ma --- drivers/memstick/host/r592.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/memstick/host/r592.c b/drivers/memstick/host/r592.c index e79a0218c492..1d35d147552d 100644 --- a/drivers/memstick/host/r592.c +++ b/drivers/memstick/host/r592.c @@ -838,15 +838,15 @@ static void r592_remove(struct pci_dev *pdev) } memstick_remove_host(dev->host); + if (dev->dummy_dma_page) + dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->dummy_dma_page, + dev->dummy_dma_page_physical_address); + free_irq(dev->irq, dev); iounmap(dev->mmio); pci_release_regions(pdev); pci_disable_device(pdev); memstick_free_host(dev->host); - - if (dev->dummy_dma_page) - dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->dummy_dma_page, - dev->dummy_dma_page_physical_address); } #ifdef CONFIG_PM_SLEEP -- 2.17.6