rlimit rules

resource limits for application developers for administrators for security researchers

set rlimit puts a ceiling on one resource limit of the tasks a profile confines. The kernel lowers the hard limit to the ceiling when a program is executed into the profile and refuses later setrlimit calls above it. The rule takes no qualifiers. Sizes and times take units.

On the map: rule classes

▶ Watch this deck — 8 scenes, ~5 min

Worth seeing first

01 A ceiling demo cannot lift

RLIMIT RULES demo open files hard limit 1048576 setrlimit(RLIMIT_NOFILE, 1048576) a leak, or an attacker, can use them all set rlimit nofile <= 1024, line 24 1024 the ceiling from the profile setrlimit(RLIMIT_NOFILE, 4096) = -1 EACCES
demo and a gauge of open files with a hard limit of 1048576. demo can raise its limit to the top. The rule set rlimit nofile <= 1024 puts a ceiling low on the gauge, and a setrlimit call for 4096 fails with EACCES.

set rlimit RESOURCE <= VALUE, caps a resource limit for tasks confined by the profile. Raising the hard limit above it fails with EACCES.

Every process has resource limits: how many files it may open, how much memory, how much CPU time. A process can raise its own soft limits up to the hard limit, and root can raise the hard limit too. If demo is compromised, that's one more thing it could do. A set rlimit rule puts a ceiling in the profile. When demo starts under the profile, its hard limit comes down to that ceiling, and attempts to go higher are refused. Line twenty-four of demo's profile keeps it at a thousand and twenty-four open files.

Details and sources

Details

  • The rule stores a ceiling for the hard limit (rlim_max) of one resource. A profile without a rule for a resource leaves that resource alone.
  • The ceiling is applied when a program is executed into the profile (resource.c:195-198) and compared on later setrlimit calls (resource.c:88-100).
  • Feature file: /sys/kernel/security/apparmor/features/rlimit/mask lists the kernel's resource names (resource.c:24-27). The parser does not read it.
  • set rlimit nofile <= 1024, is line 24 of demo's profile. The program demo and its profile are invented for the map; the starting hard limit 1048576 is an illustration.
  • Kernel statements are read from the kernel source and were not run.

02 set, rlimit, a resource, a value

GRAMMAR setrlimitRESOURCE<=VALUEUNIT, set rlimit nofile <= 1024,set rlimit data <= 100M,set rlimit cpu <= 10 minutes,set rlimit nproc <= infinity, a lower-case name value and unit, with or without a blank no ceiling for this resource QUALIFIERS audit deny owner priority= none of the four seats exists here: it is a setting, not a permission
The syntax diagram: set, rlimit, a resource, <=, a value, an optional unit, a comma. Four examples: nofile <= 1024, data <= 100M, cpu <= 10 minutes and nproc <= infinity. A panel shows that no qualifier may be written before the rule.

set rlimit RESOURCE <= VALUE [UNIT],. Both keywords are required; <= is the only operator; no qualifiers.

The rule reads like a sentence: set rlimit, the resource, less than or equal, a value. Set and rlimit are both required. The resource is a lower-case name, like nofile or data. The value is a number, with a unit for sizes and times, or the word infinity. The only operator is less than or equal. And unlike nearly every other rule, this one has no qualifiers. You can't audit it, deny it, or give it a priority. It's a setting, not a permission.

Details and sources

Details

  • 100M and 100 M give identical output; blanks around <= are optional (set rlimit nproc<=10,).
  • infinity stores "no limit"; a unit after it is ignored (set rlimit cpu <= infinity weeks, is accepted).
  • The same resource twice in one block: the last rule wins, with no message.
  • A nested block that sets a resource the enclosing block also sets: Cannot merge rlimits, more than one value specified for a single rlimit (block_rules.cc:188).
  • None of the four qualifier seats exists here. A qualifier on a block that holds a set rlimit gives prefix not allowed on rlimit rules.
  • Known discrepancy: apparmor.d(5) writes the resource and value as optional (apparmor.d.pod:379); set rlimit, is a syntax error.
  • Examples: rlimits/ok_rlimit_01.sd, apparmor.d.pod:2302-2304. The results above come from running the tree's parser.

for AppArmor developers

In the code

