Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1395468pxb; Wed, 2 Feb 2022 04:08:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJyWdO9H0Ww6gMUh6bW4dgR5n+uPQ9LeGzX31zrTu4jiJXJewtkYzvaPMVvHPi9Mz/2cU0Yt X-Received: by 2002:a17:907:6d82:: with SMTP id sb2mr24568594ejc.3.1643803696961; Wed, 02 Feb 2022 04:08:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643803696; cv=none; d=google.com; s=arc-20160816; b=rGrRCo4h1Iv/uLa9DWJyXhx49TOfFviENMEz3RFmE/SvtWfDxB6whN2zpqBiT4vgme Ly8jkDfJ9q3j0ZCt96HbB8vr12hqEKZgQAGLDGOxM3rE3khkUNGQaUXDzzZzFzGhEpH1 qc7FTpWmb8Bpzeg2gsxlreQ/uXqWP8FwPLmspZgkkax97GW9hwc5OyX+g3jUvP5fq5Je G/c0MIAGbIFeTCsisQRtiv7sdlijUOAP5uOJPZVlcDjlHiJ5TJJf49nX4eWxPmOojlRk jMU9EU/1YFmjSWWHhafRDhDydOYqNYg0gfGf+d5mnprBTMZkF/RryVuxq6K9WN+Xg6MJ gGYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=CtvsJlFbqfLsO5yxNiCSW8462wz3NLvD+3LUVcmaRSo=; b=mzpIPR+G5Su2aC6zHIHt7mBcWKPsEVcz1NGqrhoiMPC4ObwtO83JN4NdBYXIF62HyA pGp/FBQw0hM/9r4UsaEgh4cRbAvmyscdi3RRtG8SSVGBGLNW5fikmRuQZMSAwCHgI2Vf zZbD+QKWSfv7fZElti0jozLcLik/56kFYzzYCeIu+ntxAg9g8nixsHzMpqPybQhn8DjV YL0/WOF99RvUEFG615SEekdjWpVUolD2pvsk9DN6o2j+qtw7LHy4QMMtyfc8rTjZWf+p 9XvqHjdaVzWHEZ2UUm03paGJBSGoEMhPPvtPcSX0qzU0sy5t/NDSlt89FZ9ltwNtKRJQ DLtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@semihalf-com.20210112.gappssmtp.com header.s=20210112 header.b=KrAK0LqU; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n3si11303528edd.566.2022.02.02.04.07.32; Wed, 02 Feb 2022 04:08:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@semihalf-com.20210112.gappssmtp.com header.s=20210112 header.b=KrAK0LqU; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236219AbiBAUEA (ORCPT + 99 others); Tue, 1 Feb 2022 15:04:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235787AbiBAUD7 (ORCPT ); Tue, 1 Feb 2022 15:03:59 -0500 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53905C061741 for ; Tue, 1 Feb 2022 12:03:59 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id h21so34177003wrb.8 for ; Tue, 01 Feb 2022 12:03:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=semihalf-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=CtvsJlFbqfLsO5yxNiCSW8462wz3NLvD+3LUVcmaRSo=; b=KrAK0LqUBtRK2djoips1g3SIXIZxdEeQxU5BOLYi8h9c1+4j5aZERqXKbceeVUH6Ny lR3vWUcUEm/E/gZ4ITUR2coEL6DbKCe///jeOBWmKF1EFd8Kv4at9t2LwnSd5/1VvIB5 GeKS+xoFAh+iThTUrk20YUKsM6rHRl6a6hlnMOMyKS0AWFibz8rIsh6xfiCEhrFCuWmM 0XefwOvtPo+kBB2/CC14IFcBEpuzyKpjFnlKTEZgiTdlwfXVzBaSyYnSPGa2WabeXgic D8ihwYaFGM1vZcPME0Rts4SbXoV5NIWru1AntJ8hoaJ4yRPVLT5lC/AcASsaZvuQle1J n4jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=CtvsJlFbqfLsO5yxNiCSW8462wz3NLvD+3LUVcmaRSo=; b=pGn89saJJNeXD5RuqwVIflAFdKO9yg6uZqT+/y8k+cw95omkyTNM9uBim3ixfb1TdB YN/fLVOF4sLgPxcD9hYDnLXvwKb2IqizFvgImvIiOEtVhzbbB6HVR94taxgNoAnWqIRu ZHrWg5CDIQRMp6Y0BhFTuPiP3VW92z4HUHxrSIBGc15gj7WwMQ5VRnIK5usLTmIl683v MdR4Gtlac2JFW5ynyC9MKfW3jvg89NF53f3hQzT7Ynds+Lct5zojzD4Zg7lb5SnlJY1m 8fsJ797I0lT9cjAZxNcZxzXXeh+trM07W8QPz/SRmoLYZjY8S5R0a9l1la5/7KEll1k6 Z9hw== X-Gm-Message-State: AOAM5318r3whhZYcUHrPGIToQCvzuIEROwhi0aWLC39CmkFbgEMgyscl EKBzr6Wg0wwkY5+gkVH0QOirql/WG3FYxRHC X-Received: by 2002:adf:e4c4:: with SMTP id v4mr22488680wrm.332.1643745837666; Tue, 01 Feb 2022 12:03:57 -0800 (PST) Received: from biernacki.c.googlers.com.com (105.168.195.35.bc.googleusercontent.com. [35.195.168.105]) by smtp.gmail.com with ESMTPSA id m6sm3367280wmq.6.2022.02.01.12.03.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Feb 2022 12:03:56 -0800 (PST) From: Radoslaw Biernacki X-Google-Original-From: Radoslaw Biernacki To: linux-bluetooth , Luiz Augusto von Dentz , Marcel Holtmann Cc: CrosBT Upstreaming , Archie Pusaka , Miao-chen Chou , Jakub Kicinski , Johan Hedberg , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, upstream@semihalf.com, Radoslaw Biernacki , Angela Czubak , Marek Maslanka Subject: [PATCH v2 1/2] Bluetooth: Fix skb allocation in mgmt_remote_name() & mgmt_device_connected() Date: Tue, 1 Feb 2022 20:03:52 +0000 Message-Id: <20220201200353.1331443-2-rad@semihalf.ocm> X-Mailer: git-send-email 2.35.0.rc2.247.g8bbb082509-goog In-Reply-To: <20220201200353.1331443-1-rad@semihalf.ocm> References: <20220201200353.1331443-1-rad@semihalf.ocm> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Radoslaw Biernacki This patch fixes skb allocation, as lack of space for ev might push skb tail beyond its end. Also introduce eir_precalc_len() that can be used instead of magic numbers for similar eir operations on skb. Fixes: cf1bce1de7eeb ("Bluetooth: mgmt: Make use of mgmt_send_event_skb in MGMT_EV_DEVICE_FOUND") Fixes: e96741437ef0a ("Bluetooth: mgmt: Make use of mgmt_send_event_skb in MGMT_EV_DEVICE_CONNECTED") Signed-off-by: Angela Czubak Signed-off-by: Marek Maslanka Signed-off-by: Radoslaw Biernacki --- net/bluetooth/eir.h | 5 +++++ net/bluetooth/mgmt.c | 18 ++++++++---------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/net/bluetooth/eir.h b/net/bluetooth/eir.h index 05e2e917fc25..e5876751f07e 100644 --- a/net/bluetooth/eir.h +++ b/net/bluetooth/eir.h @@ -15,6 +15,11 @@ u8 eir_create_scan_rsp(struct hci_dev *hdev, u8 instance, u8 *ptr); u8 eir_append_local_name(struct hci_dev *hdev, u8 *eir, u8 ad_len); u8 eir_append_appearance(struct hci_dev *hdev, u8 *ptr, u8 ad_len); +static inline u16 eir_precalc_len(u8 data_len) +{ + return sizeof(u8) * 2 + data_len; +} + static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, u8 data_len) { diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c index 5dd684e0b259..43ca228104ce 100644 --- a/net/bluetooth/mgmt.c +++ b/net/bluetooth/mgmt.c @@ -9061,12 +9061,14 @@ void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn, u16 eir_len = 0; u32 flags = 0; + /* allocate buff for LE or BR/EDR adv */ if (conn->le_adv_data_len > 0) skb = mgmt_alloc_skb(hdev, MGMT_EV_DEVICE_CONNECTED, - conn->le_adv_data_len); + sizeof(*ev) + conn->le_adv_data_len); else skb = mgmt_alloc_skb(hdev, MGMT_EV_DEVICE_CONNECTED, - 2 + name_len + 5); + sizeof(*ev) + (name ? eir_precalc_len(name_len) : 0) + + eir_precalc_len(sizeof(conn->dev_class))); ev = skb_put(skb, sizeof(*ev)); bacpy(&ev->addr.bdaddr, &conn->dst); @@ -9785,13 +9787,11 @@ void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, { struct sk_buff *skb; struct mgmt_ev_device_found *ev; - u16 eir_len; - u32 flags; + u16 eir_len = 0; + u32 flags = 0; - if (name_len) - skb = mgmt_alloc_skb(hdev, MGMT_EV_DEVICE_FOUND, 2 + name_len); - else - skb = mgmt_alloc_skb(hdev, MGMT_EV_DEVICE_FOUND, 0); + skb = mgmt_alloc_skb(hdev, MGMT_EV_DEVICE_FOUND, + sizeof(*ev) + (name ? eir_precalc_len(name_len) : 0)); ev = skb_put(skb, sizeof(*ev)); bacpy(&ev->addr.bdaddr, bdaddr); @@ -9801,10 +9801,8 @@ void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, if (name) { eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name, name_len); - flags = 0; skb_put(skb, eir_len); } else { - eir_len = 0; flags = MGMT_DEV_FOUND_NAME_REQUEST_FAILED; } -- 2.35.0.rc2.247.g8bbb082509-goog