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=-2.6 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=no 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 A363BC433DF for ; Tue, 30 Jun 2020 12:44:25 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5E04120780 for ; Tue, 30 Jun 2020 12:44:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Ec8Ujcsv" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5E04120780 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:52026 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jqFce-0004Xy-Mr for qemu-devel@archiver.kernel.org; Tue, 30 Jun 2020 08:44:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45786) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jqFb6-0003ZN-9z for qemu-devel@nongnu.org; Tue, 30 Jun 2020 08:42:48 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:53257 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jqFb4-0005Xo-33 for qemu-devel@nongnu.org; Tue, 30 Jun 2020 08:42:48 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593520965; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:autocrypt:autocrypt; bh=7DzRUvEi6IRKw5ly9/dIFKf0AaaJecfOEFNa9WggHe0=; b=Ec8Ujcsvdc47j+R/p7HzE6qlsoMntRa7RFgkjy4zhk45CZq072S5DcwebFQN9UCBfINF/b 0n1EcTfnilySNucx/s5A73AthUkHljgxgRCsg5gUGVW2Zaep7fHFAiYT5vYK2hWawcXrqs exForfi9MgEmyfXw4Rq+292MU+YgirI= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-468-ITnzZ7YFOH-EMsWx3vh4ow-1; Tue, 30 Jun 2020 08:42:43 -0400 X-MC-Unique: ITnzZ7YFOH-EMsWx3vh4ow-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 21E5D85B68E; Tue, 30 Jun 2020 12:42:41 +0000 (UTC) Received: from [10.36.114.211] (ovpn-114-211.ams2.redhat.com [10.36.114.211]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3A67A890A1; Tue, 30 Jun 2020 12:42:39 +0000 (UTC) Subject: Re: [PATCH] net: tap: check if the file descriptor is valid before using it To: =?UTF-8?Q?Daniel_P=2e_Berrang=c3=a9?= References: <929203d2-20d2-7caf-e487-6bfe5b851974@redhat.com> <20200630092318.GE1370404@redhat.com> <20200630093148.GF1370404@redhat.com> <247f4aa8-1846-c5ec-4fe3-1d344979ad52@redhat.com> <7a110325-0123-53da-604d-8a9374903782@redhat.com> <20200630110310.GG1370404@redhat.com> <0309d626-f0b6-faa6-cc90-fe0b8cbbb535@redhat.com> <20200630123518.GL1370404@redhat.com> From: Laurent Vivier Autocrypt: addr=lvivier@redhat.com; prefer-encrypt=mutual; keydata= mQINBFYFJhkBEAC2me7w2+RizYOKZM+vZCx69GTewOwqzHrrHSG07MUAxJ6AY29/+HYf6EY2 WoeuLWDmXE7A3oJoIsRecD6BXHTb0OYS20lS608anr3B0xn5g0BX7es9Mw+hV/pL+63EOCVm SUVTEQwbGQN62guOKnJJJfphbbv82glIC/Ei4Ky8BwZkUuXd7d5NFJKC9/GDrbWdj75cDNQx UZ9XXbXEKY9MHX83Uy7JFoiFDMOVHn55HnncflUncO0zDzY7CxFeQFwYRbsCXOUL9yBtqLer Ky8/yjBskIlNrp0uQSt9LMoMsdSjYLYhvk1StsNPg74+s4u0Q6z45+l8RAsgLw5OLtTa+ePM JyS7OIGNYxAX6eZk1+91a6tnqfyPcMbduxyBaYXn94HUG162BeuyBkbNoIDkB7pCByed1A7q q9/FbuTDwgVGVLYthYSfTtN0Y60OgNkWCMtFwKxRaXt1WFA5ceqinN/XkgA+vf2Ch72zBkJL RBIhfOPFv5f2Hkkj0MvsUXpOWaOjatiu0fpPo6Hw14UEpywke1zN4NKubApQOlNKZZC4hu6/ 8pv2t4HRi7s0K88jQYBRPObjrN5+owtI51xMaYzvPitHQ2053LmgsOdN9EKOqZeHAYG2SmRW LOxYWKX14YkZI5j/TXfKlTpwSMvXho+efN4kgFvFmP6WT+tPnwARAQABtCNMYXVyZW50IFZp dmllciA8bHZpdmllckByZWRoYXQuY29tPokCOAQTAQIAIgUCVgVQgAIbAwYLCQgHAwIGFQgC CQoLBBYCAwECHgECF4AACgkQ8ww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3inzF+NFzHoM5Vwq6rc1JPjfC3oqRLJzqAEHBDjQFlqNR3IFCIAo4SYQRBdAHB CzkM4rWyRhuVABEBAAGJAh8EGAECAAkFAlYFJhkCGwwACgkQ8ww4vT8vvjwg9w//VQrcnVg3 TsjEybxDEUBm8dBmnKqcnTBFmxN5FFtIWlEuY8+YMiWRykd8Ln9RJ/98/ghABHz9TN8TRo2b 6WimV64FmlVn17Ri6FgFU3xNt9TTEChqAcNg88eYryKsYpFwegGpwUlaUaaGh1m9OrTzcQy+ klVfZWaVJ9Nw0keoGRGb8j4XjVpL8+2xOhXKrM1fzzb8JtAuSbuzZSQPDwQEI5CKKxp7zf76 J21YeRrEW4WDznPyVcDTa+tz++q2S/BpP4W98bXCBIuQgs2m+OflERv5c3Ojldp04/S4NEjX EYRWdiCxN7ca5iPml5gLtuvhJMSy36glU6IW9kn30IWuSoBpTkgV7rLUEhh9Ms82VWW/h2Tx L8enfx40PrfbDtWwqRID3WY8jLrjKfTdR3LW8BnUDNkG+c4FzvvGUs8AvuqxxyHbXAfDx9o/ jXfPHVRmJVhSmd+hC3mcQ+4iX5bBPBPMoDqSoLt5w9GoQQ6gDVP2ZjTWqwSRMLzNr37rJjZ1 pt0DCMMTbiYIUcrhX8eveCJtY7NGWNyxFCRkhxRuGcpwPmRVDwOl39MB3iTsRighiMnijkbL XiKoJ5CDVvX5yicNqYJPKh5MFXN1bvsBkmYiStMRbrD0HoY1kx5/VozBtc70OU0EB8Wrv9hZ D+Ofp0T3KOr1RUHvCZoLURfFhSQ= Message-ID: Date: Tue, 30 Jun 2020 14:42:38 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 MIME-Version: 1.0 In-Reply-To: <20200630123518.GL1370404@redhat.com> Content-Language: en-US X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=lvivier@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=205.139.110.120; envelope-from=lvivier@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/30 03:55:26 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Stefan Weil , Jason Wang , qemu-devel@nongnu.org, Markus Armbruster , Gerd Hoffmann , Paolo Bonzini Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" On 30/06/2020 14:35, Daniel P. Berrangé wrote: > On Tue, Jun 30, 2020 at 02:00:06PM +0200, Laurent Vivier wrote: >> On 30/06/2020 13:03, Daniel P. Berrangé wrote: >>> On Tue, Jun 30, 2020 at 12:35:46PM +0200, Laurent Vivier wrote: >>>> On 30/06/2020 12:03, Jason Wang wrote: >>>>> >>>>> On 2020/6/30 下午5:45, Laurent Vivier wrote: >>>>>> On 30/06/2020 11:31, Daniel P. Berrangé wrote: >>>>>>> On Tue, Jun 30, 2020 at 10:23:18AM +0100, Daniel P. Berrangé wrote: >>>>>>>> On Tue, Jun 30, 2020 at 05:21:49PM +0800, Jason Wang wrote: >>>>>>>>> On 2020/6/30 上午3:30, Laurent Vivier wrote: >>>>>>>>>> On 28/06/2020 08:31, Jason Wang wrote: >>>>>>>>>>> On 2020/6/25 下午7:56, Laurent Vivier wrote: >>>>>>>>>>>> On 25/06/2020 10:48, Daniel P. Berrangé wrote: >>>>>>>>>>>>> On Wed, Jun 24, 2020 at 09:00:09PM +0200, Laurent Vivier wrote: >>>>>>>>>>>>>> qemu_set_nonblock() checks that the file descriptor can be >>>>>>>>>>>>>> used and, if >>>>>>>>>>>>>> not, crashes QEMU. An assert() is used for that. The use of >>>>>>>>>>>>>> assert() is >>>>>>>>>>>>>> used to detect programming error and the coredump will allow >>>>>>>>>>>>>> to debug >>>>>>>>>>>>>> the problem. >>>>>>>>>>>>>> >>>>>>>>>>>>>> But in the case of the tap device, this assert() can be >>>>>>>>>>>>>> triggered by >>>>>>>>>>>>>> a misconfiguration by the user. At startup, it's not a real >>>>>>>>>>>>>> problem, >>>>>>>>>>>>>> but it >>>>>>>>>>>>>> can also happen during the hot-plug of a new device, and here >>>>>>>>>>>>>> it's a >>>>>>>>>>>>>> problem because we can crash a perfectly healthy system. >>>>>>>>>>>>> If the user/mgmt app is not correctly passing FDs, then there's >>>>>>>>>>>>> a whole >>>>>>>>>>>>> pile of bad stuff that can happen. Checking whether the FD is >>>>>>>>>>>>> valid is >>>>>>>>>>>>> only going to catch a small subset. eg consider if fd=9 refers >>>>>>>>>>>>> to the >>>>>>>>>>>>> FD that is associated with the root disk QEMU has open. We'll >>>>>>>>>>>>> fail to >>>>>>>>>>>>> setup the TAP device and close this FD, breaking the healthy >>>>>>>>>>>>> system >>>>>>>>>>>>> again. >>>>>>>>>>>>> >>>>>>>>>>>>> I'm not saying we can't check if the FD is valid, but lets be >>>>>>>>>>>>> clear that >>>>>>>>>>>>> this is not offering very much protection against a broken mgmt >>>>>>>>>>>>> apps >>>>>>>>>>>>> passing bad FDs. >>>>>>>>>>>>> >>>>>>>>>>>> I agree with you, but my only goal here is to avoid the crash in >>>>>>>>>>>> this >>>>>>>>>>>> particular case. >>>>>>>>>>>> >>>>>>>>>>>> The punishment should fit the crime. >>>>>>>>>>>> >>>>>>>>>>>> The user can think the netdev_del doesn't close the fd, and he >>>>>>>>>>>> can try >>>>>>>>>>>> to reuse it. Sending back an error is better than crashing his >>>>>>>>>>>> system. >>>>>>>>>>>> After that, if the system crashes, it will be for the good >>>>>>>>>>>> reasons, not >>>>>>>>>>>> because of an assert. >>>>>>>>>>> Yes. And on top of this we may try to validate the TAP via st_dev >>>>>>>>>>> through fstat[1]. >>>>>>>>>> I agree, but the problem I have is to know which major(st_dev) we can >>>>>>>>>> allow to use. >>>>>>>>>> >>>>>>>>>> Do we allow only macvtap major number? >>>>>>>>> >>>>>>>>> Macvtap and tuntap. >>>>>>>>> >>>>>>>>> >>>>>>>>>> How to know the macvtap major number at user level? >>>>>>>>>> [it is allocated dynamically: do we need to parse /proc/devices?] >>>>>>>>> >>>>>>>>> I think we can get them through fstat for /dev/net/tun and >>>>>>>>> /dev/macvtapX. >>>>>>>> Don't assume QEMU has any permission to access to these device nodes, >>>>>>>> only the pre-opened FDs it is given by libvirt. >>>>>>> Actually permissions are the least of the problem - the device nodes >>>>>>> won't even exist, because QEMU's almost certainly running in a private >>>>>>> mount namespace with a minimal /dev populated >>>>>>> >>>>>> I'm working on a solution using /proc/devices. >>>>> >>>>> >>>>> Similar issue with /dev. There's no guarantee that qemu can access >>>>> /proc/devices or it may not exist (CONFIG_PROCFS). >>>> >>>> There is a lot of things that will not work without /proc (several tools >>>> rely on /proc, like ps, top, lsof, mount, ...). Some information are >>>> only available from /proc, and if /proc is there, I think /proc/devices >>>> is always readable by everyone. Moreover /proc is already used by qemu >>>> in several places. >>>> >>>> It can also a best effort check. >>>> >>>> The problem with fstat() on /dev files is to guess the /dev/macvtapX as >>>> X varies (the same with /dev/tapY).. >>>> >>>>> >>>>>> macvtap has its own major number, but tuntap use "misc" (10) major >>>>>> number. >>>> >>>> Another question: it is possible to use the "fd=" parameter with macvtap >>>> as macvtap creates a /dev/tapY device, but how to do that with tuntap >>>> that does not create a /dev/tapY device? >>> >>> >>> I think we should step back and ask why we need to check this at all. >>> >>> IMHO, if the passed-in FD works with the syscalls that tap-linux.c >>> is executing, then that shows the FD is suitable for QEMU. The problem >>> is that many of the tap APIs don't use "Error **errp" parameters to >>> report errors, so we can't catch the failures. IOW, instead of checking >>> the FD major/minor number, we should make the existing code be better >>> at reporting errors, so they can be fed back to the QMP console >>> gracefully. >> >> The problem here is the very first operation of net_init_tap() is a >> qemu_set_nonblock() that has an assert() and crashes QEMU. >> >> It's why I was only checking for the validity of the file descriptor, >> not if it is a tap device or not. > > Yep, checking that it is really a FD is sufficient to avoid the > assert in nonblock. > > As for whether it is really a tap device, I think we just need to > improve error reporting of the functions that come later, instead > of doing a literal "is it a tap" check. I agree. I will update my patches to have a series with my patch checking for the validity of fd and another patch to return the errors to QMP from the tap functions. > That's what I'd tried in my old patch from a few years back > > https://patchwork.kernel.org/patch/10029443/ > > I can't remember why we didn't merge this back then Jason already gave the link in the thread. I'm going to try to use your patch in my series. Thanks, Laurent