03 Numbers, sizes and times

VALUES you write the parser stores counts: nofile nproc locks sigpending rtprio set rlimit nofile <= 1024, 1024 no unit allowed sizes: fsize data stack core rss as memlock msgqueue set rlimit data <= 100M, 104857600 bytes K KB M MB G GB powers of 1024, capitals only times: cpu rttime set rlimit cpu <= 10 minutes, 600 seconds set rlimit rttime <= 10ms, 10000 microseconds us ms s min h d week cpu <= 60 no unit: seconds rttime <= 60 no unit: microseconds set rlimit nice <= N, -20 .. 19, no unit; see the details for how it is stored
What you write and what the parser stores. Counts such as nofile <= 1024 are stored as the number. Sizes such as data <= 100M are stored in bytes, 104857600. Times: cpu <= 10 minutes is 600 seconds, rttime <= 10ms is 10000 microseconds; without a unit cpu means seconds and rttime microseconds. nice takes -20 to 19.

Counts take a number. Sizes take K, M, G (or KB, MB, GB). cpu and rttime take time units from us to weeks; without a unit cpu is seconds and rttime microseconds.

There are seventeen resource names, and they fall into three groups. Counts, like open files and processes, take a plain number and no unit. Sizes, like data and stack, take bytes, or a number with K, M or G, in capitals, and those are powers of ten twenty-four. Times are CPU time and real-time time. CPU is stored in seconds, rttime in microseconds, and each has a different default when you leave the unit out. So write the unit. Nice is the odd one, a number from minus twenty to nineteen. The full table is under the deck.

Details and sources

Details

  • Counts: nofile (alias ofile), nproc, locks, sigpending, rtprio: a number, no unit (block_rules.cc:82-92).
  • Sizes: fsize, data, stack, core, rss, as, memlock, msgqueue: bytes, or K, KB, M, MB, G, GB, powers of 1024 (block_rules.cc:102-124). rss is accepted although Linux does not enforce it.
  • Times: cpu is stored in seconds, and a result below one second is an error (block_rules.cc:57-68); rttime is stored in microseconds (block_rules.cc:70-80).
  • Time units: us, microsecond(s), ms, millisecond(s), s, sec, second(s), min, minute(s), h, hour(s), d, day(s), week(s) (parser_misc.c:1306-1346). Division is integer division: cpu <= 1500ms equals cpu <= 1.
  • Run with the tree's parser: data <= 100M, 100MB, 100 M and 104857600 give identical output; so do cpu <= 60, 1min and 1 minute; rttime <= 10 equals 10us. The stored values 600 for cpu <= 10 minutes and 10000 for rttime <= 10ms follow from the unit table; they were not run.
  • Numbers are read with base detection, so a leading 0 is octal: nofile <= 010 is 8. Hexadecimal cannot be written.
  • The warnings for a missing unit (RLIMIT 'cpu' no units specified using default units of seconds, RLIMIT 'rttime' no units specified using default units of microseconds) are off by default; --warn=missing turns them on.
  • apparmor.d(5) lists only K, M and G (apparmor.d.pod:385).

for security researchers

