Skip to content

Commit

Permalink
common: Added Linux OS guards to network and user namespaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
levy committed Mar 25, 2024
1 parent 16c05ce commit 603c3f4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/inet/common/NetworkNamespaceContext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ std::map<std::string, int> localNetworkNamespaces; // network namespace which ar

void createNetworkNamespace(const char *name, bool global)
{
#ifdef __linux__
if (global) {
std::string fullPath = std::string("/var/run/netns/") + name;
int fd = open(fullPath.c_str(), O_RDONLY | O_CREAT | O_EXCL, 0);
Expand All @@ -40,10 +41,14 @@ void createNetworkNamespace(const char *name, bool global)
// switch back to the original namespace that was used before unshare
setns(oldFd, 0);
}
#else
throw cRuntimeError("Network namespaces are only supported on Linux");
#endif
}

bool existsNetworkNamespace(const char *name)
{
#ifdef __linux__
auto it = localNetworkNamespaces.find(name);
if (it != localNetworkNamespaces.end())
return true;
Expand All @@ -56,9 +61,13 @@ bool existsNetworkNamespace(const char *name)
}
}
return false;
#else
throw cRuntimeError("Network namespaces are only supported on Linux");
#endif
}

void deleteNetworkNamespace(const char *name) {
#ifdef __linux__
auto it = localNetworkNamespaces.find(name);
if (it != localNetworkNamespaces.end()) {
auto it = localNetworkNamespaces.find(name);
Expand All @@ -72,6 +81,9 @@ void deleteNetworkNamespace(const char *name) {
if (unlink(path.c_str()) != 0)
throw cRuntimeError("Cannot unlink file: %s", path.c_str());
}
#else
throw cRuntimeError("Network namespaces are only supported on Linux");
#endif
}

NetworkNamespaceContext::NetworkNamespaceContext(const char *name)
Expand Down Expand Up @@ -117,6 +129,8 @@ NetworkNamespaceContext::~NetworkNamespaceContext()
}
oldFd = -1;
newFd = -1;
#else
throw cRuntimeError("Network namespaces are only supported on Linux");
#endif
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/inet/common/UnsharedNamespaceInitializer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ Register_GlobalConfigOption(CFGID_UNSHARE_USER_NAMESPACE, "unshare-user-namespac

UnsharedNamespaceInitializer UnsharedNamespaceInitializer::singleton;

#ifdef __linux__
EXECUTE_ON_STARTUP(getEnvir()->addLifecycleListener(&UnsharedNamespaceInitializer::singleton));
#endif

void UnsharedNamespaceInitializer::lifecycleEvent(SimulationLifecycleEventType eventType, cObject *details)
{
Expand All @@ -35,6 +37,7 @@ void UnsharedNamespaceInitializer::lifecycleEvent(SimulationLifecycleEventType e

void UnsharedNamespaceInitializer::unshareUserNamespace()
{
#ifdef __linux__
pid_t originalUid = getuid();
pid_t originalGid = getgid();
if (unshare(CLONE_NEWUSER) < 0)
Expand All @@ -52,13 +55,16 @@ void UnsharedNamespaceInitializer::unshareUserNamespace()
// change effective user to root
if (seteuid(0) < 0)
throw cRuntimeError("Failed to switch to the root user");
#endif
}

void UnsharedNamespaceInitializer::unshareNetworkNamespace()
{
#ifdef __linux__
if (unshare(CLONE_NEWNET) < 0)
throw cRuntimeError("Failed to unshare network namespace");
originalNetworkNamespaceFd = open("/proc/self/ns/net", O_RDONLY);
#endif
}

void UnsharedNamespaceInitializer::writeMapping(const char* path, const char* mapping)
Expand Down

0 comments on commit 603c3f4

Please sign in to comment.