From patchwork Wed Apr 18 10:05:02 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amit Pundir X-Patchwork-Id: 133609 Delivered-To: patch@linaro.org Received: by 10.46.84.18 with SMTP id i18csp5694414ljb; Wed, 18 Apr 2018 03:05:22 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+qhGGXgMm2MXY1gO4lNF7G4b9FL3ztLfrOe31tC4oorfi5a5ezDnvPgzEXD7/Apd/uRuA3 X-Received: by 2002:a17:902:51ce:: with SMTP id y72-v6mr1459535plh.157.1524045922589; Wed, 18 Apr 2018 03:05:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524045922; cv=none; d=google.com; s=arc-20160816; b=KD5/SJmpN5bOHcgCjBDN6fgSS2ia0VnNrZcRpu/mMMpuF74zs92EaYRG8L56831bls RhRg4+GPEwUEwaQaSNpMTRaBHWvWzRYny6Fh9li2uFGVTaGLOc1wwKei57vhBdqPtrV+ siJZn/ONI9ogcKU3xA0bUFAmLKSUxwyxgBi+h127ZCgd2gKxpLW57vsVqHJ2FxdcnW4R Na4aBNQ6Zk0ATam2UBmqjtqZOMrSVYk7lz3gmLqJA4veB0VSQSuzPOQ7ETzRftE2wksL gaBBL+xkqnPqAoDVRG2R36ejrilZKlfArJNtiorH46Vu7Id5kgLmUQEA/ey4YpIaL97s 0QVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=Q7b33P81Dqi1buU+SZECfMgqa7so/MUyD350h11szic=; b=yXon37vOTqXpZueXkEMJI+4jpJ7iQGGmb4b2BnFk4NSyl1vO9dihBzsWgGZEfHZDaz iBZzoe/e7Mz5G1geskJ5kQyNP5YO7mWsPpbxh7JV/uW0CcIcBHY4gdkuFeINeMkviEWb +YtqaI3br48jU4m1mftiySsqrAlSopwiLjrHueITqlNgbIybGp5yr80MU97xLI+aw3Im 0g4XVtFPV20qVaL344pVZr1Uv2B+Cb1OPtdmhpo9xRx8ZSja7jk4LAUUZn3/HE8bslGr eg6oXP3s99+V9hre58cDx3sORLDj7VTS1iaQDdQbOCNyVX5ZeekuQts+DgvGVJszf9vH pecQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=i0/i+u1T; spf=pass (google.com: best guess record for domain of linux-wireless-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g12-v6si941274pla.133.2018.04.18.03.05.22; Wed, 18 Apr 2018 03:05:22 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-wireless-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=i0/i+u1T; spf=pass (google.com: best guess record for domain of linux-wireless-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753073AbeDRKFV (ORCPT + 3 others); Wed, 18 Apr 2018 06:05:21 -0400 Received: from mail-pf0-f195.google.com ([209.85.192.195]:46864 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752747AbeDRKFT (ORCPT ); Wed, 18 Apr 2018 06:05:19 -0400 Received: by mail-pf0-f195.google.com with SMTP id h69so660170pfe.13 for ; Wed, 18 Apr 2018 03:05:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Q7b33P81Dqi1buU+SZECfMgqa7so/MUyD350h11szic=; b=i0/i+u1TA8GYiSnZWDkrWz9ukDrSUiebEH0RE5mvRS5xjMI2Nn3Ed8tNdbtuEdC1ay h4e1Na5NOHm9y0WbKUiseirF60zoMV+YB5nGmaBM0eka7yZ1+qTinkDHPIFiduBWkdEg 7Raf0sxc6a2DNpMZr61Hn2czYJRJ1IPZUHRiE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Q7b33P81Dqi1buU+SZECfMgqa7so/MUyD350h11szic=; b=hvjehw1imTugbjYt7ra0KQRp/EZnO921b+ITQDwoiNbTcHC4yM4B/MUwFEuQ3BtlWq KbY3GpHHwPU27nPvZK3O/ahajC2t1j43ccf9+ApBX71A5DTCm3t8J35fBJQga9Et2VRa jr9/QtG66Uf7hQuFbOAuGFAQfHAx95lVVc8wX0gRXLtjweYnodJcuP0hOkB/eSTgjDbF 7v7x85bAlEDNa7u3quepuEPdd2VAF9bwaOL/c+lRsSoArp3+OtVBck3S5LWdzFHMr7IV Up3sjnmrzeb+1AtD8vJAVuM0Y9QGmL06tlm8RgsRlW90XLoCryFgUcqtLsMOCA5oKvNk eGrw== X-Gm-Message-State: ALQs6tBoWaS+ID28BUGaaWsCXN4lYIIkU30YtCYEbh1COJ1VzXG7hdIJ xOG7Al4YVL1feiaQG2CaLYUl4Q== X-Received: by 10.101.78.134 with SMTP id b6mr1204815pgs.392.1524045919286; Wed, 18 Apr 2018 03:05:19 -0700 (PDT) Received: from localhost.localdomain ([106.51.17.69]) by smtp.gmail.com with ESMTPSA id z16sm1970298pfn.101.2018.04.18.03.05.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 18 Apr 2018 03:05:18 -0700 (PDT) From: Amit Pundir To: lkml , linux-wireless@vger.kernel.org Cc: Samuel Ortiz , Christophe Ricard , Andy Shevchenko , Greg KH , John Stultz , Dmitry Shmidt , Todd Kjos , Android Kernel Team , Suren Baghdasaryan Subject: [RESEND][PATCH 2/4] NFC: st21nfca: Fix memory OOB and leak issues in connectivity events handler Date: Wed, 18 Apr 2018 15:35:02 +0530 Message-Id: <1524045904-7005-3-git-send-email-amit.pundir@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1524045904-7005-1-git-send-email-amit.pundir@linaro.org> References: <1524045904-7005-1-git-send-email-amit.pundir@linaro.org> Sender: linux-wireless-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org From: Suren Baghdasaryan Overflow on memcpy is possible in kernel driver for st21nfca's NFC HCI layer when handling connectivity events if aid_len or params_len are bigger than the buffer size. Memory leak is possible when parameter tag is invalid. Signed-off-by: Suren Baghdasaryan Signed-off-by: Amit Pundir --- drivers/nfc/st21nfca/se.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) -- 2.7.4 diff --git a/drivers/nfc/st21nfca/se.c b/drivers/nfc/st21nfca/se.c index 4bed9e842db3..acdce231e227 100644 --- a/drivers/nfc/st21nfca/se.c +++ b/drivers/nfc/st21nfca/se.c @@ -322,23 +322,33 @@ int st21nfca_connectivity_event_received(struct nfc_hci_dev *hdev, u8 host, * AID 81 5 to 16 * PARAMETERS 82 0 to 255 */ - if (skb->len < NFC_MIN_AID_LENGTH + 2 && + if (skb->len < NFC_MIN_AID_LENGTH + 2 || skb->data[0] != NFC_EVT_TRANSACTION_AID_TAG) return -EPROTO; + /* + * Buffer should have enough space for at least + * two tag fields + two length fields + aid_len (skb->data[1]) + */ + if (skb->len < skb->data[1] + 4) + return -EPROTO; + transaction = (struct nfc_evt_transaction *)devm_kzalloc(dev, skb->len - 2, GFP_KERNEL); transaction->aid_len = skb->data[1]; memcpy(transaction->aid, &skb->data[2], transaction->aid_len); + transaction->params_len = skb->data[transaction->aid_len + 3]; - /* Check next byte is PARAMETERS tag (82) */ + /* Check next byte is PARAMETERS tag (82) and the length field */ if (skb->data[transaction->aid_len + 2] != - NFC_EVT_TRANSACTION_PARAMS_TAG) + NFC_EVT_TRANSACTION_PARAMS_TAG || + skb->len < transaction->aid_len + transaction->params_len + 4) { + devm_kfree(dev, transaction); return -EPROTO; + } - transaction->params_len = skb->data[transaction->aid_len + 3]; memcpy(transaction->params, skb->data + transaction->aid_len + 4, transaction->params_len);