Commit graph

1616 commits

Author SHA1 Message Date
Georgia Garcia
aaad725ac0 apparmor.systemd: fix shellcheck false positive
Shellcheck is complaining that some of the functions are never called,
but they are called from rc.apparmor.functions, causing a false
positive.

This issue only appears in shellcheck version 0.9.0, which is the one
used in ubuntu 24.04, that's why it only failed in the pipeline now.

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/388

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-04-30 18:30:01 -03:00
Georgia Garcia
aee0492491 parser: add error=EXX flag support
Add a flag that allows setting the error code AppArmor will send when
an operation is denied. This should not be used normally.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-04-15 16:32:16 -03:00
John Johansen
ab9e6311f3 Merge parser: add network inet mediation documentation to apparmor.d
This updates the man page for the recent inet mediation patch.

This is an extension of MR 1202, it adds a patch that changes the anonymous ip address anon to be ip address none which is a better fit.

This patch adds documentation of the recent network changes which extended all network rules to support access permissions, and added address and port matching for inet and inet6 families.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1213
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2024-04-12 03:46:23 +00:00
John Johansen
689df6d3cd switch inet mediation from using anon to none
inet mediation allows specifying rules for sockets that don't have
a known address, whether because it is unbound or because the
kernel doesn't make the address available.

The current code uses the word anon for anonymous, but that has
proven to be unclear. Switch from using anon to none, to emphasize
that this is a case where there just isn't an address to use as
part of mediation.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2024-04-11 19:03:43 -07:00
Georgia Garcia
c1ca0286e8 parser: inet conditionals should only generate rules for inet family
When a family is specified in the network rules, we have to make sure
the conditionals match the family. A netlink rule should not be able
to specify ip and port for local and remote (peer) sockets, for example.

When type or protocol is specified in network rules along with inet
conditionals, we should only generate rules for the families that
support those conditionals.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-04-10 16:46:08 -03:00
John Johansen
88ec709aac Merge parser: fix getattr and setattr perm mapping on mqueue rules
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/377
Fixes: https://gitlab.com/apparmor/apparmor/-/issues/378
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1197
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2024-04-03 08:39:08 +00:00
Georgia Garcia
e1405cba82 parser: add anon ip parser test
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-04-02 13:57:18 -03:00
Georgia Garcia
c9d54a021e parser: add network inet mediation documentation to apparmor.d
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-04-02 13:57:17 -03:00
Zygmunt Krynicki
358a8a6a65 fix typo: globally
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
2024-03-29 10:57:33 +01:00
Zygmunt Krynicki
f750a8a217 fix typo: parameter
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
2024-03-29 10:57:33 +01:00
Zygmunt Krynicki
42e7f5a52a fix typo: exercised
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
2024-03-29 10:56:21 +01:00
Zygmunt Krynicki
6fddd31b76 fix typo: aggressive
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
2024-03-29 10:52:25 +01:00
Zygmunt Krynicki
d274eb39d9 fix typo: accumulate
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
2024-03-29 10:52:24 +01:00
Georgia Garcia
8da46c5e77 parser: fix getattr and setattr perm mapping on mqueue rules
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-03-27 18:33:23 -03:00
Georgia Garcia
f4706bfdf6 utils: allow mount destination globbing
The abstraction lxc/start-container shipped by the liblxc-common
package uses the following mount rule which was not allowed by our
regexes:

  mount options=(rw, make-slave) -> **,
  mount options=(rw, make-rslave) -> **,

Since in AppArmor regex ** includes '/' but * by itself doesn't, I'm
adding explicit support for **.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-03-26 18:40:40 -03:00
Georgia Garcia
f9527d2113 parser: fix issues appointed by coverity
Fix issues introduced in coverity's snapshots 75887, 70858 and 75429.
- CID 353483: Uninitialized pointer field (UNINIT_CTOR)
- CID 349572: Unsigned compared against 0 (NO_EFFECT)

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-03-18 10:36:56 -03:00
Georgia Garcia
101651c88f parser: fix af_inet feature from network to network_v8
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-03-15 16:38:10 -03:00
Georgia Garcia
efc2ec5fdd parser: fix cmp function for network rules
The network cmp function was missing the new attributes added, causing
rules to be dropped.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-03-15 16:38:10 -03:00
Georgia Garcia
b01b9895e7 parser: add ability to specify anonymous ip
If anonymous ip is specified, then the port will match anything.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-03-15 16:38:10 -03:00
Georgia Garcia
a0a0c88d9e parser: generate all ip options when ip is not specified
When the ip is not specified, then we should generate rules for ip
types: anonymous, ipv4 and ipv6. And that's the case for both local
and peer when considering recv and send permissions.

