Quoting and escaping
quotes and escapes for application developers for security researchers
How to write a name or path that contains a space or a comma or a quote or a byte you cannot type. Double quotes and backslash escapes and the two stages in which the parser decodes them. Quotes do not switch off patterns.
On the map: profile structure
▶ Watch this deck — 8 scenes, ~6 min
Worth seeing first
- File rules — the rule as a row of words that a space breaks
- Comments and line structure — the comma and braces that quotes keep literal
01 A path with a space in it
A space ends a word. /srv/demo/My Files/** r, fails with "syntax error, unexpected TOK_ID, expecting TOK_MODE". Quoted, "/srv/demo/My Files/**" r, is one path. Both were run through the tree's parser.
Demo serves files from a directory called My Files, with a space in the name. You write the rule the obvious way, and the parser rejects it with a syntax error about an unexpected token. It doesn't say space anywhere. Here's what happened. A rule is a row of words separated by white space. The parser took everything up to the space as the path, then expected permissions, and found the word Files instead. To keep the path together, you have to tell the parser that the space belongs to it. Put the path in double quotes and it's one word again. This deck is about that, and about the other characters that need help.
Details and sources
Details
/srv/demo/My Files/** r,fails withsyntax error, unexpected TOK_ID, expecting TOK_MODE;"/srv/demo/My Files/**" r,is accepted. Both run with the parser built from the AppArmor tree at 836052d21 (-Q -K --skip-kernel-load --warn=all, nothing loaded into a kernel).- In the message
TOK_IDis a word (hereFiles/**) andTOK_MODEis a permission string such asr. - The parser's own tests have the same pair: bad_embedded_spaces_1.sd (
/abc def r,, expected FAIL) and ok_embedded_spaces_2.sd ("/abc def" r,, expected PASS). - apparmor.d(5) mentions it only inside the grammar: "Rules with embedded spaces or tabs must be quoted" (apparmor.d.pod:403).
- Quoting is the parser's business. The kernel receives the compiled pattern, in which the blank is an ordinary byte.
- The directory
My Filesand the profile are invented for the map.
Read more
- apparmor.d(5), FORMAT: see FILEGLOB and QUOTED FILEGLOB.
- File rules, comments and line structure.
for AppArmor developers for security researchers
In the code
- A bare word is
IDS, built fromID_CHARS,[^ \t\r\n"!,](parser_lex.l:264-267). The quoted form accepted at the start of a rule isQUOTED_LABEL(parser_lex.l:302); both returnTOK_ID(parser_lex.l:838-841).
02 A word is bare or quoted
A word is bare or "quoted". Quotes admit blanks, tabs, new lines, ! and ,. Escapes work in both: \", \\, \t, \n, \r, \a, \e, \f, \NNN (octal), \xHH (hex), \dDDD (decimal). The pattern escapes such as \* are a second group, in the scenes that follow. As a path in a rule a quoted string must begin with /, @, : or &.
Everywhere in the language, where a name, a path or a value goes, you can write a bare word or a string in double quotes. A bare word can't contain a blank, a tab, a new line, a double quote or an exclamation mark, and a comma with nothing after it ends the word. Inside quotes all of those are fine, and a quote itself is written \". Backslash escapes work in both forms: \t for a tab, backslash and three octal digits for any byte, backslash x and two hex digits. Profile and hat names can be quoted too.
Details and sources
Details
- A bare word cannot contain a blank, a tab, a carriage return, a new line,
"or!; a comma belongs to it only when a word character follows. - A quoted word is
", any bytes except NUL and", and". A quote inside is written\". - Escapes decoded in every word, bare or quoted:
\\,\",\a,\e(ESC, octal 033),\f,\n,\r,\t,\NNN(one to three octal digits),\xHH(one or two hex digits),\dDDD(one to three decimal digits). The numeric forms stop at the value 255:\378is\37followed by8(lib.c:158-219). - Quotes are not part of the name:
profile "demo" { }andprofile demo { }define the same profile. profile "demo service" /usr/bin/demo { ^"night shift" { } }gives the namesdemo serviceanddemo service//night shift(apparmor_parser -N, parser built from the AppArmor tree at 836052d21).- At the start of a rule a quoted string must begin with
/,@,:or&. - Quoted strings are also accepted for include file names, values after
=, list values such as flags, variable values andchange_profileoperands. - Fixtures: ok_quoted_1.sd (
"/bin/alpha\tbeta" rix,) and ok_octal_1.sd (/bin/\141bcde rix,).
Read more
- apparmor.d(5), FORMAT. The escape list is not in the man page's grammar; the source above is the reference.
- The profile name, variables, globbing.
for AppArmor developers for security researchers
In the code
- Scanner definitions:
ID_FIRST_CHARS,ID_FIRST,ID_CHARS,ID,IDS(parser_lex.l:262-267);QUOTED_LIST_VALUE_ID(parser_lex.l:273-275);ALLOWED_QUOTED_IDandQUOTED_ID(parser_lex.l:287-288);QUOTED_LABEL(parser_lex.l:302). - Decoding:
char *processid(const char *string, int len)and its helpers (parser_misc.c:468-535), usingint strn_escseq(const char **pos, const char *chrs, size_t n)(lib.c:158-219).
03 Quotes, or a backslash before the space
"/srv/demo/My Files/**" and /srv/demo/My\ Files/** are the same path. The backslash form draws the --warn=format warning "Character was quotedunnecessarily, dropped preceding quote ('\') character"; the character it names is the blank. Run with the tree's parser.
There are two ways to keep that space. Quotes around the whole path, or a backslash right before the space. Both give the same path pattern: the parser's rule dump prints the same expression for the two spellings. But they aren't equally good. Ask the parser for format warnings and the backslash version earns one: character was quoted unnecessarily, with the two words run together exactly like that. And the backslash has a sharp edge. If the space you escape is the one before the permissions, the permissions become part of the path, and you get a syntax error. So use quotes. They're also easier to read.
Details and sources
Details
"/srv/demo/My Files/**" r,and/srv/demo/My\ Files/** r,compile to the same expression,/srv/demo/My Files/[^/\x00][^\x00]*. Printed by the parser built from the AppArmor tree at 836052d21 with-D rule-exprs(-Q -K --skip-kernel-load, nothing loaded into a kernel).- The backslash form draws
Warning from FILE (FILE line N): Character was quotedunnecessarily, dropped preceding quote ('\') character. The text has no blank between "quoted" and "unnecessarily", and the character it names is the blank. - The warning's class is
format, "warn on unnecessary or confusing formatting" (parser_main.c:291). It is off by default; turn it on with--warn=formator--warn=all. The line number it prints is past the end of the profile. - A backslash before the blank that precedes the permissions joins them to the path:
/srv/demo/end\ r,givessyntax error, unexpected TOK_END_OF_RULE, expecting TOK_MODE. - Fixtures, all expected to pass: ok_embedded_spaces_1.sd (
"/abc\ def" r,), ok_embedded_spaces_2.sd ("/abc def" r,), ok_embedded_spaces_4.sd (/abc\ def r,).
Read more
- apparmor_parser(8), OPTIONS:
--warnand [-D,--dump](https://apparmor.net/man/master/apparmor_parser/#D-n---dump-n). - Running the parser.
for AppArmor developers for security researchers
In the code
- The escaped blank is part of a bare word by the scanner's
IDdefinition (parser_lex.l:266). - The backslash is dropped, with
pwarn(WARN_FORMAT, ...), in the default branch ofconvert_aaregex_to_pcre(parser_regex.c:382-401).
04 Quotes do not switch off patterns
"/srv/demo/report*.txt" is still a glob. \*, \?, \[, \], \{, \}, \, and \\ stand for the characters themselves, inside or outside quotes. The matches drawn follow from the expressions the tree's parser printed; they were not run against a kernel.
If you come from the shell, here's a habit to drop. In a shell, quotes switch off wildcards. Here they don't. A star inside double quotes is still a star: the parser compiles the quoted rule into a pattern that fits report one, report two and everything else. Quotes only keep a word together. To mean the character itself, a file that really has a star in its name, put a backslash in front of it. That works for all the pattern characters: star, question mark, square brackets, curly braces, and a comma inside braces. When a path has both a space and a literal star, use both tools: quotes around it, and a backslash before the star.
Details and sources
Details
"/srv/demo/q*"is converted to/srv/demo/q[^/\x00]*: still a glob./srv/demo/report\*.txtis converted to/srv/demo/report\*\.txt: a literal star. Both printed by the parser built from the AppArmor tree at 836052d21 with-D rule-exprs.- Literal forms, inside or outside quotes:
\*,\?,\[,\],\{,\},\,and\\. \,matters inside{ }and character classes:/srv/demo/c\,dis converted to/srv/demo/c,d..,+,(,)and the vertical bar are always literal, escaped or not.^and$are literal outside a character class.- Known discrepancy:
[\^a]loses its escape and becomes the negated class[^a];/k[\^a] r,and/k[^a] r,compile to identical output (run with the released parser 5.0.2). Put^anywhere but first in the class. - What a pattern matches is derived from the printed expression. Nothing on this slide was run against a kernel.
Read more
for AppArmor developers for security researchers
In the code
- The first stage keeps the backslash in front of
*?[]{}^,\on purpose (parser_misc.c:480-503). convert_aaregex_to_pcre: a backslash setsbEscape(parser_regex.c:152-167);*withbEscapeis emitted as\*(parser_regex.c:168-176),?as itself (parser_regex.c:231-237); brackets, braces and the comma follow (parser_regex.c:257-345);^,$and the always-escaped characters are at parser_regex.c:356-380.
05 Decoded in two stages
Stage 1 (every word): remove quotes, decode \t, \NNN, \xHH, \dDDD and the like to bytes. Stage 2 (patterns): \*, \?, \[, \{, \,, \\ become literal characters. apparmor_parser -D rule-exprs shows the text between the stages; the line drawn is what the tree's parser printed.
Why are there two groups of escapes? Because the parser decodes in two stages. Take a path with a bit of everything: quotes, a space, \x41, and \*. Stage one happens to every word in the file. The quotes come off, and escapes that stand for a single byte are decoded: \x41 becomes a capital A. Backslash star is left alone, on purpose. Stage two happens only where a pattern is expected. It reads the remaining backslashes: \* means a real star. The result matches one file name exactly. You can watch the middle step yourself: the parser's rule expression dump prints each path as it leaves stage one.
Details and sources
Details
apparmor_parser -D rule-exprs("Dump rule to expr tree conversions", common_optarg.c:31) prints each path asaare: BEFORE -> AFTER. BEFORE is the text after stage one, AFTER the regular expression that stage two produced.- The slide's example, printed by the parser built from the AppArmor tree at 836052d21:
"/srv/demo/My Files/\x41\*.txt"gives/srv/demo/My Files/A\*.txt -> /srv/demo/My Files/A\*\.txt. - Stage one (every word): quotes removed;
\a \e \f \n \r \t,\NNN,\xHH,\dDDD,\"decoded to one byte. - Stage one leaves alone: a decoded value of 0 (
\000,\x00,\d0), whose escape text is kept; a decoded value that is one of*?[]{}^,or a backslash, which is stored with a backslash in front (\d123becomes\{, run with the released parser 5.0.2); and a backslash before anything else. - Stage two (patterns only):
\*,\?,\[,\],\{,\},\,,\\become literal characters;\000and\x00become a zero byte. - More conversions:
/srv/demo/\141bcgives/srv/demo/abc;/srv/demo/e\x41\d066\103gives/srv/demo/eABC.
Read more
for AppArmor developers for security researchers
In the code
- Stage one:
processid,processquoted,processunquoted(parser_misc.c:468-535) withstrn_escseq(lib.c:158-219). - Stage two:
convert_aaregex_to_pcre(parser_regex.c:118-445). - The decks: the lexer, patterns to regular expressions.
06 processunquoted and convert_aaregex_to_pcre for AppArmor developers for security researchers
Stage 1: processid() → processquoted() → processunquoted() using strn_escseq(). Stage 2: convert_aaregex_to_pcre() with bEscape; leftovers go through str_escseq() and reach the regex scanner in libapparmor_re/parse.y.
The two stages by name. Stage one is processid, which strips the quotes and calls processunquoted. That loops over the text with strn_escseq. A decoded value of zero isn't stored; the escape text is copied instead, so that no zero byte cuts the C string short. A decoded value that is a pattern character gets a backslash put back in front. Anything else is stored as the byte. Stage two is convert_aaregex_to_pcre. A backslash sets the escape flag for the next character. Pattern characters come out escaped. Anything else is tried as an escape sequence, and either passed on to the regex parser, or dropped with a format warning. Dots, pluses, bars and parentheses always come out escaped.
Details and sources
for AppArmor developers for security researchers
Details
char *processid(const char *string, int len)callsprocessquotedwhen the text starts with", elseprocessunquoted;processquotedstrips the quotes and callsprocessunquoted(string + 1, len)(parser_misc.c:506-535).char *processunquoted(const char *string, int len)(parser_misc.c:468-503): for each backslash it callsstrn_escseq(&pos, "", len). A result of 0 copies the escape text; a result in"*?[]{}^,\\"is stored as backslash plus the character; any other result is stored as the byte; -1 copies the backslash unchanged.int strn_escseq(const char **pos, const char *chrs, size_t n)(lib.c:158-219): octal bystrntol(*pos, &end, 8, 255, min(3, n)),\ddecimal with at most 3 digits,\xhex with at most 2, then the lettersa e f n r t; returns -1 for anything else.pattern_t convert_aaregex_to_pcre(const char *aare, int anchor, int glob, std::string& pcre, int *first_re_pos)(parser_regex.c:118-445). A backslash setsbEscape(parser_regex.c:152-167).- Default branch: with
bEscape,str_escseq(&pos, "")is tried; a valid escape is passed on with its backslash, anything else getspwarn(WARN_FORMAT, "Character %c was quotedunnecessarily, dropped preceding quote ('\\') character\n", ...)(parser_regex.c:382-401). .,+,|,(,)always get a backslash;^and$outside a character class too (parser_regex.c:356-380).- A pattern that ends in a backslash:
Regex error: trailing '\' escape character(parser_regex.c:418-422; read, not run). - The regex scanner decodes passed-on escapes with
str_escseq(pos, "*+.|^$-[](){}")(parse.y:182). escape_re()puts a backslash before{}[]*?when@{profile_name}is expanded (parser_variable.c:87-103).- Unit tests:
test_processunquoted,test_processquoted(parser_misc.c:1387-1460).
Read more
- The lexer, patterns to regular expressions, the expression tree.
- TechnicalDoc_HFA on the wiki: what happens to the regular expression next.
07 When quoting goes wrong
A quoted path in a rule must begin with /, @, : or &. ! needs a backslash or quotes. A missing closing quote extends the string to the next " in the file; the resulting error and its line number point elsewhere. The uncounted new lines are a known discrepancy, not intended behaviour.
Quoting errors rarely name the real problem. A quoted path that doesn't start with a slash is rejected at the quote character. An exclamation mark in a bare word is rejected too. The nasty one is a missing closing quote. The string runs on, across your next rules, until the next quote character in the file, and the syntax error you get describes whatever came after that. New lines inside a quoted string aren't counted either, so the line number can be too low. When an error makes no sense, look upward for an unbalanced quote. And a backslash before an ordinary letter is just dropped, with a format warning if you ask.
Details and sources
Details
Run with the parser built from the AppArmor tree at 836052d21 (-Q -K --skip-kernel-load --warn=all, nothing loaded into a kernel):
- A quoted path that does not start with
/,@,:or&:Lexer found unexpected character: '"' (0x22) in state: INITIAL. - A bare
!:Lexer found unexpected character: '!' (0x21) in state: INITIAL. Write\!or quote the word. - A missing closing quote: the string runs to the next
"in the file, and the usual result issyntax error, unexpected TOK_ID, expecting TOK_MODE. With no later quote at all the unexpected character message above is reported at the line of the opening quote. - Known discrepancy: new lines inside a quoted string are not counted, so later messages report a line number that is too low. An error on line 4 was reported as line 3.
- Known discrepancy: a quoted string that ends in an escaped backslash,
"/a\\", is extended to the next"in the file, because the second backslash and the quote are read as the pair\". profile "" { }:Empty named transition profile name:.- A backslash before an ordinary character, such as
\qor\., is dropped; with--warn=formatthe parser printsCharacter C was quotedunnecessarily, dropped preceding quote ('\') character. Fixture: ok_slashquote_1.sd (/bingo/bang\o/bongo rw,). - A pattern that ends in a backslash:
Regex error: trailing '\' escape character, thenUnable to parse input line(read from the source, not run).
Read more
for AppArmor developers for security researchers
In the code
ALLOWED_QUOTED_IDis[^\0"]|\\\": a new line is an ordinary character, and\\is not treated as a unit (parser_lex.l:287-288).QUOTED_LABELrequires the first character (parser_lex.l:302); its action does not touchcurrent_lineno(parser_lex.l:838-841).- The catch-all rule with the unexpected character message: parser_lex.l:930.
- The format warning and the trailing backslash error: parser_regex.c:392-400, parser_regex.c:418-422.
08 Around quoting
Pattern characters: syn-globbing. Structure characters: syn-comments. Zero bytes in socket names: syn-unix. The two stages: fe-lexer, fe-aare.
Two neighbours explain what you were protecting. The pattern characters that a backslash makes literal are the subject of the globbing deck. The structure characters that quotes keep inside a word, the comma above all, are in the comments and line structure deck. Quoted words turn up in file rules, in profile names and in variables, and each has a deck. One special use of escapes: abstract unix socket names contain zero bytes, written \000; the unix rules deck shows that. And the two stages each have a deck in the parser district.
Details and sources
Details
- Quotes keep a word together; they do not switch off patterns. A backslash makes a pattern character literal.
- Escapes for single bytes (
\t,\NNN,\xHH,\dDDD) are decoded in every word; pattern escapes (\*,\?,\[,\{,\,) only where a pattern is expected. - Zero bytes: apparmor.d(5) says of abstract unix socket names "null characters must be specified by using an escape sequence \000 or \x00" (apparmor.d.pod:1583-1585).
- The fixtures bad_embedded_0_1.sd,
_2and_3(\000,\x00,\d00in a file path) are marked as passing despite their names; the parser has no check for a zero byte in a file path. - Commit read for this deck: AppArmor 836052d21 (6.0.0~alpha2). Nothing in it was run against a kernel.
Read more
Basis of the statements on this page: "tree" means the parser built from the AppArmor commit above was run on the fragment (-Q -K --skip-kernel-load --warn=all, with -D rule-exprs for conversions) and nothing was loaded into a kernel; "5.0.2" means the released parser 5.0.2 was run; "code" means the parser source was read, not run. What a pattern matches is derived from the expression the parser printed.
Words
WORD: BARE | QUOTED
BARE: one or more of: any character except blank, tab, CR, LF, " ! ,
, followed by a word character
the pairs \blank \tab \" \! \,
\# (as the first character only)
QUOTED: '"' { any byte except NUL and " | \" } '"'
- Quotes are not part of the name:
profile "demo" { }andprofile demo { }define the same profile. - At the start of a rule a quoted string must begin with
/,@,:or&. - Quoted strings are accepted for include file names, names after
profile,hat,^and->, values after=, identity values, list values such as flags, variable values,change_profileoperands, ids in keyword rules andifexpressions. A quoted value in a conditional list needs at least one character. profile "demo service" /usr/bin/demo { ^"night shift" { } }gives the namesdemo serviceanddemo service//night shift(tree,-N).- Quotes do not switch off patterns:
"/srv/demo/q*"is a glob (tree). - A quoted string may contain raw new lines; see Known discrepancies for the effect on line numbers.
Escapes decoded in every word
Decoded in quoted and bare words alike, in paths, profile names, flag values, variable values and conditional values (code).
| Escape | Result |
|---|---|
\\ | a backslash, kept escaped for the pattern stage |
\" | " |
\a | BEL |
\e | ESC (octal 033) |
\f | form feed |
\n | new line |
\r | carriage return |
\t | tab |
\NNN | one to three octal digits, value at most 255; \378 is \37 followed by 8 |
\xHH | one or two hex digits |
\dDDD | one to three decimal digits, value at most 255; not a digit class |
- A decoded value of 0 (
\0,\000,\x00,\d0) is not stored as a byte: the escape text is kept for the pattern stage. - A decoded value that is one of
*?[]{}^,or a backslash is stored with a backslash in front, so the pattern stage reads it as literal:\d123becomes\{(tree). - A backslash followed by anything else is copied unchanged and left to the pattern stage.
Escapes interpreted in patterns
| Written | Meaning |
|---|---|
\* \? \[ \] \{ \} | the character itself |
\, | a literal comma; matters inside { } and character classes |
\\ | a literal backslash |
. + ( ) and the vertical bar | always literal, escaped or not |
^ $ | literal outside a character class, escaped or not |
\000 \x00 \d0 | a zero byte |
any other escaped character, such as \q, \!, \. or backslash blank | the backslash is dropped, with the format warning below |
Pattern characters themselves: syn-globbing.
Worked conversions
Printed by the tree's parser with -D rule-exprs as aare: BEFORE -> AFTER.
| Written | Before conversion | After conversion |
|---|---|---|
"/srv/demo/My Files/**" | /srv/demo/My Files/** | /srv/demo/My Files/[^/\x00][^\x00]* |
/srv/demo/My\ Docs/** | /srv/demo/My\ Docs/** | /srv/demo/My Docs/[^/\x00][^\x00]*, with the format warning |
"/srv/demo/q*" | /srv/demo/q* | /srv/demo/q[^/\x00]* |
/srv/demo/report\*.txt | /srv/demo/report\*.txt | /srv/demo/report\*\.txt |
"/srv/demo/My Files/\x41\*.txt" | /srv/demo/My Files/A\*.txt | /srv/demo/My Files/A\*\.txt |
/srv/demo/\141bc | /srv/demo/abc | /srv/demo/abc |
/srv/demo/e\x41\d066\103 | /srv/demo/eABC | /srv/demo/eABC |
"/srv/demo/a\tb" | /srv/demo/a, a tab byte, b | the same |
/srv/demo/nul\000x | /srv/demo/nul\000x | /srv/demo/nul\x0x (in the rule line) |
/srv/demo/c\,d | /srv/demo/c\,d | /srv/demo/c,d |
"/srv/demo/a,b" | /srv/demo/a,b | the same |
"/srv/demo/trailing," | /srv/demo/trailing, | the same |
/srv/demo/it\"s | /srv/demo/it"s | the same |
Spaces
"/srv/demo/My Files/**" r,and/srv/demo/My\ Files/** r,are the same rule (tree). The second draws the format warning.- A backslash before the blank that precedes the permissions joins them to the path:
/srv/demo/end\ r,is a syntax error (tree). - In identities a blank is allowed only in a value that starts with
/: syn-identities. - The man page mentions quoting only in passing ("Rules with embedded spaces or tabs must be quoted", parser/apparmor.d.pod:139, 403); the backslash spelling and the escape list are not in its grammar.
Errors and warnings
| Message | Cause |
|---|---|
syntax error, unexpected TOK_ID, expecting TOK_MODE | an unquoted space in a path; also the usual result of a missing closing quote (tree) |
Lexer found unexpected character: '"' (0x22) in state: INITIAL | a quoted path that does not start with /, @, : or &; or an opening quote with no later quote in the file, reported at the line of the opening quote (tree) |
Lexer found unexpected character: '!' (0x21) in state: INITIAL | a bare !; write \! or quote the word (tree) |
syntax error, unexpected TOK_END_OF_RULE, expecting TOK_MODE | a backslash before the blank that precedes the permissions (tree) |
Empty named transition profile name: | profile "" { } (tree) |
Regex error: trailing '\' escape character, then Unable to parse input line | a pattern that ends in a backslash (code; not run) |
Warning: Character C was quotedunnecessarily, dropped preceding quote ('\') character | a backslash before a character that needs none; class format, off by default, enabled by --warn=format or --warn=all; printed as Warning from FILE (FILE line N): ... with a line number past the end of the profile (tree) |
- The warning text has no blank between "quoted" and "unnecessarily". For backslash blank the character
Cis the blank, so the message readsCharacter was quotedunnecessarily, .... - The warning is also printed for
\., although the output is the same with or without the backslash.
Known discrepancies
- New lines inside a quoted string are not counted. Every message after a multi-line quoted string, or after a missing closing quote that pairs with a later quote, reports a line number that is too low: an error on line 4 was reported as line 3 (tree).
- A quoted string that ends in an escaped backslash,
"/a\\", is extended to the next"in the file, because the scanner reads the second backslash and the quote as the pair\". Two quoted paths, the first ending in\\, fail withsyntax error, unexpected TOK_ID, expecting TOK_MODE(tree). - A bare
\\before a blank is read as a backslash plus an escaped blank:/a\\ r,fails withsyntax error, unexpected TOK_END_OF_RULE, expecting TOK_MODE(tree). [\^a]loses its escape and becomes the negated class[^a], broader than written:/k[\^a] r,and/k[^a] r,compile to identical output (tree). Put^anywhere but first in the class.- The fixtures
parser/tst/simple_tests/file/bad_embedded_0_1.sd,_2and_3(\000,\x00,\d00in a file path) are marked as passing despite their names; the parser has no check for a zero byte in a file path. - Zero bytes are useful in abstract socket addresses: syn-unix.
for AppArmor developers for security researchers
Code map
| Step | Where |
|---|---|
bare word character classes ID_CHARS, ID_FIRST, ID | parser/parser_lex.l:262-267 |
QUOTED_LIST_VALUE_ID | parser/parser_lex.l:273-275 |
ALLOWED_QUOTED_ID, QUOTED_ID; \\ is not treated as a unit, the cause of the two backslash discrepancies | parser/parser_lex.l:287-288 |
QUOTED_LABEL, the quoted form accepted at the start of a rule, and its action | parser/parser_lex.l:302, 838-841 |
processid(), processquoted(), processunquoted() | parser/parser_misc.c:468-535 |
strn_escseq() | parser/lib.c:158-219 |
unit tests test_processunquoted, test_processquoted | parser/parser_misc.c:1387-1460 |
convert_aaregex_to_pcre(); bEscape set at 152-167 | parser/parser_regex.c:118-445 |
^ $ and the always-escaped characters | parser/parser_regex.c:358-380 |
other escaped characters: str_escseq(), else pwarn(WARN_FORMAT) | parser/parser_regex.c:382-401 |
| trailing backslash error | parser/parser_regex.c:418-422 |
--warn=format | parser/parser_main.c:291 |
the regex scanner decodes passed-on escapes with str_escseq() | parser/libapparmor_re/parse.y:182 |
escape_re() adds a backslash before pattern characters when @{profile_name} is expanded | parser/parser_variable.c:87-103 |
Roads from here
START HERE
- The profile language at a glance — back to the whole profile on one page
FILE ACCESS
- Globbing (AARE) — the pattern characters a backslash makes literal
- File rules — the rules these paths are written in
PREAMBLE AND REUSE
- Variables — quoting in variable values
PROFILE HEAD
- The profile name — quoted profile names
RULE CLASSES
- Unix socket rules — escaped zero bytes in abstract socket addresses
PROFILE STRUCTURE
- Comments and line structure — commas and hashes and braces as structure
INSIDE THE PARSER
- The lexer — the first decoding stage
- From AARE to regular expressions — the second decoding stage