Skip to content

Commit 641d296

Browse files
Use-after-free and heap-size-mismatch fix.
1. Removed redundant header dependencies to allow building vlogger and state_machine independently from the rest of the code. 2. Fixed logic of set log color output parameter. 3. Rewrote the functions read_file_to_int (src/vma/util/utils.cpp) and env_to_cpuset (src/vma/util/sys_vars.cpp) to enable running the code with ASAN. 4. Fixed a new/delete size mismatch in src/vma/dev/rfs*. 5. Fixed a use-after-free issue in timer_handler.
1 parent c7f7597 commit 641d296

18 files changed

+144
-87
lines changed

src/stats/stats_printer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
#include "vma/util/utils.h"
1212
#include "vma/util/vma_stats.h"
1313
#include "vma/lwip/tcp.h"
14-
#include "vma/vma_extra.h"
15-
#include "vma/util/sys_vars.h"
14+
#include "vma/util/vtypes.h"
1615

1716
typedef enum {
1817
e_K = 1024,

src/stats/stats_publisher.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void stats_data_reader::handle_timer_expired(void *ctx)
102102

103103
void stats_data_reader::register_to_timer()
104104
{
105-
m_timer_handler = g_p_event_handler_manager->register_timer_event(STATS_PUBLISHER_TIMER_PERIOD, g_p_stats_data_reader, PERIODIC_TIMER, 0);
105+
m_timer_handler = g_p_event_handler_manager->register_timer_event(STATS_PUBLISHER_TIMER_PERIOD, this, PERIODIC_TIMER, 0);
106106
}
107107

108108
void stats_data_reader::add_data_reader(void* local_addr, void* shm_addr, int size)
@@ -150,6 +150,8 @@ void vma_shmem_stats_open(vlog_levels_t** p_p_vma_log_level, uint8_t** p_p_vma_l
150150
}
151151
BULLSEYE_EXCLUDE_BLOCK_END
152152

153+
vlog_printf(VLOG_DEBUG,"%s:%d: Allocated g_p_stats_data_reader pointer as '%p'\n", __func__, __LINE__, g_p_stats_data_reader);
154+
153155
shmem_size = SHMEM_STATS_SIZE(safe_mce_sys().stats_fd_num_max);
154156
buf = malloc(shmem_size);
155157
if (buf == NULL)
@@ -277,8 +279,12 @@ void vma_shmem_stats_close()
277279
g_sh_mem = NULL;
278280
g_p_vlogger_level = NULL;
279281
g_p_vlogger_details = NULL;
280-
delete g_p_stats_data_reader;
281-
g_p_stats_data_reader = NULL;
282+
if (g_p_stats_data_reader != NULL) {
283+
stats_data_reader* p = g_p_stats_data_reader;
284+
g_p_stats_data_reader = NULL;
285+
p->set_destroying_state(true);
286+
delete p;
287+
}
282288
}
283289

284290
void vma_stats_instance_create_socket_block(socket_stats_t* local_stats_addr)

src/stats/stats_reader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
#include <fcntl.h>
1616
#include <errno.h>
1717
#include <list>
18+
#include <netinet/in.h>
1819
#include <signal.h>
19-
#include <getopt.h> /* getopt()*/
20-
#include <errno.h>
20+
#include <getopt.h> /* getopt()*/
2121
#include <dirent.h>
2222
#include <string.h>
2323
#include <vector>

src/vlogger/vlogger.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
#include <fcntl.h>
1818

1919
#include "utils/bullseye.h"
20-
#include "vma/util/utils.h"
21-
#include "vma/util/sys_vars.h"
2220

2321
#define VLOG_DEFAULT_MODULE_NAME "VMA"
2422
#define VMA_LOG_CB_ENV_VAR "VMA_LOG_CB_FUNC_PTR"
@@ -31,7 +29,7 @@ vlog_levels_t* g_p_vlogger_level = NULL;
3129
uint8_t g_vlogger_details = 0;
3230
uint8_t* g_p_vlogger_details = NULL;
3331
uint32_t g_vlogger_usec_on_startup = 0;
34-
bool g_vlogger_log_in_colors = MCE_DEFAULT_LOG_COLORS;
32+
bool g_vlogger_log_in_colors = false;
3533
vma_log_cb_t g_vlogger_cb = NULL;
3634