std::ostringstream does not have a copy constructor, that's why in
several places one can see streaming the string of one stream into
another.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-03-15 10:33:33 -03:00
Georgia Garcia
63676459c4 parser: encode create permission separately from the others
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-03-15 10:33:33 -03:00
Georgia Garcia
9ed04cb01e parser: introduce network label attribute
We want to be able to determine label in the future and build the
policy dfa based on its presence or not.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-03-15 10:33:33 -03:00
Georgia Garcia
2a885872a3 parser: ip size encoding should be an enum, not the ip size
According to the protocol expected by the kernel, the field
representing the ip size should be an enum instead of the actual ip
size. This is more future-proof.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-03-15 10:33:33 -03:00
Georgia Garcia
989501428e parser: initial steps into encoding protocol properly
Before the inet patches, protocol was not handled, so the information
was ignored. This patch introduces the ability to start mediating
protocol.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-03-15 10:33:33 -03:00
Christian Boltz
d1d39d176e Merge parser(Makefile): don't ship /var in downstream packages
Should help with building /var free images downstream.

See below: https://gitlab.com/apparmor/apparmor/-/merge_requests/1167#note_1798547092

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1167
Approved-by: Christian Boltz <apparmor@cboltz.de>
Merged-by: Christian Boltz <apparmor@cboltz.de>
2024-03-08 12:53:32 +00:00
Max Gautier
2d654477f2
parser(Makefile): dont install /var/lib/apparmor
This directory is not used anymore.
This help downstream build of /var free images.

Links: https://0pointer.net/blog/fitting-everything-together.html
2024-03-08 10:18:16 +01:00
Georgia Garcia
2db41acd1b parser: fix generic perms in network rules
The permission for network rules when the inet mediation was not
available, or for when the family was not af_inet or af_inet6 was
being generated as one that would allow anything. Make them specific
using perms.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-03-06 10:33:54 -03:00
Georgia Garcia
a10d9044b8 parser: fix policy generation for non-af_inet rules
The layout for AF_INET and AF_INET6 rules were being applied to all
families, which causes failures in their mediation.

Fixes: ddefe11a ("parser: add fine grained conditionals to network rule")
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-03-06 10:07:17 -03:00
Christian Boltz
b53441a689 Merge Update ancient paths in apparmor and apparmor.d manpage
- replace example calls of /etc/init.d/apparmor with apparmor.service
- drop /etc/init.d/apparmor in filelist
- replace /var/lib/apparmor/ with /var/cache/apparmor/

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1171
Approved-by: Georgia Garcia <georgia.garcia@canonical.com>
Merged-by: Christian Boltz <apparmor@cboltz.de>
2024-03-06 12:24:13 +00:00
Christian Boltz
7452f34279
Update ancient paths in apparmor and apparmor.d manpage
- replace example calls of /etc/init.d/apparmor with apparmor.service
- drop /etc/init.d/apparmor in filelist
- replace /var/lib/apparmor/ with /var/cache/apparmor/
2024-03-05 22:59:18 +01:00
Christian Boltz
4e546291a5
MountRule: sync flags_keywords with parser code
... based on /mount.cc mnt_opts_table

Several keywords and aliases were missing in flags_keywords:
- B
- M
- make-private
- make-rprivate
- make-rshared
- make-rslave
- make-runbindable
- make-shared
- make-slave
- make-unbindable
- r
- R
- read-only
- w

Also sort the keywords in the same order as in mount.cc.

Note: AARE handling is still a TODO.

After that, update the list of known parsing failures:
- several valid profiles are now correctly parsed
- some `"make-*" mount opt and an invalid src` bad profiles are no
  longer detected as being invalid
2024-03-03 15:37:59 +01:00
Georgia Garcia
119e3f38f9 parser: maintain compatibility for fine grained inet network mediation
A simple rule without conditionals need to be generated for when the
kernel does not support fine grained inet network mediation.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-02-29 18:27:20 -03:00
Georgia Garcia
8a5e7227db parser: add parser tests for specified perms
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-02-29 16:25:59 -03:00
Georgia Garcia
79ee3eb180 parser: add parser tests for local conditional 2024-02-29 16:25:59 -03:00
Georgia Garcia
052dd987b3 parser: add network conditional parser tests 2024-02-29 16:25:59 -03:00
Georgia Garcia
7e25be7b0b parser: change network conditionals to allow unquoted ids
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-02-29 16:25:59 -03:00
Georgia Garcia
ddefe11a40 parser: add fine grained conditionals to network rule
Options available are ip= and port= inside the peer group or outside,
representing local addresses and ports:

network peer=(ip=127.0.0.1 port=8080),
network ip=::1 port=8080 peer=(ip=::2 port=8081),