Attack surface

  • Known discrepancy: set rlimit nice <= N stores N + 20 (5 gives 25, -20 gives 0, 19 gives 39, run with the tree's parser; block_rules.cc:94-100), while the kernel's scale for RLIMIT_NICE is 20 - nice (prio.h:33-36). The manual page says only "a number between -20 and 19" (apparmor.d.pod:394). It is confirmed (see Known bugs): do not rely on what a given nice ceiling permits.
  • Known discrepancy: a multiplied size is not checked for overflow: set rlimit data <= 9999999999G, exits 0 and stores an unrelated number.
  • A leading 0 makes a value octal, so set rlimit nofile <= 010, is a ceiling of 8 and a reviewer who reads it as ten sees a looser limit than the kernel gets. The parser prints nothing.
  • All three are on the bug list; see Known bugs.

04 Applied when a program is executed into the profile

AT EXEC systemd unconfined nofile soft 1024 / hard 524288 exec /usr/bin/demo demo profile demo set rlimit nofile <= 1024, nofile soft 1024 / hard 1024 hard = min(hard, ceiling) soft is pulled down with it a task arriving with hard 512 nofile soft 512 / hard 512 the rule only lowers change_profile without exec entering a hat reloading the profile limits of running tasks are not touched two stacked profiles with a rule for the same resource: the smaller ceiling
systemd, unconfined, with a hard limit of 524288 open files executes /usr/bin/demo. The profile demo has set rlimit nofile <= 1024, so demo starts with soft 1024 and hard 1024. A task arriving with hard 512 keeps 512. A profile change without exec, entering a hat and reloading the profile do not touch the limits of running tasks.

Limits are applied when an exec changes the task's confinement: hard = min(hard, ceiling). Profile changes without exec, hats and profile reloads do not apply them. This is what the kernel code does; it was not run.

When does the ceiling land? At exec. When a program is executed and that exec changes its confinement, the kernel walks the new profile's limits and lowers each hard limit to the ceiling, and the soft limit with it. It only lowers: a task that arrives with a smaller limit keeps it. And exec is the only moment. Changing profile without an exec, entering a hat, or reloading the profile with a new number does nothing to tasks that are already running. Restart the program to pick up a changed limit.

Details and sources

Details

  • The LSM hook is bprm_committing_creds. apparmor_bprm_committing_creds returns early when the label does not change or the new label is unconfined (lsm.c:957-973).
  • __aa_transition_rlimits (resource.c:153-206) does two things. For every resource the old profiles controlled, the soft limit is reset to the smaller of the hard limit and the soft limit of init (resource.c:166-181). Then, for every resource in the new profiles, the hard limit becomes the smaller of itself and the ceiling, the soft limit is pulled down to the hard limit, and the CPU timer is updated for the CPU time limit (resource.c:184-205).
  • With a stack of profiles the smallest ceiling wins. Nothing is logged.
  • The function has no other caller, so a profile change without exec, a hat change and profile replacement do not apply limits. Entering an unconfined label undoes nothing.
  • The numbers on the slide are illustrations.
  • Kernel statements are read from the kernel source and were not run.

05 Later setrlimit calls

AFTER EXEC demo setrlimit(RLIMIT_NOFILE, hard 512) 512 <= 1024: allowed setrlimit(RLIMIT_NOFILE, hard 4096) 4096 > 1024: EACCES setrlimit(RLIMIT_STACK, ...) no rule for stack in the profile: not AppArmor's business prlimit(pid of sshd, ...) another label: EACCES unless the profile has capability sys_resource apparmor="DENIED" operation="setrlimit" ... profile="demo" ...rlimit=nofile value=4096 built from the kernel's format strings; the values are illustrative
demo calls setrlimit and prlimit; the kernel answers. Hard 512 is at most 1024: allowed. Hard 4096 is above 1024: EACCES. A resource without a rule is not checked by AppArmor. prlimit on a task with another label is refused unless the profile has capability sys_resource. The denial record has operation setrlimit, rlimit=nofile and value=4096.

setrlimit with a hard limit above the ceiling fails with EACCES and is logged with operation="setrlimit" rlimit=NAME value=N. Changing another label's task needs capability sys_resource,.

After exec the ceiling keeps working. When demo calls setrlimit, the kernel compares the new hard limit with the profile's ceiling. At or below it is fine. Above it fails with EACCES. Resources the profile doesn't mention aren't checked by AppArmor at all. There's a second check, for prlimit on someone else. To change the limits of a task with a different label, the profile needs the capability sys_resource. A refusal is logged as operation="setrlimit", with the resource name and the value that was asked for.

Details and sources

Details

  • The LSM hook is task_setrlimit; AppArmor's handler (lsm.c:1006-1021, registered at lsm.c:1871) is skipped for an unconfined caller.
  • Ceiling case: EACCES when the resource is one the profile has a rule for and the new hard limit is larger than the ceiling (resource.c:88-100). Only the hard limit is compared.
  • Other-task case: when the target task has a different label and the profile lacks capability sys_resource, the call fails with EACCES, and the record has info="cap_sys_resource" and a peer= field (resource.c:133-138). That capability test itself is not logged. The whole function: resource.c:114-146.
  • Allowed requests pass through the same audit call, so they are logged only when the profile's audit mode asks for it.
  • The record on the slide is built from the kernel's format string rlimit=%s value=%lu (resource.c:30-42) and the operation name setrlimit (audit.h:101). The tree has no sample record with rlimit=; the values are illustrative.
  • Kernel statements are read from the kernel source and were not run.

06 A mask and an array for AppArmor developers for security researchers

IN THE PARSER set rlimit nofile <= 1024, block_rules::add_rlimit rlimit_table: "nofile" -> RLIMIT_NOFILE (7) value: strtoll, units rlimits.specified |= 1 << 7 rlimits.limits[7] = 1024 serialised profile struct "rlimits" { u32 specified = 0x80 array[RLIM_NLIMITS] of u64 } written only if a limit is set unpack_rlimits() rlimits.mask = specified limits[aa_map_resource(i)].rlim_max = value no feature test guards the fields; the kernel's rlimit/mask is not read by the parser
The rule set rlimit nofile <= 1024 goes through block_rules::add_rlimit, which looks up the name, converts the value, sets bit 7 of a mask and slot 7 of an array. The serialised profile has a struct rlimits with the mask and the array. In the kernel unpack_rlimits stores the mask and each value as a hard-limit ceiling.

block_rules.cc:38-131 fills aa_rlimits; parser_interface.c:398-411 writes it; policy_unpack.c:667-700 stores the ceilings.

Like capabilities, rlimits skip the policydb. add_rlimit looks the name up in a table, converts the value, sets one bit in a mask and one slot in an array. The serialiser writes a struct with the mask and the whole array, and only if some limit was set. In the kernel, unpack_rlimits copies the mask and stores each value as the hard-limit ceiling for its resource, mapping the index through a table generated from the generic resource numbers. The two kernel functions you met earlier then read mask and array together.

Details and sources

for AppArmor developers for security researchers

Details

for AppArmor developers for security researchers

Read more

07 Mistakes and their messages

MISTAKES rlimit nofile <= 1024, syntax error, unexpected TOK_RLIMIT, expecting TOK_ID or TOK_MODE or TOK_SET_VAR set is part of the rule set rlimit nofile <= 1k, RLIMIT 'nofile' invalid value 1 k counts take no unit; size units are capitals set rlimit cpu <= 500ms, RLIMIT 'cpu 500' < minimum value of 1s at least one second audit set rlimit nofile <= 1024, syntax error, unexpected TOK_SET, expecting TOK_ID or TOK_MODE or TOK_SET_VAR no qualifiers set rlimit NOFILE <= 1024, INVALID RLIMIT 'NOFILE' names are lower case set rlimit nofile <= 010, accepted: the ceiling is 8 a leading zero means octal
Five refused rlimit rules with the message of the parser: set left out; a lower-case k on a count; cpu below one second; audit before set; the resource in capitals. A sixth rule is accepted: nofile <= 010 is read as octal and means 8.

set is required, names are lower case, size units are capitals, counts take no unit, and a leading 0 makes a number octal.

Forget the word set, and the parser stops at rlimit. Give a count a unit, or write a size unit in lower case, and you get invalid value, with the number and the unit pulled apart. CPU time can't be less than a second. Qualifiers aren't allowed, so audit set rlimit is a syntax error. Names are lower case. And one that isn't an error at all: a number with a leading zero is read as octal, so 010 is eight.

Details and sources

Details

  • The parser prints an error as AppArmor parser error for FILE in profile FILE at line N: MESSAGE; the slide shows MESSAGE, produced by the tree's parser. The message of the fourth row continues with the tokens the parser expected.
  • set rlimit nproc < 10, gives Lexer found unexpected character: '<' (0x3c) in state: RLIMIT_MODE.
  • set rlimit nofile 1024, gives syntax error, unexpected TOK_VALUE, expecting TOK_LE.
  • set rlimit data <= 100m, gives RLIMIT 'data' invalid value 100 m. set rlimit nproc <= -1, is refused.
  • set rlimit nice <= 20, gives RLIMIT 'nice' out of range (-20 .. 19) 20.
  • set rlimit rttime <= 60m, fails (rlimits/bad_rlimit_01.sd).
  • A qualifier on a block that contains a set rlimit gives prefix not allowed on rlimit rules (rlimits/bad_rlimit_block_audit.sd, and the _deny and _owner files next to it).

08 A setting among permissions

ROADS set rlimit nofile <= 1024, a ceiling, applied at exec syn-exec-modes the exec that applies it k-exec what else exec does syn-change-profile a change that skips it syn-hats so does a hat syn-capability sys_resource stacking the smaller ceiling adm-denials the setrlimit record
rlimit rules in the centre with roads to syn-exec-modes and k-exec, to syn-change-profile and syn-hats, and to syn-capability, stacking and adm-denials.

A ceiling applied at exec. See the exec decks for when that happens.

Set rlimit is the one rule in a profile that's a setting. It lands at exec, so the exec decks are the natural next step. Change profile and hats are the ways into a profile that skip it. The capability deck covers sys_resource, the one privilege that touches limits. And with stacked profiles, it's the smaller ceiling that counts.

Details and sources

Details

  • set rlimit RESOURCE <= VALUE [UNIT], stores a ceiling for the hard limit of one resource. No qualifiers.
  • Applied when an exec changes the task's confinement; compared on later setrlimit calls; refusals return EACCES.
  • 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.

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), with nothing loaded into a kernel; "code" means the parser source or the kernel source was read, not run. Nothing here was observed on a running kernel.

