From patchwork Wed Nov 14 01:08:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Venkatesh Srinivas X-Patchwork-Id: 10681759 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 442361709 for ; Wed, 14 Nov 2018 01:09:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3D98A2B7D1 for ; Wed, 14 Nov 2018 01:09:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 31E122B7B5; Wed, 14 Nov 2018 01:09:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BD3852B0CD for ; Wed, 14 Nov 2018 01:09:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731348AbeKNLKC (ORCPT ); Wed, 14 Nov 2018 06:10:02 -0500 Received: from mail-qk1-f202.google.com ([209.85.222.202]:54779 "EHLO mail-qk1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726573AbeKNLKC (ORCPT ); Wed, 14 Nov 2018 06:10:02 -0500 Received: by mail-qk1-f202.google.com with SMTP id v74so12904561qkb.21 for ; Tue, 13 Nov 2018 17:09:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc:content-transfer-encoding; bh=LePG+wo/JGerGb1kjcpt7o0i+JQ0OdO9ADWnJ7WEyxg=; b=GAiabtqVMf5WwCVH7mZdD/X/d6D2XC4P0+nmwHKC1cZLpyHNeCp0V3DjpcI/l0VTmB u+M6swEBbhLLcyb15Bvq/TWhRKMuqgfK32dvIMA5KNvnsLu9JFSNJNIXkBREILpaNTcF qF1wyhlT2OTWUafJvTnUZm79Hob6bu8M4rJhLNJVft56IfrN2+0SDr4R3evAhX72aRG6 UMqeEHaIe87IXb/EUdP080+rEYOobdgnUdpASaMcuZMw8dko7Hfwi8/JuiSy/B+w3DZk tgliEkz8MRQvBI7gSRqkv46sWkN2JAWEPX8Yb8GbZFm/BOQt259MqdwkkupzJhbB3HP5 FwtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc:content-transfer-encoding; bh=LePG+wo/JGerGb1kjcpt7o0i+JQ0OdO9ADWnJ7WEyxg=; b=N8k133pJnG/iZnszGwoS7ZQRfLvvyoxDiCHlWt8CYkKcmO8jeLj4ya60P1ASl2QBJk FX+RlyetB6X3W8nb3+3TE+Kavq+wj4Ps9cFkLcIPhT+UwEuQuU/++64jPnVHdHVW2qVe AgqUGfBF2XXBk+nFxj9j5lMelZawL2Y3vorWGal+NwlF9Nvd/C8TI2BUGQIZrNdXpjQ+ ChAtyoOBpL8ZEX2Lqtm9mtdJJGINn+/w9bcdGQoL6NnVHXka7rOlMTNkoAaqhtv3x2yK ZAhZ2BSZB8lUD3BWFcv4gvrq+IARXpakLubytmFIEvl8Q2KeOAgFc1WydW3lwTwH0zTC MRyQ== X-Gm-Message-State: AGRZ1gLEVFIrk5iTU44z3VjQTCZ+tlzwxCu4dbj5MQPezEm3WFloLgst oF02zlcob8ex9FgB5IKUxC9qIqQzD8YLOG7s X-Google-Smtp-Source: AJdET5c2oohhKbClzZ6/uPD/7Dj4Tyu+1OfnqMiVMnFJ9QSc2Yud3d56MdKJay9ZZzKyZfWc0X7lsmqkd5WNqTIs X-Received: by 2002:a37:8e05:: with SMTP id q5mr4824497qkd.7.1542157748982; Tue, 13 Nov 2018 17:09:08 -0800 (PST) Date: Tue, 13 Nov 2018 20:08:58 -0500 In-Reply-To: <20181109231637.231582-1-venkateshs@google.com> Message-Id: <20181114010858.37880-1-venkateshs@google.com> Mime-Version: 1.0 References: <20181109231637.231582-1-venkateshs@google.com> X-Mailer: git-send-email 2.19.1.1215.g8438c0b245-goog Subject: [v2] uio/uio_pci_generic: Disable bus-mastering on release From: Venkatesh Srinivas To: mst@redhat.com Cc: kvm@vger.kernel.org, venkateshs@google.com, vsrinivas@ops101.org, Bjorn Helgaas , Catherine Sullivan Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Userspace code using uio_pci_generic may enable bus-mastering by directly manipulating a PCI device's command register. If a userspace program enables bus-mastering but exits/crashes uncleanly, bus- mastering will still be enabled and stale DMA addresses may be programmed and live in the device. Disable bus-mastering unconditionally on last close of a UIO PCI fd to avoid this. If the device did not have bus-mastering enabled, pci_clear_master() is a no-op. Signed-off-by: Venkatesh Srinivas Reviewed-by: Bjorn Helgaas Reviewed-by: Catherine Sullivan Acked-by: Michael S. Tsirkin --- drivers/uio/uio_pci_generic.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/drivers/uio/uio_pci_generic.c b/drivers/uio/uio_pci_generic.c index 8773e373ffe5..dde5cbb27178 100644 --- a/drivers/uio/uio_pci_generic.c +++ b/drivers/uio/uio_pci_generic.c @@ -39,6 +39,22 @@ to_uio_pci_generic_dev(struct uio_info *info) return container_of(info, struct uio_pci_generic_dev, info); } +static int release(struct uio_info *info, struct inode *inode) +{ + struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info); + + /* + * This driver is insecure when used with devices doing DMA, but some +  * people (mis)use it with such devices. +  * Let's at least make sure DMA isn't left enabled after the userspace +  * driver closes the fd. +  * Note that there's a non-zero chance doing this will wedge the device +  * at least until reset. + */ + pci_clear_master(gdev->pdev); + return 0; +} + /* Interrupt handler. Read/modify/write the command register to disable * the interrupt. */ static irqreturn_t irqhandler(int irq, struct uio_info *info) @@ -78,6 +94,7 @@ static int probe(struct pci_dev *pdev, gdev->info.name = "uio_pci_generic"; gdev->info.version = DRIVER_VERSION; + gdev->info.release = release; gdev->pdev = pdev; if (pdev->irq) { gdev->info.irq = pdev->irq;