The 'ip' option supports both IPv4 and IPv6. Examples would be
ip=192.168.0.4, or ip=::578d

The 'port' option accepts a 16-bit unsigned integer. An example would
be port=1234

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-02-29 16:25:59 -03: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
35f25a251b parser: fix coverity issues found in snapshot 70858
This commit add fixes for issues found in coverity's snapshot 70858.
  - CID 323127:  Performance inefficiencies  (COPY_INSTEAD_OF_MOVE)
  - CID 323125:  Uninitialized members  (UNINIT_CTOR)

I'm also removing Novell, Inc. from the copyright notice added by a
copy-paste error, and an unused variable left over from debugging.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-02-28 10:24:08 -03:00
Félix Poisot
420d3395fc makefiles: test for support of flto-partition flag
Test for compiler support of "-flto-partition=none" flag before passing
it.

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/310
2024-02-24 16:07:55 +00:00
John Johansen
f10e106a08 Merge parser: Add support for a default_allow mode
Add support for a default_allow mode that facillitates writing profiles
in that allow everything by default. This is not normally recomended
but fascilitates creating basic profiles while working to transition
policy away from unconfined.

This mode is being added specifically to replace the use of the
unconfined flag in these transitional profiles as the use of unconfined
in policy is confusing and does not reflect the semantics of what is
being done.

Generally the goal for policy should be to remove all default_allow
profiles once the policy is fully developed.

Note: this patch only adds parsing of default_allow mode. Currently
it sets the unconfined flag to achieve default allow but this
prevents deny rules from being applied. Once dominance is fixed a
subsequent patch will transition default_allow away from using
the unconfined flag.

Signed-off-by: John Johansen <john.johansen@canonical.com>

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1109
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2023-12-23 08:38:18 +00:00
Christian Boltz
3ee47af402
Fix typo in apparmor_parser manpage
man apparmor_parser gives examples for the --warn command line option as

             apparmor_parser --warn=rules-not-enforced ...
and
             apparmor_parser --warn=no-rules-not-enforced ...

but the actual --warn options are rule-not-enforced / no-rule-not-enforced
(without s)

Fixes: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1057453
2023-12-05 13:27:09 +01:00
Georgia Garcia
923cbcf3be parser: fix subprofile name in profile serialization
Given the following profile:

profile foo {
  profile bar {
    profile baz {
    }
  }
}

The parser would correctly serialize the "foo" profile and the
"foo//bar" profile, but it would incorrectly name "bar//baz" when it
should be "foo//bar//baz". This would cause issues loading the profile
in certain kernels causing a "parent does not exist" error.

Partially addresses #346.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2023-12-01 16:14:19 -03:00
Georgia Garcia
78a2c9f5f3 parser: constify unchanged strings in sd_write_*
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2023-12-01 15:18:51 -03:00
John Johansen
832bb8f417 parser: Add support for a default_allow mode
Add support for a default_allow mode that facillitates writing profiles
in that allow everything by default. This is not normally recomended
but fascilitates creating basic profiles while working to transition
policy away from unconfined.

This mode is being added specifically to replace the use of the
unconfined flag in these transitional profiles as the use of unconfined
in policy is confusing and does not reflect the semantics of what is
being done.

Generally the goal for policy should be to remove all default_allow
profiles once the policy is fully developed.

Note: this patch only adds parsing of default_allow mode. Currently
it sets the unconfined flag to achieve default allow but this
prevents deny rules from being applied. Once dominance is fixed a
subsequent patch will transition default_allow away from using
the unconfined flag.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-11-24 15:38:19 -08:00
Georgia Garcia
dcad01ccc3 parser: fix regex parser leak on parsing failure
When the regex parser failed, the Chars objects created/used in rules
charset and cset_chars would not be cleaned up properly and would
leak.

Closes #361

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2023-11-23 17:37:46 -03:00
Christian Boltz
5d9d4483fb
Add Documentation=... to apparmor.service
This is taken from Debian's apparmor.service, and is the first (and
easiest) step to get the upstream and Debian file closer.
2023-10-29 10:49:33 +01:00
Stéphane Graber
659a187687
parser/rc.apparmor: Handle Incus
Signed-off-by: Stéphane Graber <stgraber@stgraber.org>
2023-10-12 00:55:03 -04:00
John Johansen
197d00d21a parser: add support for a generic all rule type
Extend the policy syntax to have a rule that allows specifying all
permissions for all rule types.

  allow all,

This is useful for making blacklist based policy, but can also be
useful when combined with other rule prefixes, eg. to add audit
to all rules.

  audit access all,

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-09-07 01:30:15 -07:00
Georgia Garcia
75ca0e7919 parser: track leading zeros required for ipv6 range regex generator
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2023-09-07 00:13:52 -07:00