Grammar

RLIMIT RULE:  set rlimit RESOURCE <= VALUE [UNIT] ,
VALUE:        a number, or infinity
  • set and rlimit are both required; <= is the only operator (tree).
  • Blanks around <= and between value and unit are optional (tree).
  • infinity means no ceiling; a unit after it is ignored (tree).
  • No qualifier may be written before the rule, and a block with a qualifier cannot contain one: prefix not allowed on rlimit rules (tree).
  • The same resource twice in one block: the last rule wins without a message (tree). A nested block that sets a resource of its enclosing block: Cannot merge rlimits, more than one value specified for a single rlimit (code).

Resources

ResourceKernel constantValueUnitsWithout a unitStored as
cpuRLIMIT_CPUnumbertime unitssecondsseconds; below 1 s is an error
rttimeRLIMIT_RTTIMEnumbertime unitsmicrosecondsmicroseconds
nofile, ofileRLIMIT_NOFILEnumbernone-the number
nprocRLIMIT_NPROCnumbernone-the number
locksRLIMIT_LOCKSnumbernone-the number
sigpendingRLIMIT_SIGPENDINGnumbernone-the number
rtprioRLIMIT_RTPRIOnumbernone-the number
niceRLIMIT_NICE-20 .. 19none-the number plus 20 (see Known discrepancies in the slide details of "Numbers, sizes and times")
fsizeRLIMIT_FSIZEnumbersize unitsbytesbytes
dataRLIMIT_DATAnumbersize unitsbytesbytes
stackRLIMIT_STACKnumbersize unitsbytesbytes
coreRLIMIT_COREnumbersize unitsbytesbytes
rssRLIMIT_RSSnumbersize unitsbytesbytes (Linux does not enforce this limit)
asRLIMIT_ASnumbersize unitsbytesbytes
memlockRLIMIT_MEMLOCKnumbersize unitsbytesbytes
msgqueueRLIMIT_MSGQUEUEnumbersize unitsbytesbytes

