Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E937AC433EF for ; Thu, 16 Jun 2022 03:29:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358385AbiFPD33 (ORCPT ); Wed, 15 Jun 2022 23:29:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1358435AbiFPD31 (ORCPT ); Wed, 15 Jun 2022 23:29:27 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 124A359BAE for ; Wed, 15 Jun 2022 20:29:26 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-30c99cb3d4dso2318977b3.6 for ; Wed, 15 Jun 2022 20:29:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=M/POJcqn9ITpqmIH2wqZ32ppEzx88Qp+XfArNnn0GaM=; b=h1Tf+ySweEiAdgvwZq/adDSzp6dZ6qfuV7UzTJGep83VBCtWrHxdi9YpffNlkBYXz/ CyiglkK+foGZ50ZfKY7AkqpA8a3cVkWkmI1W/HCSjKcJCgcVN0PyeUmrDtfSwtCr56O3 VIJ+bVrK3TZOACty27wKq8MRpm0ZCFHDs/jrpAGB0em5m5T0q6cbETQ/fa4+p6EDS740 pkTrROBkM2uSaf2tTlvRMMEGDku0uBPzJozdGVBLztMwa7oKydiN5QYopOyAPTu/ISqh if9aRE3nhuPbXEZP60FLvM3+DKKKpyUKBLv6D2M8ffWoV1WfSdn0GxIy/lcWJ6R4Lv4n Mn0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=M/POJcqn9ITpqmIH2wqZ32ppEzx88Qp+XfArNnn0GaM=; b=qns1f+jZOz6V2xC1Xlj1RocJJrb+9AUwMvewnHecU1YyB63Tv1TuPG3MiiY/mhhi+A chz3XjLh503HjAsfBdRK4iTUyp/rXCByYS0Y/9sG/2THY87gnfZfZmtiUelMeaqRsDK5 dpobkB7FRfQwPYs0Ow64goP3eeC9OstJmmVC2Eb03FL27m2hFD8QlPkrUjhBinuXmzOD A/mCGtonfSjn1pKtqMxW/MvjmxbqW+ERHK7nBKmpEKDtaRfH3Q7ntC8l16Qbh/6ouOhU TOPCo7pECnecaf3Yl5Kid2hqs+XDVU6DcwNk9Ax2GDmKia9EREysLTj382yq9+Sy68Fi RWJw== X-Gm-Message-State: AJIora+Md6yegMr6gKrX/lU9QfqQPQBRiT1fYBs95fDNQITTtLEtfIeS 4YpNooPg/rkznqjs4f7XHbREKDFKOg1m8VkOEHbiv2egjhgMvplGJFm1Xe10iJZoODgsoDaqFRE bgiBfI/jXrPLw4EfZ1hRxqSWapfr4UpO/jsLzkzUPXRYr7raga65BEUyR0qO9zIWCsET7yw== X-Google-Smtp-Source: AGRyM1v1DBRcxStpwx2ybgSicixf0ZF9xpNufBx4ebvvVTn8AvyenaXryRxWcGTDnkDAQvA8XYoOMNSDTYzmwE4= X-Received: from sunrising.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:fb8]) (user=mfaltesek job=sendgmr) by 2002:a81:12d6:0:b0:314:6097:b272 with SMTP id 205-20020a8112d6000000b003146097b272mr3366471yws.159.1655350165229; Wed, 15 Jun 2022 20:29:25 -0700 (PDT) Date: Wed, 15 Jun 2022 22:29:24 -0500 Message-Id: <20220616032924.3726425-1-mfaltesek@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.36.1.476.g0c4daa206d-goog Subject: [PATCH] nfc: st21nfca: fix incorrect sizing calculations in EVT_TRANSACTION From: Martin Faltesek To: stable@vger.kernel.org, gregkh@linuxfoundation.org Cc: mfaltesek@google.com, groeck@google.com, krzysztof.kozlowski@linaro.org, kuba@kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org commit f2e19b36593caed4c977c2f55aeba7408aeb2132 upstream. The transaction buffer is allocated by using the size of the packet buf, and subtracting two which seem intended to remove the two tags which are not present in the target structure. This calculation leads to under counting memory because of differences between the packet contents and the target structure. The aid_len field is a u8 in the packet, but a u32 in the structure, resulting in at least 3 bytes always being under counted. Further, the aid data is a variable length field in the packet, but fixed in the structure, so if this field is less than the max, the difference is added to the under counting. The last validation check for transaction->params_len is also incorrect since it employs the same accounting error. To fix, perform validation checks progressively to safely reach the next field, to determine the size of both buffers and verify both tags. Once all validation checks pass, allocate the buffer and copy the data. This eliminates freeing memory on the error path, as those checks are moved ahead of memory allocation. Fixes: 26fc6c7f02cb ("NFC: st21nfca: Add HCI transaction event support") Fixes: 4fbcc1a4cb20 ("nfc: st21nfca: Fix potential buffer overflows in EVT_TRANSACTION") Cc: stable@vger.kernel.org Signed-off-by: Martin Faltesek Reviewed-by: Guenter Roeck Reviewed-by: Krzysztof Kozlowski Signed-off-by: Jakub Kicinski Change-Id: I22f9d58293b64566c43a7ba254d9e0e8c4dc35fe --- drivers/nfc/st21nfca/se.c | 61 +++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 31 deletions(-) This patch cleanly applies in stable kernels 4.9 through 5.4. The following difference between upstream and 4.9 though 5.4 caused the upstream patch to fail applying: < - transaction = devm_kzalloc(dev, skb->len - 2, GFP_KERNEL); --- > - transaction = (struct nfc_evt_transaction *)devm_kzalloc(dev, > - skb->len - 2, GFP_KERNEL); This is the only change made in this patch from the original. diff --git a/drivers/nfc/st21nfca/se.c b/drivers/nfc/st21nfca/se.c index ccaace2a5b0e..d41636504246 100644 --- a/drivers/nfc/st21nfca/se.c +++ b/drivers/nfc/st21nfca/se.c @@ -304,6 +304,8 @@ int st21nfca_connectivity_event_received(struct nfc_hci_dev *hdev, u8 host, int r = 0; struct device *dev = &hdev->ndev->dev; struct nfc_evt_transaction *transaction; + u32 aid_len; + u8 params_len; pr_debug("connectivity gate event: %x\n", event); @@ -312,51 +314,48 @@ int st21nfca_connectivity_event_received(struct nfc_hci_dev *hdev, u8 host, r = nfc_se_connectivity(hdev->ndev, host); break; case ST21NFCA_EVT_TRANSACTION: - /* - * According to specification etsi 102 622 + /* According to specification etsi 102 622 * 11.2.2.4 EVT_TRANSACTION Table 52 * Description Tag Length * AID 81 5 to 16 * PARAMETERS 82 0 to 255 + * + * The key differences are aid storage length is variably sized + * in the packet, but fixed in nfc_evt_transaction, and that the aid_len + * is u8 in the packet, but u32 in the structure, and the tags in + * the packet are not included in nfc_evt_transaction. + * + * size in bytes: 1 1 5-16 1 1 0-255 + * offset: 0 1 2 aid_len + 2 aid_len + 3 aid_len + 4 + * member name: aid_tag(M) aid_len aid params_tag(M) params_len params + * example: 0x81 5-16 X 0x82 0-255 X */ - if (skb->len < NFC_MIN_AID_LENGTH + 2 || - skb->data[0] != NFC_EVT_TRANSACTION_AID_TAG) + if (skb->len < 2 || skb->data[0] != NFC_EVT_TRANSACTION_AID_TAG) return -EPROTO; - transaction = (struct nfc_evt_transaction *)devm_kzalloc(dev, - skb->len - 2, GFP_KERNEL); - if (!transaction) - return -ENOMEM; - - transaction->aid_len = skb->data[1]; + aid_len = skb->data[1]; - /* Checking if the length of the AID is valid */ - if (transaction->aid_len > sizeof(transaction->aid)) { - devm_kfree(dev, transaction); - return -EINVAL; - } + if (skb->len < aid_len + 4 || aid_len > sizeof(transaction->aid)) + return -EPROTO; - memcpy(transaction->aid, &skb->data[2], - transaction->aid_len); + params_len = skb->data[aid_len + 3]; - /* Check next byte is PARAMETERS tag (82) */ - if (skb->data[transaction->aid_len + 2] != - NFC_EVT_TRANSACTION_PARAMS_TAG) { - devm_kfree(dev, transaction); + /* Verify PARAMETERS tag is (82), and final check that there is enough + * space in the packet to read everything. + */ + if ((skb->data[aid_len + 2] != NFC_EVT_TRANSACTION_PARAMS_TAG) || + (skb->len < aid_len + 4 + params_len)) return -EPROTO; - } - transaction->params_len = skb->data[transaction->aid_len + 3]; + transaction = devm_kzalloc(dev, sizeof(*transaction) + params_len, GFP_KERNEL); + if (!transaction) + return -ENOMEM; - /* Total size is allocated (skb->len - 2) minus fixed array members */ - if (transaction->params_len > ((skb->len - 2) - - sizeof(struct nfc_evt_transaction))) { - devm_kfree(dev, transaction); - return -EINVAL; - } + transaction->aid_len = aid_len; + transaction->params_len = params_len; - memcpy(transaction->params, skb->data + - transaction->aid_len + 4, transaction->params_len); + memcpy(transaction->aid, &skb->data[2], aid_len); + memcpy(transaction->params, &skb->data[aid_len + 4], params_len); r = nfc_se_transaction(hdev->ndev, host, transaction); break; base-commit: 9d6e67bf50908cc661972969e8f073ec1d1bc97d -- 2.36.1.476.g0c4daa206d-goog