Received: by 2002:a05:6500:1b8f:b0:1fa:5c73:8e2d with SMTP id df15csp1357765lqb; Thu, 30 May 2024 08:01:58 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUiRIJ1QEM3MdRXjemps4GrjaxQTaTcZe7dAH7ONGHC3K/sRn4K9nkZ9OQKpaBDADRAavQgoHggrMpYUK/0hT27ytqvFCO+t/KPZ4YUcA== X-Google-Smtp-Source: AGHT+IEk25HPDUAjegGMEcgEjC3mAflCEVzNWR1EwUEWwBrK1ZpI3SxvGZsqRTvabrHtv4xLXDez X-Received: by 2002:a05:6a21:9991:b0:1af:b89b:a7f1 with SMTP id adf61e73a8af0-1b264d4da1fmr3266382637.27.1717081318411; Thu, 30 May 2024 08:01:58 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717081317; cv=pass; d=google.com; s=arc-20160816; b=nJoPkMOV2QK4KeSvxxX1C2N8P66VsSyo6zmQlGHE85Fyl7ZfR4fy6ddIMd5Fnv2Ty5 2/SHWrF7rEB6w7YUPtVf9Kk5Iac270UZAcy+DlN3CyzsM6YzxjFk+IZauxGRkgJNF4lx mWpnmNFLW1oc7gLXqzZDmzxEdfxsjjw8xIzbSLefHLi7540rFJeWTQQc0nqkMdVKPbiC HqmrAkw6fq4MayYhIvgchrRyYkOqfX0Dvw3zkdLJH8uY1fhfE5YW6A99ZeDP+M50gSck UjaiQOkuiB8X1RyrRqhyaxSyxqDA8YE0kWekoWUaA1M9aQ7fMR6fagXpzH0psT2nECpT 0DDQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=IupUN9fpIPdAkXZSVyN/lJ5enhGYD7Vb8cQ+0hunhlM=; fh=uo5u2TQK/HQlhydMwGLjBAnuDwaLxndJ/cgWTjIagS8=; b=b8mvA0737lkXOHLlgGsx5FfDRycdJsCF4K6O1sv0EYS8hh0Z3Ezyj5JjtKOKOuapyI TYp54wtWdJPxID8cPYTeBHB+LId78zKeJj81ebLKAj/jizzus4xOt+uzloXkW60R8krB 5tMP8H6bW8jFUr/uR9mcRp3eckaU9OIwib+8Zee4O4Et11RJQlmSEhqFzpk5FUTOHpG/ DS9cf0P6zKZG3JFYug/APWkFHyKnsRuSEEBthA6JbQSV4ZES12jvx+rVrtMuiwkzTJqA 24a9SLrHqCnIfDXR0JWNXPKZf3hPIfYyJ6KZ7LUIqoeknH4adsb+OhU6Z00z3K2ESGzp PA3g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=hadess.net); spf=pass (google.com: domain of linux-bluetooth+bounces-5042-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-5042-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id d2e1a72fcca58-6f8fd4d396esi12040314b3a.259.2024.05.30.08.01.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 May 2024 08:01:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth+bounces-5042-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=hadess.net); spf=pass (google.com: domain of linux-bluetooth+bounces-5042-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-5042-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id E6125B23BBB for ; Thu, 30 May 2024 15:01:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1433417F518; Thu, 30 May 2024 15:01:04 +0000 (UTC) X-Original-To: linux-bluetooth@vger.kernel.org Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DB84517D881 for ; Thu, 30 May 2024 15:01:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.200 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717081263; cv=none; b=VeydX1Aa24adh+iZvRp3H4/MuclRuzWhF2JgqAQPmYrPviEdeLZhqlCZDwP72F8YLIS08Ot76/8WArcQFTJ45Gdgqu3H7CnXkEYary29ZVB2QaF0P+adsimC/2hq6nQtkRSsWUFYqo8PCnGZxGYHQ4u/61/IsYa5sFK3WXZ+/Yo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717081263; c=relaxed/simple; bh=0Ir4+Cm2Qgu/7c90GLMzOafurRrJ9DGDxA3byKKmbN8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=s2VJhzDO5zJ2FYZPglpvFprcK3bjNSuXLg2SLsvFNoVDxAY2xKmAbSyzCYcPulAVLDM0ycSyT6hxyBhqCwKv8dYTjwJ5fJvlN+yxNAQ2e/PFTUBDI5cxN72U81/EAuR6DFdl9EJF6X2vnGN+EGzwqUEaiLkL7cEPOWUTF1kDCuY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=hadess.net; spf=pass smtp.mailfrom=hadess.net; arc=none smtp.client-ip=217.70.183.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=hadess.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=hadess.net Received: by mail.gandi.net (Postfix) with ESMTPSA id 44C0320018; Thu, 30 May 2024 15:01:00 +0000 (UTC) From: Bastien Nocera To: linux-bluetooth@vger.kernel.org Cc: Bastien Nocera Subject: [BlueZ 9/9] avdtp: Fix manipulating struct as an array Date: Thu, 30 May 2024 16:58:03 +0200 Message-ID: <20240530150057.444585-10-hadess@hadess.net> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240530150057.444585-1-hadess@hadess.net> References: <20240530150057.444585-1-hadess@hadess.net> Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-GND-Sasl: hadess@hadess.net Don't manipulate the "req" structs as if they were flat arrays, static analysis and humans are both equally confused by this kind of usage. Error: ARRAY_VS_SINGLETON (CWE-119): [#def26] [important] bluez-5.76/profiles/audio/avdtp.c:1675:2: address_of: Taking address with "&start->first_seid" yields a singleton pointer. bluez-5.76/profiles/audio/avdtp.c:1675:2: assign: Assigning: "seid" = "&start->first_seid". bluez-5.76/profiles/audio/avdtp.c:1679:25: ptr_arith: Using "seid" as an array. This might corrupt or misinterpret adjacent memory locations. 1677| int i; 1678| 1679|-> for (i = 0; i < count; i++, seid++) { 1680| if (seid->seid == id) { 1681| req->collided = TRUE; Error: ARRAY_VS_SINGLETON (CWE-119): [#def27] [important] bluez-5.76/profiles/audio/avdtp.c:1690:2: address_of: Taking address with "&suspend->first_seid" yields a singleton pointer. bluez-5.76/profiles/audio/avdtp.c:1690:2: assign: Assigning: "seid" = "&suspend->first_seid". bluez-5.76/profiles/audio/avdtp.c:1694:25: ptr_arith: Using "seid" as an array. This might corrupt or misinterpret adjacent memory locations. 1692| int i; 1693| 1694|-> for (i = 0; i < count; i++, seid++) { 1695| if (seid->seid == id) { 1696| req->collided = TRUE; Error: ARRAY_VS_SINGLETON (CWE-119): [#def28] [important] bluez-5.76/profiles/audio/avdtp.c:1799:2: address_of: Taking address with "&req->first_seid" yields a singleton pointer. bluez-5.76/profiles/audio/avdtp.c:1799:2: assign: Assigning: "seid" = "&req->first_seid". bluez-5.76/profiles/audio/avdtp.c:1801:30: ptr_arith: Using "seid" as an array. This might corrupt or misinterpret adjacent memory locations. 1799| seid = &req->first_seid; 1800| 1801|-> for (i = 0; i < seid_count; i++, seid++) { 1802| failed_seid = seid->seid; 1803| Error: ARRAY_VS_SINGLETON (CWE-119): [#def29] [important] bluez-5.76/profiles/audio/avdtp.c:1912:2: address_of: Taking address with "&req->first_seid" yields a singleton pointer. bluez-5.76/profiles/audio/avdtp.c:1912:2: assign: Assigning: "seid" = "&req->first_seid". bluez-5.76/profiles/audio/avdtp.c:1914:30: ptr_arith: Using "seid" as an array. This might corrupt or misinterpret adjacent memory locations. 1912| seid = &req->first_seid; 1913| 1914|-> for (i = 0; i < seid_count; i++, seid++) { 1915| failed_seid = seid->seid; 1916| --- profiles/audio/avdtp.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/profiles/audio/avdtp.c b/profiles/audio/avdtp.c index 3667e08400dd..38c1870e619d 100644 --- a/profiles/audio/avdtp.c +++ b/profiles/audio/avdtp.c @@ -429,6 +429,20 @@ static void avdtp_sep_set_state(struct avdtp *session, struct avdtp_local_sep *sep, avdtp_state_t state); +#define REQ_GET_NTH_SEID(x) \ + static struct seid * \ + x##_req_get_nth_seid(struct x##_req *req, int count, int i) \ + { \ + if (count == 0 || i >= count) \ + return NULL; \ + if (i == 1) \ + return &req->first_seid; \ + return &req->other_seids[i]; \ + } + +REQ_GET_NTH_SEID(start) +REQ_GET_NTH_SEID(suspend) + static const char *avdtp_statestr(avdtp_state_t state) { switch (state) { @@ -1672,11 +1686,11 @@ static void check_seid_collision(struct pending_req *req, uint8_t id) static void check_start_collision(struct pending_req *req, uint8_t id) { struct start_req *start = req->data; - struct seid *seid = &start->first_seid; int count = 1 + req->data_size - sizeof(struct start_req); int i; - for (i = 0; i < count; i++, seid++) { + for (i = 0; i < count; i++) { + struct seid *seid = start_req_get_nth_seid(start, count, i); if (seid->seid == id) { req->collided = TRUE; return; @@ -1687,11 +1701,11 @@ static void check_start_collision(struct pending_req *req, uint8_t id) static void check_suspend_collision(struct pending_req *req, uint8_t id) { struct suspend_req *suspend = req->data; - struct seid *seid = &suspend->first_seid; int count = 1 + req->data_size - sizeof(struct suspend_req); int i; - for (i = 0; i < count; i++, seid++) { + for (i = 0; i < count; i++) { + struct seid *seid = suspend_req_get_nth_seid(suspend, count, i); if (seid->seid == id) { req->collided = TRUE; return; @@ -1785,7 +1799,6 @@ static gboolean avdtp_start_cmd(struct avdtp *session, uint8_t transaction, struct avdtp_local_sep *sep; struct avdtp_stream *stream; struct stream_rej rej; - struct seid *seid; uint8_t err, failed_seid; int seid_count, i; @@ -1796,9 +1809,9 @@ static gboolean avdtp_start_cmd(struct avdtp *session, uint8_t transaction, seid_count = 1 + size - sizeof(struct start_req); - seid = &req->first_seid; + for (i = 0; i < seid_count; i++) { + struct seid *seid = start_req_get_nth_seid(req, seid_count, i); - for (i = 0; i < seid_count; i++, seid++) { failed_seid = seid->seid; sep = find_local_sep_by_seid(session, seid->seid); @@ -1898,7 +1911,6 @@ static gboolean avdtp_suspend_cmd(struct avdtp *session, uint8_t transaction, struct avdtp_local_sep *sep; struct avdtp_stream *stream; struct stream_rej rej; - struct seid *seid; uint8_t err, failed_seid; int seid_count, i; @@ -1909,9 +1921,8 @@ static gboolean avdtp_suspend_cmd(struct avdtp *session, uint8_t transaction, seid_count = 1 + size - sizeof(struct suspend_req); - seid = &req->first_seid; - - for (i = 0; i < seid_count; i++, seid++) { + for (i = 0; i < seid_count; i++) { + struct seid *seid = suspend_req_get_nth_seid(req, seid_count, i); failed_seid = seid->seid; sep = find_local_sep_by_seid(session, seid->seid); -- 2.45.1