-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bpf_helper: Do not crash the process when too many labels
Add tests for bpf_helper Drive-by: Reformat code + code style fixes Fix potential OOB read in resolve_jumps PiperOrigin-RevId: 621191165 Change-Id: I8c2909564dc626ed8488536f50267e916dc6819f
- Loading branch information
1 parent
fde80c1
commit c4cbf83
Showing
4 changed files
with
184 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#include "sandboxed_api/sandbox2/util/bpf_helper.h" | ||
|
||
#include <linux/filter.h> | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "gtest/gtest.h" | ||
#include "absl/strings/str_cat.h" | ||
|
||
namespace { | ||
|
||
void AddMaxLabels(bpf_labels& labels) { | ||
static std::vector<std::string>* label_strs = []() { | ||
auto* label_strs = new std::vector<std::string>(); | ||
for (int i = 0; i < BPF_LABELS_MAX; ++i) { | ||
label_strs->push_back(absl::StrCat("lbl", i)); | ||
} | ||
return label_strs; | ||
}(); | ||
for (int i = 0; i < BPF_LABELS_MAX; ++i) { | ||
seccomp_bpf_label(&labels, (*label_strs)[i].c_str()); | ||
} | ||
} | ||
|
||
TEST(BpfHelperTest, MaxLabels) { | ||
bpf_labels labels = {}; | ||
AddMaxLabels(labels); | ||
std::vector<struct sock_filter> filter = { | ||
ALLOW, | ||
}; | ||
EXPECT_EQ(bpf_resolve_jumps(&labels, filter.data(), filter.size()), 0); | ||
} | ||
|
||
TEST(BpfHelperTest, LabelOverflow) { | ||
bpf_labels labels = {}; | ||
AddMaxLabels(labels); | ||
std::vector<struct sock_filter> filter = { | ||
JUMP(&labels, overflow), | ||
LABEL(&labels, overflow), | ||
ALLOW, | ||
}; | ||
filter.push_back(ALLOW); | ||
EXPECT_NE(bpf_resolve_jumps(&labels, filter.data(), filter.size()), 0); | ||
} | ||
|
||
TEST(BpfHelperTest, UnresolvedLabel) { | ||
bpf_labels labels = {}; | ||
std::vector<struct sock_filter> filter = { | ||
JUMP(&labels, unresolved), | ||
LABEL(&labels, unused), | ||
}; | ||
filter.push_back(ALLOW); | ||
EXPECT_NE(bpf_resolve_jumps(&labels, filter.data(), filter.size()), 0); | ||
} | ||
|
||
TEST(BpfHelperTest, BackwardJump) { | ||
bpf_labels labels = {}; | ||
std::vector<struct sock_filter> filter = { | ||
LABEL(&labels, backward), | ||
JUMP(&labels, backward), | ||
}; | ||
filter.push_back(ALLOW); | ||
EXPECT_NE(bpf_resolve_jumps(&labels, filter.data(), filter.size()), 0); | ||
} | ||
|
||
TEST(BpfHelperTest, Duplicate) { | ||
bpf_labels labels = {}; | ||
std::vector<struct sock_filter> filter = { | ||
JUMP(&labels, dup), | ||
LABEL(&labels, dup), | ||
LABEL(&labels, dup), | ||
}; | ||
filter.push_back(ALLOW); | ||
EXPECT_NE(bpf_resolve_jumps(&labels, filter.data(), filter.size()), 0); | ||
} | ||
|
||
TEST(BpfHelperTest, OutOfBoundsLabel) { | ||
bpf_labels labels = {}; | ||
std::vector<struct sock_filter> filter = { | ||
JUMP(&labels, normal), | ||
LABEL(&labels, normal), | ||
BPF_JUMP(BPF_JMP + BPF_JA, 1, JUMP_JT, JUMP_JF), | ||
}; | ||
filter.push_back(ALLOW); | ||
EXPECT_NE(bpf_resolve_jumps(&labels, filter.data(), filter.size()), 0); | ||
} | ||
|
||
} // namespace |