2
0
mirror of https://gitlab.com/apparmor/apparmor synced 2025-08-22 01:57:43 +00:00
John Johansen 93c660e376 Merge parser: Add support for automatic @{attach_path} variable
Have the parser extract the attachment path from the profile declaration        
and make it available as a local variable within the profile. This allows             
profile rules to use the executable attachment path in rules.                   
                                                                                
eg.                                                                             
```                                                                             
  profile ex /bin/** {                                                          
     @{attach_path} r,                                                          
     # ...                                                                      
  }                                                                             
                                                                                
  profile /path/to/bin {                                                        
     @{attach_path} r,                                                          
     # ...                                                                      
}                                                                               
```                                                                             
                                                                                
if a profile does not define an attachment like                                 
                                                                                
```                                                                             
  profile noattach {                                                            
     @{attach_path} r,                                                          
  }                                                                             
```                                                                             
                                                                                
the apparmor_parser will fail the compile with the error.                       
                                                                                
```                                                                             
  Found reference to variable attach_path, but is never declared                
```                                                                             

While not recommended for rules directly in a profile the above
the undeclared variable error can be avoided in in abstractions
by wrapping the variable in a conditional.

```
if defined @{attach_path} {
   @{attach_path r,
}
```
                                                             
The attachment xattr/label conditionals are not made available at               
this time as regular file path rules can not use them.                          
                                                                                
Similarly a @{exec_path} variable is made available. It is different            
than @{attach_path} in that it is intended to be a kernel variable              
that represents the specific executable that was matched at run                 
time. However to support policy on kernels that don't define the                
kernel variable it has a fallback value that is the same as                     
@{attach_path}.                                                                 
                                                                                
This patch is a follow on to MR:1637 (https://gitlab.com/apparmor/apparmor/-/me\
rge_requests/1637)                                                              
and is similar to how the apparmor.d project uses the manually setup            
@{exec_path} variable.                                                          
                                                                                
We can bike shed over the variable name. @{attach_path} was chosen              
here because this is the attachment conditional path for the                    
executable, not the executable's actual path. While @{exec_path} is             
intended to be the applications actual executable path.                         
support the @{exec_path} kernel variable (all of them atm).                     
                                                                                
Notes:                                                                          
                                                                                
The minimize.sh tests are changed because this patch causes path based          
profile names to create an attachment. This could be done by doing the          
attach_variable expansion in the alternate location marked by the               
patch, but since the kernel is going to start doing this for all                
profiles that don't have an attachment it is better for the parser to           
do it, as it can optimize better.                                               
                                                                                
This patch series may cause breakage if policy declares either
@{attach_path} or @{exec_path} by shadowing those previously declared
variables in the profile block. The previously declared variable
is available in the attachment specification so uses like the
apparmor.d project won't break as it with transfer its variable
value to the attachment which will the transfer that value into
the automatic local var.
                                                                            
Signed-off-by: John Johansen <john.johansen@canonical.com>

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1643
Approved-by: Maxime Bélair <maxime.belair@canonical.com>
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2025-05-12 09:08:44 +00:00
..
2025-02-24 01:28:04 -08:00
2024-03-29 10:52:25 +01:00
2020-10-11 12:22:23 +02:00
2025-05-07 23:04:59 +00:00
2025-05-07 23:04:59 +00:00
2013-09-27 16:16:37 -07:00

The apparmor_parser allows you to add, replace, and remove AppArmor
policy through the use of command line options. The default is to add.
`apparmor_parser --help` shows what the command line options are.

You can also find more information at https://wiki.apparmor.net

-- The AppArmor development team