Any resource takes infinity. ofile is an alias of nofile, which makes seventeen names.

Units

Size unitMultiplier
K, KB1024
M, MB1024 x 1024
G, GB1024 x 1024 x 1024
Time unitLength
us, microsecond, microsecondsone microsecond
ms, millisecond, milliseconds1000 microseconds
s, sec, second, secondsone second
min, minute, minutes60 seconds
h, hour, hours3600 seconds
d, day, days86400 seconds
week, weeks604800 seconds

Size units are capitals only. The manual page lists K, M and G.

Number formats

  • Decimal. A leading 0 makes the number octal: nofile <= 010 is 8 (tree). Hexadecimal cannot be written.
  • Time conversion divides as integers: cpu <= 1500ms equals cpu <= 1 (tree). cpu below one second is an error.
  • A multiplied size is not checked for overflow (tree; a known discrepancy).

What the kernel does

All statements in this section are from the kernel source (linux:security/apparmor/resource.c, linux:security/apparmor/lsm.c:957-1021), not from a run.

  • At an exec that changes the task's label to a confined one: for every resource with a rule, the hard limit becomes the smaller of itself and the ceiling, and the soft limit is pulled down to it. Nothing is logged.
  • On setrlimit and prlimit by a confined task: a new hard limit above the ceiling is refused with EACCES; resources without a rule are not checked; changing a task with a different label needs capability sys_resource,.
  • Not applied by: a profile change without exec, a hat change, replacing the profile. Entering an unconfined label undoes nothing.
  • With a stack of profiles, the smallest ceiling for a resource wins.

