From nobody Fri Nov 29 22:33:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1620900211; cv=none; d=zohomail.com; s=zohoarc; b=RO8wKTthawC0afDbRUDnezzqSJHB/6yOXOSxkjc6v5hQM35BXEUYiuzhkC7asrywCWXo44SiYogCw9kUWLD+3I6ccvmW7QADpca4wyHyRYjUNdSuxIwmw6qwpuV0kDezhxUjfpD8QqgiIx9dkczJWSRCjkrnh/uBLbJ7amZb1/4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620900211; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=gN4/rgxtzqVWaIyPGz03VdvKju8BDVEvYHrK0+8yrQE=; b=KU8bMWVH8ozDkh9E25WnAW+ymEo8bPct/JIZF/pquWK1g+sccwGnAfBW06npvNiPH7XSO4xDndI0Ys6UvC6JpH8mVWzIjY295f9gxzsRV9yOvQOO+ZuC232dFPVI+hKEMW6woCUg+O35bdTbt+hyJXBvyyDrB84esksIVAC5VbQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1620900211863341.6094005581065; Thu, 13 May 2021 03:03:31 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.126808.238309 (Exim 4.92) (envelope-from ) id 1lh8BX-0004oo-2X; Thu, 13 May 2021 10:03:15 +0000 Received: by outflank-mailman (output) from mailman id 126808.238309; Thu, 13 May 2021 10:03:15 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lh8BW-0004oW-V5; Thu, 13 May 2021 10:03:14 +0000 Received: by outflank-mailman (input) for mailman id 126808; Thu, 13 May 2021 10:03:13 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lh8BV-0003vw-Lh for xen-devel@lists.xenproject.org; Thu, 13 May 2021 10:03:13 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 4a9dd3b9-34ba-4a9b-a670-7154e734e457; Thu, 13 May 2021 10:03:06 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 845DCAFD5; Thu, 13 May 2021 10:03:05 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 4a9dd3b9-34ba-4a9b-a670-7154e734e457 X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1620900185; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gN4/rgxtzqVWaIyPGz03VdvKju8BDVEvYHrK0+8yrQE=; b=Qh2deAEmR6Q8nSdsLJ8lUqzSI4K8jVie2Wh3Br1Tzx8DdAbZnb/EnLyq49zG2YnzxnMmhO ois/KXtSxCCBenc/q1t5Iv0v7UKfC8/S5vxSR2UKSCur51mtgyGPzlCDf4DKCiLICMO1/r zHOzmGakvaDZYd99BPsLX5sz4brlCkU= From: Juergen Gross To: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org Cc: Juergen Gross , Boris Ostrovsky , Stefano Stabellini Subject: [PATCH 1/8] xen: sync include/xen/interface/io/ring.h with Xen's newest version Date: Thu, 13 May 2021 12:02:55 +0200 Message-Id: <20210513100302.22027-2-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210513100302.22027-1-jgross@suse.com> References: <20210513100302.22027-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) Content-Type: text/plain; charset="utf-8" Sync include/xen/interface/io/ring.h with Xen's newest version in order to get the RING_COPY_RESPONSE() and RING_RESPONSE_PROD_OVERFLOW() macros. Note that this will correct the wrong license info by adding the missing original copyright notice. Signed-off-by: Juergen Gross --- include/xen/interface/io/ring.h | 278 ++++++++++++++++++-------------- 1 file changed, 156 insertions(+), 122 deletions(-) diff --git a/include/xen/interface/io/ring.h b/include/xen/interface/io/rin= g.h index 2af7a1cd6658..b39cdbc522ec 100644 --- a/include/xen/interface/io/ring.h +++ b/include/xen/interface/io/ring.h @@ -1,21 +1,53 @@ -/* SPDX-License-Identifier: GPL-2.0 */ /*************************************************************************= ***** * ring.h * * Shared producer-consumer ring macros. * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation = the + * rights to use, copy, modify, merge, publish, distribute, sublicense, an= d/or + * sell copies of the Software, and to permit persons to whom the Software= is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL= THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * * Tim Deegan and Andrew Warfield November 2004. */ =20 #ifndef __XEN_PUBLIC_IO_RING_H__ #define __XEN_PUBLIC_IO_RING_H__ =20 +/* + * When #include'ing this header, you need to provide the following + * declaration upfront: + * - standard integers types (uint8_t, uint16_t, etc) + * They are provided by stdint.h of the standard headers. + * + * In addition, if you intend to use the FLEX macros, you also need to + * provide the following, before invoking the FLEX macros: + * - size_t + * - memcpy + * - grant_ref_t + * These declarations are provided by string.h of the standard headers, + * and grant_table.h from the Xen public headers. + */ + #include =20 typedef unsigned int RING_IDX; =20 /* Round a 32-bit unsigned constant down to the nearest power of two. */ -#define __RD2(_x) (((_x) & 0x00000002) ? 0x2 : ((_x) & 0x1)) +#define __RD2(_x) (((_x) & 0x00000002) ? 0x2 : ((_x) & 0= x1)) #define __RD4(_x) (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2 : __RD2(_x)) #define __RD8(_x) (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4 : __RD4(_x)) #define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8 : __RD8(_x)) @@ -27,82 +59,79 @@ typedef unsigned int RING_IDX; * A ring contains as many entries as will fit, rounded down to the nearest * power of two (so we can mask with (size-1) to loop around). */ -#define __CONST_RING_SIZE(_s, _sz) \ - (__RD32(((_sz) - offsetof(struct _s##_sring, ring)) / \ - sizeof(((struct _s##_sring *)0)->ring[0]))) - +#define __CONST_RING_SIZE(_s, _sz) \ + (__RD32(((_sz) - offsetof(struct _s##_sring, ring)) / \ + sizeof(((struct _s##_sring *)0)->ring[0]))) /* * The same for passing in an actual pointer instead of a name tag. */ -#define __RING_SIZE(_s, _sz) \ - (__RD32(((_sz) - (long)&(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0]))) +#define __RING_SIZE(_s, _sz) \ + (__RD32(((_sz) - (long)(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0]= ))) =20 /* * Macros to make the correct C datatypes for a new kind of ring. * * To make a new ring datatype, you need to have two message structures, - * let's say struct request, and struct response already defined. + * let's say request_t, and response_t already defined. * * In a header where you want the ring datatype declared, you then do: * - * DEFINE_RING_TYPES(mytag, struct request, struct response); + * DEFINE_RING_TYPES(mytag, request_t, response_t); * * These expand out to give you a set of types, as you can see below. * The most important of these are: * - * struct mytag_sring - The shared ring. - * struct mytag_front_ring - The 'front' half of the ring. - * struct mytag_back_ring - The 'back' half of the ring. + * mytag_sring_t - The shared ring. + * mytag_front_ring_t - The 'front' half of the ring. + * mytag_back_ring_t - The 'back' half of the ring. * * To initialize a ring in your code you need to know the location and size * of the shared memory area (PAGE_SIZE, for instance). To initialise * the front half: * - * struct mytag_front_ring front_ring; - * SHARED_RING_INIT((struct mytag_sring *)shared_page); - * FRONT_RING_INIT(&front_ring, (struct mytag_sring *)shared_page, - * PAGE_SIZE); + * mytag_front_ring_t front_ring; + * SHARED_RING_INIT((mytag_sring_t *)shared_page); + * FRONT_RING_INIT(&front_ring, (mytag_sring_t *)shared_page, PAGE_SIZ= E); * * Initializing the back follows similarly (note that only the front * initializes the shared ring): * - * struct mytag_back_ring back_ring; - * BACK_RING_INIT(&back_ring, (struct mytag_sring *)shared_page, - * PAGE_SIZE); + * mytag_back_ring_t back_ring; + * BACK_RING_INIT(&back_ring, (mytag_sring_t *)shared_page, PAGE_SIZE); */ =20 -#define DEFINE_RING_TYPES(__name, __req_t, __rsp_t) \ - \ -/* Shared ring entry */ \ -union __name##_sring_entry { \ - __req_t req; \ - __rsp_t rsp; \ -}; \ - \ -/* Shared ring page */ \ -struct __name##_sring { \ - RING_IDX req_prod, req_event; \ - RING_IDX rsp_prod, rsp_event; \ - uint8_t pad[48]; \ - union __name##_sring_entry ring[1]; /* variable-length */ \ -}; \ - \ -/* "Front" end's private variables */ \ -struct __name##_front_ring { \ - RING_IDX req_prod_pvt; \ - RING_IDX rsp_cons; \ - unsigned int nr_ents; \ - struct __name##_sring *sring; \ -}; \ - \ -/* "Back" end's private variables */ \ -struct __name##_back_ring { \ - RING_IDX rsp_prod_pvt; \ - RING_IDX req_cons; \ - unsigned int nr_ents; \ - struct __name##_sring *sring; \ -}; - +#define DEFINE_RING_TYPES(__name, __req_t, __rsp_t) \ + \ +/* Shared ring entry */ \ +union __name##_sring_entry { \ + __req_t req; \ + __rsp_t rsp; \ +}; \ + \ +/* Shared ring page */ \ +struct __name##_sring { \ + RING_IDX req_prod, req_event; \ + RING_IDX rsp_prod, rsp_event; \ + uint8_t __pad[48]; \ + union __name##_sring_entry ring[1]; /* variable-length */ \ +}; \ + \ +/* "Front" end's private variables */ \ +struct __name##_front_ring { \ + RING_IDX req_prod_pvt; \ + RING_IDX rsp_cons; \ + unsigned int nr_ents; \ + struct __name##_sring *sring; \ +}; \ + \ +/* "Back" end's private variables */ \ +struct __name##_back_ring { \ + RING_IDX rsp_prod_pvt; \ + RING_IDX req_cons; \ + unsigned int nr_ents; \ + struct __name##_sring *sring; \ +}; \ + \ /* * Macros for manipulating rings. * @@ -119,94 +148,99 @@ struct __name##_back_ring { \ */ =20 /* Initialising empty rings */ -#define SHARED_RING_INIT(_s) do { \ - (_s)->req_prod =3D (_s)->rsp_prod =3D 0; \ - (_s)->req_event =3D (_s)->rsp_event =3D 1; \ - memset((_s)->pad, 0, sizeof((_s)->pad)); \ +#define SHARED_RING_INIT(_s) do { \ + (_s)->req_prod =3D (_s)->rsp_prod =3D 0; = \ + (_s)->req_event =3D (_s)->rsp_event =3D 1; = \ + (void)memset((_s)->__pad, 0, sizeof((_s)->__pad)); \ } while(0) =20 -#define FRONT_RING_ATTACH(_r, _s, _i, __size) do { \ - (_r)->req_prod_pvt =3D (_i); \ - (_r)->rsp_cons =3D (_i); \ - (_r)->nr_ents =3D __RING_SIZE(_s, __size); \ - (_r)->sring =3D (_s); \ +#define FRONT_RING_ATTACH(_r, _s, _i, __size) do { \ + (_r)->req_prod_pvt =3D (_i); \ + (_r)->rsp_cons =3D (_i); \ + (_r)->nr_ents =3D __RING_SIZE(_s, __size); \ + (_r)->sring =3D (_s); \ } while (0) =20 #define FRONT_RING_INIT(_r, _s, __size) FRONT_RING_ATTACH(_r, _s, 0, __siz= e) =20 -#define BACK_RING_ATTACH(_r, _s, _i, __size) do { \ - (_r)->rsp_prod_pvt =3D (_i); \ - (_r)->req_cons =3D (_i); \ - (_r)->nr_ents =3D __RING_SIZE(_s, __size); \ - (_r)->sring =3D (_s); \ +#define BACK_RING_ATTACH(_r, _s, _i, __size) do { \ + (_r)->rsp_prod_pvt =3D (_i); \ + (_r)->req_cons =3D (_i); \ + (_r)->nr_ents =3D __RING_SIZE(_s, __size); \ + (_r)->sring =3D (_s); \ } while (0) =20 #define BACK_RING_INIT(_r, _s, __size) BACK_RING_ATTACH(_r, _s, 0, __size) =20 /* How big is this ring? */ -#define RING_SIZE(_r) \ +#define RING_SIZE(_r) \ ((_r)->nr_ents) =20 /* Number of free requests (for use on front side only). */ -#define RING_FREE_REQUESTS(_r) \ +#define RING_FREE_REQUESTS(_r) \ (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons)) =20 /* Test if there is an empty slot available on the front ring. * (This is only meaningful from the front. ) */ -#define RING_FULL(_r) \ +#define RING_FULL(_r) \ (RING_FREE_REQUESTS(_r) =3D=3D 0) =20 /* Test if there are outstanding messages to be processed on a ring. */ -#define RING_HAS_UNCONSUMED_RESPONSES(_r) \ +#define RING_HAS_UNCONSUMED_RESPONSES(_r) \ ((_r)->sring->rsp_prod - (_r)->rsp_cons) =20 -#define RING_HAS_UNCONSUMED_REQUESTS(_r) \ - ({ \ - unsigned int req =3D (_r)->sring->req_prod - (_r)->req_cons; \ - unsigned int rsp =3D RING_SIZE(_r) - \ - ((_r)->req_cons - (_r)->rsp_prod_pvt); \ - req < rsp ? req : rsp; \ - }) +#define RING_HAS_UNCONSUMED_REQUESTS(_r) ({ \ + unsigned int req =3D (_r)->sring->req_prod - (_r)->req_cons; \ + unsigned int rsp =3D RING_SIZE(_r) - \ + ((_r)->req_cons - (_r)->rsp_prod_pvt); \ + req < rsp ? req : rsp; \ +}) =20 /* Direct access to individual ring elements, by index. */ -#define RING_GET_REQUEST(_r, _idx) \ +#define RING_GET_REQUEST(_r, _idx) \ (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req)) =20 +#define RING_GET_RESPONSE(_r, _idx) \ + (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp)) + /* - * Get a local copy of a request. + * Get a local copy of a request/response. * - * Use this in preference to RING_GET_REQUEST() so all processing is + * Use this in preference to RING_GET_{REQUEST,RESPONSE}() so all processi= ng is * done on a local copy that cannot be modified by the other end. * * Note that https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D58145 may caus= e this - * to be ineffective where _req is a struct which consists of only bitfiel= ds. + * to be ineffective where dest is a struct which consists of only bitfiel= ds. */ -#define RING_COPY_REQUEST(_r, _idx, _req) do { \ - /* Use volatile to force the copy into _req. */ \ - *(_req) =3D *(volatile typeof(_req))RING_GET_REQUEST(_r, _idx); \ +#define RING_COPY_(type, r, idx, dest) do { \ + /* Use volatile to force the copy into dest. */ \ + *(dest) =3D *(volatile typeof(dest))RING_GET_##type(r, idx); \ } while (0) =20 -#define RING_GET_RESPONSE(_r, _idx) \ - (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp)) +#define RING_COPY_REQUEST(r, idx, req) RING_COPY_(REQUEST, r, idx, req) +#define RING_COPY_RESPONSE(r, idx, rsp) RING_COPY_(RESPONSE, r, idx, rsp) =20 /* Loop termination condition: Would the specified index overflow the ring= ? */ -#define RING_REQUEST_CONS_OVERFLOW(_r, _cons) \ +#define RING_REQUEST_CONS_OVERFLOW(_r, _cons) \ (((_cons) - (_r)->rsp_prod_pvt) >=3D RING_SIZE(_r)) =20 /* Ill-behaved frontend determination: Can there be this many requests? */ -#define RING_REQUEST_PROD_OVERFLOW(_r, _prod) \ +#define RING_REQUEST_PROD_OVERFLOW(_r, _prod) \ (((_prod) - (_r)->rsp_prod_pvt) > RING_SIZE(_r)) =20 +/* Ill-behaved backend determination: Can there be this many responses? */ +#define RING_RESPONSE_PROD_OVERFLOW(_r, _prod) \ + (((_prod) - (_r)->rsp_cons) > RING_SIZE(_r)) =20 -#define RING_PUSH_REQUESTS(_r) do { \ - virt_wmb(); /* back sees requests /before/ updated producer index */ \ - (_r)->sring->req_prod =3D (_r)->req_prod_pvt; \ +#define RING_PUSH_REQUESTS(_r) do { \ + virt_wmb(); /* back sees requests /before/ updated producer index */\ + (_r)->sring->req_prod =3D (_r)->req_prod_pvt; \ } while (0) =20 -#define RING_PUSH_RESPONSES(_r) do { \ - virt_wmb(); /* front sees responses /before/ updated producer index */= \ - (_r)->sring->rsp_prod =3D (_r)->rsp_prod_pvt; \ +#define RING_PUSH_RESPONSES(_r) do { \ + virt_wmb(); /* front sees resps /before/ updated producer index */ \ + (_r)->sring->rsp_prod =3D (_r)->rsp_prod_pvt; \ } while (0) =20 /* @@ -239,40 +273,40 @@ struct __name##_back_ring { \ * field appropriately. */ =20 -#define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do { \ - RING_IDX __old =3D (_r)->sring->req_prod; \ - RING_IDX __new =3D (_r)->req_prod_pvt; \ - virt_wmb(); /* back sees requests /before/ updated producer index */ \ - (_r)->sring->req_prod =3D __new; \ - virt_mb(); /* back sees new requests /before/ we check req_event */ \ - (_notify) =3D ((RING_IDX)(__new - (_r)->sring->req_event) < \ - (RING_IDX)(__new - __old)); \ +#define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do { \ + RING_IDX __old =3D (_r)->sring->req_prod; \ + RING_IDX __new =3D (_r)->req_prod_pvt; \ + virt_wmb(); /* back sees requests /before/ updated producer index */\ + (_r)->sring->req_prod =3D __new; \ + virt_mb(); /* back sees new requests /before/ we check req_event */ \ + (_notify) =3D ((RING_IDX)(__new - (_r)->sring->req_event) < \ + (RING_IDX)(__new - __old)); \ } while (0) =20 -#define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do { \ - RING_IDX __old =3D (_r)->sring->rsp_prod; \ - RING_IDX __new =3D (_r)->rsp_prod_pvt; \ - virt_wmb(); /* front sees responses /before/ updated producer index */= \ - (_r)->sring->rsp_prod =3D __new; \ - virt_mb(); /* front sees new responses /before/ we check rsp_event */ \ - (_notify) =3D ((RING_IDX)(__new - (_r)->sring->rsp_event) < \ - (RING_IDX)(__new - __old)); \ +#define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do { \ + RING_IDX __old =3D (_r)->sring->rsp_prod; \ + RING_IDX __new =3D (_r)->rsp_prod_pvt; \ + virt_wmb(); /* front sees resps /before/ updated producer index */ \ + (_r)->sring->rsp_prod =3D __new; \ + virt_mb(); /* front sees new resps /before/ we check rsp_event */ \ + (_notify) =3D ((RING_IDX)(__new - (_r)->sring->rsp_event) < \ + (RING_IDX)(__new - __old)); \ } while (0) =20 -#define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do { \ - (_work_to_do) =3D RING_HAS_UNCONSUMED_REQUESTS(_r); \ - if (_work_to_do) break; \ - (_r)->sring->req_event =3D (_r)->req_cons + 1; \ - virt_mb(); \ - (_work_to_do) =3D RING_HAS_UNCONSUMED_REQUESTS(_r); \ +#define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do { \ + (_work_to_do) =3D RING_HAS_UNCONSUMED_REQUESTS(_r); \ + if (_work_to_do) break; \ + (_r)->sring->req_event =3D (_r)->req_cons + 1; \ + virt_mb(); \ + (_work_to_do) =3D RING_HAS_UNCONSUMED_REQUESTS(_r); \ } while (0) =20 -#define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do { \ - (_work_to_do) =3D RING_HAS_UNCONSUMED_RESPONSES(_r); \ - if (_work_to_do) break; \ - (_r)->sring->rsp_event =3D (_r)->rsp_cons + 1; \ - virt_mb(); \ - (_work_to_do) =3D RING_HAS_UNCONSUMED_RESPONSES(_r); \ +#define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do { \ + (_work_to_do) =3D RING_HAS_UNCONSUMED_RESPONSES(_r); \ + if (_work_to_do) break; \ + (_r)->sring->rsp_event =3D (_r)->rsp_cons + 1; \ + virt_mb(); \ + (_work_to_do) =3D RING_HAS_UNCONSUMED_RESPONSES(_r); \ } while (0) =20 =20 --=20 2.26.2 From nobody Fri Nov 29 22:33:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1620900209; cv=none; d=zohomail.com; s=zohoarc; b=RwFGrih7OWPv6xowkFVmiL5kYQMUt/2g1cpRET4KrQmZ+yXyaiLyHl8IZ722hDJwVhMXYI+6OdumIuUVJq99GJJQux7eytytNsnsSn/1MHcexJJCgxLEN//MIlkEY2zvaLWgfLYb3ZfvCLk8I2XbBR/OC/V43drz3r9jJOo/az4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620900209; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=lutTQX8ZA5u3khaAXqUgHLKbfap899noTeivh9Sx9NQ=; b=VhlwNnekkXXMVm8vuxDw8RwgR06MAaEAKzB5dsZQ3zrACv7vKpxvYpANiJQeRoNnJqoXgCGhLq7ZxEUdfsOZlMZm4YHNMw/tcCzRgmVnKx00HnxWhdZ36SFNODOoMzagEvo6BLt+3ntUNhzgz1kVkPPdQLBVwxXcqL2F4D9xmpk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 162090020971868.23437857683484; Thu, 13 May 2021 03:03:29 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.126807.238297 (Exim 4.92) (envelope-from ) id 1lh8BV-0004WH-JW; Thu, 13 May 2021 10:03:13 +0000 Received: by outflank-mailman (output) from mailman id 126807.238297; Thu, 13 May 2021 10:03:13 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lh8BV-0004W2-Ex; Thu, 13 May 2021 10:03:13 +0000 Received: by outflank-mailman (input) for mailman id 126807; Thu, 13 May 2021 10:03:12 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lh8BU-0003vg-2p for xen-devel@lists.xenproject.org; Thu, 13 May 2021 10:03:12 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 29589f22-b6ae-440a-94dd-2a81fbe2126e; Thu, 13 May 2021 10:03:06 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id B132FAFF5; Thu, 13 May 2021 10:03:05 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 29589f22-b6ae-440a-94dd-2a81fbe2126e X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1620900185; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lutTQX8ZA5u3khaAXqUgHLKbfap899noTeivh9Sx9NQ=; b=X9zTq81SYVIejJbZ3hqVnPgnP7pcUDx2BE/zHP4wRq7hBon4FcfafyN+fXITzioqYy+QoR pBz0uhcESpMAUzIdg50l7VW/TsBGw3s6HDsmczQ3YLqYI5+JC1vvrfRDwWsaYqSOxIH89y FBMrmVY8k7hSXXCMQGggw+D5zhxq8r4= From: Juergen Gross To: xen-devel@lists.xenproject.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Juergen Gross , Konrad Rzeszutek Wilk , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Boris Ostrovsky , Stefano Stabellini , Jens Axboe Subject: [PATCH 2/8] xen/blkfront: read response from backend only once Date: Thu, 13 May 2021 12:02:56 +0200 Message-Id: <20210513100302.22027-3-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210513100302.22027-1-jgross@suse.com> References: <20210513100302.22027-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) Content-Type: text/plain; charset="utf-8" In order to avoid problems in case the backend is modifying a response on the ring page while the frontend has already seen it, just read the response into a local buffer in one go and then operate on that buffer only. Signed-off-by: Juergen Gross Reviewed-by: Jan Beulich --- drivers/block/xen-blkfront.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/drivers/block/xen-blkfront.c b/drivers/block/xen-blkfront.c index 10df39a8b18d..a8b56c153330 100644 --- a/drivers/block/xen-blkfront.c +++ b/drivers/block/xen-blkfront.c @@ -1557,7 +1557,7 @@ static bool blkif_completion(unsigned long *id, static irqreturn_t blkif_interrupt(int irq, void *dev_id) { struct request *req; - struct blkif_response *bret; + struct blkif_response bret; RING_IDX i, rp; unsigned long flags; struct blkfront_ring_info *rinfo =3D (struct blkfront_ring_info *)dev_id; @@ -1574,8 +1574,9 @@ static irqreturn_t blkif_interrupt(int irq, void *dev= _id) for (i =3D rinfo->ring.rsp_cons; i !=3D rp; i++) { unsigned long id; =20 - bret =3D RING_GET_RESPONSE(&rinfo->ring, i); - id =3D bret->id; + RING_COPY_RESPONSE(&rinfo->ring, i, &bret); + id =3D bret.id; + /* * The backend has messed up and given us an id that we would * never have given to it (we stamp it up to BLK_RING_SIZE - @@ -1583,39 +1584,39 @@ static irqreturn_t blkif_interrupt(int irq, void *d= ev_id) */ if (id >=3D BLK_RING_SIZE(info)) { WARN(1, "%s: response to %s has incorrect id (%ld)\n", - info->gd->disk_name, op_name(bret->operation), id); + info->gd->disk_name, op_name(bret.operation), id); /* We can't safely get the 'struct request' as * the id is busted. */ continue; } req =3D rinfo->shadow[id].request; =20 - if (bret->operation !=3D BLKIF_OP_DISCARD) { + if (bret.operation !=3D BLKIF_OP_DISCARD) { /* * We may need to wait for an extra response if the * I/O request is split in 2 */ - if (!blkif_completion(&id, rinfo, bret)) + if (!blkif_completion(&id, rinfo, &bret)) continue; } =20 if (add_id_to_freelist(rinfo, id)) { WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n", - info->gd->disk_name, op_name(bret->operation), id); + info->gd->disk_name, op_name(bret.operation), id); continue; } =20 - if (bret->status =3D=3D BLKIF_RSP_OKAY) + if (bret.status =3D=3D BLKIF_RSP_OKAY) blkif_req(req)->error =3D BLK_STS_OK; else blkif_req(req)->error =3D BLK_STS_IOERR; =20 - switch (bret->operation) { + switch (bret.operation) { case BLKIF_OP_DISCARD: - if (unlikely(bret->status =3D=3D BLKIF_RSP_EOPNOTSUPP)) { + if (unlikely(bret.status =3D=3D BLKIF_RSP_EOPNOTSUPP)) { struct request_queue *rq =3D info->rq; printk(KERN_WARNING "blkfront: %s: %s op failed\n", - info->gd->disk_name, op_name(bret->operation)); + info->gd->disk_name, op_name(bret.operation)); blkif_req(req)->error =3D BLK_STS_NOTSUPP; info->feature_discard =3D 0; info->feature_secdiscard =3D 0; @@ -1625,15 +1626,15 @@ static irqreturn_t blkif_interrupt(int irq, void *d= ev_id) break; case BLKIF_OP_FLUSH_DISKCACHE: case BLKIF_OP_WRITE_BARRIER: - if (unlikely(bret->status =3D=3D BLKIF_RSP_EOPNOTSUPP)) { + if (unlikely(bret.status =3D=3D BLKIF_RSP_EOPNOTSUPP)) { printk(KERN_WARNING "blkfront: %s: %s op failed\n", - info->gd->disk_name, op_name(bret->operation)); + info->gd->disk_name, op_name(bret.operation)); blkif_req(req)->error =3D BLK_STS_NOTSUPP; } - if (unlikely(bret->status =3D=3D BLKIF_RSP_ERROR && + if (unlikely(bret.status =3D=3D BLKIF_RSP_ERROR && rinfo->shadow[id].req.u.rw.nr_segments =3D=3D 0)) { printk(KERN_WARNING "blkfront: %s: empty %s op failed\n", - info->gd->disk_name, op_name(bret->operation)); + info->gd->disk_name, op_name(bret.operation)); blkif_req(req)->error =3D BLK_STS_NOTSUPP; } if (unlikely(blkif_req(req)->error)) { @@ -1646,9 +1647,9 @@ static irqreturn_t blkif_interrupt(int irq, void *dev= _id) fallthrough; case BLKIF_OP_READ: case BLKIF_OP_WRITE: - if (unlikely(bret->status !=3D BLKIF_RSP_OKAY)) + if (unlikely(bret.status !=3D BLKIF_RSP_OKAY)) dev_dbg(&info->xbdev->dev, "Bad return from blkdev data " - "request: %x\n", bret->status); + "request: %x\n", bret.status); =20 break; default: --=20 2.26.2 From nobody Fri Nov 29 22:33:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1620900222; cv=none; d=zohomail.com; s=zohoarc; b=SW9CDKaY14V+/d0mTcJU0yzpXTaiQbWyRxCm9pkggDe0bPd0d8WOJweBeC3/XivW5aGpFYjaFD9rOe6Q/L8Z1hmdolhouuryENPUIaJ8r72CrFIh2IA8Xhb2Qxu35sbAiji/ftplMBmSwblKinZfkv8dEWSX1skIi/iio8stcsU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620900222; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=0Q6ThoS9c4shqaPBcM7YVpxtTsCqScDLVsepBzmlgCI=; b=jIt39MgNQhrSIgoVfLhXN4idZgRKoQJhZrycD0FsXh2zAa/VK9eETIk28bLrN3XAPpvn8fpkvjKZ2bTZNO36CbRu1IVbnQHHFCMUZ6jPboK5SH13iut1abYtSWWXDO4qFCj8mveQzD4MQg3DOGlyMu4rSulyESa3IT7fAmvUtMY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1620900222336509.6309138216784; Thu, 13 May 2021 03:03:42 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.126809.238320 (Exim 4.92) (envelope-from ) id 1lh8Ba-0005B6-DM; Thu, 13 May 2021 10:03:18 +0000 Received: by outflank-mailman (output) from mailman id 126809.238320; Thu, 13 May 2021 10:03:18 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lh8Ba-0005Ar-9m; Thu, 13 May 2021 10:03:18 +0000 Received: by outflank-mailman (input) for mailman id 126809; Thu, 13 May 2021 10:03:17 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lh8BZ-0003vg-34 for xen-devel@lists.xenproject.org; Thu, 13 May 2021 10:03:17 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 1c7d2393-cef0-489a-a074-21fe2b426a38; Thu, 13 May 2021 10:03:06 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id EB135AFF6; Thu, 13 May 2021 10:03:05 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 1c7d2393-cef0-489a-a074-21fe2b426a38 X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1620900186; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0Q6ThoS9c4shqaPBcM7YVpxtTsCqScDLVsepBzmlgCI=; b=KB4VO2b29rr8hcdZZAjHAppb7tKug2TKs7hwZV1cULAeBgNVDz9y3rAzxIUyt8uz4zhN1m wbkrBEc19J6BDwt9BDdkNjqE7EkKbsb19lj8El+kyBKqof1o5TWtGOxWrVw6bpy14oUpOL hA7SOkSFBinvHJbDqRzyWiVv2QpDJHA= From: Juergen Gross To: xen-devel@lists.xenproject.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Juergen Gross , Boris Ostrovsky , Stefano Stabellini , Konrad Rzeszutek Wilk , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jens Axboe Subject: [PATCH 3/8] xen/blkfront: don't take local copy of a request from the ring page Date: Thu, 13 May 2021 12:02:57 +0200 Message-Id: <20210513100302.22027-4-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210513100302.22027-1-jgross@suse.com> References: <20210513100302.22027-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) Content-Type: text/plain; charset="utf-8" In order to avoid a malicious backend being able to influence the local copy of a request build the request locally first and then copy it to the ring page instead of doing it the other way round as today. Signed-off-by: Juergen Gross Reviewed-by: Jan Beulich --- drivers/block/xen-blkfront.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/drivers/block/xen-blkfront.c b/drivers/block/xen-blkfront.c index a8b56c153330..c6a05de4f15f 100644 --- a/drivers/block/xen-blkfront.c +++ b/drivers/block/xen-blkfront.c @@ -546,7 +546,7 @@ static unsigned long blkif_ring_get_request(struct blkf= ront_ring_info *rinfo, rinfo->shadow[id].status =3D REQ_WAITING; rinfo->shadow[id].associated_id =3D NO_ASSOCIATED_ID; =20 - (*ring_req)->u.rw.id =3D id; + rinfo->shadow[id].req.u.rw.id =3D id; =20 return id; } @@ -554,11 +554,12 @@ static unsigned long blkif_ring_get_request(struct bl= kfront_ring_info *rinfo, static int blkif_queue_discard_req(struct request *req, struct blkfront_ri= ng_info *rinfo) { struct blkfront_info *info =3D rinfo->dev_info; - struct blkif_request *ring_req; + struct blkif_request *ring_req, *final_ring_req; unsigned long id; =20 /* Fill out a communications ring structure. */ - id =3D blkif_ring_get_request(rinfo, req, &ring_req); + id =3D blkif_ring_get_request(rinfo, req, &final_ring_req); + ring_req =3D &rinfo->shadow[id].req; =20 ring_req->operation =3D BLKIF_OP_DISCARD; ring_req->u.discard.nr_sectors =3D blk_rq_sectors(req); @@ -569,8 +570,8 @@ static int blkif_queue_discard_req(struct request *req,= struct blkfront_ring_inf else ring_req->u.discard.flag =3D 0; =20 - /* Keep a private copy so we can reissue requests when recovering. */ - rinfo->shadow[id].req =3D *ring_req; + /* Copy the request to the ring page. */ + *final_ring_req =3D *ring_req; =20 return 0; } @@ -703,6 +704,7 @@ static int blkif_queue_rw_req(struct request *req, stru= ct blkfront_ring_info *ri { struct blkfront_info *info =3D rinfo->dev_info; struct blkif_request *ring_req, *extra_ring_req =3D NULL; + struct blkif_request *final_ring_req, *final_extra_ring_req; unsigned long id, extra_id =3D NO_ASSOCIATED_ID; bool require_extra_req =3D false; int i; @@ -747,7 +749,8 @@ static int blkif_queue_rw_req(struct request *req, stru= ct blkfront_ring_info *ri } =20 /* Fill out a communications ring structure. */ - id =3D blkif_ring_get_request(rinfo, req, &ring_req); + id =3D blkif_ring_get_request(rinfo, req, &final_ring_req); + ring_req =3D &rinfo->shadow[id].req; =20 num_sg =3D blk_rq_map_sg(req->q, req, rinfo->shadow[id].sg); num_grant =3D 0; @@ -798,7 +801,9 @@ static int blkif_queue_rw_req(struct request *req, stru= ct blkfront_ring_info *ri ring_req->u.rw.nr_segments =3D num_grant; if (unlikely(require_extra_req)) { extra_id =3D blkif_ring_get_request(rinfo, req, - &extra_ring_req); + &final_extra_ring_req); + extra_ring_req =3D &rinfo->shadow[extra_id].req; + /* * Only the first request contains the scatter-gather * list. @@ -840,10 +845,10 @@ static int blkif_queue_rw_req(struct request *req, st= ruct blkfront_ring_info *ri if (setup.segments) kunmap_atomic(setup.segments); =20 - /* Keep a private copy so we can reissue requests when recovering. */ - rinfo->shadow[id].req =3D *ring_req; + /* Copy request(s) to the ring page. */ + *final_ring_req =3D *ring_req; if (unlikely(require_extra_req)) - rinfo->shadow[extra_id].req =3D *extra_ring_req; + *final_extra_ring_req =3D *extra_ring_req; =20 if (new_persistent_gnts) gnttab_free_grant_references(setup.gref_head); --=20 2.26.2 From nobody Fri Nov 29 22:33:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1620900217; cv=none; d=zohomail.com; s=zohoarc; b=fNkUTTDVjViayDTQY8G4MJ5/6BHcNaG/L0OvNvCrrUat2uuZFEVv60O5C0JouhB4mYcXVAK9ACEXmNpzRo7So5/nExoPpHlWBbSSvMYPmTInWAHFZhTH765wWZNZkvXScQ1xQZTdnZS4QFoKjZUM7TGkZG8owxTAWkzvB1esqjY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620900217; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=+55VMtjzppNIHp9ZFTWUzvbF11WwY57Fyil6Rw9VV3g=; b=ek3xBBdVqEsK6SIBG2vVAs8xPHn6MQ7vFEZcrVjjQ+wxJgw/pSSipAOWu22GleIk/4jTj/m5nrOgfCz8ZbYoZBwfJIemnTmxoyYkc0WXpfppYjg01RWtNCoMQ7hYg63Mafx3OGvyLTifWrtwfIyK7yb9XM53LqipprK+Kam/rnc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1620900217182186.99321307911634; Thu, 13 May 2021 03:03:37 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.126811.238345 (Exim 4.92) (envelope-from ) id 1lh8Bg-0005zi-Dh; Thu, 13 May 2021 10:03:24 +0000 Received: by outflank-mailman (output) from mailman id 126811.238345; Thu, 13 May 2021 10:03:24 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lh8Bg-0005zL-8S; Thu, 13 May 2021 10:03:24 +0000 Received: by outflank-mailman (input) for mailman id 126811; Thu, 13 May 2021 10:03:22 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lh8Be-0003vg-3U for xen-devel@lists.xenproject.org; Thu, 13 May 2021 10:03:22 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id a743bf04-aef4-409d-a481-7e626ae6bc5f; Thu, 13 May 2021 10:03:07 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 30CD0B021; Thu, 13 May 2021 10:03:06 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: a743bf04-aef4-409d-a481-7e626ae6bc5f X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1620900186; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+55VMtjzppNIHp9ZFTWUzvbF11WwY57Fyil6Rw9VV3g=; b=WIbclcgSxN14F4SQTwhdICOoy/oKAZ9BoqgICnTIhIpgk6Erd4l4kEsUzugP1UaejvjNbV SF+VCrj+ofIatBefBM2F1ruGfq4tOuGWV2bH5lzn6RI0YeWS3mPXlGIxGVYjtjjZvVYhSc QH0d4mMri26CSMZx7ZAGaRASXWH8GmQ= From: Juergen Gross To: xen-devel@lists.xenproject.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Juergen Gross , Konrad Rzeszutek Wilk , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Boris Ostrovsky , Stefano Stabellini , Jens Axboe Subject: [PATCH 4/8] xen/blkfront: don't trust the backend response data blindly Date: Thu, 13 May 2021 12:02:58 +0200 Message-Id: <20210513100302.22027-5-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210513100302.22027-1-jgross@suse.com> References: <20210513100302.22027-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) Content-Type: text/plain; charset="utf-8" Today blkfront will trust the backend to send only sane response data. In order to avoid privilege escalations or crashes in case of malicious backends verify the data to be within expected limits. Especially make sure that the response always references an outstanding request. Introduce a new state of the ring BLKIF_STATE_ERROR which will be switched to in case an inconsistency is being detected. Signed-off-by: Juergen Gross --- drivers/block/xen-blkfront.c | 62 +++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/drivers/block/xen-blkfront.c b/drivers/block/xen-blkfront.c index c6a05de4f15f..aa0f159829b4 100644 --- a/drivers/block/xen-blkfront.c +++ b/drivers/block/xen-blkfront.c @@ -80,6 +80,7 @@ enum blkif_state { BLKIF_STATE_DISCONNECTED, BLKIF_STATE_CONNECTED, BLKIF_STATE_SUSPENDED, + BLKIF_STATE_ERROR, }; =20 struct grant { @@ -89,6 +90,7 @@ struct grant { }; =20 enum blk_req_status { + REQ_PROCESSING, REQ_WAITING, REQ_DONE, REQ_ERROR, @@ -543,7 +545,7 @@ static unsigned long blkif_ring_get_request(struct blkf= ront_ring_info *rinfo, =20 id =3D get_id_from_freelist(rinfo); rinfo->shadow[id].request =3D req; - rinfo->shadow[id].status =3D REQ_WAITING; + rinfo->shadow[id].status =3D REQ_PROCESSING; rinfo->shadow[id].associated_id =3D NO_ASSOCIATED_ID; =20 rinfo->shadow[id].req.u.rw.id =3D id; @@ -572,6 +574,7 @@ static int blkif_queue_discard_req(struct request *req,= struct blkfront_ring_inf =20 /* Copy the request to the ring page. */ *final_ring_req =3D *ring_req; + rinfo->shadow[id].status =3D REQ_WAITING; =20 return 0; } @@ -847,8 +850,11 @@ static int blkif_queue_rw_req(struct request *req, str= uct blkfront_ring_info *ri =20 /* Copy request(s) to the ring page. */ *final_ring_req =3D *ring_req; - if (unlikely(require_extra_req)) + rinfo->shadow[id].status =3D REQ_WAITING; + if (unlikely(require_extra_req)) { *final_extra_ring_req =3D *extra_ring_req; + rinfo->shadow[extra_id].status =3D REQ_WAITING; + } =20 if (new_persistent_gnts) gnttab_free_grant_references(setup.gref_head); @@ -1420,8 +1426,8 @@ static enum blk_req_status blkif_rsp_to_req_status(in= t rsp) static int blkif_get_final_status(enum blk_req_status s1, enum blk_req_status s2) { - BUG_ON(s1 =3D=3D REQ_WAITING); - BUG_ON(s2 =3D=3D REQ_WAITING); + BUG_ON(s1 < REQ_DONE); + BUG_ON(s2 < REQ_DONE); =20 if (s1 =3D=3D REQ_ERROR || s2 =3D=3D REQ_ERROR) return BLKIF_RSP_ERROR; @@ -1454,7 +1460,7 @@ static bool blkif_completion(unsigned long *id, s->status =3D blkif_rsp_to_req_status(bret->status); =20 /* Wait the second response if not yet here. */ - if (s2->status =3D=3D REQ_WAITING) + if (s2->status < REQ_DONE) return false; =20 bret->status =3D blkif_get_final_status(s->status, @@ -1574,10 +1580,16 @@ static irqreturn_t blkif_interrupt(int irq, void *d= ev_id) spin_lock_irqsave(&rinfo->ring_lock, flags); again: rp =3D rinfo->ring.sring->rsp_prod; + if (RING_RESPONSE_PROD_OVERFLOW(&rinfo->ring, rp)) { + pr_alert("%s: illegal number of responses %u\n", + info->gd->disk_name, rp - rinfo->ring.rsp_cons); + goto err; + } rmb(); /* Ensure we see queued responses up to 'rp'. */ =20 for (i =3D rinfo->ring.rsp_cons; i !=3D rp; i++) { unsigned long id; + unsigned int op; =20 RING_COPY_RESPONSE(&rinfo->ring, i, &bret); id =3D bret.id; @@ -1588,14 +1600,28 @@ static irqreturn_t blkif_interrupt(int irq, void *d= ev_id) * look in get_id_from_freelist. */ if (id >=3D BLK_RING_SIZE(info)) { - WARN(1, "%s: response to %s has incorrect id (%ld)\n", - info->gd->disk_name, op_name(bret.operation), id); - /* We can't safely get the 'struct request' as - * the id is busted. */ - continue; + pr_alert("%s: response has incorrect id (%ld)\n", + info->gd->disk_name, id); + goto err; } + if (rinfo->shadow[id].status !=3D REQ_WAITING) { + pr_alert("%s: response references no pending request\n", + info->gd->disk_name); + goto err; + } + + rinfo->shadow[id].status =3D REQ_PROCESSING; req =3D rinfo->shadow[id].request; =20 + op =3D rinfo->shadow[id].req.operation; + if (op =3D=3D BLKIF_OP_INDIRECT) + op =3D rinfo->shadow[id].req.u.indirect.indirect_op; + if (bret.operation !=3D op) { + pr_alert("%s: response has wrong operation (%u instead of %u)\n", + info->gd->disk_name, bret.operation, op); + goto err; + } + if (bret.operation !=3D BLKIF_OP_DISCARD) { /* * We may need to wait for an extra response if the @@ -1620,7 +1646,8 @@ static irqreturn_t blkif_interrupt(int irq, void *dev= _id) case BLKIF_OP_DISCARD: if (unlikely(bret.status =3D=3D BLKIF_RSP_EOPNOTSUPP)) { struct request_queue *rq =3D info->rq; - printk(KERN_WARNING "blkfront: %s: %s op failed\n", + + pr_warn_ratelimited("blkfront: %s: %s op failed\n", info->gd->disk_name, op_name(bret.operation)); blkif_req(req)->error =3D BLK_STS_NOTSUPP; info->feature_discard =3D 0; @@ -1632,13 +1659,13 @@ static irqreturn_t blkif_interrupt(int irq, void *d= ev_id) case BLKIF_OP_FLUSH_DISKCACHE: case BLKIF_OP_WRITE_BARRIER: if (unlikely(bret.status =3D=3D BLKIF_RSP_EOPNOTSUPP)) { - printk(KERN_WARNING "blkfront: %s: %s op failed\n", + pr_warn_ratelimited("blkfront: %s: %s op failed\n", info->gd->disk_name, op_name(bret.operation)); blkif_req(req)->error =3D BLK_STS_NOTSUPP; } if (unlikely(bret.status =3D=3D BLKIF_RSP_ERROR && rinfo->shadow[id].req.u.rw.nr_segments =3D=3D 0)) { - printk(KERN_WARNING "blkfront: %s: empty %s op failed\n", + pr_warn_ratelimited("blkfront: %s: empty %s op failed\n", info->gd->disk_name, op_name(bret.operation)); blkif_req(req)->error =3D BLK_STS_NOTSUPP; } @@ -1653,8 +1680,8 @@ static irqreturn_t blkif_interrupt(int irq, void *dev= _id) case BLKIF_OP_READ: case BLKIF_OP_WRITE: if (unlikely(bret.status !=3D BLKIF_RSP_OKAY)) - dev_dbg(&info->xbdev->dev, "Bad return from blkdev data " - "request: %x\n", bret.status); + dev_dbg_ratelimited(&info->xbdev->dev, + "Bad return from blkdev data request: %x\n", bret.status); =20 break; default: @@ -1680,6 +1707,11 @@ static irqreturn_t blkif_interrupt(int irq, void *de= v_id) spin_unlock_irqrestore(&rinfo->ring_lock, flags); =20 return IRQ_HANDLED; + + err: + info->connected =3D BLKIF_STATE_ERROR; + pr_alert("%s disabled for further use\n", info->gd->disk_name); + return IRQ_HANDLED; } =20 =20 --=20 2.26.2 From nobody Fri Nov 29 22:33:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1620900228; cv=none; d=zohomail.com; s=zohoarc; b=lLg+B9JWZhE17VDnY13Gwvwf7SAifXSbo5OjshNEtdHwk28fV2AfPHUVbvrvyOUjbb+iNyYQyUhib4mPY5ZtNW2zUnB4L2uA7hi3NlF0gensCuZijqzXqKb1Bti4U4ZVcnXmcmyAHVGCUaFnyWadMqysJV9V3KpepexqAujZPmM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620900228; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ygDKbDhfDMlj04vAZT1nQUuD2Bsz2E2EX2ert/+Sldc=; b=At8v1gifPQ9wxH4c7xvV93Wff5PwliBzPbO9Xx6oxvk2cC0Qs9cu3zCkLUF9/VYgcMP9TQDdT+MJiRzzKYII9U1sBoeQsCLSVF1iqWwFgt92q6ulxROOrkRA+SEDKK+HPOnuTxVxQNI7tmfEV/9LI1IF7DeDf0I8bduz8fcF0rg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1620900228177876.7214082971051; Thu, 13 May 2021 03:03:48 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.126814.238357 (Exim 4.92) (envelope-from ) id 1lh8Bk-0006XQ-Rf; Thu, 13 May 2021 10:03:28 +0000 Received: by outflank-mailman (output) from mailman id 126814.238357; Thu, 13 May 2021 10:03:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lh8Bk-0006X1-LP; Thu, 13 May 2021 10:03:28 +0000 Received: by outflank-mailman (input) for mailman id 126814; Thu, 13 May 2021 10:03:27 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lh8Bj-0003vg-3n for xen-devel@lists.xenproject.org; Thu, 13 May 2021 10:03:27 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id e15c491b-d62f-4b73-aa93-74a38cbc588f; Thu, 13 May 2021 10:03:07 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 68AE3B062; Thu, 13 May 2021 10:03:06 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: e15c491b-d62f-4b73-aa93-74a38cbc588f X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1620900186; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ygDKbDhfDMlj04vAZT1nQUuD2Bsz2E2EX2ert/+Sldc=; b=iGbXPTsViBTDLv+bdZJGVl+oV4HRm0TVz4r6wDci4iCRCW2Hm5WIIRHkHZxUc0LEd1sO1H vwqUv4y3NiJHJBX0idWsuTAMxSTOznt4q/IRhurE8VkFp4pjlCxdnjec6RwkyKSaTT5CBc PnbZFESlVi1Q5KeT0sJ2wcfmvqVNRzk= From: Juergen Gross To: xen-devel@lists.xenproject.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Juergen Gross , Boris Ostrovsky , Stefano Stabellini , "David S. Miller" , Jakub Kicinski Subject: [PATCH 5/8] xen/netfront: read response from backend only once Date: Thu, 13 May 2021 12:02:59 +0200 Message-Id: <20210513100302.22027-6-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210513100302.22027-1-jgross@suse.com> References: <20210513100302.22027-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) Content-Type: text/plain; charset="utf-8" In order to avoid problems in case the backend is modifying a response on the ring page while the frontend has already seen it, just read the response into a local buffer in one go and then operate on that buffer only. Signed-off-by: Juergen Gross Reviewed-by: Jan Beulich --- drivers/net/xen-netfront.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/drivers/net/xen-netfront.c b/drivers/net/xen-netfront.c index 44275908d61a..f91e41ece554 100644 --- a/drivers/net/xen-netfront.c +++ b/drivers/net/xen-netfront.c @@ -399,13 +399,13 @@ static void xennet_tx_buf_gc(struct netfront_queue *q= ueue) rmb(); /* Ensure we see responses up to 'rp'. */ =20 for (cons =3D queue->tx.rsp_cons; cons !=3D prod; cons++) { - struct xen_netif_tx_response *txrsp; + struct xen_netif_tx_response txrsp; =20 - txrsp =3D RING_GET_RESPONSE(&queue->tx, cons); - if (txrsp->status =3D=3D XEN_NETIF_RSP_NULL) + RING_COPY_RESPONSE(&queue->tx, cons, &txrsp); + if (txrsp.status =3D=3D XEN_NETIF_RSP_NULL) continue; =20 - id =3D txrsp->id; + id =3D txrsp.id; skb =3D queue->tx_skbs[id].skb; if (unlikely(gnttab_query_foreign_access( queue->grant_tx_ref[id]) !=3D 0)) { @@ -814,7 +814,7 @@ static int xennet_get_extras(struct netfront_queue *que= ue, RING_IDX rp) =20 { - struct xen_netif_extra_info *extra; + struct xen_netif_extra_info extra; struct device *dev =3D &queue->info->netdev->dev; RING_IDX cons =3D queue->rx.rsp_cons; int err =3D 0; @@ -830,24 +830,22 @@ static int xennet_get_extras(struct netfront_queue *q= ueue, break; } =20 - extra =3D (struct xen_netif_extra_info *) - RING_GET_RESPONSE(&queue->rx, ++cons); + RING_COPY_RESPONSE(&queue->rx, ++cons, &extra); =20 - if (unlikely(!extra->type || - extra->type >=3D XEN_NETIF_EXTRA_TYPE_MAX)) { + if (unlikely(!extra.type || + extra.type >=3D XEN_NETIF_EXTRA_TYPE_MAX)) { if (net_ratelimit()) dev_warn(dev, "Invalid extra type: %d\n", - extra->type); + extra.type); err =3D -EINVAL; } else { - memcpy(&extras[extra->type - 1], extra, - sizeof(*extra)); + memcpy(&extras[extra.type - 1], &extra, sizeof(extra)); } =20 skb =3D xennet_get_rx_skb(queue, cons); ref =3D xennet_get_rx_ref(queue, cons); xennet_move_rx_slot(queue, skb, ref); - } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE); + } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE); =20 queue->rx.rsp_cons =3D cons; return err; @@ -905,7 +903,7 @@ static int xennet_get_responses(struct netfront_queue *= queue, struct sk_buff_head *list, bool *need_xdp_flush) { - struct xen_netif_rx_response *rx =3D &rinfo->rx; + struct xen_netif_rx_response *rx =3D &rinfo->rx, rx_local; int max =3D XEN_NETIF_NR_SLOTS_MIN + (rx->status <=3D RX_COPY_THRESHOLD); RING_IDX cons =3D queue->rx.rsp_cons; struct sk_buff *skb =3D xennet_get_rx_skb(queue, cons); @@ -989,7 +987,8 @@ static int xennet_get_responses(struct netfront_queue *= queue, break; } =20 - rx =3D RING_GET_RESPONSE(&queue->rx, cons + slots); + RING_COPY_RESPONSE(&queue->rx, cons + slots, &rx_local); + rx =3D &rx_local; skb =3D xennet_get_rx_skb(queue, cons + slots); ref =3D xennet_get_rx_ref(queue, cons + slots); slots++; @@ -1044,10 +1043,11 @@ static int xennet_fill_frags(struct netfront_queue = *queue, struct sk_buff *nskb; =20 while ((nskb =3D __skb_dequeue(list))) { - struct xen_netif_rx_response *rx =3D - RING_GET_RESPONSE(&queue->rx, ++cons); + struct xen_netif_rx_response rx; skb_frag_t *nfrag =3D &skb_shinfo(nskb)->frags[0]; =20 + RING_COPY_RESPONSE(&queue->rx, ++cons, &rx); + if (skb_shinfo(skb)->nr_frags =3D=3D MAX_SKB_FRAGS) { unsigned int pull_to =3D NETFRONT_SKB_CB(skb)->pull_to; =20 @@ -1062,7 +1062,7 @@ static int xennet_fill_frags(struct netfront_queue *q= ueue, =20 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, skb_frag_page(nfrag), - rx->offset, rx->status, PAGE_SIZE); + rx.offset, rx.status, PAGE_SIZE); =20 skb_shinfo(nskb)->nr_frags =3D 0; kfree_skb(nskb); @@ -1161,7 +1161,7 @@ static int xennet_poll(struct napi_struct *napi, int = budget) i =3D queue->rx.rsp_cons; work_done =3D 0; while ((i !=3D rp) && (work_done < budget)) { - memcpy(rx, RING_GET_RESPONSE(&queue->rx, i), sizeof(*rx)); + RING_COPY_RESPONSE(&queue->rx, i, rx); memset(extras, 0, sizeof(rinfo.extras)); =20 err =3D xennet_get_responses(queue, &rinfo, rp, &tmpq, --=20 2.26.2 From nobody Fri Nov 29 22:33:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1620900208; cv=none; d=zohomail.com; s=zohoarc; b=JU5TtsVyi2MB9LMAyn0auHb9Fu5STFRpAc2d27+MVLsSihDzTukoYpWo7+0ND9bCtp42E0FuYZAIvf7Mz555UxBHi2HhW2B9ogDRfXAVP8UvDyOEf+c2Sn1l7yZnvxwrTVtOenfaqSiGI372uluPhpu0/nn9kTKr89Ic/g28Hk8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620900208; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=g+1fyoENkOuy7Zh/t/9f7BoPHxXHhIGX5kJfVKmIRpA=; b=TNyS6R22RvtA+tbJTNKcZKu5IZlIti6LATqi7hgjJPGPcH3HFsIGKbzZ7FKwE9OZyDSrMlPysiypR4w5vtOmXPucbhmM3842R6Vo0XfpWYC50KtLmNuNH+QUlpfe9jbSGIypCOom/KVMdJ9Q6ta0HXEMgoqqhckpIyRZoy3xZVg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1620900208589388.73788645769446; Thu, 13 May 2021 03:03:28 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.126806.238285 (Exim 4.92) (envelope-from ) id 1lh8BS-0004CR-A3; Thu, 13 May 2021 10:03:10 +0000 Received: by outflank-mailman (output) from mailman id 126806.238285; Thu, 13 May 2021 10:03:10 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lh8BS-0004CK-5n; Thu, 13 May 2021 10:03:10 +0000 Received: by outflank-mailman (input) for mailman id 126806; Thu, 13 May 2021 10:03:08 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lh8BQ-0003vw-Nm for xen-devel@lists.xenproject.org; Thu, 13 May 2021 10:03:08 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 5e7d7ef8-c3c2-48a1-a0e8-26c163d772ed; Thu, 13 May 2021 10:03:07 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 9FC13B05D; Thu, 13 May 2021 10:03:06 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 5e7d7ef8-c3c2-48a1-a0e8-26c163d772ed X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1620900186; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=g+1fyoENkOuy7Zh/t/9f7BoPHxXHhIGX5kJfVKmIRpA=; b=g/KYhXDBmr+jGf2DUNwYpTYmyFb5iID0n+JwQIYx5JoW7VIHybKPbSnGj1UPKNEw9rGj7B MiaQabJO4iSCO/G/rIVuX8Zf026zsfTmGZsYXeuDjGQ5Idoe7g910FX9BfA6fL9/j32Q2m vig7EKf4L+OCtklY3VcDg8L1QLco2XY= From: Juergen Gross To: xen-devel@lists.xenproject.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Juergen Gross , Boris Ostrovsky , Stefano Stabellini , "David S. Miller" , Jakub Kicinski Subject: [PATCH 6/8] xen/netfront: don't read data from request on the ring page Date: Thu, 13 May 2021 12:03:00 +0200 Message-Id: <20210513100302.22027-7-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210513100302.22027-1-jgross@suse.com> References: <20210513100302.22027-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) Content-Type: text/plain; charset="utf-8" In order to avoid a malicious backend being able to influence the local processing of a request build the request locally first and then copy it to the ring page. Any reading from the request needs to be done on the local instance. Signed-off-by: Juergen Gross --- drivers/net/xen-netfront.c | 75 ++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 39 deletions(-) diff --git a/drivers/net/xen-netfront.c b/drivers/net/xen-netfront.c index f91e41ece554..261c35be0147 100644 --- a/drivers/net/xen-netfront.c +++ b/drivers/net/xen-netfront.c @@ -435,7 +435,8 @@ struct xennet_gnttab_make_txreq { struct netfront_queue *queue; struct sk_buff *skb; struct page *page; - struct xen_netif_tx_request *tx; /* Last request */ + struct xen_netif_tx_request *tx; /* Last request on ring page */ + struct xen_netif_tx_request tx_local; /* Last request local copy*/ unsigned int size; }; =20 @@ -463,30 +464,27 @@ static void xennet_tx_setup_grant(unsigned long gfn, = unsigned int offset, queue->grant_tx_page[id] =3D page; queue->grant_tx_ref[id] =3D ref; =20 - tx->id =3D id; - tx->gref =3D ref; - tx->offset =3D offset; - tx->size =3D len; - tx->flags =3D 0; + info->tx_local.id =3D id; + info->tx_local.gref =3D ref; + info->tx_local.offset =3D offset; + info->tx_local.size =3D len; + info->tx_local.flags =3D 0; + + *tx =3D info->tx_local; =20 info->tx =3D tx; - info->size +=3D tx->size; + info->size +=3D info->tx_local.size; } =20 static struct xen_netif_tx_request *xennet_make_first_txreq( - struct netfront_queue *queue, struct sk_buff *skb, - struct page *page, unsigned int offset, unsigned int len) + struct xennet_gnttab_make_txreq *info, + unsigned int offset, unsigned int len) { - struct xennet_gnttab_make_txreq info =3D { - .queue =3D queue, - .skb =3D skb, - .page =3D page, - .size =3D 0, - }; + info->size =3D 0; =20 - gnttab_for_one_grant(page, offset, len, xennet_tx_setup_grant, &info); + gnttab_for_one_grant(info->page, offset, len, xennet_tx_setup_grant, info= ); =20 - return info.tx; + return info->tx; } =20 static void xennet_make_one_txreq(unsigned long gfn, unsigned int offset, @@ -499,35 +497,27 @@ static void xennet_make_one_txreq(unsigned long gfn, = unsigned int offset, xennet_tx_setup_grant(gfn, offset, len, data); } =20 -static struct xen_netif_tx_request *xennet_make_txreqs( - struct netfront_queue *queue, struct xen_netif_tx_request *tx, - struct sk_buff *skb, struct page *page, +static void xennet_make_txreqs( + struct xennet_gnttab_make_txreq *info, + struct page *page, unsigned int offset, unsigned int len) { - struct xennet_gnttab_make_txreq info =3D { - .queue =3D queue, - .skb =3D skb, - .tx =3D tx, - }; - /* Skip unused frames from start of page */ page +=3D offset >> PAGE_SHIFT; offset &=3D ~PAGE_MASK; =20 while (len) { - info.page =3D page; - info.size =3D 0; + info->page =3D page; + info->size =3D 0; =20 gnttab_foreach_grant_in_range(page, offset, len, xennet_make_one_txreq, - &info); + info); =20 page++; offset =3D 0; - len -=3D info.size; + len -=3D info->size; } - - return info.tx; } =20 /* @@ -580,10 +570,14 @@ static int xennet_xdp_xmit_one(struct net_device *dev, { struct netfront_info *np =3D netdev_priv(dev); struct netfront_stats *tx_stats =3D this_cpu_ptr(np->tx_stats); + struct xennet_gnttab_make_txreq info =3D { + .queue =3D queue, + .skb =3D NULL, + .page =3D virt_to_page(xdpf->data), + }; int notify; =20 - xennet_make_first_txreq(queue, NULL, - virt_to_page(xdpf->data), + xennet_make_first_txreq(&info, offset_in_page(xdpf->data), xdpf->len); =20 @@ -647,6 +641,7 @@ static netdev_tx_t xennet_start_xmit(struct sk_buff *sk= b, struct net_device *dev unsigned int len; unsigned long flags; struct netfront_queue *queue =3D NULL; + struct xennet_gnttab_make_txreq info =3D { }; unsigned int num_queues =3D dev->real_num_tx_queues; u16 queue_index; struct sk_buff *nskb; @@ -704,14 +699,16 @@ static netdev_tx_t xennet_start_xmit(struct sk_buff *= skb, struct net_device *dev } =20 /* First request for the linear area. */ - first_tx =3D tx =3D xennet_make_first_txreq(queue, skb, - page, offset, len); + info.queue =3D queue; + info.skb =3D skb; + info.page =3D page; + first_tx =3D tx =3D xennet_make_first_txreq(&info, offset, len); offset +=3D tx->size; if (offset =3D=3D PAGE_SIZE) { page++; offset =3D 0; } - len -=3D tx->size; + len -=3D info.tx_local.size; =20 if (skb->ip_summed =3D=3D CHECKSUM_PARTIAL) /* local packet? */ @@ -741,12 +738,12 @@ static netdev_tx_t xennet_start_xmit(struct sk_buff *= skb, struct net_device *dev } =20 /* Requests for the rest of the linear area. */ - tx =3D xennet_make_txreqs(queue, tx, skb, page, offset, len); + xennet_make_txreqs(&info, page, offset, len); =20 /* Requests for all the frags. */ for (i =3D 0; i < skb_shinfo(skb)->nr_frags; i++) { skb_frag_t *frag =3D &skb_shinfo(skb)->frags[i]; - tx =3D xennet_make_txreqs(queue, tx, skb, skb_frag_page(frag), + xennet_make_txreqs(&info, skb_frag_page(frag), skb_frag_off(frag), skb_frag_size(frag)); } --=20 2.26.2 From nobody Fri Nov 29 22:33:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1620900212; cv=none; d=zohomail.com; s=zohoarc; b=XTckmd1NpLwm5dri+//kURrmcx7zrvh6NJwEcVy3v7jLQxJdzSHUVi2U1fms/E/DjcAEp439BG2cf0sKkVt2lNQmuYDwoEPgWgj3WOBP5pcEy2FzCMNr2F8kHnEeqDdjvgyqCRNA+I0ABcz6KEoK5CrQow0NVeY0Wqs6VG+zJuk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620900212; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=IvMcIqdk9ustdCpVTzueaYDVUeZhxZ/EAhd4OmpuXOI=; b=Y5Idtbw6qzhUtcp7faIVp/xnE1dFEykzXxruGpIAI4ePImniImMIRq7cyqwO1Y2zG98mBkeEgsWUGcvCdteCzsG5T7GrrsKd72cldKA0ymH7HmfocAfVHqmI2RfqZEOw0EpvdOVRQ9mxOV8LsxtGYK4+/7EYAGqqBCTxDYMBrnw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 162090021246262.09849731493102; Thu, 13 May 2021 03:03:32 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.126810.238333 (Exim 4.92) (envelope-from ) id 1lh8Bb-0005Sa-Po; Thu, 13 May 2021 10:03:19 +0000 Received: by outflank-mailman (output) from mailman id 126810.238333; Thu, 13 May 2021 10:03:19 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lh8Bb-0005SL-JS; Thu, 13 May 2021 10:03:19 +0000 Received: by outflank-mailman (input) for mailman id 126810; Thu, 13 May 2021 10:03:18 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lh8Ba-0003vw-Lj for xen-devel@lists.xenproject.org; Thu, 13 May 2021 10:03:18 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 43127006-13b6-4fd0-b5f8-b9b88ad5c80f; Thu, 13 May 2021 10:03:08 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id D6B09B156; Thu, 13 May 2021 10:03:06 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 43127006-13b6-4fd0-b5f8-b9b88ad5c80f X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1620900187; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IvMcIqdk9ustdCpVTzueaYDVUeZhxZ/EAhd4OmpuXOI=; b=ZtkMcNuE8kcXYMHWB1eAm7u1BAxzlLMEVdB2/nA1DPxetMUzGjtcv/vVkiGbyoq1n6El5i PAGibUSBe7Lu5nOiM0Qi+5bXkfUI8pECgwiYR2WcylRx5+Zaxpept3Jo1M/ifP+MvuluDv yvkasKkirJJf3j0dKyowWy7EMykFZjc= From: Juergen Gross To: xen-devel@lists.xenproject.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Juergen Gross , Boris Ostrovsky , Stefano Stabellini , "David S. Miller" , Jakub Kicinski Subject: [PATCH 7/8] xen/netfront: don't trust the backend response data blindly Date: Thu, 13 May 2021 12:03:01 +0200 Message-Id: <20210513100302.22027-8-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210513100302.22027-1-jgross@suse.com> References: <20210513100302.22027-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) Content-Type: text/plain; charset="utf-8" Today netfront will trust the backend to send only sane response data. In order to avoid privilege escalations or crashes in case of malicious backends verify the data to be within expected limits. Especially make sure that the response always references an outstanding request. Note that only the tx queue needs special id handling, as for the rx queue the id is equal to the index in the ring page. Introduce a new indicator for the device whether it is broken and let the device stop working when it is set. Set this indicator in case the backend sets any weird data. Signed-off-by: Juergen Gross --- drivers/net/xen-netfront.c | 71 +++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 4 deletions(-) diff --git a/drivers/net/xen-netfront.c b/drivers/net/xen-netfront.c index 261c35be0147..ccd6d1389b0a 100644 --- a/drivers/net/xen-netfront.c +++ b/drivers/net/xen-netfront.c @@ -154,6 +154,8 @@ struct netfront_queue { =20 struct page_pool *page_pool; struct xdp_rxq_info xdp_rxq; + + bool tx_pending[NET_TX_RING_SIZE]; }; =20 struct netfront_info { @@ -173,6 +175,9 @@ struct netfront_info { bool netback_has_xdp_headroom; bool netfront_xdp_enabled; =20 + /* Is device behaving sane? */ + bool broken; + atomic_t rx_gso_checksum_fixup; }; =20 @@ -363,7 +368,7 @@ static int xennet_open(struct net_device *dev) unsigned int i =3D 0; struct netfront_queue *queue =3D NULL; =20 - if (!np->queues) + if (!np->queues || np->broken) return -ENODEV; =20 for (i =3D 0; i < num_queues; ++i) { @@ -391,11 +396,17 @@ static void xennet_tx_buf_gc(struct netfront_queue *q= ueue) unsigned short id; struct sk_buff *skb; bool more_to_do; + const struct device *dev =3D &queue->info->netdev->dev; =20 BUG_ON(!netif_carrier_ok(queue->info->netdev)); =20 do { prod =3D queue->tx.sring->rsp_prod; + if (RING_RESPONSE_PROD_OVERFLOW(&queue->tx, prod)) { + dev_alert(dev, "Illegal number of responses %u\n", + prod - queue->tx.rsp_cons); + goto err; + } rmb(); /* Ensure we see responses up to 'rp'. */ =20 for (cons =3D queue->tx.rsp_cons; cons !=3D prod; cons++) { @@ -406,12 +417,25 @@ static void xennet_tx_buf_gc(struct netfront_queue *q= ueue) continue; =20 id =3D txrsp.id; + if (id >=3D RING_SIZE(&queue->tx)) { + dev_alert(dev, + "Response has incorrect id (%u)\n", + id); + goto err; + } + if (!queue->tx_pending[id]) { + dev_alert(dev, + "Response for inactive request\n"); + goto err; + } + + queue->tx_pending[id] =3D false; skb =3D queue->tx_skbs[id].skb; if (unlikely(gnttab_query_foreign_access( queue->grant_tx_ref[id]) !=3D 0)) { - pr_alert("%s: warning -- grant still in use by backend domain\n", - __func__); - BUG(); + dev_alert(dev, + "Grant still in use by backend domain\n"); + goto err; } gnttab_end_foreign_access_ref( queue->grant_tx_ref[id], GNTMAP_readonly); @@ -429,6 +453,12 @@ static void xennet_tx_buf_gc(struct netfront_queue *qu= eue) } while (more_to_do); =20 xennet_maybe_wake_tx(queue); + + return; + + err: + queue->info->broken =3D true; + dev_alert(dev, "Disabled for further use\n"); } =20 struct xennet_gnttab_make_txreq { @@ -472,6 +502,13 @@ static void xennet_tx_setup_grant(unsigned long gfn, u= nsigned int offset, =20 *tx =3D info->tx_local; =20 + /* + * The request is not in its final form, as size and flags might be + * modified later, but even if a malicious backend will send a response + * now, nothing bad regarding security could happen. + */ + queue->tx_pending[id] =3D true; + info->tx =3D tx; info->size +=3D info->tx_local.size; } @@ -605,6 +642,8 @@ static int xennet_xdp_xmit(struct net_device *dev, int = n, int nxmit =3D 0; int i; =20 + if (unlikely(np->broken)) + return -ENODEV; if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) return -EINVAL; =20 @@ -649,6 +688,8 @@ static netdev_tx_t xennet_start_xmit(struct sk_buff *sk= b, struct net_device *dev /* Drop the packet if no queues are set up */ if (num_queues < 1) goto drop; + if (unlikely(np->broken)) + goto drop; /* Determine which queue to transmit this SKB on */ queue_index =3D skb_get_queue_mapping(skb); queue =3D &np->queues[queue_index]; @@ -1153,6 +1194,13 @@ static int xennet_poll(struct napi_struct *napi, int= budget) skb_queue_head_init(&tmpq); =20 rp =3D queue->rx.sring->rsp_prod; + if (RING_RESPONSE_PROD_OVERFLOW(&queue->rx, rp)) { + dev_alert(&dev->dev, "Illegal number of responses %u\n", + rp - queue->rx.rsp_cons); + queue->info->broken =3D true; + spin_unlock(&queue->rx_lock); + return 0; + } rmb(); /* Ensure we see queued responses up to 'rp'. */ =20 i =3D queue->rx.rsp_cons; @@ -1373,6 +1421,9 @@ static irqreturn_t xennet_tx_interrupt(int irq, void = *dev_id) struct netfront_queue *queue =3D dev_id; unsigned long flags; =20 + if (queue->info->broken) + return IRQ_HANDLED; + spin_lock_irqsave(&queue->tx_lock, flags); xennet_tx_buf_gc(queue); spin_unlock_irqrestore(&queue->tx_lock, flags); @@ -1385,6 +1436,9 @@ static irqreturn_t xennet_rx_interrupt(int irq, void = *dev_id) struct netfront_queue *queue =3D dev_id; struct net_device *dev =3D queue->info->netdev; =20 + if (queue->info->broken) + return IRQ_HANDLED; + if (likely(netif_carrier_ok(dev) && RING_HAS_UNCONSUMED_RESPONSES(&queue->rx))) napi_schedule(&queue->napi); @@ -1406,6 +1460,10 @@ static void xennet_poll_controller(struct net_device= *dev) struct netfront_info *info =3D netdev_priv(dev); unsigned int num_queues =3D dev->real_num_tx_queues; unsigned int i; + + if (info->broken) + return; + for (i =3D 0; i < num_queues; ++i) xennet_interrupt(0, &info->queues[i]); } @@ -1477,6 +1535,11 @@ static int xennet_xdp_set(struct net_device *dev, st= ruct bpf_prog *prog, =20 static int xennet_xdp(struct net_device *dev, struct netdev_bpf *xdp) { + struct netfront_info *np =3D netdev_priv(dev); + + if (np->broken) + return -ENODEV; + switch (xdp->command) { case XDP_SETUP_PROG: return xennet_xdp_set(dev, xdp->prog, xdp->extack); --=20 2.26.2 From nobody Fri Nov 29 22:33:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1620900227; cv=none; d=zohomail.com; s=zohoarc; b=Kpb7hVJJ3qQ14Jf/Hf/aVudW5wenyv6mEFjwMQ6s8L9VhQvILTLTldbKP9+zCFv9+FN1dT6hqU82RIWN+Pmn/TaHoDrEHRTRvuz/WccbURpwdf2yuh943yoIhJW6lLCusSTWP273R5ptSXI+pw9Rnx2Qc7Dw8bWWF5ZKCXhjFEs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620900227; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=j+l5tq5M/f1kECXbl74yHxqhOUkbjQkUE4NZC677IzI=; b=Pjcj2H/WlYJ0qrE+Iw3AShkyKIQzIFJodiTW+IsxS5XIZc3wjBl2YLd/uiUiVb+Ek4AS0BhR2Kl53CBlacqqBT3kvf6MRR5LC/RG8A5lR9A2kSbHfMzLr8OeTx2u335TidDVCLkDU+OPYBAGZts+YIdp2M0cTvHgsM5Sq8cPM60= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1620900227808369.2239151010624; Thu, 13 May 2021 03:03:47 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.126817.238369 (Exim 4.92) (envelope-from ) id 1lh8Bq-0007OF-5m; Thu, 13 May 2021 10:03:34 +0000 Received: by outflank-mailman (output) from mailman id 126817.238369; Thu, 13 May 2021 10:03:34 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lh8Bq-0007O2-10; Thu, 13 May 2021 10:03:34 +0000 Received: by outflank-mailman (input) for mailman id 126817; Thu, 13 May 2021 10:03:32 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lh8Bo-0003vg-3g for xen-devel@lists.xenproject.org; Thu, 13 May 2021 10:03:32 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 70819697-d6d0-4f9d-a70f-64adb0fb8e2a; Thu, 13 May 2021 10:03:08 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 0DC46B15E; Thu, 13 May 2021 10:03:07 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 70819697-d6d0-4f9d-a70f-64adb0fb8e2a X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1620900187; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=j+l5tq5M/f1kECXbl74yHxqhOUkbjQkUE4NZC677IzI=; b=muXNug23i8D8WshIzxtjKc3x9GnLaAOKXKGlzsXTDl/23evlPFNCHKHd4a4SVRdSANqXEp icn4g/Bx1ghMjx9o1JXTxQFB4zJLnEvn3Te0fnTR9PVC/fhKkMdndGrcacL/hxHOGrnzad pwoTCoUMnAz3BRbea9XDOTFJf6ozUQ4= From: Juergen Gross To: xen-devel@lists.xenproject.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org Cc: Juergen Gross , Greg Kroah-Hartman , Jiri Slaby Subject: [PATCH 8/8] xen/hvc: replace BUG_ON() with negative return value Date: Thu, 13 May 2021 12:03:02 +0200 Message-Id: <20210513100302.22027-9-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210513100302.22027-1-jgross@suse.com> References: <20210513100302.22027-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) Content-Type: text/plain; charset="utf-8" Xen frontends shouldn't BUG() in case of illegal data received from their backends. So replace the BUG_ON()s when reading illegal data from the ring page with negative return values. Signed-off-by: Juergen Gross --- drivers/tty/hvc/hvc_xen.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/drivers/tty/hvc/hvc_xen.c b/drivers/tty/hvc/hvc_xen.c index 92c9a476defc..30d7ffb1e04c 100644 --- a/drivers/tty/hvc/hvc_xen.c +++ b/drivers/tty/hvc/hvc_xen.c @@ -86,6 +86,11 @@ static int __write_console(struct xencons_info *xencons, cons =3D intf->out_cons; prod =3D intf->out_prod; mb(); /* update queue values before going on */ + + if (WARN_ONCE((prod - cons) > sizeof(intf->out), + "Illegal ring page indices")) + return -EINVAL; + BUG_ON((prod - cons) > sizeof(intf->out)); =20 while ((sent < len) && ((prod - cons) < sizeof(intf->out))) @@ -114,7 +119,10 @@ static int domU_write_console(uint32_t vtermno, const = char *data, int len) */ while (len) { int sent =3D __write_console(cons, data, len); - =09 + + if (sent < 0) + return sent; + data +=3D sent; len -=3D sent; =20 @@ -138,7 +146,10 @@ static int domU_read_console(uint32_t vtermno, char *b= uf, int len) cons =3D intf->in_cons; prod =3D intf->in_prod; mb(); /* get pointers before reading ring */ - BUG_ON((prod - cons) > sizeof(intf->in)); + + if (WARN_ONCE((prod - cons) > sizeof(intf->in), + "Illegal ring page indices")) + return -EINVAL; =20 while (cons !=3D prod && recv < len) buf[recv++] =3D intf->in[MASK_XENCONS_IDX(cons++, intf->in)]; --=20 2.26.2