Received: by 2002:ac2:464d:0:0:0:0:0 with SMTP id s13csp1992232lfo; Sat, 28 May 2022 13:00:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy729BOCF5wLTOO3dsplAOSF3T6lHPUyw/LKpfPyKyzWl9XYeVeJbro1ZzyE5SuOOk1RCiF X-Received: by 2002:a17:90a:7e02:b0:1e0:a6a7:6eb with SMTP id i2-20020a17090a7e0200b001e0a6a706ebmr14862354pjl.203.1653768003065; Sat, 28 May 2022 13:00:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653768003; cv=none; d=google.com; s=arc-20160816; b=cSwIYJWCwWGliHL8C23zLng3OU1y6uNFekY4MaJagerSVWdVUJCHat5foLp5N/YFY6 ht9lYnPmRkO0LCFBMRKqyL4tPDsBNjN/v7ZCdO8QQayFwSZmNVkGpIemmcAr4cWCJG16 Ephf1nMAvalxDsmUCy9T/YyhGw5KpGUL+UYcpTJam5phizT8ruWVuD456ni2BsFauZUX IA1UNhoBM6T4RG/uFN9FbedqeAan8nxqP2EP7JG3squA6Jm/KEOUUvtqDrR6Bk2ZmjGv 0OkdqsIhBYxIaS+nuSUI/6aqa8EzXgL65rur2wUzQSZ3ZGcVWRDTxhw+0ihKc8FS2L7B y5pA== 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 :message-id:date:subject:to:from:dkim-signature; bh=skLWkT/iLiyO+ZJaBBi68hpqA9oX7BHCtwP4O3+Wrsc=; b=T6KiMuI3cKZfSJW6l4EnTbBSfvCMMyLcbjyD4QZkgPXGQmiHGedJLyNCFn5IhTPjPy Wd+97wU+skwf8CDSNMyVAaw8B0PNdr92JzueG+BoyGwdbDVOXx35O5N3ek3x3hsQ9vV0 0oBb868CmXd1wEhPZc3G9oTC56IvKVJxtEqolfAnDFYlyKCBUohA/Ai5gfGrCuIDgp5G GeHyFcECCOZE48pyXaT5yud7dEunN7f1FmgrHNE4pk7XaNLuhZDguUkBtG0ksYhNx85B gVAHnpiynAm4DT9RiYR+6bj7Rob5zgVnfIZ7P6A+75lUuu/c6Q+DeM5Hro7g6I150CHU Topg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=WzIEtwmw; spf=softfail (google.com: domain of transitioning linux-bluetooth-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id rm11-20020a17090b3ecb00b001df518c9e46si7485011pjb.190.2022.05.28.13.00.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 May 2022 13:00:03 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-bluetooth-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=WzIEtwmw; spf=softfail (google.com: domain of transitioning linux-bluetooth-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 95C3BC9EF1; Sat, 28 May 2022 12:18:17 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232196AbiE1Afd (ORCPT + 99 others); Fri, 27 May 2022 20:35:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229854AbiE1Afc (ORCPT ); Fri, 27 May 2022 20:35:32 -0400 Received: from mail-il1-x12e.google.com (mail-il1-x12e.google.com [IPv6:2607:f8b0:4864:20::12e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7BC7FFD369 for ; Fri, 27 May 2022 17:35:31 -0700 (PDT) Received: by mail-il1-x12e.google.com with SMTP id f14so4064199ilj.1 for ; Fri, 27 May 2022 17:35:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=skLWkT/iLiyO+ZJaBBi68hpqA9oX7BHCtwP4O3+Wrsc=; b=WzIEtwmwGatBrVRR7F1YG3pyczFweKfUEj3L7mEB8Q5fJinMW4E8+HUG3arfjOFiTy rJMlJjtkeDOO44BOoGbC0UBUsGj9pLDHPk9/4T868uG+WE0LhgSBiLb/aJHfCfGEB5Hj SSagsJD1ldrC++sxRgNkwRcPsAeYdTE6piCyX/s2P4+BDOeOEQ+S8PsgjQx8YxaN7atG cJNn7Ol0CollHEqrv+OUu9cI48tVFcknjAr/1eoL4cN6dQjKRzY/SjGGs0SpbmfkSGY+ lKuFt4XhwKUIkZovL4HJxFHeK4EmSMFt6bmktoX3ndlaYNTt04nUWxC+/I3Jz+ktk3A3 SHlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=skLWkT/iLiyO+ZJaBBi68hpqA9oX7BHCtwP4O3+Wrsc=; b=JpIHklezxZ7dOdGcjP4LaHnMbHoDgmyTp4bwcUpYL0OwC/PaV8lS78wllplXD8EoFD ETIgW6qRCICX/ePjGiwFYkpIfI9xFjkOjT4asjIFcT7gtWxISeU/8U3Hb7MZfQvVCo11 t9WFI4hDbgrOD/R5XW8taKQFrnUIGU6sS4Jt04+siCoaTWD7Snhbl8avBMvk2BnxXxdi qE32SccQWyWv3mPQEBkivR8g5JMgfF8VeCyFhGh51kDCTjW+kgb4lrz5wW9zgdySBA0u IKAL6XAzUZcOeMqgQjwi1O0PI4Kgu+SVTEN/FpEc6EC+R6JoqUThGHHsCr8MvyfcCWtg 8O8Q== X-Gm-Message-State: AOAM530vN82JodTNfjhQNsFo+P9+UAirpGvbUCs4sjuHCERumS3j7n6T rmC93iWJJ49Iyg1BcySHu69B5pZv49A= X-Received: by 2002:a05:6e02:1bea:b0:2d1:ae3c:d041 with SMTP id y10-20020a056e021bea00b002d1ae3cd041mr15203456ilv.7.1653698130507; Fri, 27 May 2022 17:35:30 -0700 (PDT) Received: from lvondent-mobl4.. (c-71-56-157-77.hsd1.or.comcast.net. [71.56.157.77]) by smtp.gmail.com with ESMTPSA id m11-20020a056602314b00b0066579afd3cbsm1533714ioy.50.2022.05.27.17.35.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 May 2022 17:35:29 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH-stable v2] Bluetooth: eir: Fix using strlen with hdev->{dev_name,short_name} Date: Fri, 27 May 2022 17:35:28 -0700 Message-Id: <20220528003528.571351-1-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Luiz Augusto von Dentz Both dev_name and short_name are not guaranteed to be NULL terminated so this instead use strnlen and then attempt to determine if the resulting string needs to be truncated or not. Link: https://bugzilla.kernel.org/show_bug.cgi?id=216018 Signed-off-by: Luiz Augusto von Dentz --- net/bluetooth/eir.c | 41 ++++++++++++++++++++++++++--------------- net/bluetooth/mgmt.c | 4 ++-- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/net/bluetooth/eir.c b/net/bluetooth/eir.c index 7d77fb00c2bf..776d27f7e18d 100644 --- a/net/bluetooth/eir.c +++ b/net/bluetooth/eir.c @@ -13,6 +13,20 @@ #define PNP_INFO_SVCLASS_ID 0x1200 +static u8 eir_append_name(u8 *eir, u16 eir_len, u8 type, u8 *data, u8 data_len) +{ + u8 name[HCI_MAX_SHORT_NAME_LENGTH + 1]; + + /* If data is already NULL terminated just pass it directly */ + if (data[data_len - 1] == '\0') + return eir_append_data(eir, eir_len, type, data, data_len); + + memcpy(name, data, HCI_MAX_SHORT_NAME_LENGTH); + name[HCI_MAX_SHORT_NAME_LENGTH] = '\0'; + + return eir_append_data(eir, eir_len, type, name, sizeof(name)); +} + u8 eir_append_local_name(struct hci_dev *hdev, u8 *ptr, u8 ad_len) { size_t short_len; @@ -23,29 +37,26 @@ u8 eir_append_local_name(struct hci_dev *hdev, u8 *ptr, u8 ad_len) return ad_len; /* use complete name if present and fits */ - complete_len = strlen(hdev->dev_name); + complete_len = strnlen(hdev->dev_name, sizeof(hdev->dev_name)); if (complete_len && complete_len <= HCI_MAX_SHORT_NAME_LENGTH) - return eir_append_data(ptr, ad_len, EIR_NAME_COMPLETE, + return eir_append_name(ptr, ad_len, EIR_NAME_COMPLETE, hdev->dev_name, complete_len + 1); /* use short name if present */ - short_len = strlen(hdev->short_name); + short_len = strnlen(hdev->short_name, sizeof(hdev->short_name)); if (short_len) - return eir_append_data(ptr, ad_len, EIR_NAME_SHORT, - hdev->short_name, short_len + 1); + return eir_append_name(ptr, ad_len, EIR_NAME_SHORT, + hdev->short_name, + short_len == HCI_MAX_SHORT_NAME_LENGTH ? + short_len : short_len + 1); /* use shortened full name if present, we already know that name * is longer then HCI_MAX_SHORT_NAME_LENGTH */ - if (complete_len) { - u8 name[HCI_MAX_SHORT_NAME_LENGTH + 1]; - - memcpy(name, hdev->dev_name, HCI_MAX_SHORT_NAME_LENGTH); - name[HCI_MAX_SHORT_NAME_LENGTH] = '\0'; - - return eir_append_data(ptr, ad_len, EIR_NAME_SHORT, name, - sizeof(name)); - } + if (complete_len) + return eir_append_name(ptr, ad_len, EIR_NAME_SHORT, + hdev->dev_name, + HCI_MAX_SHORT_NAME_LENGTH); return ad_len; } @@ -181,7 +192,7 @@ void eir_create(struct hci_dev *hdev, u8 *data) u8 *ptr = data; size_t name_len; - name_len = strlen(hdev->dev_name); + name_len = strnlen(hdev->dev_name, sizeof(hdev->dev_name)); if (name_len > 0) { /* EIR Data type */ diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c index cd1b300b9be7..82cc645193f2 100644 --- a/net/bluetooth/mgmt.c +++ b/net/bluetooth/mgmt.c @@ -1082,11 +1082,11 @@ static u16 append_eir_data_to_buf(struct hci_dev *hdev, u8 *eir) eir_len = eir_append_le16(eir, eir_len, EIR_APPEARANCE, hdev->appearance); - name_len = strlen(hdev->dev_name); + name_len = strnlen(hdev->dev_name, sizeof(hdev->dev_name)); eir_len = eir_append_data(eir, eir_len, EIR_NAME_COMPLETE, hdev->dev_name, name_len); - name_len = strlen(hdev->short_name); + name_len = strnlen(hdev->short_name, sizeof(hdev->short_name)); eir_len = eir_append_data(eir, eir_len, EIR_NAME_SHORT, hdev->short_name, name_len); -- 2.35.1