diff options
author | Jing Peng <pj.hades@gmail.com> | 2022-05-31 11:20:31 -0400 |
---|---|---|
committer | Matthew Smith <mgsmith@netgate.com> | 2022-08-16 19:32:14 +0000 |
commit | 5c9f9968de63fa627b4a72b344df36cdc686d18a (patch) | |
tree | 3e191980cbe4e3d4f7da3f01709947b81bf5171d /src/plugins/nat/nat64/nat64.c | |
parent | b5339c64d1100463058d1719ea23e0af353ce697 (diff) |
nat: fix potential out-of-bound worker array index
In several NAT submodules, the number of available ports (0xffff - 1024)
may not be divisible by the number of workers, so port_per_thread is
determined by integer division, which is the floor of the quotient.
Later when a worker index is needed, dividing the port with port_per_thread
may yield an out-of-bound array index into the workers array.
As an example, assume 2 workers are configured, then port_per_thread
will be (0xffff - 1024) / 2, which is 32255. When we compute a worker
index with port 0xffff, we get (0xffff - 1024) / 32255, which is 2,
but since we only have 2 workers, only 0 and 1 are valid indices.
This patch fixes the problem by adding a modulo at the end of the division.
Type: fix
Signed-off-by: Jing Peng <pj.hades@gmail.com>
Change-Id: Ieae3d5faf716410422610484a68222f1c957f3f8
Diffstat (limited to 'src/plugins/nat/nat64/nat64.c')
-rw-r--r-- | src/plugins/nat/nat64/nat64.c | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/src/plugins/nat/nat64/nat64.c b/src/plugins/nat/nat64/nat64.c index 1c1cdfba3fb..79e9da03a1e 100644 --- a/src/plugins/nat/nat64/nat64.c +++ b/src/plugins/nat/nat64/nat64.c @@ -135,6 +135,20 @@ nat64_get_worker_in2out (ip6_address_t * addr) return next_worker_index; } +static u32 +get_thread_idx_by_port (u16 e_port) +{ + nat64_main_t *nm = &nat64_main; + u32 thread_idx = nm->num_workers; + if (nm->num_workers > 1) + { + thread_idx = nm->first_worker_index + + nm->workers[(e_port - 1024) / nm->port_per_thread % + _vec_len (nm->workers)]; + } + return thread_idx; +} + u32 nat64_get_worker_out2in (vlib_buffer_t * b, ip4_header_t * ip) { @@ -202,7 +216,7 @@ nat64_get_worker_out2in (vlib_buffer_t * b, ip4_header_t * ip) /* worker by outside port (TCP/UDP) */ port = clib_net_to_host_u16 (port); if (port > 1024) - return nm->first_worker_index + ((port - 1024) / nm->port_per_thread); + return get_thread_idx_by_port (port); return vlib_get_thread_index (); } @@ -916,7 +930,7 @@ nat64_add_del_static_bib_entry (ip6_address_t * in_addr, /* outside port must be assigned to same thread as internall address */ if ((out_port > 1024) && (nm->num_workers > 1)) { - if (thread_index != ((out_port - 1024) / nm->port_per_thread)) + if (thread_index != get_thread_idx_by_port (out_port)) return VNET_API_ERROR_INVALID_VALUE_2; } |