Commit graph

43 commits

Author SHA1 Message Date
John Johansen
e3fca60d11 parser: add the ability to specify a priority prefix to rules
This enables adding a priority to a rules in policy, finishing out the
priority work done to plumb priority support through the internals in
the previous patch.

Rules have a default priority of 0. The priority prefix can be added
before the other currently support rule prefixes, ie.

  [priority prefix][audit qualifier][rule mode][owner]

If present a numerical priority can be assigned to the rule, where the
greater the number the higher the priority. Eg.

    priority=1 audit file r /etc/passwd,

    priority=-1 deny file w /etc/**,

Rule priority allows the rule with the highest priority to completely
override lower priority rules where they overlap. Within a given
priority level rules will accumulate in standard apparmor fashion.

    Eg. given
        priority=1 w   /*c,
        priority=0 r   /a*,
        priority=-1 k  /*b*,

    /abc, /bc, /ac   .. will have permissions of w
    /ab, /abb, /aaa, .. will have permissions of r
    /b, /bcb, /bab,  .. will have permissions of k

User specified rule priorities are currently capped at the arbitrary
values of 1000, and -1000.

Notes:
* not all rule types support the priority prefix. Rukes like
  - network
  - capability
  - rlimits need to be reworked
  need to be reworked to properly preserve the policy rule structure.
* this patch does not support priority on rule blocks
* this patch does not support using a variable in the priority value.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2024-08-14 17:15:24 -07:00
John Johansen
2737cb2c2b parser: minimization - remove unnecessary second minimization pass
Moving apply_and_clear_deny() before the first minimization pass, which
was necessary to propperly support building accept information for
older none extended permission dfas, allows us to also get rid of doing a
second minimization pass if we want to force clearing explicit deny
info from extended permission tables.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2024-08-14 17:15:24 -07:00
John Johansen
5c2bd20720 parser: pass rule mode prompt through to backend
Signed-off-by: John Johansen <john.johansen@canonical.com>
2024-08-14 15:47:13 -07:00
John Johansen
2e18cb9aed parser: rename rules.h perms_t to perm32_t
There are two distinct declarations of perms_t.
  rule.h: typedef uint32_t perms_t
  hfa.h: class perms_t

these definitions clash when the front end and backend share more info.
To avoid this rename rule.h to perm32_t, and move the definition into
perms.h and use it in struct aa_perms.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2024-08-14 14:39:18 -07:00
Georgia Garcia
746f76d3e1 parser: add ability to specify permission in network rules
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-02-28 21:42:18 -03:00
Georgia Garcia
820f1fb5f2 parser: refactor network to use rule class as its base.
There is one significant difference in the encoding of the network
rules. Before this change, when the parser was encoding a "network,"
rule, it would generate an entry for every family and every
type/protocol. After this patch the parser should generate an entry
for every family, but the type/protocol is changed to .. in the pcre
syntax. There should be no difference in behavior.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2023-09-07 00:12:51 -07:00
Georgia Garcia
210ad63cbf parser: fix encoding of unix permissions for setopt and getopt
The permissions for AA_NET_OPT need to be bounded by mask so we can
make sure it matches when a policy specified only setopt or only
getopt. This was causing failures on the regression tests
unix_socket_pathname, unix_socket_abstract, unix_socket_unnamed and
unix_socket_autobind

Fixes: 44f3be091 ("parser: convert the stored audit from a bit mask to a bool")
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2023-08-01 14:25:41 -03:00
John Johansen
e84e481263 parser: cleanup and rework optimization and dump flag handling
In preparation for more flags (not all of the backend dfa based),
rework the optimization and dump flag handling which has been exclusively
around the dfa up to this point.

- split dfa control and dump flags into separate fields. This gives more
  room for new flags in the existing DFA set
- rename DFA_DUMP, and DFA_CONTROL to CONTROL_DFA and DUMP_DFA as
  this will provide more uniform naming for none dfa flags
- group dump and control flags into a structure so they can be passed
  together.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-07 17:47:41 -07:00
John Johansen
a2d56c3c74 parser: consolidate rule class handling into aa_class
Instead of having each rule individually handle the class info
introduce a class_rule_t into the hierarchy and consolidate.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-03-31 02:21:19 -07:00
John Johansen
355730d8c7 parser: convert deny flag from bool to rule_mode
We need to be able to support more rule types than allow and deny so
convert to an enum.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-03-31 02:21:11 -07:00
John Johansen
fdf5b062a9 parser: fixup audit struct to audit enum
This removes the struct wrapper used in the previous patch to ensure
that all uses are properly converted.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-03-31 02:12:35 -07:00
John Johansen
7a318d99f2 parser: convert audit from bool to enum
Audit control support is going to be extended to support allowing
policy to which rules should quiet auditing. Update the frontend
internals to prepare for this.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-03-31 02:08:20 -07:00
John Johansen
134e95f783 parser: fixup remove struct from the audit bool conversion
This removes the struct wrapper used in the previous patch to ensure
that all uses are properly converted.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-03-31 01:08:25 -07:00
John Johansen
44f3be091a parser: convert the stored audit from a bit mask to a bool
This delays the convertion of the audit flag until passing to the
backend. This is a step towards fix the parser front end so that it
doesn't use encoded permission mappings.

Note: the patch embedds the bool conversion into a struct to ensure
the compiler will fail to build unless every use is fixed. The
struct is removed in the following patch.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-03-31 01:05:29 -07:00
John Johansen
fd9a6fe133 parser: int mode to perms
Move from using and int for permissions bit mask to a perms_t type.
Also move any perms mask that uses the name mode to perms to avoid
confusing it with other uses of mode.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-03-29 10:45:44 -07:00
Robert Ancell
618a2260a0 Fix mode not being printed when debugging AF_UNIX socket rules.
This was due to the values being defined in both af_unix and af_rule leaving the latter values unset.
2023-02-02 11:10:04 +13:00
Robert Ancell
22b1267b6e Fix spacing when printing out AF_UNIX addresses 2023-02-01 12:38:09 +13:00
John Johansen
855dbd4ac8 parser: fix rule downgrade for unix rules
Rule downgrades are used to provide some confinement when a feature
is only partially supported by the kernel.

  Eg. On a kernel that doesn't support fine grained af_unix mediation
      but does support network mediation.

        unix (connect, receive, send)
              type=stream
              peer=(addr="@/tmp/.ICE-unix/[0-9]*"),

      will be downgraded to

        network unix type=stream,

Which while more permissive still provides some mediation while
allowing the appication to still function. However making the rule
a deny rule result in tightening the profile.

  Eg.
        deny unix (connect, receive, send)
              type=stream
              peer=(addr="@/tmp/.ICE-unix/[0-9]*"),

      will be downgraded to

        deny network unix type=stream,

and that deny rule will take priority over any allow rule. Which means
that if the profile also had unix allow rules they will get blocked by
the downgraded deny rule, because deny rules have a higher priority,
and the application will break. Even worse there is no way to add the
functionality back to the profile without deleting the offending deny
rule.

To fix this we drop deny rules that can't be downgraded in a way that
won't break the application.

Fixes: https://bugzilla.opensuse.org/show_bug.cgi?id=1180766
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/700
Signed-off-by: John Johansen <john.johansen@canonical.com>
2021-03-14 07:16:24 -07:00
John Johansen
d50262cf2c parser: fix warning for rule not enforced
If af_unix rules are not supported but network rules are and
--warn=rule-downgraded is not set then the parser will incorrectly
output warning when the rule is actually being downgraded.

  Warning from profile test-profile (./prof): extended network unix socket rules not enforced

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/699
Fixes: https://gitlab.com/apparmor/apparmor/-/issues/144
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Steve Beattie <steve.beattie@canonical.com>
2021-02-10 18:48:28 -08:00
Steve Beattie
461d9c2294
treewide: spelling/typo fixes in comments and docs
With the exception of the documentation fixes, these should all be
invisible to users.

Signed-off-by: Steve Beattie <steve.beattie@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/687
2020-12-01 12:47:11 -08:00
John Johansen
6af05006d9 parser: Fix expansion of variables in unix rules addr= conditional
The parser is not treating unix addr as a path and filtering slashes
after variable expansion. This can lead to errors where

@{foo}=/a/
unix bind addr=@{foo}/bar,

will always fail because addr is being matched as /a//bar instead of
/a/bar.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/607
Fixes: https://bugs.launchpad.net/apparmor/+bug/1856738
Signed-off-by: John Johansen <john.johansen@canonical.com>
2020-09-29 04:14:35 -07:00
John Johansen
0a52cf81e3 parser: add support for autobind sockets
af_unix allows for sockets to be bound to a name that is autogenerated.
Currently this type of binding is only supported by a very generic
rule.

  unix (bind) type=dgram,

but this allows both sockets with specified names and anonymous
sockets. Extend unix rule syntax to support specifying just an
auto bind socket by specifying addr=auto

eg.

  unix (bind) addr=auto,

It is important to note that addr=auto only works for the bind
permission as once the socket is bound to an autogenerated address,
the addr with have a valid unique value that can be matched against
with a regular

  addr=@name

expression

Fixes: https://bugs.launchpad.net/apparmor/+bug/1867216
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/521
Signed-off-by: John Johansen <john.johansen@canonical.com>
2020-09-29 03:34:56 -07:00
John Johansen
e92478a9c5 parser: add support for kernel 4.17 v8 networking
Make it so the parser can properly support network socket mediation
in the upstream kernel,

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/521
Signed-off-by: John Johansen <john.johansen@canonical.com>
2020-09-29 03:33:55 -07:00
Mike Salvatore
52d9529d1b parser: replace duplicate warn_once() with common function
The warn_once() function is duplicated in 6 different places. A common,
reusable version has been added to parser_common.c.

Signed-off-by: Mike Salvatore <mike.salvatore@canonical.com>
2020-08-09 17:56:31 -04:00
John Johansen
a29e232831 parser: feature abi: setup parser to intersect policy and kernel features
The features abi adds the ability to track the policy abi separate
from the kernel. This allow the compiler to determine whether policy
was developed with a certain feature in mind, eg. unix rules.

This allows the compiler to know whether it should tell the kernel to
enforce the feature if the kernel supports the rule but the policy
doesn't use it.

To find if a feature is supported we take the intersection of what is
supported by the policy and what is supported by the kernel.

Policy encoding features like whether to diff_encode policy are not
influenced by policy so these remain kernel only features.

In addition to adding the above intersection of policy rename
--compile-features to --policy-features as better represents what it
represents. --compile-features is left as a hidden item for backwards
compatibility.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/491
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Steve Beattie <sbeattie@ubuntu.com>
2020-05-29 00:23:17 -07:00
John Johansen
4eece9d5ee with unix rules we output a downgraded rule compatible with network rules
so that policy will work on kernels that support network socket controls
but not the extended af_unix rules

however this is currently broken if the socket type is left unspecified
(initialized to -1), resulting in denials for kernels that don't support
the extended af_unix rules.

Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: timeout
2017-09-07 02:26:15 -07:00
John Johansen
a0706d3a46 And the related patch to fix globbing for af_unix abstract names
Abstract af_unix socket names can contain a null character, however the
aare to pcre conversion explicitly disallows null characters because they
are not valid characters for pathnames. Fix this so that they type of
globbing is selectable.

this is a partial fix for

Bug: http://bugs.launchpad.net/bugs/1413410

Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Steve Beattie <steve@nxnw.org>
2015-02-12 10:19:16 -08:00
John Johansen
c2b8a72317 disable downgrade and not enforced rule messages by default
Currently the apparmor parser warns about rules that are not enforced or
downgraded. This is a problem for distros that are not carrying the out of
tree kernel patches, as most profile loads result in warnings.

Change the behavior to not output a message unless a warn flag is passed.
This patch adds 2 different warn flags
  --warn rule-downgraded    	 # warn if a rule is downgraded
  --warn rule-not-enforced	   # warn if a rule is not enforced at all

If the warnings are desired by default the flags can be set in the
parser.conf file.

v2 of patch
- update man page
- add --warn to usage statement
- make --quiet clear warn flags

Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Steve Beattie <steve@nxnw.org>
2014-10-08 13:20:20 -07:00
Tyler Hicks
a154d14f5a parser: Sync parser and man page regarding local and peer perms
This patch updates the parser code to reject rules that contain local
socket permissions and peer conditional elements. The error message for
that condition is also corrected to resolve a copy and paste mistake
from the D-Bus rule parsing code.

The patch also updates the man page to correctly describe the two sets
of socket permissions and fixes an example rule that resulted in a
parser error after the change described above.

Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
Acked-by: Steve Beattie <steve@nxnw.org>
2014-09-22 11:34:32 -05:00
John Johansen
ffa2f682ea Do not output local permissions for rules that have peer_conditionals
while it is not possible to specify a rule with local conditionals with
peer conditionals
eg.
   unix listen peer=(addr=@foo),

a rule such as
   unix peer=(addr=@foo),

is possible, and was setting all permissions for local as well as the peer
condition permissions.

Currently this means the create permission must be specified in a separate
rule from a rule with a peer= condition, if create is to be allowed. This
isn't too much of an issue but it does mean rule such as
  unix connect peer=(addr=@foo),

Can not imply the ability to create a socket. Which may indeed be the
behavior if we wish to enforce that the socket was created in another
process and passed in. Is this what we want to do?

Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Tyler Hicks <tyhicks@canonical.com>
2014-09-22 11:33:49 -05:00
John Johansen
2259857281 parser: Fix the permission encoding output of getopt/setopt
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
Acked-by: Steve Beattie <steve@nxnw.org>
2014-09-04 12:40:47 -07:00
John Johansen
29c776e4fc parser: fix rejecting of unix rules with listen or bind permissions
Only reject rules with explicit listen or bind permissions if a peer
conditional is specified.

Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
2014-09-03 16:09:06 -07:00
John Johansen
e119901e3a parser: fix output of listen and setopts commands
The listen and setopts commands have broken encodings because the
tmp stream they use to handle diverging from the other commands
has does not set its write position to to the end of the copied data.
Instead the write head is set to the beginning so that when the
new data for the command is written it overwrites the begging of
the command instead of appending to it.

Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
2014-09-03 16:04:31 -07:00
John Johansen
4a616e3545 parser: allow specifying the unix perm with peer perms
Fix to allow specifying the unix perm with peer perms. This is allowed
now and even supported, since for unix sockets the peer accept is
mediated in the unix_stream_connect hook (something that is not
possible in the lsm accept hook).

Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
2014-09-03 16:01:37 -07:00
John Johansen
4f80b4d5ca parser: change/fix the encoding for unix socket rules.
This changes/fixes the encoding for unix socket rules. The changes
look larger than they are because it refactors the code, instead
of duplicating.

The major changes are:
- it changes where the accept perm is stored
- it moves anyone_match_pattern to default_match_pattern
- it fixes the layout of the local addr only being written when local
  perms are present

Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Steve Beattie <steve@nxnw.org>
2014-09-03 15:57:17 -07:00
Tyler Hicks
0005895e3d parser: Don't write the stream's address to the rule buffer
The writeu16() function was returning the address of the passed in
std::ostringstream and then the callers of that function were
incorrectly writing that address to the rule buffer.

Before:

  $ echo "/t { unix (connect,read,write) type=stream, }" |
apparmor_parser -qQD dfa-states
  {1} <== (allow/deny/audit/quiet)
  {2} (0x 4/0/0/0)
  {3} (0x 4/0/0/0)
  {43} (0x 46/0/0/0)
  {44} (0x 46/0/0/0)
  
  {1} -> {2}: 0x2
  {1} -> {3}: 0x4
  {1} -> {2}: 0x7
  {1} -> {2}: 0x9
  {1} -> {2}: 0xa
  {1} -> {2}: 0x20 \ 
  {1} -> {4}: 0x34 4
  {3}  (0x 4/0/0/0) -> {5}: 0x0
  {4} -> {6}: 0x0
  {5} -> {7}: 0x1
  {6} -> {2}: 0x31 1
  {7} -> {8}: 0x30 0
  {8} -> {9}: 0x78 x
  {9} -> {10}: 0x37 7
  {10} -> {11}: 0x66 f
  {11} -> {12}: 0x66 f
  {12} -> {13}: 0x66 f
  {13} -> {14}: 0x31 1
  {14} -> {15}: 0x30 0
  {15} -> {16}: 0x34 4
  {16} -> {17}: 0x66 f
  {17} -> {18}: 0x33 3
  {18} -> {19}: 0x35 5
  {19} -> {20}: 0x31 1
  {20} -> {21}: 0x38 8
  {21} -> {22}: 0x0
  {22} -> {23}: 0x1
  {23} -> {24}: 0x30 0
  {24} -> {25}: 0x78 x
  {25} -> {26}: 0x37 7
  {26} -> {27}: 0x66 f
  {27} -> {28}: 0x66 f
  {28} -> {29}: 0x66 f
  {29} -> {30}: 0x31 1
  {30} -> {31}: 0x30 0
  {31} -> {32}: 0x34 4
  {32} -> {33}: 0x66 f
  {33} -> {34}: 0x33 3
  {34} -> {35}: 0x35 5
  {35} -> {36}: 0x31 1
  {36} -> {37}: 0x38 8
  {37} -> {38}: []
  {38} -> {39}: []
  {39} -> {40}: 0x0
  {39} -> {39}: []
  {40} -> {40}: 0x0
  {40} -> {41}: 0x1
  {40} -> {39}: []
  {41} -> {42}: 0x0
  {41} -> {39}: []
  {42} -> {40}: 0x0
  {42} -> {44}: 0x1
  {42} -> {43}: []
  {43}  (0x 46/0/0/0) -> {40}: 0x0
  {43}  (0x 46/0/0/0) -> {43}: []
  {44}  (0x 46/0/0/0) -> {42}: 0x0
  {44}  (0x 46/0/0/0) -> {43}: []

After:

  $ echo "/t { unix (connect,read,write) type=stream, }" |
apparmor_parser -qQD dfa-states
  {1} <== (allow/deny/audit/quiet)
  {2} (0x 4/0/0/0)
  {3} (0x 4/0/0/0)
  {15} (0x 46/0/0/0)
  {16} (0x 46/0/0/0)
  
  {1} -> {2}: 0x2
  {1} -> {3}: 0x4
  {1} -> {2}: 0x7
  {1} -> {2}: 0x9
  {1} -> {2}: 0xa
  {1} -> {2}: 0x20 \ 
  {1} -> {4}: 0x34 4
  {3}  (0x 4/0/0/0) -> {5}: 0x0
  {4} -> {6}: 0x0
  {5} -> {7}: 0x1
  {6} -> {2}: 0x31 1
  {7} -> {8}: 0x0
  {8} -> {9}: 0x1
  {9} -> {10}: []
  {10} -> {11}: []
  {11} -> {12}: 0x0
  {11} -> {11}: []
  {12} -> {12}: 0x0
  {12} -> {13}: 0x1
  {12} -> {11}: []
  {13} -> {14}: 0x0
  {13} -> {11}: []
  {14} -> {12}: 0x0
  {14} -> {16}: 0x1
  {14} -> {15}: []
  {15}  (0x 46/0/0/0) -> {12}: 0x0
  {15}  (0x 46/0/0/0) -> {15}: []
  {16}  (0x 46/0/0/0) -> {14}: 0x0
  {16}  (0x 46/0/0/0) -> {15}: []

Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
2014-09-03 15:46:39 -07:00
Tyler Hicks
b5c3ce638d parser: Adjust writeu16() to output escaped byte sequences
The writeu16() function was outputting unescaped byte sequences to the
rule buffer. That resulted the generation of in an incomplete rule if
one of those unescaped byte sequences contained 0x00.

This patch uses u8 pointers, instead of char pointers, when writing out
the big endian u16 value. More importantly, it casts the u8 values to
unsigned ints, which is what's needed to get the properly escaped byte
sequences.

Before:

  $ echo "/t { unix (connect,read,write) type=stream, }" |
apparmor_parser -qQD dfa-states
  {1} <== (allow/deny/audit/quiet)
  {2} (0x 4/0/0/0)
  {3} (0x 4/0/0/0)
  {5} (0x 46/0/0/0)
  
  {1} -> {2}: 0x2
  {1} -> {3}: 0x4
  {1} -> {2}: 0x7
  {1} -> {2}: 0x9
  {1} -> {2}: 0xa
  {1} -> {2}: 0x20 \ 
  {1} -> {4}: 0x34 4
  {3}  (0x 4/0/0/0) -> {5}: 0x0
  {4} -> {6}: 0x0
  {6} -> {2}: 0x31 1
  

After (the next patch fixes the pointer values that are being written
out):

  $ echo "/t { unix (connect,read,write) type=stream, }" |
apparmor_parser -qQD dfa-states
  {1} <== (allow/deny/audit/quiet)
  {2} (0x 4/0/0/0)
  {3} (0x 4/0/0/0)
  {43} (0x 46/0/0/0)
  {44} (0x 46/0/0/0)
  
  {1} -> {2}: 0x2
  {1} -> {3}: 0x4
  {1} -> {2}: 0x7
  {1} -> {2}: 0x9
  {1} -> {2}: 0xa
  {1} -> {2}: 0x20 \ 
  {1} -> {4}: 0x34 4
  {3}  (0x 4/0/0/0) -> {5}: 0x0
  {4} -> {6}: 0x0
  {5} -> {7}: 0x1
  {6} -> {2}: 0x31 1
  {7} -> {8}: 0x30 0
  {8} -> {9}: 0x78 x
  {9} -> {10}: 0x37 7
  {10} -> {11}: 0x66 f
  {11} -> {12}: 0x66 f
  {12} -> {13}: 0x66 f
  {13} -> {14}: 0x31 1
  {14} -> {15}: 0x30 0
  {15} -> {16}: 0x34 4
  {16} -> {17}: 0x66 f
  {17} -> {18}: 0x33 3
  {18} -> {19}: 0x35 5
  {19} -> {20}: 0x31 1
  {20} -> {21}: 0x38 8
  {21} -> {22}: 0x0
  {22} -> {23}: 0x1
  {23} -> {24}: 0x30 0
  {24} -> {25}: 0x78 x
  {25} -> {26}: 0x37 7
  {26} -> {27}: 0x66 f
  {27} -> {28}: 0x66 f
  {28} -> {29}: 0x66 f
  {29} -> {30}: 0x31 1
  {30} -> {31}: 0x30 0
  {31} -> {32}: 0x34 4
  {32} -> {33}: 0x66 f
  {33} -> {34}: 0x33 3
  {34} -> {35}: 0x35 5
  {35} -> {36}: 0x31 1
  {36} -> {37}: 0x38 8
  {37} -> {38}: []
  {38} -> {39}: []
  {39} -> {40}: 0x0
  {39} -> {39}: []
  {40} -> {40}: 0x0
  {40} -> {41}: 0x1
  {40} -> {39}: []
  {41} -> {42}: 0x0
  {41} -> {39}: []
  {42} -> {40}: 0x0
  {42} -> {44}: 0x1
  {42} -> {43}: []
  {43}  (0x 46/0/0/0) -> {40}: 0x0
  {43}  (0x 46/0/0/0) -> {43}: []
  {44}  (0x 46/0/0/0) -> {42}: 0x0
  {44}  (0x 46/0/0/0) -> {43}: []

Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
2014-09-03 15:10:41 -07:00
Steve Beattie
ab93c858d3 parser: initialize perms in unix_rule constructor
Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
2014-09-03 15:03:05 -07:00
John Johansen
e811d8f2bf parser: map net permission set into a form compatible with the old dfa table
The old dfa table format has 2 64 bit permission field used to store
all of allow, quiet, audit, owner/!owner and transition mask. This
leaves 7 bits for entry + a few other special bits.

Since policydb entries when using old style dfa permission format
don't use support the !owner permission entries we can map, the
high net work permission bits to these entries.

This allows us to enforce base network permissions on system with
only support for the old dfa table format.

  Bits 0-7 inclusive stay put
  Bits 8-9 inclusive move (14 - 8) = 6 to 14-15 GETATTR | SETATTR
  Bits 20-22 inclusive move -4 to 16-18  ACCEPT | BIND | LISTEN  (notice 22 not 23)
  Bit 23 is skipped, hence the need to shift 5 for 24-25 instead of 4
  Bits 24-25 inclusive move -5 to 19-20

Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Steve Beattie <steve@nxnw.org>
2014-09-03 14:50:22 -07:00
Steve Beattie
ade71dc171 parser: Fix segfault in af_unix rule processing
This patch fixes a segfault that was occurring in testing over the
weekend. The problem existed in the original patch that adds af_unix
rules (lp:apparmor commit 2615).

Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: John Johansen <john.johansen@canonical.com>
2014-09-03 14:08:48 -07:00
Steve Beattie
e85777a57c parser: Convert af_unix rules to support addr= rather than path=
This patch converts the path= modifier to the af_unix rules to use
addr= instead.

Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
2014-09-03 14:02:25 -07:00
Steve Beattie
019de74059 parser: fix logic error and incorrect reference from previous commit
Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: John Johansen <john.johansen@canonical.com>
2014-09-03 13:34:10 -07:00
John Johansen
dd44858e60 parser: first step implementing fine grained mediation for unix domain sockets
This patch implements parsing of fine grained mediation for unix domain
sockets, that have abstract and anonymous paths. Sockets with file
system paths are handled by regular file access rules.

The unix network rules follow the general fine grained network
rule pattern of

  [<qualifiers>] af_name [<access expr>] [<rule conds>] [<local expr>] [<peer expr>]

specifically for af_unix this is

  [<qualifiers>] 'unix' [<access expr>] [<rule conds>] [<local expr>] [<peer expr>]

  <qualifiers> = [ 'audit' ] [ 'allow' | 'deny' ]

  <access expr> = ( <access> | <access list> )

  <access> = ( 'server' | 'create' | 'bind' | 'listen' | 'accept' |
               'connect' | 'shutdown' | 'getattr' | 'setattr' |
	       'getopt' | 'setopt' |
               'send' | 'receive' | 'r' | 'w' | 'rw' )
  (some access modes are incompatible with some rules or require additional
   parameters)

  <access list> = '(' <access> ( [','] <WS> <access> )* ')'

  <WS> = white space

  <rule conds> = ( <type cond> | <protocol cond> )*
     each cond can appear at most once

  <type cond> = 'type' '='  ( <AARE> | '(' ( '"' <AARE> '"' | <AARE> )+ ')' )

  <protocol cond> = 'protocol' '='  ( <AARE> | '(' ( '"' <AARE> '"' | <AARE> )+ ')' )

  <local expr> = ( <path cond> | <attr cond> | <opt cond> )*
     each cond can appear at most once

  <peer expr> = 'peer' '=' ( <path cond> | <label cond> )+
     each cond can appear at most once

  <path cond> = 'path' '=' ( <AARE> | '(' '"' <AARE> '"' | <AARE> ')' )

  <label cond> = 'label' '=' ( <AARE> | '(' '"' <AARE> '"' | <AARE> ')')

  <attr cond> = 'attr' '=' ( <AARE> | '(' '"' <AARE> '"' | <AARE> ')' )

  <opt cond> = 'opt' '=' ( <AARE> | '(' '"' <AARE> '"' | <AARE> ')' )

  <AARE> = ?*[]{}^ ( see man page )

 unix domain socket rules are accumulated so that the granted unix
 socket permissions are the union of all the listed unix rule permissions.

 unix domain socket rules are broad and general and become more restrictive
 as further information is specified. Policy may be specified down to
 the path and label level. The content of the communication is not
 examined.

 Some permissions are not compatible with all unix rules.

 unix socket rule permissions are implied when a rule does not explicitly
 state an access list. By default if a rule does not have an access list
 all permissions that are compatible with the specified set of local
 and peer conditionals are implied.

 The 'server', 'r', 'w' and 'rw' permissions are aliases for other permissions.
 server = (create, bind, listen, accept)
 r = (receive, getattr, getopt)
 w = (create, connect, send, setattr, setopt)

In addition it supports the v7 kernel abi semantics around generic
network rules. The v7 abi removes the masking unix and netlink
address families from the generic masking and uses fine grained
mediation for an address type if supplied.

This means that the rules

  network unix,
  network netlink,

are now enforced instead of ignored. The parser previously could accept
these but the kernel would ignore anything written to them. If a network
rule is supplied it takes precedence over the finer grained mediation
rule. If permission is not granted via a broad network access rule
fine grained mediation is applied.

Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
2014-09-03 13:22:26 -07:00