Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp2369544ioo; Sat, 28 May 2022 11:31:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxh/G15MZAeWDyv4Jk5ZmRp/sn670jxKQhu8S+CrAdxt3JgvQIHnSnEMvhIky11LsHRNU2i X-Received: by 2002:a63:ef41:0:b0:3fb:c201:797d with SMTP id c1-20020a63ef41000000b003fbc201797dmr5799401pgk.104.1653762679703; Sat, 28 May 2022 11:31:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653762679; cv=none; d=google.com; s=arc-20160816; b=e6HUe3pLcVgplY72AbjnU6hjLkdJrjjDKqHDWITaIT1ZEgJNJGYpDOCpeKokMO+JTg RVZhR6xvuEzoLuzumroIltxue5AfUj8ileV/Yp4N9gRZcynd5A9UZBe7qdtynh3AbAug JlM8k4N9Os0Aya0QovF8DO4g2rNXqTqLn4bAbpXAJgFUTt5D8m1af07Effqj/GUeEsdz lUuyzLUKMW2A+Rc2w4h34HXn5SOiYDwZIWntmIhDURdedn+er2Dmcr6wrTlO0qAJ23/C B504Pmr8rCmraBoFA66aL8kbFjmXIF7A/nFamISkpqfw6QcnHEJjYFiHQzO5WVV9k6ZG Sikg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from:cc :references:to:content-language:subject:user-agent:mime-version:date :message-id; bh=Jf0lt5hAN0d0WXljVzJvVdobBQdmoPovZymcPwS1kt0=; b=LnjQLg4P2VUVZSP/bmLO5GPydwapCCQMwRh/T6ST4fqlwBIzDWQXFNVD35GRBc0t8m 2icaLiJAX/PEwmKPsnbIAOD7GOmssBTx5hwN6OEaK87XhonOk8KRxm7d+ensidaKRynQ WfxAW1SDD8D5RosXrhaPK4Ud6JpOukriobFLKmbeUr6+E72O651F5pA0hEPd5IG4nWFa vSrpRCamE+SO6nQz/K7Y9WJRVjo3r9jDoJpPAOGVrrXJHmAod5TN2BG+UEgAH3C1K0fj VVAcXnxukbKuggWSyb11gMsAMt+XrwxBFOwbycuYgLN8/ptLZhO8mrfU7S59oXwpQj5F L7tg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id ot1-20020a17090b3b4100b001cb4d9e17c6si8143744pjb.84.2022.05.28.11.31.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 May 2022 11:31:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5B1721F638; Sat, 28 May 2022 11:28:37 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229633AbiE1Hiq (ORCPT + 99 others); Sat, 28 May 2022 03:38:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229618AbiE1Hip (ORCPT ); Sat, 28 May 2022 03:38:45 -0400 Received: from mx1.molgen.mpg.de (mx3.molgen.mpg.de [141.14.17.11]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E44F72DF9 for ; Sat, 28 May 2022 00:38:42 -0700 (PDT) Received: from [192.168.0.21] (ip5f5af60f.dynamic.kabel-deutschland.de [95.90.246.15]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: pmenzel) by mx.molgen.mpg.de (Postfix) with ESMTPSA id A0FB161EA192D; Sat, 28 May 2022 09:38:39 +0200 (CEST) Message-ID: <7c90bc97-555f-8dc5-5e27-cfb6cdb0e428@molgen.mpg.de> Date: Sat, 28 May 2022 09:38:38 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 Subject: Re: [PATCH-stable v2] Bluetooth: eir: Fix using strlen with hdev->{dev_name,short_name} Content-Language: en-US To: Luiz Augusto von Dentz References: <20220528003528.571351-1-luiz.dentz@gmail.com> Cc: linux-bluetooth@vger.kernel.org From: Paul Menzel In-Reply-To: <20220528003528.571351-1-luiz.dentz@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 Dear Luiz, Thank you for your patch. Am 28.05.22 um 02:35 schrieb Luiz Augusto von Dentz: > 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 The *this* does not seem to belong into the sentence. > string needs to be truncated or not. > > Link: https://bugzilla.kernel.org/show_bug.cgi?id=216018 > Signed-off-by: Luiz Augusto von Dentz Should this be tagged for the stable series? Kind regards, Paul > --- > 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); >