3735
namespace log_level

src/vma/dev/rfs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ rfs::~rfs()
120120
delete[] m_sinks_list;
121121

122122
while (m_attach_flow_data_vector.size() > 0) {
123-
delete m_attach_flow_data_vector.back();
123+
free(m_attach_flow_data_vector.back());
124124
m_attach_flow_data_vector.pop_back();
125125
}
126126
}

src/vma/dev/rfs.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#ifndef RFS_H
99
#define RFS_H
1010

11+
#include <stdlib.h>
12+
#include <type_traits>
1113
#include <vector>
1214

1315
#include "vma/ib/base/verbs_extra.h"
@@ -213,6 +215,13 @@ class rfs
213215
virtual bool rx_dispatch_packet(mem_buf_desc_t* p_rx_wc_buf_desc, void* pv_fd_ready_array) = 0;
214216

215217
protected:
218+
template <class T, typename ...Args>
219+
T * new_malloc(Args ... args) {
220+
static_assert(std::is_trivially_destructible<T>::value == true);
221+
void * p = aligned_alloc(alignof(T), sizeof(T));
222+
return new(p) T(args...);
223+
}
224+
216225
flow_tuple m_flow_tuple;
217226
ring_slave* m_p_ring;
218227
rfs_rule_filter* m_p_rule_filter;

src/vma/dev/rfs_mc.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ bool rfs_mc::prepare_flow_spec()
5656
#ifdef DEFINED_IBV_FLOW_SPEC_IB
5757
attach_flow_data_ib_v1_t* attach_flow_data_ib_v1 = NULL;
5858

59-
attach_flow_data_ib_v1 = new attach_flow_data_ib_v1_t(p_ring->m_p_qp_mgr);
59+
attach_flow_data_ib_v1 = new_malloc<attach_flow_data_ib_v1_t>(p_ring->m_p_qp_mgr);
6060

6161
uint8_t dst_gid[16];
6262
create_mgid_from_ipv4_mc_ip(dst_gid, p_ring->m_p_qp_mgr->get_partiton(), m_flow_tuple.get_dst_ip());
@@ -70,7 +70,7 @@ bool rfs_mc::prepare_flow_spec()
7070
#endif
7171
}
7272

73-
attach_flow_data_ib_v2 = new attach_flow_data_ib_v2_t(p_ring->m_p_qp_mgr);
73+
attach_flow_data_ib_v2 = new_malloc<attach_flow_data_ib_v2_t>(p_ring->m_p_qp_mgr);
7474

