Received: by 10.192.165.148 with SMTP id m20csp3285320imm; Mon, 23 Apr 2018 04:04:40 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+6/7NRiBJg2B4uzdZQ0qrIGA4ES0VJJeu8hCOeEI8ETaiLj2nIsXH1IkgCLHM5ypjrc6rh X-Received: by 2002:a17:902:31a4:: with SMTP id x33-v6mr20529593plb.355.1524481479947; Mon, 23 Apr 2018 04:04:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524481479; cv=none; d=google.com; s=arc-20160816; b=RIyUI+PahylOIJASo9y7gB7KWPeveHwJAJs4Na7Dv3DC5k3jl61Qo92xaiHihnUfdF aUGO1e6AFXuzh0VK015DmruozAyD0EJBIusvNCcg0ajYyvI15bTe9V1uuZlMX/ceeDDS q7g3kyGUyEGIkLVZFB8KtmjWHuVoKde+o87pheUvIMHMhqbqgXuJid4uRHyquUTm5Vt3 ToQG66lD0W8DB5ggjtEPQyNDINSapxEGv0tjuIQRs7XYqNV/UzlZwnfB2Ctk+pC5Q77e mtkZG3XTd9ARyIEqm8huT97un7odVQpV5967eUfsusE8tDknKQC9N3dr72IFgrhUGFH1 fEDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:references:in-reply-to:date :subject:cc:to:from:arc-authentication-results; bh=tLxdMWwNKslX44DYmPcsm0kAanGoPQMmzjneuzsbs6E=; b=pS9vzKM58YlgnUl2/+LgqQvDMRYYnjH54hXEQ75qPapEcWdRC9gUp0VRxVGcXCpcx4 gIfYvAktVilXbCnkX2pbDfo69dsDJN7o+EWCCuqFBnclrApCh3hFU0OHfJL8VBHmIlOc 7bF0qkos2Risou4o0Wa6bziHwrJXhULgzdgJP9yoRdhq44Qa4YZfMu7590rFiGIX2o+F rC6cxWr8JvxKAA/7KjYMqBKnT0p8b/tn9f9KXbS9MgYjTV8MnFRh/RrltpQ+v7IpS1ju xqfaT6zlKWVw8151obrCck/30tBhLET6QlN6sX2Xj4dPxm197O5GGqlhnb8rOuzsYy72 0tZA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 33-v6si11509945ply.517.2018.04.23.04.04.25; Mon, 23 Apr 2018 04:04:39 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754972AbeDWLCp (ORCPT + 99 others); Mon, 23 Apr 2018 07:02:45 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:56728 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754651AbeDWLBg (ORCPT ); Mon, 23 Apr 2018 07:01:36 -0400 Received: from pps.filterd (m0098410.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w3NAxNPc050417 for ; Mon, 23 Apr 2018 07:01:35 -0400 Received: from e38.co.us.ibm.com (e38.co.us.ibm.com [32.97.110.159]) by mx0a-001b2d01.pphosted.com with ESMTP id 2hhd3gk6gp-1 (version=TLSv1.2 cipher=AES256-SHA256 bits=256 verify=NOT) for ; Mon, 23 Apr 2018 07:01:35 -0400 Received: from localhost by e38.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 23 Apr 2018 05:01:34 -0600 Received: from b03cxnp07029.gho.boulder.ibm.com (9.17.130.16) by e38.co.us.ibm.com (192.168.1.138) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 23 Apr 2018 05:01:32 -0600 Received: from b03ledav001.gho.boulder.ibm.com (b03ledav001.gho.boulder.ibm.com [9.17.130.232]) by b03cxnp07029.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w3NB1UFl11338210; Mon, 23 Apr 2018 04:01:30 -0700 Received: from b03ledav001.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 404D46E044; Mon, 23 Apr 2018 05:01:30 -0600 (MDT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by b03ledav001.gho.boulder.ibm.com (Postfix) with ESMTP id 7F7136E035; Mon, 23 Apr 2018 05:01:28 -0600 (MDT) From: Dong Jia Shi To: linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org, kvm@vger.kernel.org Cc: cohuck@redhat.com, borntraeger@de.ibm.com, bjsdjshi@linux.ibm.com, pasic@linux.ibm.com, pmorel@linux.ibm.com, Dong Jia Shi Subject: [PATCH v2 4/5] vfio: ccw: set ccw->cda to NULL defensively Date: Mon, 23 Apr 2018 13:01:12 +0200 X-Mailer: git-send-email 2.13.5 In-Reply-To: <20180423110113.59385-1-bjsdjshi@linux.vnet.ibm.com> References: <20180423110113.59385-1-bjsdjshi@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 18042311-0028-0000-0000-0000097C2839 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00008905; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000257; SDB=6.01022058; UDB=6.00521638; IPR=6.00801293; MB=3.00020722; MTD=3.00000008; XFM=3.00000015; UTC=2018-04-23 11:01:33 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18042311-0029-0000-0000-00003A7DD092 Message-Id: <20180423110113.59385-5-bjsdjshi@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-04-23_05:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=2 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1804230116 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Let's avoid free on ccw->cda that points to a guest address or an already freed memory area by setting it to NULL if memory allocation didn't happen or failed. Signed-off-by: Dong Jia Shi --- drivers/s390/cio/vfio_ccw_cp.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/drivers/s390/cio/vfio_ccw_cp.c b/drivers/s390/cio/vfio_ccw_cp.c index 340804b45f6a..1eef102c685e 100644 --- a/drivers/s390/cio/vfio_ccw_cp.c +++ b/drivers/s390/cio/vfio_ccw_cp.c @@ -491,7 +491,7 @@ static int ccwchain_fetch_direct(struct ccwchain *chain, struct ccw1 *ccw; struct pfn_array_table *pat; unsigned long *idaws; - int idaw_nr; + int ret; ccw = chain->ch_ccw + idx; @@ -511,18 +511,19 @@ static int ccwchain_fetch_direct(struct ccwchain *chain, * needed when translating a direct ccw to a idal ccw. */ pat = chain->ch_pat + idx; - if (pfn_array_table_init(pat, 1)) - return -ENOMEM; - idaw_nr = pfn_array_alloc_pin(pat->pat_pa, cp->mdev, - ccw->cda, ccw->count); - if (idaw_nr < 0) - return idaw_nr; + ret = pfn_array_table_init(pat, 1); + if (ret) + goto out_init; + + ret = pfn_array_alloc_pin(pat->pat_pa, cp->mdev, ccw->cda, ccw->count); + if (ret < 0) + goto out_init; /* Translate this direct ccw to a idal ccw. */ - idaws = kcalloc(idaw_nr, sizeof(*idaws), GFP_DMA | GFP_KERNEL); + idaws = kcalloc(ret, sizeof(*idaws), GFP_DMA | GFP_KERNEL); if (!idaws) { - pfn_array_table_unpin_free(pat, cp->mdev); - return -ENOMEM; + ret = -ENOMEM; + goto out_unpin; } ccw->cda = (__u32) virt_to_phys(idaws); ccw->flags |= CCW_FLAG_IDA; @@ -530,6 +531,12 @@ static int ccwchain_fetch_direct(struct ccwchain *chain, pfn_array_table_idal_create_words(pat, idaws); return 0; + +out_unpin: + pfn_array_table_unpin_free(pat, cp->mdev); +out_init: + ccw->cda = 0; + return ret; } static int ccwchain_fetch_idal(struct ccwchain *chain, @@ -559,7 +566,7 @@ static int ccwchain_fetch_idal(struct ccwchain *chain, pat = chain->ch_pat + idx; ret = pfn_array_table_init(pat, idaw_nr); if (ret) - return ret; + goto out_init; /* Translate idal ccw to use new allocated idaws. */ idaws = kzalloc(idaw_len, GFP_DMA | GFP_KERNEL); @@ -591,6 +598,8 @@ static int ccwchain_fetch_idal(struct ccwchain *chain, kfree(idaws); out_unpin: pfn_array_table_unpin_free(pat, cp->mdev); +out_init: + ccw->cda = 0; return ret; } -- 2.13.5