From d413bf7d444507bf0807d98ad6291e24b6ea87a4 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Thu, 15 Oct 2020 10:05:27 +0100 Subject: [PATCH] agent: Fix crasher if AddARPNeighbors request empty Check if the ARP neighbours specified in the `AddARPNeighbors` API is set before using it to avoid crashing the agent. Fixes: #955. Signed-off-by: James O. D. Hunt --- src/agent/src/rpc.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/agent/src/rpc.rs b/src/agent/src/rpc.rs index cbb20469bd..ac9b5c5c3b 100644 --- a/src/agent/src/rpc.rs +++ b/src/agent/src/rpc.rs @@ -1127,6 +1127,13 @@ impl protocols::agent_ttrpc::AgentService for agentService { _ctx: &ttrpc::TtrpcContext, req: protocols::agent::AddARPNeighborsRequest, ) -> ttrpc::Result { + if req.neighbors.is_none() { + return Err(ttrpc::Error::RpcStatus(ttrpc::get_status( + ttrpc::Code::INVALID_ARGUMENT, + format!("empty add arp neighbours request"), + ))); + } + let neighs = req.neighbors.clone().unwrap().ARPNeighbors.into_vec(); let s = Arc::clone(&self.sandbox); @@ -1818,4 +1825,21 @@ mod tests { assert!(result.is_err(), "expected update routes to fail"); } + + #[test] + fn test_add_arp_neighbors() { + let logger = slog::Logger::root(slog::Discard, o!()); + let sandbox = Sandbox::new(&logger).unwrap(); + + let agent_service = Box::new(agentService { + sandbox: Arc::new(Mutex::new(sandbox)), + }); + + let req = protocols::agent::AddARPNeighborsRequest::default(); + let (ctx, _) = mk_ttrpc_context(); + + let result = agent_service.add_arp_neighbors(&ctx, req); + + assert!(result.is_err(), "expected add arp neighbors to fail"); + } }