Skip to content

Commit 3d41f60

Browse files
committed
Fix Coverity CID 434914 & 406935: add mutex protection in mcast_membership_report_on_query
Protect access to mcast->group_num and mcast->group_list with group_mutex to prevent data race conditions. These fields are written with the mutex held elsewhere, so reads must also be protected. - Acquire group_mutex before reading group_num - Keep mutex held during TAILQ_FOREACH iteration of group_list - Add proper unlock on all early return paths (no groups, alloc failure)
1 parent 151697f commit 3d41f60

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

lib/src/mt_mcast.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,17 @@ int mcast_membership_general_query(struct mtl_main_impl* impl, enum mtl_port por
183183
static int mcast_membership_report_on_query(struct mtl_main_impl* impl,
184184
enum mtl_port port) {
185185
struct mt_mcast_impl* mcast = get_mcast(impl, port);
186-
uint16_t group_num = mcast->group_num;
187186
struct rte_mbuf* pkt;
188187
struct rte_ipv4_hdr* ip_hdr;
189188
struct mcast_mb_report_v3* mb_report;
190189
size_t hdr_offset = 0;
191190
size_t mb_report_len = sizeof(struct mcast_mb_report_v3);
192191

192+
mt_pthread_mutex_lock(&mcast->group_mutex);
193+
uint16_t group_num = mcast->group_num;
194+
193195
if (group_num <= 0) {
196+
mt_pthread_mutex_unlock(&mcast->group_mutex);
194197
dbg("%s(%d), no group to join\n", __func__, port);
195198
return 0;
196199
}
@@ -199,6 +202,7 @@ static int mcast_membership_report_on_query(struct mtl_main_impl* impl,
199202

200203
pkt = rte_pktmbuf_alloc(mt_sys_tx_mempool(impl, port));
201204
if (!pkt) {
205+
mt_pthread_mutex_unlock(&mcast->group_mutex);
202206
err("%s(%d), report packet alloc failed\n", __func__, port);
203207
return -ENOMEM;
204208
}
@@ -222,6 +226,7 @@ static int mcast_membership_report_on_query(struct mtl_main_impl* impl,
222226
group_record_addr += record_len;
223227
mb_report_len += record_len;
224228
}
229+
mt_pthread_mutex_unlock(&mcast->group_mutex);
225230

226231
uint16_t checksum = mcast_msg_checksum(MEMBERSHIP_REPORT_V3, mb_report, mb_report_len);
227232
if (checksum <= 0) {

0 commit comments

Comments
 (0)