Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp1313631pxy; Fri, 23 Apr 2021 05:28:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwhE13GErojUxIJHaHHi15k3w2tpacS92qysCOfo4E6oIOnK+5LT87MRg3MmECkRJZwmdO6 X-Received: by 2002:a17:907:1624:: with SMTP id hb36mr3897151ejc.497.1619180916686; Fri, 23 Apr 2021 05:28:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619180916; cv=none; d=google.com; s=arc-20160816; b=qjfL5CeHETdwhNqc27KuX/KqCwq8RX3kxeXCZFjoUTEZQOA63ImpqhbiEjoi0nNOGg JQj/TybMTMLS9q5slFF6/R2Z3IGlL4ClslmQ1znQ8L9pcoSQNVEV4iUdrccn7/64km6M zMzqPc29YvTA9FqwW1fLnsSfw4N2TfxPsw0SHnWgxoFx0nXowmHQfz5zNy/VPfPeLkzL xR3t4gjyeko3nZzd4GF4nLRdveW46Z3ytkWCJcvrjSMByB/buygmktFXWscUwkx3n42U ySgZHPCTjG9j3E6apSIb4+76Geq504gJ+u6RNZY77+IGcZSFKff0eKD42bXAn2xYXvre qyZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:references:message-id :content-transfer-encoding:cc:date:in-reply-to:from:subject :mime-version; bh=rayC3IqzJgDSlcDFiFn30XvVS5i0/ijVLLCM+jcZUS8=; b=q+BNYZztqmR6+bblGUU9+HmEHHyGvNSWLTchixxSjoIrdeatqzUm6qgJd8QS0Ek6ep 2mpJoSM2HvXKLoS+U37gi7/wxY/ub7p5VyiPdGCWUAryUQ4pdCvUz/dfOaDuRuUNupG6 Xr3KK5bvlhGNxn10aRyNjCat+ukj/MStgm7C59u7n8lsfdu+MbYrWh7CPGGvj3Udi2sR cyFZUzpSWTLp6O8CWKkBgTPUOcDYq/VY9EjTEkBLnWBw2svOA/3+qOabfS+Fx0bsw32c 100nH6esZ5U2ATV1hB6An3FQ3gX7XwrfBsnIwLHB02Yyol3dsCdCVXrcPI0P47H5Sz4a lzNw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p15si5146124ejj.88.2021.04.23.05.27.59; Fri, 23 Apr 2021 05:28:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229479AbhDWM1A convert rfc822-to-8bit (ORCPT + 99 others); Fri, 23 Apr 2021 08:27:00 -0400 Received: from coyote.holtmann.net ([212.227.132.17]:48506 "EHLO mail.holtmann.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230225AbhDWM07 (ORCPT ); Fri, 23 Apr 2021 08:26:59 -0400 Received: from marcel-macbook.holtmann.net (p4fefc624.dip0.t-ipconnect.de [79.239.198.36]) by mail.holtmann.org (Postfix) with ESMTPSA id 6AD24CED00; Fri, 23 Apr 2021 14:34:09 +0200 (CEST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.60.0.2.21\)) Subject: Re: [PATCH v2 01/10] Bluetooth: HCI: Use skb_pull to parse BR/EDR events From: Marcel Holtmann In-Reply-To: <20210419171257.3865181-2-luiz.dentz@gmail.com> Date: Fri, 23 Apr 2021 14:26:21 +0200 Cc: linux-bluetooth@vger.kernel.org Content-Transfer-Encoding: 8BIT Message-Id: <25D86C94-89CE-44CC-BB9C-724486444C12@holtmann.org> References: <20210419171257.3865181-1-luiz.dentz@gmail.com> <20210419171257.3865181-2-luiz.dentz@gmail.com> To: Luiz Augusto von Dentz X-Mailer: Apple Mail (2.3654.60.0.2.21) Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org Hi Luiz, > This uses skb_pull to check the BR/EDR events received have the minimum > required length. > > Signed-off-by: Luiz Augusto von Dentz > --- > include/net/bluetooth/hci.h | 4 + > net/bluetooth/hci_event.c | 272 +++++++++++++++++++++++++++++------- > 2 files changed, 229 insertions(+), 47 deletions(-) > > diff --git a/include/net/bluetooth/hci.h b/include/net/bluetooth/hci.h > index ea4ae551c426..f1f505355e81 100644 > --- a/include/net/bluetooth/hci.h > +++ b/include/net/bluetooth/hci.h > @@ -1894,6 +1894,10 @@ struct hci_cp_le_reject_cis { > } __packed; > > /* ---- HCI Events ---- */ > +struct hci_ev_status { > + __u8 status; > +} __packed; > + > #define HCI_EV_INQUIRY_COMPLETE 0x01 > > #define HCI_EV_INQUIRY_RESULT 0x02 > diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c > index 5e99968939ce..077541fcba41 100644 > --- a/net/bluetooth/hci_event.c > +++ b/net/bluetooth/hci_event.c > @@ -42,6 +42,30 @@ > > /* Handle HCI Event packets */ > > +static void *hci_skb_pull(struct sk_buff *skb, size_t len) > +{ > + void *data = skb->data; > + > + if (skb->len < len) > + return NULL; > + > + skb_pull(skb, len); > + > + return data; > +} > + > +static void *hci_ev_skb_pull(struct hci_dev *hdev, struct sk_buff *skb, > + u8 ev, size_t len) > +{ > + void *data; > + > + data = hci_skb_pull(skb, len); > + if (!data) > + bt_dev_err(hdev, "Malformed Event: 0x%2.2x", ev); > + > + return data; > +} > + so if you do it in one function, this will look like this: static void *hci_ev_skb_pull(..) { void *data = skb->data; if (skb->len < len) { bt_dev_err(hdev, “Malformed ..”); return NULL; } skb_pull(skb, len); return data; } static void *hci_cc_skb_pull(..) { void *data = skb->data; if (skb->len < len) { bt_dev_err(..); return NULL; } skb_pull(..); return data; } I realize that you want to optimize the code with hci_skb_pull, but I don’t see how that makes it simpler or cleaner. In the end you just have spaghetti code and don’t win anything in size reduction or complexity. > static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb, > u8 *new_status) > { > @@ -2507,11 +2531,15 @@ static void hci_cs_switch_role(struct hci_dev *hdev, u8 status) > > static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb) > { > - __u8 status = *((__u8 *) skb->data); > + struct hci_ev_status *ev; > struct discovery_state *discov = &hdev->discovery; > struct inquiry_entry *e; > > - BT_DBG("%s status 0x%2.2x", hdev->name, status); > + ev = hci_ev_skb_pull(hdev, skb, HCI_EV_INQUIRY_COMPLETE, sizeof(*ev)); > + if (!ev) > + return; > + Now since we also have this pattern: ev = hci_ev_skb_pull(..); if (!ev) return; The question is now why not just use a #define here. hci_ev_skb_pull(HCI_EV_INQUIRY_COMPLETE, ev); And then the define would look like this (untested): #define hci_ev_skb_pull(evt, var) do { \ (var) = skb->data; \ if (skb->len < sizeof(*(var))) { \ bt_dev_err(hdev, “Malformed ..”); \ return NULL; \ } \ skb_pull(skb, sizeof(*(var))); \ } while (0); > + BT_DBG("%s status 0x%2.2x", hdev->name, ev->status); If we have every event with an ev->status, we could even include bt_dev_dbg in the macro. > > hci_conn_check_pending(hdev); > > @@ -2604,9 +2632,13 @@ static void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb) > > static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb) > { > - struct hci_ev_conn_complete *ev = (void *) skb->data; > + struct hci_ev_conn_complete *ev; > struct hci_conn *conn; > > + ev = hci_ev_skb_pull(hdev, skb, HCI_EV_CONN_COMPLETE, sizeof(*ev)); > + if (!ev) > + return; > + > BT_DBG("%s", hdev->name); If you are touching the above part anyway, then move towards bt_dev_dbg() at the same time. Regards Marcel