Skip to content

Commit eb6bbdf

Browse files
Ivan Morettfacebook-github-bot
authored andcommitted
binary changes to enable userns in shiv
Summary: For enabling userns for shiv in twshared we need a few changes. - In the spec we add the required flag and enable userns + use bpf token capability gated by feature rollout (D82478708) - In the binary we need to retrieve the bpf token and use it for all map operations, if not passed operations fail (P1948903648). - For bpf program names have a limit of 15 chars + null termination (https://www.kernel.org/doc/html/v5.14/bpf/libbpf/libbpf_naming_convention.html?utm_source=chatgpt.com) due to how it's accessed now, the name is truncated when we retrieve it and try to validate it so it fails with: P1948906354. For this now we check if the full name is correct for backwards compatibility, if not we truncate to 15 chars and check again. Reviewed By: avasylev Differential Revision: D77978567 fbshipit-source-id: 1d4f8fb4e5410b3df67fb1ad57c46c89f930a4e6
1 parent 753eb70 commit eb6bbdf

3 files changed

Lines changed: 48 additions & 5 deletions

File tree

katran/lib/BaseBpfAdapter.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ int BaseBpfAdapter::createNamedBpfMap(
240240
.map_flags = map_flags | (numa_node >= 0 ? BPF_F_NUMA_NODE : 0),
241241
.numa_node = (__u32)numa_node);
242242

243+
if (bpfTokenFd_ >= 0) {
244+
opts.token_fd = bpfTokenFd_;
245+
opts.map_flags |= BPF_F_TOKEN_FD;
246+
}
247+
243248
return bpf_map_create(
244249
static_cast<enum bpf_map_type>(type),
245250
name_ptr,
@@ -597,7 +602,7 @@ int BaseBpfAdapter::addClsActQD(const unsigned int ifindex) {
597602
}
598603

599604
int BaseBpfAdapter::getDirFd(const std::string& path) {
600-
return ::open(path.c_str(), O_DIRECTORY, O_RDONLY);
605+
return ::open(path.c_str(), O_DIRECTORY | O_RDONLY);
601606
}
602607

603608
int BaseBpfAdapter::attachCgroupProg(
@@ -921,4 +926,26 @@ bool BaseBpfAdapter::isBatchOpsEnabled() const {
921926
return batchOpsEnabled_;
922927
}
923928

929+
int BaseBpfAdapter::setBpfTokenFromFilePath(const char* path) {
930+
LOG(INFO) << "bpf token path: " << path;
931+
auto bpfFsFd = getDirFd(path);
932+
if (bpfFsFd < 0) {
933+
LOG(ERROR) << "failed to open bpf token path: " << path
934+
<< " error: " << folly::errnoStr(errno);
935+
return -1;
936+
}
937+
auto tokenFd = bpf_token_create(bpfFsFd, nullptr);
938+
close(bpfFsFd);
939+
if (tokenFd < 0) {
940+
LOG(ERROR) << "failed to create bpf token from path: " << path
941+
<< " error: " << folly::errnoStr(errno);
942+
return -1;
943+
}
944+
bpfTokenFd_ = tokenFd;
945+
LOG(INFO) << "Successfully created BPF token with fd: " << tokenFd;
946+
return 0;
947+
}
948+
949+
int64_t BaseBpfAdapter::bpfTokenFd_ = -1;
950+
924951
} // namespace katran

katran/lib/BaseBpfAdapter.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ constexpr unsigned int kBpfMapTypeHashOfMaps = 13;
5050

5151
class BaseBpfAdapter {
5252
public:
53+
static constexpr const char* kBpfTokenPathEnvVar = "LIBBPF_BPF_TOKEN_PATH";
54+
5355
BaseBpfAdapter(bool set_limits, bool enableBatchOpsIfSupported);
5456

5557
virtual ~BaseBpfAdapter() {}
@@ -672,6 +674,20 @@ class BaseBpfAdapter {
672674

673675
bool isBatchOpsEnabled() const;
674676

677+
/**
678+
* @param const char* path Path to the BPF filesystem directory
679+
* @return int 0 on success, -1 on failure
680+
*
681+
* Creates a BPF token from the specified filesystem path and sets it
682+
* for use with BPF operations. The BPF token enables unprivileged access
683+
* to BPF functionality when running in restricted environments like
684+
* user namespaces or containers.
685+
*
686+
* Once set, the token will be automatically used when creating BPF maps
687+
* and other BPF objects to enable operations in restricted contexts.
688+
*/
689+
int setBpfTokenFromFilePath(const char* path);
690+
675691
protected:
676692
/**
677693
* helper function to modify (add/delete/replace) tc's bpf prog.
@@ -729,6 +745,8 @@ class BaseBpfAdapter {
729745
* enabled and supported.
730746
*/
731747
bool batchOpsEnabled_{false};
748+
749+
static int64_t bpfTokenFd_;
732750
};
733751

734752
} // namespace katran

katran/lib/KatranLb.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,10 +487,8 @@ void KatranLb::initLrus(bool flowDebug, bool globalLru) {
487487
int lru_fd =
488488
createLruMap(per_core_lru_size, lru_map_flags, numa_node, core);
489489
if (lru_fd < 0) {
490-
LOG(FATAL) << "can't creat lru for core: " << core;
491-
throw std::runtime_error(fmt::format(
492-
"can't create LRU for forwarding core, error: {}",
493-
folly::errnoStr(errno)));
490+
LOG(FATAL) << "can't creat lru for core: " << core
491+
<< ", error: " << folly::errnoStr(errno);
494492
}
495493
lruMapsFd_[core] = lru_fd;
496494
if (flowDebug) {

0 commit comments

Comments
 (0)