2
0
mirror of https://gitlab.com/apparmor/apparmor synced 2025-08-22 18:17:09 +00:00

8 Commits

Author SHA1 Message Date
Georgia Garcia
b8dee97ed3 parser: fix leaking name in variable expansion
Fixes: https://gitlab.com/apparmor/apparmor/-/issues/533
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2025-08-04 18:55:58 -03:00
Georgia Garcia
05458768cf parser: constify and pass by reference unchanged value
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2025-08-04 18:55:58 -03:00
Georgia Garcia
a2f2ca6119 parser: fix variable expansion
When the variable was being expanded, it needed to be reevaluated to
check if there was still unresolved variables. That allowed for a
weird bug to happen: If the string contained a variable preceded by @,
like in "user@@{uid}" and the variable was resolved to a case where {
is used, like in @{uid}={[0-9],[1-9][0-9]}, then on the second pass,
the parser would try to resolve the following variable
@{[0-9],[1-9][0-9]}, which is incorrect behavior. Fix it by not
including part of the string that was already resolved on the
subsequent passes.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2025-07-31 18:04:16 -03:00
Steve Beattie
6673be07aa
parser/variables: fix read-after-free in error case
When variable expansion occurs, the expansion attempts to replace the
memory location of the string containing the variable, and frees the
string it is replacing. However, this occurs before the variable lookup
occurs to determine if there is an appropriate declaration for the
variable. When the failing expansion occurs in a profile name, this
causes a read-after-free (followed by a double free) because the error
handling path attempts to report the profile name in the error message.

This can be reproduced like so, using the
tst/simple_tests/vars/vars_profile_name_23.sd testcase:

```
$ ../apparmor_parser --config-file=./parser.conf -M features_files/features.all -S -I /home/sbeattie/git/apparmor/parser/tst/./simple_tests/ ./simple_tests/vars/vars_profile_name_23.sd
Failed to find declaration for: @{FOO}
ERROR expanding variables for profile #xQV, failed to load
free(): double free detected in tcache 2
```

Fix this by waiting to free the profile name field until after the
variable declaration has successfully been looked up. This results in
the test case reporting the following error:

```
$ ../apparmor_parser --config-file=./parser.conf -M features_files/features.all -S -I /home/sbeattie/git/apparmor/parser/tst/./simple_tests/ ./simple_tests/vars/vars_profile_name_23.sd
Failed to find declaration for: @{FOO}
ERROR expanding variables for profile /does/not/exist@{FOO}, failed to load

```

Fixes: dfbd2dc4b ("parser: refactor variables and symbols table into their own class")
Signed-off-by: Steve Beattie <steve@nxnw.org>
Ref: https://gitlab.com/apparmor/apparmor/-/merge_requests/1747
2025-07-24 23:37:24 -07:00
Georgia Garcia
efb13aac0a parser: fix leaks from variable refactoring
Fix issues introduced in coverity's snapshot 89167
- CID 532796: (#4 of 4): Resource leak (RESOURCE_LEAK)

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2025-07-18 14:56:29 -03:00
Georgia Garcia
e4f3ef72ab parser: use const auto & to avoid copy
Fix issues introduced in coverity's snapshot 89167
- CID 532797: (#1 of 1): Use of auto that causes a copy (AUTO_CAUSES_COPY)

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2025-07-18 14:56:29 -03:00
Georgia Garcia
69c248a431 parser: initialize non-static variable
Fix issues introduced in coverity's snapshot 89167
- CID 532798: (#1 of 1): Uninitialized scalar field (UNINIT_CTOR)

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2025-07-18 14:15:38 -03:00
Georgia Garcia
dfbd2dc4b1 parser: refactor variables and symbols table into their own class
While symtab for now has only static members, it will allow for a
change in the future for each profile to have their own symbols like
profile_name, etc.
2025-06-25 12:29:17 -03:00