From 5011e66346894d0847af2f6409ef3f207475ab75 Mon Sep 17 00:00:00 2001
From: Patrick Ziegler
Date: Thu, 9 Sep 2021 21:47:23 +0200
Subject: [PATCH] fix(net): Check if interface is valid first. (#2497)
Fixes #2496
---
include/adapters/net.hpp | 1 +
src/adapters/net.cpp | 22 ++++++++++++++--------
src/modules/network.cpp | 6 +++++-
3 files changed, 20 insertions(+), 9 deletions(-)
diff --git a/include/adapters/net.hpp b/include/adapters/net.hpp
index 74ef7cea..27cf2c72 100644
--- a/include/adapters/net.hpp
+++ b/include/adapters/net.hpp
@@ -37,6 +37,7 @@ class file_descriptor;
namespace net {
DEFINE_ERROR(network_error);
+ bool is_interface_valid(const string& ifname);
bool is_wireless_interface(const string& ifname);
std::string find_wireless_interface();
std::string find_wired_interface();
diff --git a/src/adapters/net.cpp b/src/adapters/net.cpp
index 99a421ff..63e3415a 100644
--- a/src/adapters/net.cpp
+++ b/src/adapters/net.cpp
@@ -12,8 +12,7 @@
#include
#include
-#include
-
+#include
#include
#include "common.hpp"
@@ -37,6 +36,11 @@ namespace net {
static bool is_virtual(const std::string& ifname) {
char* target = realpath((NET_PATH + ifname).c_str(), nullptr);
+
+ if (!target) {
+ throw system_error("realpath");
+ }
+
const std::string real_path{target};
free(target);
return real_path.rfind(VIRTUAL_PATH, 0) == 0;
@@ -54,6 +58,10 @@ namespace net {
return NetType::ETHERNET;
}
+ bool is_interface_valid(const string& ifname) {
+ return if_nametoindex(ifname.c_str()) != 0;
+ }
+
/**
* Test if interface with given name is a wireless device
*/
@@ -91,9 +99,7 @@ namespace net {
* Construct network interface
*/
network::network(string interface) : m_log(logger::make()), m_interface(move(interface)) {
- if (if_nametoindex(m_interface.c_str()) == 0) {
- throw network_error("Invalid network interface \"" + m_interface + "\"");
- }
+ assert(is_virtual(interface));
m_socketfd = file_util::make_file_descriptor(socket(AF_INET, SOCK_DGRAM, 0));
if (!*m_socketfd) {
@@ -284,7 +290,7 @@ namespace net {
float time_diff = duration.count();
float speedrate = bytes_diff / time_diff;
- vector> units{make_pair("G", 2), make_pair("M", 1)};
+ vector> units{make_pair("G", 2), make_pair("M", 1)};
string suffix{"K"};
int precision = 0;
@@ -294,8 +300,8 @@ namespace net {
units.pop_back();
}
- return sstream() << std::setw(minwidth) << std::setfill(' ') << std::setprecision(precision) << std::fixed << speedrate
- << " " << suffix << unit;
+ return sstream() << std::setw(minwidth) << std::setfill(' ') << std::setprecision(precision) << std::fixed
+ << speedrate << " " << suffix << unit;
}
// }}}
diff --git a/src/modules/network.cpp b/src/modules/network.cpp
index 4f365710..96ca6b83 100644
--- a/src/modules/network.cpp
+++ b/src/modules/network.cpp
@@ -38,7 +38,11 @@ namespace modules {
}
if (m_interface.empty()) {
- throw module_error("missing 'interface' or 'interface-type'");
+ throw module_error("Missing 'interface' or 'interface-type'");
+ }
+
+ if (!net::is_interface_valid(m_interface)) {
+ throw module_error("Invalid network interface \"" + m_interface + "\"");
}
m_ping_nth_update = m_conf.get(name(), "ping-interval", m_ping_nth_update);