| 1 | use std::borrow::Cow; |
| 2 | use std::convert::{From, TryFrom}; |
| 3 | use std::str; |
| 4 | |
| 5 | use nom::{ |
| 6 | branch::*, bytes::complete::*, character::complete::*, combinator::*, multi::*, sequence::*, |
| 7 | AsChar, IResult, Parser as P, |
| 8 | }; |
| 9 | |
| 10 | use nom::character::complete::{i64 as dec_i64, u16 as dec_u16, u32 as dec_u32, u64 as dec_u64}; |
| 11 | |
| 12 | use thiserror::Error; |
| 13 | |
| 14 | use crate::constants::*; |
| 15 | use crate::*; |
| 16 | |
| 17 | /// Parser for Linux Audit messages, with a few configurable options |
| 18 | #[derive(Debug)] |
| 19 | pub struct Parser { |
| 20 | /// Process enriched (i.e. ALL-CAPS keys). Default: true |
| 21 | pub enriched: bool, |
| 22 | /// Try to process common msg='…' strings into key/value maps. Default: true |
| 23 | pub split_msg: bool, |
| 24 | } |
| 25 | |
| 26 | impl Default for Parser { |
| 27 | fn default() -> Self { |
| 28 | Self { |
| 29 | enriched: true, |
| 30 | split_msg: true, |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /// Audit parser error type |
| 36 | #[derive(Debug, Error)] |
| 37 | pub enum ParseError { |
| 38 | /// The header (`type= … msg=audit(…):`) could not be parsed. |
| 39 | #[error("cannot parse header: {}", String::from_utf8_lossy(.0))] |
| 40 | MalformedHeader(Vec<u8>), |
| 41 | /// The body (everything after the event ID) could not be parsed. |
| 42 | #[error("cannot parse body: {}", String::from_utf8_lossy(.0))] |
| 43 | MalformedBody(Vec<u8>), |
| 44 | /// Garbage text was found at the end of the body. |
| 45 | #[error("garbage at end of message: {}", String::from_utf8_lossy(.0))] |
| 46 | TrailingGarbage(Vec<u8>), |
| 47 | /// A value in hexadecimal encoding could not be converted. |
| 48 | #[error("{id} ({ty}) can't hex-decode {}", String::from_utf8_lossy(.hex_str))] |
| 49 | HexDecodeError { |
| 50 | ty: MessageType, |
| 51 | id: EventID, |
| 52 | hex_str: Vec<u8>, |
| 53 | }, |
| 54 | } |
| 55 | |
| 56 | /// Parse a single log line as produced by _auditd(8)_ |
| 57 | /// |
| 58 | /// If `skip_enriched` is set and _auditd_ has been configured to |
| 59 | /// produce `log_format=ENRICHED` logs, i.e. to resolve `uid`, `gid`, |
| 60 | /// `syscall`, `arch`, `sockaddr` fields, those resolved values are |
| 61 | /// dropped by the parser. |
| 62 | /// |
| 63 | /// To maintain compatibility, `parse` does not attempt to process |
| 64 | /// single-quoted `msg='…'` strings into key/value maps. |
| 65 | pub fn parse<'a>(raw: &[u8], skip_enriched: bool) -> Result<Message<'a>, ParseError> { |
| 66 | Parser { |
| 67 | enriched: !skip_enriched, |
| 68 | ..Parser::default() |
| 69 | } |
| 70 | .parse(raw) |
| 71 | } |
| 72 | |
| 73 | impl Parser { |
| 74 | /// Parse a single log line as produced by _auditd(8)_ |
| 75 | pub fn parse<'a, 'b>(&'a self, raw: &'a [u8]) -> Result<Message<'b>, ParseError> { |
| 76 | let (rest, (node, ty, id)) = |
| 77 | parse_header(raw).map_err(|_| ParseError::MalformedHeader(raw.to_vec()))?; |
| 78 | |
| 79 | let (rest, kv) = self |
| 80 | .parse_body(rest, ty) |
| 81 | .map_err(|_| ParseError::MalformedBody(rest.to_vec()))?; |
| 82 | |
| 83 | if !rest.is_empty() { |
| 84 | return Err(ParseError::TrailingGarbage(rest.to_vec())); |
| 85 | } |
| 86 | |
| 87 | let node = node.map(|s| s.to_vec()); |
| 88 | |
| 89 | let mut body = Body::new(); |
| 90 | for (k, v) in kv { |
| 91 | body.push((k, v)); |
| 92 | } |
| 93 | |
| 94 | Ok(Message { id, node, ty, body }) |
| 95 | } |
| 96 | |
| 97 | /// Recognize the body: Multiple key/value pairs, with special cases |
| 98 | /// for some irregular messages |
| 99 | #[inline(always)] |
| 100 | fn parse_body<'a>( |
| 101 | &'a self, |
| 102 | input: &'a [u8], |
| 103 | ty: MessageType, |
| 104 | ) -> IResult<&'a [u8], Vec<(Key, Value<'a>)>> { |
| 105 | // Handle some corner cases that don't fit the general key=value |
| 106 | // scheme. |
| 107 | let (input, special) = match ty { |
| 108 | MessageType::AVC => opt(( |
| 109 | preceded( |
| 110 | pair(tag("avc:"), space0), |
| 111 | alt((tag("granted"), tag("denied"))), |
| 112 | ), |
| 113 | delimited( |
| 114 | (space0, tag("{"), space0), |
| 115 | many1(terminated(parse_identifier, space0)), |
| 116 | (tag("}"), space0, tag("for"), space0), |
| 117 | ), |
| 118 | ) |
| 119 | .map(|(k, v)| { |
| 120 | ( |
| 121 | Key::Name(NVec::from(k)), |
| 122 | Value::List( |
| 123 | v.iter() |
| 124 | .map(|e| Value::Str(e, Quote::None)) |
| 125 | .collect::<Vec<_>>(), |
| 126 | ), |
| 127 | ) |
| 128 | })) |
| 129 | .parse(input)?, |
| 130 | MessageType::TTY => value(None, opt(tag("tty "))).parse(input)?, |
| 131 | MessageType::MAC_POLICY_LOAD => value(None, opt(tag("policy loaded "))).parse(input)?, |
| 132 | _ => opt(terminated(tag("netlabel"), pair(tag(":"), space0)) |
| 133 | .map(|s| (Key::Name(NVec::from(s)), Value::Empty))) |
| 134 | .parse(input)?, |
| 135 | }; |
| 136 | |
| 137 | let (input, mut kv) = if !self.enriched { |
| 138 | terminated( |
| 139 | separated_list0(take_while1(|c| c == b' '), |input| self.parse_kv(input, ty)), |
| 140 | alt(( |
| 141 | value((), (tag("\x1d"), is_not("\n"), tag("\n"))), |
| 142 | value((), tag("\n")), |
| 143 | )), |
| 144 | ) |
| 145 | .parse(input)? |
| 146 | } else { |
| 147 | terminated( |
| 148 | separated_list0(take_while1(|c| c == b' ' || c == b'\x1d'), |input| { |
| 149 | self.parse_kv(input, ty) |
| 150 | }), |
| 151 | newline, |
| 152 | ) |
| 153 | .parse(input)? |
| 154 | }; |
| 155 | |
| 156 | if let Some(s) = special { |
| 157 | kv.push(s) |
| 158 | } |
| 159 | |
| 160 | Ok((input, kv)) |
| 161 | } |
| 162 | |
| 163 | /// Recognize one key/value pair |
| 164 | #[inline(always)] |
| 165 | fn parse_kv<'a>( |
| 166 | &'a self, |
| 167 | input: &'a [u8], |
| 168 | ty: MessageType, |
| 169 | ) -> IResult<&'a [u8], (Key, Value<'a>)> { |
| 170 | let (input, key) = match ty { |
| 171 | // Special case for execve arguments: aX, aX[Y], aX_len |
| 172 | MessageType::EXECVE |
| 173 | if !input.is_empty() && input[0] == b'a' && !input.starts_with(b"argc") => |
| 174 | { |
| 175 | terminated( |
| 176 | alt(( |
| 177 | parse_key_a_x_len.map(Key::ArgLen), |
| 178 | parse_key_a_xy.map(|(x, y)| Key::Arg(x, Some(y))), |
| 179 | parse_key_a_x.map(|x| Key::Arg(x, None)), |
| 180 | )), |
| 181 | tag("="), |
| 182 | ) |
| 183 | .parse(input) |
| 184 | } |
| 185 | // Special case for syscall params: aX |
| 186 | MessageType::SYSCALL => terminated( |
| 187 | alt((parse_key_a_x.map(|x| Key::Arg(x, None)), parse_key)), |
| 188 | tag("="), |
| 189 | ) |
| 190 | .parse(input), |
| 191 | _ => terminated(parse_key, tag("=")).parse(input), |
| 192 | }?; |
| 193 | |
| 194 | let (input, value) = match (ty, &key) { |
| 195 | (MessageType::SYSCALL, Key::Arg(_, None)) => { |
| 196 | terminated(take_while1(AsChar::is_hex_digit), peek(take_while1(is_sep))) |
| 197 | .map(|s| { |
| 198 | let ps = unsafe { str::from_utf8_unchecked(s) }; |
| 199 | match u64::from_str_radix(ps, 16) { |
| 200 | Ok(n) => Value::Number(Number::Hex(n)), |
| 201 | Err(_) => Value::Str(s, Quote::None), |
| 202 | } |
| 203 | }) |
| 204 | .parse(input)? |
| 205 | } |
| 206 | (MessageType::SYSCALL, Key::Common(c)) => self.parse_common(input, ty, *c)?, |
| 207 | (MessageType::EXECVE, Key::Arg(_, _)) => parse_encoded.map(Value::from).parse(input)?, |
| 208 | (MessageType::EXECVE, Key::ArgLen(_)) => parse_dec(input)?, |
| 209 | (_, Key::Name(name)) => parse_named(input, ty, name)?, |
| 210 | (_, Key::Common(c)) => self.parse_common(input, ty, *c)?, |
| 211 | (_, Key::NameUID(name)) | (_, Key::NameGID(name)) => { |
| 212 | alt((parse_dec, |input| parse_unspec_value(input, ty, name))).parse(input)? |
| 213 | } |
| 214 | _ => parse_encoded.map(Value::from).parse(input)?, |
| 215 | }; |
| 216 | |
| 217 | Ok((input, (key, value))) |
| 218 | } |
| 219 | |
| 220 | #[inline(always)] |
| 221 | fn parse_common<'a>( |
| 222 | &'a self, |
| 223 | input: &'a [u8], |
| 224 | ty: MessageType, |
| 225 | c: Common, |
| 226 | ) -> IResult<&'a [u8], Value<'a>> { |
| 227 | let name = <&str>::from(c).as_bytes(); |
| 228 | match c { |
| 229 | Common::Arch | Common::CapFi | Common::CapFp | Common::CapFver => { |
| 230 | alt((parse_hex, |input| parse_unspec_value(input, ty, name))).parse(input) |
| 231 | } |
| 232 | Common::Argc |
| 233 | | Common::Exit |
| 234 | | Common::CapFe |
| 235 | | Common::Inode |
| 236 | | Common::Item |
| 237 | | Common::Items |
| 238 | | Common::Pid |
| 239 | | Common::PPid |
| 240 | | Common::Ses |
| 241 | | Common::Syscall => { |
| 242 | alt((parse_dec, |input| parse_unspec_value(input, ty, name))).parse(input) |
| 243 | } |
| 244 | Common::Success |
| 245 | | Common::Cwd |
| 246 | | Common::Dev |
| 247 | | Common::Tty |
| 248 | | Common::Comm |
| 249 | | Common::Exe |
| 250 | | Common::Name |
| 251 | | Common::Nametype |
| 252 | | Common::Subj |
| 253 | | Common::Key => alt((parse_encoded.map(Value::from), |input| { |
| 254 | parse_unspec_value(input, ty, name) |
| 255 | })) |
| 256 | .parse(input), |
| 257 | Common::Mode => { |
| 258 | alt((parse_oct, |input| parse_unspec_value(input, ty, name))).parse(input) |
| 259 | } |
| 260 | Common::Msg => { |
| 261 | if self.split_msg { |
| 262 | alt((parse_kv_sq_as_map, |input| { |
| 263 | parse_unspec_value(input, ty, name) |
| 264 | })) |
| 265 | .parse(input) |
| 266 | } else { |
| 267 | alt((parse_encoded.map(Value::from), |input| { |
| 268 | parse_unspec_value(input, ty, name) |
| 269 | })) |
| 270 | .parse(input) |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /// Recognize the header: node, type, event identifier |
| 278 | #[inline(always)] |
| 279 | #[allow(clippy::type_complexity)] |
| 280 | fn parse_header(input: &[u8]) -> IResult<&[u8], (Option<&[u8]>, MessageType, EventID)> { |
| 281 | ( |
| 282 | opt(terminated(parse_node, is_a(" "))), |
| 283 | terminated(parse_type, is_a(" ")), |
| 284 | parse_msgid, |
| 285 | ) |
| 286 | .parse(input) |
| 287 | } |
| 288 | |
| 289 | /// Recognize the node name |
| 290 | #[inline(always)] |
| 291 | fn parse_node(input: &[u8]) -> IResult<&[u8], &[u8]> { |
| 292 | preceded(tag("node="), is_not(" \t\r\n")).parse(input) |
| 293 | } |
| 294 | |
| 295 | /// Recognize event type |
| 296 | #[inline(always)] |
| 297 | fn parse_type(input: &[u8]) -> IResult<&[u8], MessageType> { |
| 298 | preceded( |
| 299 | tag("type="), |
| 300 | alt(( |
| 301 | recognize(many1_count(alt((alphanumeric1, tag("_"))))).map_res(|s| { |
| 302 | EVENT_IDS |
| 303 | .get(s) |
| 304 | .ok_or(format!("unknown event id {}", String::from_utf8_lossy(s))) |
| 305 | .map(|n| MessageType(*n)) |
| 306 | }), |
| 307 | delimited(tag("UNKNOWN["), dec_u32, tag("]")).map(MessageType), |
| 308 | )), |
| 309 | ) |
| 310 | .parse(input) |
| 311 | } |
| 312 | |
| 313 | /// Recognize the "msg=audit(…):" event identifier |
| 314 | #[inline(always)] |
| 315 | fn parse_msgid(input: &[u8]) -> IResult<&[u8], EventID> { |
| 316 | ( |
| 317 | preceded(tag("msg=audit("), dec_u64), |
| 318 | delimited(tag("."), dec_u64, tag(":")), |
| 319 | terminated(dec_u32, pair(tag("):"), space0)), |
| 320 | ) |
| 321 | .map(|(sec, msec, sequence)| EventID { |
| 322 | timestamp: 1000 * sec + msec, |
| 323 | sequence, |
| 324 | }) |
| 325 | .parse(input) |
| 326 | } |
| 327 | |
| 328 | #[inline(always)] |
| 329 | fn parse_named<'a>(input: &'a [u8], ty: MessageType, name: &[u8]) -> IResult<&'a [u8], Value<'a>> { |
| 330 | match FIELD_TYPES.get(name) { |
| 331 | Some(&FieldType::Encoded) => alt((parse_encoded.map(Value::from), |input| { |
| 332 | parse_unspec_value(input, ty, name) |
| 333 | })) |
| 334 | .parse(input), |
| 335 | Some(&FieldType::NumericHex) => { |
| 336 | alt((parse_hex, |input| parse_unspec_value(input, ty, name))).parse(input) |
| 337 | } |
| 338 | Some(&FieldType::NumericDec) => { |
| 339 | alt((parse_dec, |input| parse_unspec_value(input, ty, name))).parse(input) |
| 340 | } |
| 341 | Some(&FieldType::NumericOct) => { |
| 342 | alt((parse_oct, |input| parse_unspec_value(input, ty, name))).parse(input) |
| 343 | } |
| 344 | // FIXME: Some(&FieldType::Numeric) |
| 345 | _ => alt((parse_encoded.map(Value::from), |input| { |
| 346 | parse_unspec_value(input, ty, name) |
| 347 | })) |
| 348 | .parse(input), |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /// Recognize encoded value: |
| 353 | /// |
| 354 | /// May be double-quoted string, hex-encoded blob, (null), ?. |
| 355 | #[inline(always)] |
| 356 | fn parse_encoded(input: &[u8]) -> IResult<&[u8], Option<Cow<'_, [u8]>>> { |
| 357 | alt(( |
| 358 | parse_encoded_non_null.map(Some), |
| 359 | terminated( |
| 360 | value(None, alt((tag("(null)"), tag("?")))), |
| 361 | peek(take_while1(is_sep)), |
| 362 | ), |
| 363 | )) |
| 364 | .parse(input) |
| 365 | } |
| 366 | |
| 367 | #[inline(always)] |
| 368 | fn parse_encoded_non_null(input: &[u8]) -> IResult<&[u8], Cow<'_, [u8]>> { |
| 369 | alt(( |
| 370 | parse_str_dq_safe.map(Cow::Borrowed), |
| 371 | terminated( |
| 372 | recognize(many1_count(take_while_m_n(2, 2, AsChar::is_hex_digit))), |
| 373 | peek(take_while1(is_sep)), |
| 374 | ) |
| 375 | .map(|hexstr: &[u8]| { |
| 376 | let mut recoded = Vec::with_capacity(hexstr.len() / 2); |
| 377 | for i in 0..hexstr.len() / 2 { |
| 378 | let d = unsafe { str::from_utf8_unchecked(&hexstr[2 * i..2 * i + 2]) }; |
| 379 | recoded.push(u8::from_str_radix(d, 16).unwrap()); |
| 380 | } |
| 381 | Cow::Owned(recoded) |
| 382 | }), |
| 383 | )) |
| 384 | .parse(input) |
| 385 | } |
| 386 | |
| 387 | /// Recognize hexadecimal value |
| 388 | #[inline(always)] |
| 389 | fn parse_hex(input: &[u8]) -> IResult<&[u8], Value<'_>> { |
| 390 | terminated(take_while1(AsChar::is_hex_digit), peek(take_while1(is_sep))) |
| 391 | .map_res(|digits| -> Result<_, std::num::ParseIntError> { |
| 392 | let digits = unsafe { str::from_utf8_unchecked(digits) }; |
| 393 | Ok(Value::Number(Number::Hex(u64::from_str_radix(digits, 16)?))) |
| 394 | }) |
| 395 | .parse(input) |
| 396 | } |
| 397 | |
| 398 | /// Recognize decimal value |
| 399 | #[inline(always)] |
| 400 | fn parse_dec(input: &[u8]) -> IResult<&[u8], Value<'_>> { |
| 401 | terminated(dec_i64, peek(take_while1(is_sep))) |
| 402 | .map(|n| Value::Number(Number::Dec(n))) |
| 403 | .parse(input) |
| 404 | } |
| 405 | |
| 406 | /// Recognize octal value |
| 407 | #[inline(always)] |
| 408 | fn parse_oct(input: &[u8]) -> IResult<&[u8], Value<'_>> { |
| 409 | terminated(take_while1(AsChar::is_oct_digit), peek(take_while1(is_sep))) |
| 410 | .map_res(|digits| -> Result<_, std::num::ParseIntError> { |
| 411 | let digits = unsafe { str::from_utf8_unchecked(digits) }; |
| 412 | Ok(Value::Number(Number::Oct(u64::from_str_radix(digits, 8)?))) |
| 413 | }) |
| 414 | .parse(input) |
| 415 | } |
| 416 | |
| 417 | #[inline(always)] |
| 418 | fn parse_unspec_value<'a>( |
| 419 | input: &'a [u8], |
| 420 | ty: MessageType, |
| 421 | name: &[u8], |
| 422 | ) -> IResult<&'a [u8], Value<'a>> { |
| 423 | // work around apparent AppArmor breakage |
| 424 | match (ty, name) { |
| 425 | (_, b"subj") => { |
| 426 | if let Ok((input, s)) = recognize(( |
| 427 | opt(tag("=")), |
| 428 | parse_str_unq, |
| 429 | opt(delimited(tag(" ("), parse_identifier, tag(")"))), |
| 430 | )) |
| 431 | .parse(input) |
| 432 | { |
| 433 | return Ok((input, Value::Str(s, Quote::None))); |
| 434 | } |
| 435 | } |
| 436 | (MessageType::AVC, b"info") => { |
| 437 | if let Ok((input, s)) = parse_str_dq(input) { |
| 438 | return Ok((input, Value::Str(s, Quote::None))); |
| 439 | } |
| 440 | } |
| 441 | (MessageType::SOCKADDR, b"SADDR") => { |
| 442 | let broken_string: IResult<&[u8], &[u8]> = |
| 443 | recognize(pair(tag("unknown family"), opt(take_till(is_sep)))).parse(input); |
| 444 | if let Ok((input, s)) = broken_string { |
| 445 | return Ok((input, Value::Str(s, Quote::None))); |
| 446 | } |
| 447 | } |
| 448 | _ => (), |
| 449 | }; |
| 450 | |
| 451 | alt(( |
| 452 | terminated(take_while1(is_safe_unquoted_chr), peek(take_while1(is_sep))) |
| 453 | .map(|s| Value::Str(s, Quote::None)), |
| 454 | parse_kv_sq.map(|s| Value::Str(s, Quote::Single)), |
| 455 | parse_str_sq.map(|s| Value::Str(s, Quote::Single)), |
| 456 | parse_str_dq.map(|s| Value::Str(s, Quote::Double)), |
| 457 | parse_kv_braced.map(|s| Value::Str(s, Quote::Braces)), |
| 458 | parse_str_braced.map(|s| Value::Str(s, Quote::Braces)), |
| 459 | value(Value::Empty, peek(take_while1(is_sep))), |
| 460 | )) |
| 461 | .parse(input) |
| 462 | } |
| 463 | |
| 464 | #[inline(always)] |
| 465 | fn parse_str_sq(input: &[u8]) -> IResult<&[u8], &[u8]> { |
| 466 | delimited(tag("'"), take_while(|c| c != b'\''), tag("'")).parse(input) |
| 467 | } |
| 468 | |
| 469 | #[inline(always)] |
| 470 | fn parse_str_dq_safe(input: &[u8]) -> IResult<&[u8], &[u8]> { |
| 471 | delimited(tag("\""), take_while(is_safe_chr), tag("\"")).parse(input) |
| 472 | } |
| 473 | |
| 474 | #[inline(always)] |
| 475 | fn parse_str_dq(input: &[u8]) -> IResult<&[u8], &[u8]> { |
| 476 | delimited(tag("\""), take_while(|c| c != b'"'), tag("\"")).parse(input) |
| 477 | } |
| 478 | |
| 479 | #[inline(always)] |
| 480 | fn parse_str_braced(input: &[u8]) -> IResult<&[u8], &[u8]> { |
| 481 | delimited(tag("{ "), take_until(" }"), tag(" }")).parse(input) |
| 482 | } |
| 483 | |
| 484 | #[inline(always)] |
| 485 | fn parse_str_unq(input: &[u8]) -> IResult<&[u8], &[u8]> { |
| 486 | take_while(is_safe_chr).parse(input) |
| 487 | } |
| 488 | |
| 489 | #[inline(always)] |
| 490 | fn parse_str_unq_inside_sq(input: &[u8]) -> IResult<&[u8], &[u8]> { |
| 491 | take_while(|c| is_safe_chr(c) && c != b'\'').parse(input) |
| 492 | } |
| 493 | |
| 494 | #[inline(always)] |
| 495 | fn parse_str_words_inside_sq(input: &[u8]) -> IResult<&[u8], &[u8]> { |
| 496 | let mut rest = input; |
| 497 | loop { |
| 498 | (rest, _) = take_while(|c| !b"' ".contains(&c))(rest)?; |
| 499 | if alt((recognize((space1, parse_key, tag("="))), tag("'"))) |
| 500 | .parse(rest) |
| 501 | .is_ok() |
| 502 | { |
| 503 | break; |
| 504 | } |
| 505 | (rest, _) = space1(rest)?; |
| 506 | } |
| 507 | let l = input.len() - rest.len(); |
| 508 | Ok((rest, &input[..l])) |
| 509 | } |
| 510 | |
| 511 | /// More "correct" variant of parse_str_sq |
| 512 | #[inline(always)] |
| 513 | fn parse_kv_sq(input: &[u8]) -> IResult<&[u8], &[u8]> { |
| 514 | delimited( |
| 515 | tag("'"), |
| 516 | recognize(separated_list0( |
| 517 | tag(" "), |
| 518 | ( |
| 519 | recognize(pair(alpha1, many0_count(alt((alphanumeric1, is_a("-_")))))), |
| 520 | tag("="), |
| 521 | alt((parse_str_dq, parse_str_braced, parse_str_unq_inside_sq)), |
| 522 | ), |
| 523 | )), |
| 524 | tag("'"), |
| 525 | ) |
| 526 | .parse(input) |
| 527 | } |
| 528 | |
| 529 | /// Recognize a map enclosed in single quotes |
| 530 | #[inline(always)] |
| 531 | fn parse_kv_sq_as_map(input: &[u8]) -> IResult<&[u8], Value<'_>> { |
| 532 | delimited( |
| 533 | tag("'"), |
| 534 | separated_list0( |
| 535 | space1, |
| 536 | alt((separated_pair( |
| 537 | parse_key, |
| 538 | alt(( |
| 539 | tag("="), |
| 540 | recognize((tag(":"), space0)), // for 'avc: mumble mumble mumble …' |
| 541 | )), |
| 542 | alt(( |
| 543 | parse_encoded.map(Value::from), |
| 544 | parse_str_words_inside_sq.map(|v| Value::Str(v, Quote::None)), |
| 545 | parse_str_unq_inside_sq.map(|v| Value::Str(v, Quote::None)), |
| 546 | )), |
| 547 | ),)), |
| 548 | ), |
| 549 | tag("'"), |
| 550 | ) |
| 551 | .map(Value::Map) |
| 552 | .parse(input) |
| 553 | } |
| 554 | |
| 555 | /// More "correct" variant of parse_str_braced |
| 556 | #[inline(always)] |
| 557 | fn parse_kv_braced(input: &[u8]) -> IResult<&[u8], &[u8]> { |
| 558 | delimited( |
| 559 | tag("{ "), |
| 560 | recognize(separated_list0( |
| 561 | tag(" "), |
| 562 | ( |
| 563 | recognize(pair(alpha1, many0_count(alt((alphanumeric1, is_a("-_")))))), |
| 564 | tag("="), |
| 565 | alt((parse_str_sq, parse_str_dq, parse_str_unq)), |
| 566 | ), |
| 567 | )), |
| 568 | tag(" }"), |
| 569 | ) |
| 570 | .parse(input) |
| 571 | } |
| 572 | |
| 573 | /// Recognize regular keys of key/value pairs |
| 574 | #[inline(always)] |
| 575 | fn parse_key(input: &[u8]) -> IResult<&[u8], Key> { |
| 576 | recognize(pair(alpha1, many0_count(alt((alphanumeric1, is_a("-_")))))) |
| 577 | .map(|s: &[u8]| { |
| 578 | if let Ok(c) = Common::try_from(s) { |
| 579 | Key::Common(c) |
| 580 | } else if s.ends_with(b"uid") { |
| 581 | Key::NameUID(NVec::from(s)) |
| 582 | } else if s.ends_with(b"gid") { |
| 583 | Key::NameGID(NVec::from(s)) |
| 584 | } else { |
| 585 | Key::Name(NVec::from(s)) |
| 586 | } |
| 587 | }) |
| 588 | .parse(input) |
| 589 | } |
| 590 | |
| 591 | /// Recognize length specifier for EXECVE split arguments, e.g. a1_len |
| 592 | #[inline(always)] |
| 593 | fn parse_key_a_x_len(input: &[u8]) -> IResult<&[u8], u32> { |
| 594 | delimited(tag("a"), dec_u32, tag("_len")).parse(input) |
| 595 | } |
| 596 | |
| 597 | /// Recognize EXECVE split arguments, e.g. a1[3] |
| 598 | #[inline(always)] |
| 599 | fn parse_key_a_xy(input: &[u8]) -> IResult<&[u8], (u32, u16)> { |
| 600 | pair( |
| 601 | preceded(tag("a"), dec_u32), |
| 602 | delimited(tag("["), dec_u16, tag("]")), |
| 603 | ) |
| 604 | .parse(input) |
| 605 | } |
| 606 | |
| 607 | /// Recognize SYSCALL, EXECVE regular argument keys, e.g. a1, a2, a3… |
| 608 | #[inline(always)] |
| 609 | fn parse_key_a_x(input: &[u8]) -> IResult<&[u8], u32> { |
| 610 | preceded(tag("a"), u32).parse(input) |
| 611 | } |
| 612 | |
| 613 | /// Recognize identifiers (used in some irregular messages) |
| 614 | /// Like [A-Za-z_][A-Za-z0-9_]* |
| 615 | #[inline(always)] |
| 616 | fn parse_identifier(input: &[u8]) -> IResult<&[u8], &[u8]> { |
| 617 | recognize(pair( |
| 618 | alt((alpha1, tag("_"))), |
| 619 | many0_count(alt((alphanumeric1, tag("_")))), |
| 620 | )) |
| 621 | .parse(input) |
| 622 | } |
| 623 | |
| 624 | /// Characters permitted in kernel "encoded" strings that would |
| 625 | /// otherwise be hex-encoded. |
| 626 | #[inline(always)] |
| 627 | fn is_safe_chr(c: u8) -> bool { |
| 628 | c == b'!' || (b'#'..=b'~').contains(&c) |
| 629 | } |
| 630 | |
| 631 | /// Characters permitted in kernel "encoded" strings, minus |
| 632 | /// single-quotes, braces |
| 633 | #[inline(always)] |
| 634 | fn is_safe_unquoted_chr(c: u8) -> bool { |
| 635 | (b'#'..=b'&').contains(&c) || (b'('..=b'z').contains(&c) || c == b'!' || c == b'|' || c == b'~' |
| 636 | } |
| 637 | |
| 638 | /// Separator characters |
| 639 | #[inline(always)] |
| 640 | fn is_sep(c: u8) -> bool { |
| 641 | c == b' ' || c == b'\x1d' || c == b'\n' |
| 642 | } |