Message queue rules
message queues for application developers for administrators for security researchers
An mqueue rule names permissions on a POSIX or System V message queue. The queue name decides the type. A name starting with a slash is POSIX and a number is a System V key. The parser compiles the rule when the feature set lists message queues. The mainline kernel has no hook that consults it and Ubuntu kernels do.
On the map: rule classes
▶ Watch this deck — 8 scenes, ~5 min
Worth seeing first
- Rule conditionals — the conditional syntax for type and label
- The abi rule and the abi files — why a rule class can be accepted and not enforced
01 demo posts jobs to a queue
mqueue PERMISSIONS [type=...] NAME, says what a profile may do with a message queue.
Demo passes jobs to its worker through a message queue: a named mailbox in the kernel that one process writes to and another reads from. The profile language has a rule for them, mqueue, and line 20 of demo's profile is one. It lets demo create and open the POSIX queue called /demo-queue. The rule has three parts: permissions, a type, and the name of the queue. One thing up front. Not every kernel enforces these rules, and we'll get to which ones.
Details and sources
Details
- The system calls the rule is meant to control:
mq_open,mq_unlink,mq_send,mq_receive,mq_getattr,mq_setattrfor POSIX queues;msgget,msgsnd,msgrcv,msgctlfor System V queues. - The mainline kernel has no message-queue hook in AppArmor (lsm.c:1785-1890) and no
ipcdirectory under/sys/kernel/security/apparmor/features/(apparmorfs.c:2684-2701). See "Who enforces it". - Line 20 compiles without a warning under the feature set abi/5.0 with the parser built from the tree.
- The program
demo, its worker and the queue/demo-queueare invented for the map. - Kernel statements are read from the kernel source and were not run.
02 Permissions, type, label, name
[QUALIFIERS] mqueue [PERMISSIONS] [type=posix|sysv] [label=LABEL] [NAME],. Permissions before conditionals, the name last.
The order is fixed: the keyword, then the permissions, then the conditionals, then the name of the queue. Permissions are one word, or several in parentheses. There are two conditionals: type, which is posix or sysv, and label. The name comes last. Everything after the keyword can be left out, and the bare rule covers every queue of both types with every permission. Owner is the one qualifier this rule refuses.
Details and sources
Details
- The conditionals take
=only:only "=" allowed in conditions of mqueue rules(mqueue.cc:76). Each may be given once. An unknown name givesinvalid mqueue rule conditional "%s"(mqueue.cc:91). - The label is a pattern and takes variables.
- Qualifiers:
audit,allow,deny,promptandpriority=are taken (prompt mqueue,is accepted by the parser built from the tree;deny mqueue,andaudit allow mqueue,are in ok_12.sd).owneris refused:owner prefix not allowed on mqueue rules(mqueue.h:113-119). - Another test file: ok_05.sd.
- Known discrepancy:
type=(posix sysv)andlabel=(a b)are accepted, and only the first value is used.
Read more
for AppArmor developers for security researchers
In the code
- The
mqueue_ruleproductions: parser_yacc.y:1586-1595; the permissions: parser_yacc.y:1541-1584. - The scanner state
MQUEUE_MODE: parser_lex.l:899-900, parser_lex.l:636-695. - The conditionals: mqueue.cc:73-94.
03 The name says which kind of queue
/name is a POSIX queue, digits are a System V key. type= is needed only without a name and must not contradict it.
Linux has two families of message queue, and they name their queues differently. A POSIX queue has a name that starts with a slash. A System V queue is identified by a number, its key. The rule uses exactly that to tell them apart. A name with a leading slash makes it a POSIX rule. Digits make it a System V rule. Anything else is an error. So type= is only needed when there's no name, to say which family the rule is about. If you do write both, they have to agree.
Details and sources
Details
- The three name errors: mqueue.cc:54-56, mqueue.cc:60-62, mqueue.cc:65. How the name picks the type: mqueue.cc:49-67.
- A POSIX name is a pattern in the globbing syntax:
mqueue /q*,is accepted by the parser built from the tree. A System V key must be all digits. - Without a name,
type=posixortype=sysvsays which family the rule is about; without either, the rule is about both. - Known discrepancy: the name cannot begin with a variable.
mqueue @{profile_name},fails with the invalid-name message, because the name is checked before variables are expanded (mqueue.cc:106-107, mqueue.cc:179-183). - Known discrepancy (latent): the System V key is converted without a range check (mqueue.cc:289-291);
mqueue r 99999999999,is accepted.
04 Seven permissions and whose queue it is
Permissions: create open delete getattr setattr read write (r, w, rw). label= narrows the rule to queues with that label.
Seven permissions cover the life of a queue: create it, open it, write to it and read from it, get and set its attributes, delete it. One word needs no parentheses, several do, and r and w work as short forms. Demo's side needs create, open and write. The worker's needs open and read. Label is the conditional that says whose queue. With label=demo, the worker's rule applies only to a queue that carries demo's label, not to a queue of the same name made by something else.
Details and sources
Details
- The seven permissions are valid for both types. Outside parentheses only one word is allowed.
- The first rule on the slide is demo's line 20 with
writeadded. - The library calls next to the permissions are the usual POSIX ones and illustrate the words. Neither source tree contains kernel code that maps calls to these permissions.
- With
createandlabel=in one rule,createand anyreadorwriteof the rule are also granted on the name without the label, because a queue has no label before it exists (mqueue.cc:262-272). Seen withmqueue (create read) label=l /q2,and the parser built from the tree. - How a kernel that enforces these rules assigns and compares the label is outside both trees.
- What the mainline kernel does with these permissions: nothing. See "Who enforces it".
Read more
- apparmor.d(5), Message Queue rules and apparmor.d(5), Label Match Conditional.
- Rule conditionals: the three spellings of a label.
- Labels.
for AppArmor developers for security researchers
In the code
- Permission bits: write
0x2, read0x4, create0x10, delete0x20, open0x40, setattr0x100, getattr0x200(mqueue.h:31-40).
05 Who enforces it
On the mainline kernel mqueue rules are accepted and not enforced, deny mqueue, included; a POSIX queue is still refused there, as a file with a disconnected path. --warn=rule-not-enforced shows when the parser drops them. See Upstream and Ubuntu.
Now the catch. A rule only matters if two things say yes. First the parser. It compiles an mqueue rule only when the feature set lists message queues. The abi files 4.0 and 5.0 do, for POSIX queues. Without that the rule is dropped, and the warning is off by default. Second, the mainline kernel. It has no message queue entry in its feature directory and no code for these rules. That doesn't open the queues: there, opening a POSIX queue is refused as a file with a disconnected path, rule or no rule. Ubuntu's kernels are different; the upstream and Ubuntu deck has the details.
Details and sources
Details
- Feature names:
ipc/posix_mqueueandipc/sysv_mqueue(parser_main.c:1102-1107). The tree's abi/4.0 and abi/5.0 listipc/posix_mqueue(4.0:34, 5.0:49). - Which feature set the parser uses: the
abirule of the profile if there is one, else the running kernel's; see the abi rule. Withabi <abi/5.0>,the rule is compiled and loaded, and the mainline kernel never looks at it. With the mainline kernel's own feature set the rule is dropped. - No ABI file in the tree lists
ipc/sysv_mqueue, sotype=sysvrules compile to nothing under every shipped ABI:mqueue type=sysv rules not enforcedunder abi/5.0. - The three warnings, of class
rule-not-enforced:mqueue rules not enforced,mqueue type=posix rules not enforced,mqueue type=sysv rules not enforced(mqueue.cc:192-200). The line on the slide was printed by the parser built from the tree for a profile withabi <abi/3.0>,, a feature set withoutipc.deny mqueue,under abi/3.0 givesmqueue rules not enforced. - The listing on the slide is built from the kernel's table of feature directories (apparmorfs.c:2684-2701):
caps,capability,dbus,domain,file,io_uring,mount,namespaces,network_v8,network_v9,policy,ptrace,query,rlimit,signal. It was not taken from a running system. - In the mainline kernel class 17 and its log name
posix_mqueueare defined (apparmor.h:33, audit.c:58), class 18 is not, and no code walks either. The hook table: lsm.c:1785-1890. - The tree's log fixtures for message queues (
testcase_mqueue_01.intotestcase_mqueue_09.ininlibraries/libapparmor/testsuite/test_multi/) come from kernels with the extra patches. - Known discrepancy: a POSIX message queue on a kernel without mqueue mediation is refused as a file with a disconnected path. The mainline kernel does not consult
mqueuerules, and that does not leave POSIX queues open: the queue is a file on an internal mount, and opening it reaches the file-open check with a path that does not connect to the root. Observed on Debian 13 and Debian sid, with and without anmqueuerule:mq_openfails with error 13 and the log hasapparmor="DENIED" operation="open" class="file" info="Failed name lookup - disconnected path". See Kernel features and the policy ABI. - Kernel statements are read from the kernel source and were not run, except the one about the refused queue.
for security researchers
Attack surface
- On the mainline kernel a profile's mqueue rules, allow or deny, are not a boundary: a confined task's access to message queues is not decided by this class.
- Known discrepancy: a POSIX message queue on a kernel without mqueue mediation is refused as a file with a disconnected path. Adding an
mqueuerule to the profile did not change the refusal; a file rule for the queue was not tried in the recorded runs. Observed on Debian 13 and Debian sid, with and without anmqueuerule:mq_openfails with error 13 and the log hasapparmor="DENIED" operation="open" class="file" info="Failed name lookup - disconnected path". See Kernel features and the policy ABI and What is not mediated. - A reviewer has to know the target kernel's feature set. The parser is silent by default when it drops the rule.
06 Two classes, name and label for AppArmor developers for security researchers
mqueue.cc:202-345 writes \x11 name NUL label (POSIX) and \x12 key NUL label (System V). The mainline kernel defines class 17 and never walks it.
An mqueue rule writes into two classes: seventeen for POSIX, eighteen for System V. A POSIX rule is the class byte, the queue name, a null and the label. Demo's line produces three rules. The permissions on the name alone, because no label was given. Create on the name alone, because a queue has no label until it exists. And the permissions on the name plus any label. System V would use a four-byte key, but no abi file in the tree enables it. On the kernel side the class number and its log name are defined, and that's all. No hook starts a match there.
Details and sources
for AppArmor developers for security researchers
Details
- Printed by the parser built from the tree with
-D rule-exprsfor demo's line 20 under abi/5.0:\x11/demo-queue0x50,\x11/demo-queue0x10,\x11/demo-queue\x00[^\000]+0x50, and the class marker\d17at priority 1001. - The rules are written by
gen_policy_re(mqueue.cc:202-345); the constructor: mqueue.cc:97-125. - Permission bits are remapped before storing: the low seven stay, setattr and getattr move to
0x4000and0x8000(map_mqueue_perms, mqueue.h:91-95), so the full set is0xc076. - More dumps:
mqueue read /q,gives\x11/qand\x11/q\x00[^\000]+with0x4;mqueue (create read) label=l /q2,gives\x11/q2and\x11/q2\x00l, both0x14. - The short forms
randwcome from the permission parser shared with dbus, signal and ptrace (parser_misc.c:763-772). - Class markers
\d17and\d18at priority 1001 (parser_regex.c:1444-1449). - Known discrepancy: an untyped rule such as
mqueue,under abi/5.0 also emits three class-18 rules (\x12....) although no class-18 marker is added (mqueue.cc:216-223, mqueue.cc:284-338). - Known discrepancy: the kernel's table of class names has
io_uringat index 18 (audit.c:58-59). - Kernel statements are read from the kernel source and were not run.
for security researchers
Attack surface
- The create rule widens a labelled rule:
mqueue (create read) label=l /q2,grantscreateandreadon/q2without the label. It follows the reasoning of the code comment (mqueue.cc:262-272) and is how the parser behaves. How an enforcing kernel queries the two forms is outside the trees.
for AppArmor developers for security researchers
Read more
07 Mistakes and their messages
Permissions first and in parentheses when there are several; then type=, label=; the name last, starting with / or all digits.
Two permissions without parentheses: the parser stops at the second word. The type before the permissions: wrong order, because permissions come first. A name without a leading slash that isn't a number. An unknown permission in a list gives an odd message that starts with the word internal; it means the word isn't a permission. Owner isn't allowed. And remember the mistake that produces no message at all: relying on a rule the kernel doesn't enforce.
Details and sources
Details
- Messages are from the parser built from the AppArmor tree (
-Q -K --skip-kernel-load --warn=all, nothing loaded into a kernel). The full form isAppArmor parser error for FILE in profile FILE at line N: MESSAGE. mqueue invalid type='%s'for a type other thanposixorsysv(mqueue.cc:88).invalid mqueue rule conditional "%s"for an unknown conditional (mqueue.cc:91).mqueue type=sysv write 42,is a syntax error: permissions come first.mqueue invalidaccess /queuename,fails (bad_05.sd).- The first row names
TOK_OPENbecause the wordopenis a token shared with{; see known bugs. perms contains invalid permissions for mqueue(mqueue.cc:112-116) exists for permission bits outside the set.- The warnings for a dropped rule are under "Who enforces it".
Read more
08 Which kernel, and what else
mqueue rules are syntax everywhere and enforcement on some kernels. See Upstream and Ubuntu.
That's the mqueue rule: permissions, a type, a label, a name. Whether it does anything is decided by the kernel you run, and the upstream and Ubuntu deck tells you which is which. The feature decks show how to read what your own kernel supports. And for messaging between processes that the mainline kernel does check, see unix socket rules and signal rules.
Details and sources
Details
- An mqueue rule is permissions,
type=,label=and a queue name. A name starting with/is a POSIX queue; digits are a System V key. - The parser compiles the rule when the feature set lists message queues. The mainline kernel has no hook that consults it.
- Known discrepancy: a POSIX message queue on a kernel without mqueue mediation is refused as a file with a disconnected path. See Kernel features and the policy ABI.
- Commits read for this deck: AppArmor 836052d21 (6.0.0~alpha2) and the kernel tree named on the page. The kernel behaviour was read from the source, not observed on a running kernel, except the refused queue (Debian 13 and Debian sid).
Read more
Basis of the statements on this page: "tree" means the parser built from the AppArmor commit above was run on the line (-Q -K --skip-kernel-load --warn=all, feature set abi/5.0 unless another is named), with nothing loaded into a kernel; "code" means the source was read, not run. Every statement about the kernel is read from the kernel source. What kernels with extra patches do is in Upstream and Ubuntu kernels.
Grammar
MQUEUE RULE: [QUALIFIERS] mqueue [PERMISSIONS] [type=posix|sysv] [label=LABEL] [NAME] ,
PERMISSIONS: PERMISSION | ( PERMISSION [,] ... )
NAME: /PATTERN a POSIX queue
DIGITS a System V key
- Permissions come before the conditionals; the name is last (tree).
typeandlabelmay be written in either order, each once, with=only (tree).mqueue,covers every permission, both types and any queue (tree).
| Seat | On an mqueue rule |
|---|---|
priority= | taken |
audit | taken |
allow, deny, prompt | taken |
owner, other | refused: owner prefix not allowed on mqueue rules |
Permissions
| Word | Short form | Stands for |
|---|---|---|
create | - | making a new queue |
open | - | opening an existing queue |
delete | - | removing a queue |
getattr | - | reading the queue's attributes |
setattr | - | changing the queue's attributes |
read | r | receiving messages |
write | w | sending messages |
rw and wr are read and write. No permission word means all seven.
Name and type
| Name | Type | Notes |
|---|---|---|
starts with / | POSIX | a pattern in the globbing syntax |
| digits only | System V | the key |
| anything else | error | mqueue invalid name 'NAME', message queue names must begin with a / or be a positive integer. |
| none | type= decides; without it, both | - |
type= must agree with the name: mqueue type=sysv /demo-queue, gives mqueue type=sysv invalid name '/demo-queue', sysv message queues must be identified by a positive integer. and mqueue type=posix 1234, gives mqueue type=posix invalid name '1234', posix message queues names must begin with a /.
Feature set and result
| Feature set | POSIX rule | System V rule | Warning with --warn=rule-not-enforced | Enforced by the mainline kernel |
|---|---|---|---|---|
| abi/3.0 | dropped | dropped | mqueue type=posix rules not enforced; mqueue rules not enforced for an untyped rule | no |
| abi/4.0 | compiled | dropped | mqueue type=sysv rules not enforced | no |
| abi/5.0 | compiled | dropped | mqueue type=sysv rules not enforced | no |
the mainline kernel's own (no ipc) | dropped | dropped | as for abi/3.0 | no |
Which kernels do enforce the rules: Upstream and Ubuntu kernels.
Errors and warnings
| Line | Message |
|---|---|
mqueue create open /demo-queue, | syntax error, unexpected TOK_OPEN, expecting TOK_ID or TOK_CONDID or TOK_END_OF_RULE |
mqueue type=posix create /demo-queue, | syntax error, unexpected TOK_CREATE, expecting TOK_ID or TOK_CONDID or TOK_END_OF_RULE |
mqueue (create, open) demo-queue, | mqueue invalid name 'demo-queue', message queue names must begin with a / or be a positive integer. |
mqueue type=sysv /demo-queue, | mqueue type=sysv invalid name '/demo-queue', sysv message queues must be identified by a positive integer. |
mqueue type=posix 1234, | mqueue type=posix invalid name '1234', posix message queues names must begin with a / |
mqueue (create, bogus) /demo-queue, | Internal: unexpected mqueue perms character 'b' in input |
owner mqueue /demo-queue, | owner prefix not allowed on mqueue rules |
a type other than posix or sysv | mqueue invalid type='TYPE' |
| an unknown conditional | invalid mqueue rule conditional "NAME" |
an operator other than = | only "=" allowed in conditions of mqueue rules |
| feature set without message queues (warnings, off by default) | mqueue rules not enforced, mqueue type=posix rules not enforced, mqueue type=sysv rules not enforced |
for AppArmor developers for security researchers
In the parser and the kernel
- Classes: 17 (
\x11) for POSIX, 18 (\x12) for System V. A POSIX rule is the class byte, the name, a zero byte and the label; a System V rule is the class byte, the key as four bytes, a zero byte and the label (mqueue.cc:202-345). - Up to three policydb rules per type: the permissions on the name alone when no label is given;
create(with anyreadandwrite) on the name alone; the permissions on the name and the label. - Permission bits in the parser: write
0x2, read0x4, create0x10, delete0x20, open0x40, setattr0x100, getattr0x200(mqueue.h:31-40). - Permission remap: setattr and getattr move to
0x4000and0x8000(mqueue.h:91-95). - Class markers
\d17and\d18at priority 1001 (parser_regex.c:1444-1449). - The mainline kernel defines class 17 and its log name
posix_mqueue(apparmor.h:33, audit.c:58). It has no message-queue hook (lsm.c:1785-1890) and noipcfeature directory (apparmorfs.c:2684-2701).
for security researchers
Known discrepancies
- A variable cannot begin the queue name:
mqueue @{profile_name},fails with the invalid-name message (tree). - The System V key is converted without a range check:
mqueue r 99999999999,is accepted (tree; latent). - An untyped rule such as
mqueue,under abi/5.0 emits class-18 rules although only POSIX queues are in the feature set (tree). - The kernel's table of class names has
io_uringat index 18 (code). type=(posix sysv)andlabel=(a b)are accepted and only the first value is used (tree).- The verdicts are in known bugs.
- Known discrepancy: a POSIX message queue on a kernel without mqueue mediation is refused as a file with a disconnected path. Observed on Debian 13 and Debian sid, with and without an
mqueuerule:mq_openfails with error 13 and the log hasapparmor="DENIED" operation="open" class="file" info="Failed name lookup - disconnected path". See Kernel features and the policy ABI. - Not determined: how an enforcing kernel queries name and label.
Roads from here
START HERE
- Upstream and Ubuntu kernels — which kernels enforce mqueue rules
- The profile language at a glance — every kind of line on one page
PREAMBLE AND REUSE
- The abi rule and the abi files — the feature set that lists or lacks message queues
QUALIFIERS
- allow and deny — what a deny means when nothing is enforced
RULE CLASSES
- Rule conditionals — type= and label= and where conditionals go
- Unix socket rules — the socket rule for local messaging that mainline enforces
- Signal rules — another rule between two processes
- The all rule — the rule that includes mqueue
SECURITY · threat model and limits
- What is not mediated — what no rule class covers on a given kernel
RUNNING THE PARSER
- Kernel features and the policy ABI — reading the feature set of the running kernel