2020-05-17 18:00:15 +00:00
|
|
|
#if defined(ANDROID) || NETNS == 0
|
2018-08-26 12:51:41 +00:00
|
|
|
|
2018-11-08 12:31:50 +00:00
|
|
|
#else
|
2018-08-26 12:51:41 +00:00
|
|
|
#include <asm/types.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <sys/capability.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/statvfs.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <fstream>
|
2018-12-12 01:43:25 +00:00
|
|
|
#include <linux/netns.hpp>
|
2019-01-13 22:39:10 +00:00
|
|
|
#include <util/logger.hpp>
|
2018-08-26 12:51:41 +00:00
|
|
|
#ifndef MS_REC
|
|
|
|
#define MS_REC (16384)
|
|
|
|
#endif
|
2019-01-13 22:39:10 +00:00
|
|
|
#include <util/fs.hpp>
|
2018-08-26 12:51:41 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2018-09-28 14:29:34 +00:00
|
|
|
namespace GNULinux
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
static const char netns_rundir[] = "/var/run/netns";
|
|
|
|
static const char netns_etcdir[] = "/etc/netns";
|
|
|
|
|
|
|
|
static bool
|
|
|
|
GetCGroups2MountPoint(fs::path& cgroups2_mount)
|
|
|
|
{
|
|
|
|
std::string mountpoint;
|
|
|
|
std::ifstream inf;
|
|
|
|
inf.open("/proc/mounts");
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!inf.is_open())
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to open /proc/mounts");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::string line;
|
2020-04-07 18:38:56 +00:00
|
|
|
while (std::getline(inf, line))
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
std::string part;
|
|
|
|
std::stringstream parts;
|
|
|
|
parts.str(line);
|
|
|
|
// discard
|
|
|
|
std::getline(parts, part);
|
|
|
|
// mount point
|
|
|
|
std::getline(parts, part);
|
|
|
|
mountpoint = part;
|
|
|
|
// type
|
|
|
|
std::getline(parts, part);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (part == "cgroup2")
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
// found cgroup2 mountpoint
|
|
|
|
cgroups2_mount = mountpoint;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
llarp::LogError("cannot find cgroups2 in /proc/mounts");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
GetNetNS(std::string& netns)
|
|
|
|
{
|
|
|
|
auto nfd = open("/proc/self/ns/net", O_RDONLY);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (nfd < 0)
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogError("Failed to get our own netns, could not open /proc/self/ns/net");
|
2018-08-26 12:51:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
struct stat netst;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (::fstat(nfd, &netst) < 0)
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
close(nfd);
|
|
|
|
llarp::LogError("stat of netns failed: ", strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
close(nfd);
|
|
|
|
fs::path run_dir = netns_rundir;
|
2020-04-07 18:38:56 +00:00
|
|
|
bool foundIt = false;
|
2018-08-26 12:51:41 +00:00
|
|
|
// find corrosponding file for netns
|
|
|
|
llarp::util::IterDir(run_dir, [&](const fs::path& f) -> bool {
|
|
|
|
struct stat fst;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (::stat(f.string().c_str(), &fst) >= 0)
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (fst.st_dev == netst.st_dev && fst.st_ino == netst.st_ino)
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
// found it
|
|
|
|
foundIt = true;
|
2020-04-07 18:38:56 +00:00
|
|
|
netns = f.filename().string();
|
2018-08-26 12:51:41 +00:00
|
|
|
// break iteration
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// continue iteration
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
return foundIt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
GetVRFPath(std::string& path)
|
|
|
|
{
|
|
|
|
char p[256] = {0};
|
|
|
|
snprintf(p, sizeof(p), "/proc/%d/cgroup", getpid());
|
|
|
|
std::ifstream inf;
|
|
|
|
inf.open(p);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!inf.is_open())
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("could not open '", p, "': ", strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
path = "";
|
|
|
|
std::string line;
|
2020-04-07 18:38:56 +00:00
|
|
|
while (std::getline(inf, line))
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
auto pos = line.find("::/");
|
2020-04-07 18:38:56 +00:00
|
|
|
if (pos != std::string::npos)
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
line = line.substr(pos + 2);
|
2020-04-07 18:38:56 +00:00
|
|
|
pos = line.find("/vrf");
|
|
|
|
if (pos != std::string::npos)
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
path = line.substr(pos);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (path == "/")
|
2018-08-26 12:51:41 +00:00
|
|
|
path = "";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
ResetVRF()
|
|
|
|
{
|
|
|
|
fs::path cgroups2_mount;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!GetCGroups2MountPoint(cgroups2_mount))
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("could not find cgroup2 mount point, is it mounted?");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::string netns;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!GetNetNS(netns))
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("could not get our netns: ", strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::string vrfpath;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!GetVRFPath(vrfpath))
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogError("could not determine vrf cgroup path: ", strerror(errno));
|
2018-08-26 12:51:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
fs::path cgroup_path = cgroups2_mount / vrfpath / netns / "vrf" / "default";
|
2018-08-26 12:51:41 +00:00
|
|
|
std::error_code ec;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!fs::exists(cgroup_path, ec))
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!fs::create_directories(cgroup_path, ec))
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogError("could not create '", cgroup_path.string(), "': ", ec);
|
2018-08-26 12:51:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (ec)
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("Could not check '", cgroup_path.string(), "': ", ec);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
cgroup_path /= "cgroup.procs";
|
|
|
|
auto fd = open(cgroup_path.string().c_str(), O_RDWR | O_APPEND);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (fd < 0)
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogError("could not open '", cgroup_path.string(), "': ", strerror(errno));
|
2018-08-26 12:51:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
bool success = true;
|
2018-08-26 12:51:41 +00:00
|
|
|
std::string pid = std::to_string(getpid());
|
2020-04-07 18:38:56 +00:00
|
|
|
if (write(fd, pid.c_str(), pid.size()) < 0)
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to join cgroup");
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// bind network namespace paths into /etc/
|
|
|
|
static bool
|
|
|
|
BindNetworkNS(const char* name)
|
|
|
|
{
|
|
|
|
fs::path etc_dir = netns_etcdir;
|
|
|
|
etc_dir /= name;
|
|
|
|
std::error_code ec;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!fs::exists(etc_dir, ec))
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
llarp::LogInfo(etc_dir, " does not exist, skipping");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool didFail = false;
|
|
|
|
llarp::util::IterDir(etc_dir, [&](const fs::path& f) -> bool {
|
2020-04-07 18:38:56 +00:00
|
|
|
if (fs::is_regular_file(f))
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
fs::path netns_path = "/etc";
|
|
|
|
netns_path /= f.filename();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (mount(f.string().c_str(), netns_path.string().c_str(), "none", MS_BIND, nullptr) < 0)
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogError(
|
|
|
|
"failed to bind '",
|
|
|
|
f.string(),
|
|
|
|
"' to '",
|
|
|
|
netns_path.string(),
|
|
|
|
"': ",
|
|
|
|
strerror(errno));
|
2018-08-26 12:51:41 +00:00
|
|
|
didFail = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// continue iteration
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
return !didFail;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
DropCap()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (getuid() != 0 && geteuid() != 0)
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
cap_t capabilities;
|
2020-04-07 18:38:56 +00:00
|
|
|
cap_value_t net_admin = CAP_NET_ADMIN;
|
2018-08-26 12:51:41 +00:00
|
|
|
cap_flag_t inheritable = CAP_INHERITABLE;
|
|
|
|
cap_flag_value_t is_set;
|
|
|
|
|
|
|
|
capabilities = cap_get_proc();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!capabilities)
|
2018-08-26 12:51:41 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (cap_get_flag(capabilities, net_admin, inheritable, &is_set) != 0)
|
2018-08-26 12:51:41 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (is_set == CAP_CLEAR)
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (cap_clear(capabilities) != 0)
|
2018-08-26 12:51:41 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (cap_set_proc(capabilities) != 0)
|
2018-08-26 12:51:41 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
cap_free(capabilities);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
NetNSSwitch(const char* name)
|
|
|
|
{
|
|
|
|
fs::path netns_path = netns_rundir;
|
|
|
|
netns_path /= name;
|
|
|
|
auto nsfd = open(netns_path.string().c_str(), O_RDONLY | O_CLOEXEC);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (nsfd < 0)
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogError("Failed to open network namespace '", name, "': ", strerror(errno));
|
2018-08-26 12:51:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (setns(nsfd, CLONE_NEWNET) < 0)
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogError("Failed to enter network namespace '", name, "': ", strerror(errno));
|
2018-08-26 12:51:41 +00:00
|
|
|
close(nsfd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
close(nsfd);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (unshare(CLONE_NEWNS) < 0)
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("unshare failed: ", strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// dont let any mount points prop back to parent
|
|
|
|
// iproute2 source does this
|
2020-04-07 18:38:56 +00:00
|
|
|
if (mount("", "/", "none", MS_SLAVE | MS_REC, nullptr))
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("mount --make-rslave failed: ", strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
unsigned long mountflags = 0;
|
|
|
|
// ensaure /sys not mounted
|
2020-04-07 18:38:56 +00:00
|
|
|
if (umount2("/sys", MNT_DETACH) < 0)
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
struct statvfs fsstat;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (statvfs("/sys", &fsstat) == 0)
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (fsstat.f_flag & ST_RDONLY)
|
2018-08-26 12:51:41 +00:00
|
|
|
mountflags = MS_RDONLY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// mount sysfs for our namespace
|
2020-04-07 18:38:56 +00:00
|
|
|
if (mount(name, "/sys", "sysfs", mountflags, nullptr) < 0)
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to mount sysfs: ", strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BindNetworkNS(name))
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to bind namespace directories");
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!ResetVRF())
|
2018-08-26 12:51:41 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to reset vrf");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
DropCap();
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-28 14:29:34 +00:00
|
|
|
} // namespace GNULinux
|
2018-08-26 12:51:41 +00:00
|
|
|
} // namespace llarp
|
2018-11-08 12:31:50 +00:00
|
|
|
|
2018-12-12 01:43:25 +00:00
|
|
|
#endif
|