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
- Capability rules — the sys_resource capability that raises limits
01 A ceiling demo cannot lift
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
setrlimitcalls (resource.c:88-100). - Feature file:
/sys/kernel/security/apparmor/features/rlimit/masklists 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 programdemoand 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
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
100Mand100 Mgive identical output; blanks around<=are optional (set rlimit nproc<=10,).infinitystores "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 rlimitgivesprefix 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.
Read more
for AppArmor developers
In the code
rules: rules TOK_SET TOK_RLIMIT TOK_ID TOK_LE TOK_VALUE opt_id TOK_END_OF_RULE, with noopt_prefix(parser_yacc.y:852-859).- The scanner state
RLIMIT_MODE: parser_lex.l:611-625, entered at parser_lex.l:867-869.
03 Numbers, sizes and times
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(aliasofile),nproc,locks,sigpending,rtprio: a number, no unit (block_rules.cc:82-92). - Sizes:
fsize,data,stack,core,rss,as,memlock,msgqueue: bytes, orK,KB,M,MB,G,GB, powers of 1024 (block_rules.cc:102-124).rssis accepted although Linux does not enforce it. - Times:
cpuis stored in seconds, and a result below one second is an error (block_rules.cc:57-68);rttimeis 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 <= 1500msequalscpu <= 1. - Run with the tree's parser:
data <= 100M,100MB,100 Mand104857600give identical output; so docpu <= 60,1minand1 minute;rttime <= 10equals10us. The stored values 600 forcpu <= 10 minutesand 10000 forrttime <= 10msfollow from the unit table; they were not run. - Numbers are read with base detection, so a leading
0is octal:nofile <= 010is 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=missingturns them on. - apparmor.d(5) lists only
K,MandG(apparmor.d.pod:385).
for security researchers
Attack surface
- Known discrepancy:
set rlimit nice <= NstoresN + 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 forRLIMIT_NICEis20 - 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 givenniceceiling permits. - Known discrepancy: a multiplied size is not checked for overflow:
set rlimit data <= 9999999999G,exits 0 and stores an unrelated number. - A leading
0makes a value octal, soset 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
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_credsreturns 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
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:
EACCESwhen 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 withEACCES, and the record hasinfo="cap_sys_resource"and apeer=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 namesetrlimit(audit.h:101). The tree has no sample record withrlimit=; 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
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
struct aa_rlimits(parser.h:166-181) also carries the errorprefix not allowed on rlimit rules(parser.h:177). It is filled at block_rules.cc:38-131 and merged into the profile at block.cc:132-135.sd_serialize_rlimits: parser_interface.c:398-411. The kernel side: policy_unpack.c:667-700.rlim_map[]andaa_map_resource: resource.c:83-86; the table is generated at build time (Makefile:89-99).- The size-unit test is "the unit is a prefix of
KB,MBorGB" (block_rules.cc:114-119); times go throughconvert_time_units(parser_misc.c:1331-1346). nofileis index 7, sospecifiedis 0x80. Observed with the tree's parser:niceis index 13,specified=0x2000.- Kernel statements are read from the kernel source and were not run.
for AppArmor developers for security researchers
Read more
07 Mistakes and their messages
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,givesLexer found unexpected character: '<' (0x3c) in state: RLIMIT_MODE.set rlimit nofile 1024,givessyntax error, unexpected TOK_VALUE, expecting TOK_LE.set rlimit data <= 100m,givesRLIMIT 'data' invalid value 100 m.set rlimit nproc <= -1,is refused.set rlimit nice <= 20,givesRLIMIT '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 rlimitgivesprefix not allowed on rlimit rules(rlimits/bad_rlimit_block_audit.sd, and the_denyand_ownerfiles next to it).
Read more
08 A setting among permissions
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
setrlimitcalls; refusals returnEACCES. - 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
setandrlimitare both required;<=is the only operator (tree).- Blanks around
<=and between value and unit are optional (tree). infinitymeans 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
| Resource | Kernel constant | Value | Units | Without a unit | Stored as |
|---|---|---|---|---|---|
cpu | RLIMIT_CPU | number | time units | seconds | seconds; below 1 s is an error |
rttime | RLIMIT_RTTIME | number | time units | microseconds | microseconds |
nofile, ofile | RLIMIT_NOFILE | number | none | - | the number |
nproc | RLIMIT_NPROC | number | none | - | the number |
locks | RLIMIT_LOCKS | number | none | - | the number |
sigpending | RLIMIT_SIGPENDING | number | none | - | the number |
rtprio | RLIMIT_RTPRIO | number | none | - | the number |
nice | RLIMIT_NICE | -20 .. 19 | none | - | the number plus 20 (see Known discrepancies in the slide details of "Numbers, sizes and times") |
fsize | RLIMIT_FSIZE | number | size units | bytes | bytes |
data | RLIMIT_DATA | number | size units | bytes | bytes |
stack | RLIMIT_STACK | number | size units | bytes | bytes |
core | RLIMIT_CORE | number | size units | bytes | bytes |
rss | RLIMIT_RSS | number | size units | bytes | bytes (Linux does not enforce this limit) |
as | RLIMIT_AS | number | size units | bytes | bytes |
memlock | RLIMIT_MEMLOCK | number | size units | bytes | bytes |
msgqueue | RLIMIT_MSGQUEUE | number | size units | bytes | bytes |
Any resource takes infinity. ofile is an alias of nofile, which makes seventeen names.
Units
| Size unit | Multiplier |
|---|---|
K, KB | 1024 |
M, MB | 1024 x 1024 |
G, GB | 1024 x 1024 x 1024 |
| Time unit | Length |
|---|---|
us, microsecond, microseconds | one microsecond |
ms, millisecond, milliseconds | 1000 microseconds |
s, sec, second, seconds | one second |
min, minute, minutes | 60 seconds |
h, hour, hours | 3600 seconds |
d, day, days | 86400 seconds |
week, weeks | 604800 seconds |
Size units are capitals only. The manual page lists K, M and G.
Number formats
- Decimal. A leading
0makes the number octal:nofile <= 010is 8 (tree). Hexadecimal cannot be written. - Time conversion divides as integers:
cpu <= 1500msequalscpu <= 1(tree).cpubelow 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
setrlimitandprlimitby a confined task: a new hard limit above the ceiling is refused withEACCES; resources without a rule are not checked; changing a task with a different label needscapability 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.
| Field | Meaning |
|---|---|
operation="setrlimit" | the operation |
rlimit=NAME | the resource, by the kernel's name |
value=N | the hard limit that was asked for |
peer=LABEL | the 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
| Rule | Message |
|---|---|
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=missing | warning RLIMIT 'cpu' no units specified using default units of seconds |
set rlimit rttime <= 60, with --warn=missing | warning 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) setsrlimits.specified |= 1 << indexandrlimits.limits[index]instruct aa_rlimits(parser/parser.h:166-181). sd_serialize_rlimits(parser/parser_interface.c:398-411) writes a structrlimitswith the u32 mask and an array ofRLIM_NLIMITSu64 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 asrlim_maxataa_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 isRLIMIT_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 <= NstoresN + 20; the kernel's scale forRLIMIT_NICEis20 - 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), sorss,nproc,nofile,memlockandaswould be tested against the wrong slot (code, not run).
Roads from here
START HERE
- The profile language at a glance — every kind of line on one page
ATTACHMENT, NAMESPACES, STACKING
- Profile stacking — with several profiles the smallest ceiling wins
FILE ACCESS
- Exec modes — the exec that enters a profile and applies its limits
QUALIFIERS
- Qualifier blocks — why a block with a qualifier cannot hold a set rlimit
RULE CLASSES
- Capability rules — sys_resource for changing another task's limits
- change_profile rules — a profile change without exec does not apply limits
PROFILE STRUCTURE
- Hats — neither does entering a hat
RUNNING THE PARSER
- Reading a denial — reading the setrlimit record
IN THE KERNEL
- Exec and domain transitions — what else happens when an exec changes the label