The log record

Read from the kernel's format strings; the tree has no sample record.

FieldMeaning
operation="setrlimit"the operation
rlimit=NAMEthe resource, by the kernel's name
value=Nthe hard limit that was asked for
peer=LABELthe label of the other task, when another task's limits were to be changed
info="cap_sys_resource"the refusal was for lack of capability sys_resource

Errors and warnings

RuleMessage
rlimit nofile <= 1024,syntax error, unexpected TOK_RLIMIT, expecting TOK_ID or TOK_MODE or TOK_SET_VAR
set rlimit,syntax error, unexpected TOK_END_OF_RULE, expecting TOK_ID
set rlimit nofile 1024,syntax error, unexpected TOK_VALUE, expecting TOK_LE
set rlimit nproc < 10,Lexer found unexpected character: '<' (0x3c) in state: RLIMIT_MODE
set rlimit NOFILE <= 1024,INVALID RLIMIT 'NOFILE'
set rlimit nofile <= 1k,RLIMIT 'nofile' invalid value 1 k
set rlimit data <= 100m,RLIMIT 'data' invalid value 100 m
set rlimit cpu <= 500ms,RLIMIT 'cpu 500' < minimum value of 1s
set rlimit nice <= 20,RLIMIT 'nice' out of range (-20 .. 19) 20
audit set rlimit nofile <= 1024,syntax error, unexpected TOK_SET, expecting TOK_ID or TOK_MODE or TOK_SET_VAR
audit { set rlimit nofile <= 1024, }prefix not allowed on rlimit rules
set rlimit cpu <= 60, with --warn=missingwarning RLIMIT 'cpu' no units specified using default units of seconds
set rlimit rttime <= 60, with --warn=missingwarning RLIMIT 'rttime' no units specified using default units of microseconds

All rows are from the tree's parser. The two warnings are off by default.

for AppArmor developers

Encoding and unpacking

  • rlimits are not in the policydb. block_rules::add_rlimit (parser/block_rules.cc:38-131) sets rlimits.specified |= 1 << index and rlimits.limits[index] in struct aa_rlimits (parser/parser.h:166-181).
  • sd_serialize_rlimits (parser/parser_interface.c:398-411) writes a struct rlimits with the u32 mask and an array of RLIM_NLIMITS u64 values, only when a limit is set.
  • The kernel's unpack_rlimits (linux:security/apparmor/policy_unpack.c:667-700) copies the mask and stores each value as rlim_max at aa_map_resource(i) (linux:security/apparmor/resource.c:83-86).
  • The grammar rule has no opt_prefix (parser/parser_yacc.y:852-859); the scanner state is RLIMIT_MODE (parser/parser_lex.l:611-625).

for security researchers

Known discrepancies

None of these is taught as intended behaviour; the verdicts come from the review.

  • set rlimit nice <= N stores N + 20; the kernel's scale for RLIMIT_NICE is 20 - nice (tree for the stored value, code for the kernel's scale).
  • Size values are not checked for overflow (set rlimit data <= 9999999999G, exits 0), and a leading zero means octal (tree).
  • On architectures with their own rlimit numbering the kernel remaps the array index but not the mask bit (linux:security/apparmor/policy_unpack.c:678-688), so rss, nproc, nofile, memlock and as would be tested against the wrong slot (code, not run).

Roads from here

START HERE

ATTACHMENT, NAMESPACES, STACKING

FILE ACCESS

  • Exec modes — the exec that enters a profile and applies its limits

QUALIFIERS

RULE CLASSES

PROFILE STRUCTURE

  • Hats — neither does entering a hat

RUNNING THE PARSER

IN THE KERNEL