7575
ibv_flow_spec_ipv4_set(&(attach_flow_data_ib_v2->ibv_flow_attr.ipv4),
7676
m_flow_tuple.get_dst_ip(),
@@ -88,7 +88,7 @@ bool rfs_mc::prepare_flow_spec()
8888
{
8989
attach_flow_data_eth_ipv4_tcp_udp_t* attach_flow_data_eth = NULL;
9090

91-
attach_flow_data_eth = new attach_flow_data_eth_ipv4_tcp_udp_t(p_ring->m_p_qp_mgr);
91+
attach_flow_data_eth = new_malloc<attach_flow_data_eth_ipv4_tcp_udp_t>(p_ring->m_p_qp_mgr);
9292

9393
uint8_t dst_mac[6];
9494
create_multicast_mac_from_ip(dst_mac, m_flow_tuple.get_dst_ip());

src/vma/dev/rfs_uc.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ bool rfs_uc::prepare_flow_spec()
5959
if (0 == p_ring->m_p_qp_mgr->get_underly_qpn()) {
6060
attach_flow_data_ib_ipv4_tcp_udp_v1_t* attach_flow_data_ib_v1 = NULL;
6161

62-
attach_flow_data_ib_v1 = new attach_flow_data_ib_ipv4_tcp_udp_v1_t(p_ring->m_p_qp_mgr);
62+
attach_flow_data_ib_v1 = new_malloc<attach_flow_data_ib_ipv4_tcp_udp_v1_t>(p_ring->m_p_qp_mgr);
6363
ibv_flow_spec_ib_set_by_dst_qpn(&(attach_flow_data_ib_v1->ibv_flow_attr.ib),
6464
htonl(((IPoIB_addr*)p_ring->m_p_l2_addr)->get_qpn()));
6565
p_ipv4 = &(attach_flow_data_ib_v1->ibv_flow_attr.ipv4);
@@ -68,7 +68,7 @@ bool rfs_uc::prepare_flow_spec()
6868
break;
6969
}
7070
#endif
71-
attach_flow_data_ib_v2 = new attach_flow_data_ib_ipv4_tcp_udp_v2_t(p_ring->m_p_qp_mgr);
71+
attach_flow_data_ib_v2 = new_malloc<attach_flow_data_ib_ipv4_tcp_udp_v2_t>(p_ring->m_p_qp_mgr);
7272

7373
p_ipv4 = &(attach_flow_data_ib_v2->ibv_flow_attr.ipv4);
7474
p_tcp_udp = &(attach_flow_data_ib_v2->ibv_flow_attr.tcp_udp);
@@ -77,7 +77,7 @@ bool rfs_uc::prepare_flow_spec()
7777
}
7878
case VMA_TRANSPORT_ETH:
7979
{
80-
attach_flow_data_eth = new attach_flow_data_eth_ipv4_tcp_udp_t(p_ring->m_p_qp_mgr);
80+
attach_flow_data_eth = new_malloc<attach_flow_data_eth_ipv4_tcp_udp_t>(p_ring->m_p_qp_mgr);
8181

8282
ibv_flow_spec_eth_set(&(attach_flow_data_eth->ibv_flow_attr.eth),
8383
p_ring->m_p_l2_addr->get_address(),

src/vma/dev/time_converter_ptp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define TIME_CONVERTER_PTP_H
1010

1111
#include <infiniband/verbs.h>
12+
#include "vma/ib/base/verbs_extra.h"
1213
#include "vma/event/timer_handler.h"
1314
#include <vma/util/sys_vars.h>
1415
#include "time_converter.h"

src/vma/event/delta_timer.cpp

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -200,19 +200,11 @@ void timer::process_registered_timers()
200200
timer_node_t* iter = m_list_head;
201201
timer_node_t* next_iter;
202202
while (iter && (iter->delta_time_msec == milliseconds(0))) {
203-
tmr_logfuncall("timer expired on %p", iter->handler);
204-
205-
/* Special check is need to protect
206-
* from using destroyed object pointed by handler
207-
* See unregister_timer_event()
208-
* Object can be destoyed from another thread (lock protection)
209-
* and from current thread (lock and lock count condition)
210-
*/
211-
if (iter->handler &&
212-
!iter->lock_timer.trylock() &&
213-
(1 == iter->lock_timer.is_locked_by_me())) {
214-
iter->handler->handle_timer_expired(iter->user_data);
215-
iter->lock_timer.unlock();
203+
timer_handler * handler = iter->handler.load();
204+
tmr_logfuncall("timer expired on %p", handler);
205+
206+
if (handler) {
207+
handler->safe_handle_timer_expired(iter->user_data);
216208
}
217209
next_iter = iter->next;
218210

@@ -225,13 +217,13 @@ void timer::process_registered_timers()
225217
break;
226218

227219
case ONE_SHOT_TIMER:
228-
remove_timer(iter, iter->handler);
220+
remove_timer(iter, handler);
229221
break;
230222

231223
BULLSEYE_EXCLUDE_BLOCK_START
232224
case INVALID_TIMER:
233225
default:
234-
tmr_logwarn("invalid timer expired on %p", iter->handler);
226+
tmr_logwarn("invalid timer expired on %p", handler);
235227
break;
236228
}
237229
BULLSEYE_EXCLUDE_BLOCK_END

0 commit comments

Comments
 (0)