Change a little parsing handling

This commit is contained in:
topjohnwu 2024-03-21 00:04:09 -07:00
parent a9ee2d7d18
commit 6b4baa3bcd
4 changed files with 24 additions and 32 deletions

View File

@ -54,17 +54,26 @@ static inline void expand(F &&f, T &&...args) {
f(std::forward<T>(args)...); f(std::forward<T>(args)...);
} }
template<typename ...T>
static inline void expand(const Str &s, T &&...args) {
char buf[64];
if (s.length() >= sizeof(buf)) return;
if (s.empty()) {
expand(std::forward<T>(args)..., (char *) nullptr);
} else {
memcpy(buf, s.data(), s.length());
buf[s.length()] = '\0';
expand(std::forward<T>(args)..., buf);
}
}
template<typename ...T> template<typename ...T>
static inline void expand(const StrVec &vec, T &&...args) { static inline void expand(const StrVec &vec, T &&...args) {
if (vec.empty()) { if (vec.empty()) {
expand(std::forward<T>(args)..., (char *) nullptr); expand(std::forward<T>(args)..., (char *) nullptr);
} else { } else {
char buf[64];
for (auto &s : vec) { for (auto &s : vec) {
if (s.length() >= sizeof(buf)) continue; expand(s, std::forward<T>(args)...);
memcpy(buf, s.data(), s.length());
buf[s.length()] = '\0';
expand(std::forward<T>(args)..., buf);
} }
} }
} }
@ -76,15 +85,6 @@ static inline void expand(const Xperms &vec, T &&...args) {
} }
} }
template<typename ...T>
static inline void expand(const Str &s, T &&...args) {
char buf[64];
if (s.length() >= sizeof(buf)) return;
memcpy(buf, s.data(), s.length());
buf[s.length()] = '\0';
expand(std::forward<T>(args)..., buf);
}
void sepolicy::allow(StrVec src, StrVec tgt, StrVec cls, StrVec perm) { void sepolicy::allow(StrVec src, StrVec tgt, StrVec cls, StrVec perm) {
expand(src, tgt, cls, perm, [this](auto ...args) { expand(src, tgt, cls, perm, [this](auto ...args) {
print_rule("allow", args...); print_rule("allow", args...);
@ -148,15 +148,14 @@ void sepolicy::attribute(Str name) {
}); });
} }
void sepolicy::type_transition(Str src, Str tgt, Str cls, Str def, StrVec obj) { void sepolicy::type_transition(Str src, Str tgt, Str cls, Str def, Str obj) {
auto obj_str = obj.empty() ? std::string() : std::string(obj[0]); expand(src, tgt, cls, def, obj, [this](auto s, auto t, auto c, auto d, auto o) {
auto o = obj.empty() ? nullptr : obj_str.data();
expand(src, tgt, cls, def, [this, &o](auto ...args) {
print_rule("type_transition", args..., o);
if (o) { if (o) {
impl->add_filename_trans(args..., o); print_rule("type_transition", s, t, c, d, o);
impl->add_filename_trans(s, t, c, d, o);
} else { } else {
impl->add_type_rule(args..., AVTAB_TRANSITION); print_rule("type_transition", s, t, c, d);
impl->add_type_rule(s, t, c, d, AVTAB_TRANSITION);
} }
}); });
} }

View File

@ -62,7 +62,7 @@ struct sepolicy {
void dontauditxperm(StrVec src, StrVec tgt, StrVec cls, Xperms xperm); void dontauditxperm(StrVec src, StrVec tgt, StrVec cls, Xperms xperm);
// Type rules // Type rules
void type_transition(Str src, Str tgt, Str cls, Str def, StrVec obj); void type_transition(Str src, Str tgt, Str cls, Str def, Str obj);
void type_change(Str src, Str tgt, Str cls, Str def); void type_change(Str src, Str tgt, Str cls, Str def);
void type_member(Str src, Str tgt, Str cls, Str def); void type_member(Str src, Str tgt, Str cls, Str def);

View File

@ -74,14 +74,7 @@ mod ffi {
#[cxx_name = "type"] #[cxx_name = "type"]
fn type_(self: Pin<&mut sepolicy>, t: &str, a: Vec<&str>); fn type_(self: Pin<&mut sepolicy>, t: &str, a: Vec<&str>);
fn attribute(self: Pin<&mut sepolicy>, t: &str); fn attribute(self: Pin<&mut sepolicy>, t: &str);
fn type_transition( fn type_transition(self: Pin<&mut sepolicy>, s: &str, t: &str, c: &str, d: &str, o: &str);
self: Pin<&mut sepolicy>,
s: &str,
t: &str,
c: &str,
d: &str,
o: Vec<&str>,
);
fn type_change(self: Pin<&mut sepolicy>, s: &str, t: &str, c: &str, d: &str); fn type_change(self: Pin<&mut sepolicy>, s: &str, t: &str, c: &str, d: &str);
fn type_member(self: Pin<&mut sepolicy>, s: &str, t: &str, c: &str, d: &str); fn type_member(self: Pin<&mut sepolicy>, s: &str, t: &str, c: &str, d: &str);
fn genfscon(self: Pin<&mut sepolicy>, s: &str, t: &str, c: &str); fn genfscon(self: Pin<&mut sepolicy>, s: &str, t: &str, c: &str);

View File

@ -257,9 +257,9 @@ fn exec_statement(sepolicy: Pin<&mut sepolicy>, tokens: &mut Tokens) -> LoggedRe
match action { match action {
Token::TT => { Token::TT => {
let o = if tokens.peek().is_none() { let o = if tokens.peek().is_none() {
vec![] ""
} else { } else {
vec![parse_id(tokens)?] parse_id(tokens)?
}; };
sepolicy.type_transition(s, t, c, d, o) sepolicy.type_transition(s, t, c, d, o)
} }