Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i/builtin: allow @ in custom-device filepaths #14651

Merged

Conversation

olivercalder
Copy link
Member

Allow the @ symbol to occur in filepaths for the custom-device interface. This is common in paths under /sys/, such as /sys/devices/platform/soc@0/soc@0:bus@30000000/30350000.ocotp-ctrl/imx-ocotp0/nvmem.

This work is tracked internally by https://warthogs.atlassian.net/browse/SNAPDENG-32542

@olivercalder olivercalder added this to the 2.67 milestone Oct 18, 2024
Copy link

codecov bot commented Oct 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.01%. Comparing base (96ea7b0) to head (8cf28da).
Report is 68 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #14651      +/-   ##
==========================================
+ Coverage   78.95%   79.01%   +0.06%     
==========================================
  Files        1084     1085       +1     
  Lines      146638   147512     +874     
==========================================
+ Hits       115773   116558     +785     
- Misses      23667    23725      +58     
- Partials     7198     7229      +31     
Flag Coverage Δ
unittests 79.01% <ø> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@olivercalder
Copy link
Member Author

Need to modify the slot so that the @ is not treated as a variable marker in AppArmor profiles.

@alexmurray
Copy link
Contributor

CC @jslarraz we might need to update review-tools to also allow this


// Validating regexp for udev device names.
// We forbid:
// - `|`: it's valid for udev, but more work for us
// - `{}`: have a special meaning for AppArmor
// - `@`: denotes a variable in AppArmor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this is entirely correct, looking at apparmor.d manpage, a variable is defined to have this format:

VARIABLE = '@{' ALPHA [ ( ALPHANUMERIC | '_' ) ... ] '}'

I am also told that folks are using system-files as a workaround, which means that AppArmor parser may actually be happy with paths containing @ as long as it's not @{..}. I'm guessing, it's likely @ does not need escaping at all in this case, since the profiles worked for system-files? At the same time, I suspect that system-files may simply not validate the path correctly, so it'd be great to align both interface implementations to use common code for validation.

Copy link
Contributor

@bboozzoo bboozzoo Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexmurray @jslarraz do you know what are the rules around @ in profiles is it's not part of a variable reference nor denotes an abstract unit socket path?

Copy link
Member Author

@olivercalder olivercalder Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My motivation behind prohibiting @ in device paths was to preserve the existing behavior, while allowing @ for filepaths. But there's not a technical reason for this, I just wanted to minimize the change in behavior.

Regarding usage of @, I think there are cases where @ has a special meaning in profiles without being followed by {. Some examples from the manpage:

       the @ character, similar to how they are reported (as paths) by netstat
       -x.  The  address then follows and may contain pattern matching and any
       characters including the null character. In  apparmor  null  characters
       must be specified by using an escape sequence \000 or \x00. The pattern
       matching  is  the  same  as is used by file path matching so * will not
       match / even though it has no  special  meaning  with  in  an  abstract
       socket name. Eg.

         unix addr=@*,
         # Allow SOCK_STREAM connect, receive and send on an abstract socket @bar
         # with peer running under profile '/foo'
         unix (connect, receive, send) type=stream peer=(label=/foo,addr="@bar"),

         # Allow accepting connections from and receiving from peer running under
         # profile '/bar' on abstract socket '@foo'
         unix (accept, receive) addr=@foo peer=(label=/bar),

But these examples all pertain to unix sockets, not filepaths, so probably it would be fine to not escape @ in filepaths? Though then we need to ensure that filepaths don't contain @{, as right now they can contain {} characters (though device paths cannot). I do think it is safe to universally escape @ symbols, if that would be simpler. I'd be interested to hear what the security people think :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think abstract sockets have somewhat unusual apparmor names, at least in audit messages. I don't recall how this behaves in the parser though. I could check quickly and report back.

@ernestl
Copy link
Collaborator

ernestl commented Oct 21, 2024

If feels like we need a test to actually prove whatever we end up doing works?

Copy link
Contributor

@zyga zyga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a pair of comments but ultimately we need to see what happens in the parser.


// Validating regexp for udev device names.
// We forbid:
// - `|`: it's valid for udev, but more work for us
// - `{}`: have a special meaning for AppArmor
// - `@`: denotes a variable in AppArmor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think abstract sockets have somewhat unusual apparmor names, at least in audit messages. I don't recall how this behaves in the parser though. I could check quickly and report back.

@olivercalder olivercalder removed this from the 2.67 milestone Oct 24, 2024
@olivercalder olivercalder force-pushed the SNAPDENG-32542-allow-at-in-paths branch from 53deba2 to 27757ed Compare November 2, 2024 01:04
@olivercalder
Copy link
Member Author

Spotted by @ZeyadYasser and myself today, there's a bug in common-files-based interfaces (system-files and personal-files) at the moment. These interfaces allow paths which contain @ characters. This includes paths which end in @. However, filepaths in common-files have {,/,/**} appended to them, which can result in filepaths of the form /foo@{,/,/**}, where the trailing @{,/,/**} looks like an AppArmor variable even though it's not. I'm not yet sure how the AppArmor parser deals with this, when there's no such variable defined.

In a separate PR, I think we should add validation that common-files doesn't allow trailing unescaped @ characters in filepaths.

@olivercalder
Copy link
Member Author

Before merging, the "escape @ ..." commit and its revert should be purged from the git log, since the associated messages are no longer correct.

@olivercalder olivercalder added the Needs security review Can only be merged once security gave a :+1: label Nov 2, 2024
Copy link
Contributor

@bboozzoo bboozzoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

Copy link
Member

@andrewphelpsj andrewphelpsj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link
Contributor

@zyga zyga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a look at the parser and at this change. I think the conservative approach works well. LGTM!

@olivercalder olivercalder force-pushed the SNAPDENG-32542-allow-at-in-paths branch from 27757ed to 4b963b0 Compare November 13, 2024 15:04
Copy link
Contributor

@alexmurray alexmurray left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - this is the correct approach IMO - allow @ but only if not followed by {. Thanks.

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>
AppArmor variables take the form @{foo} in rules, so we cannot allow a
specified filepath to contain substrings of this form. Such paths should
never be necessary.

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>
@olivercalder olivercalder force-pushed the SNAPDENG-32542-allow-at-in-paths branch from 213abe7 to 8cf28da Compare November 13, 2024 23:27
@olivercalder olivercalder merged commit db0e079 into canonical:master Nov 14, 2024
54 of 57 checks passed
sespiros added a commit to sespiros/snapd that referenced this pull request Nov 19, 2024
* master: (44 commits)
  wrappers: do not reload activation units (canonical#14724)
  gadget/install: support for no{exec,dev,suid} mount flags
  interfaces/builtin/mount_control: add support for nfs mounts (canonical#14694)
  tests: use gojq - part 1 (canonical#14686)
  interfaces/desktop-legacy: allow DBus access to com.canonical.dbusmenu
  interfaces/builtin/fwupd.go: allow access to nvmem for thunderbolt plugin
  tests: removing uc16 executions (canonical#14575)
  tests: Added arm github runner to build snapd (canonical#14504)
  tests: no need to run spread when there are not tests matching the filter (canonical#14728)
  tests/lib/tools/store-state: exit on errors, update relevant tests (canonical#14725)
  tests: udpate the github workflow to run tests suggested by spread-filter tool (canonical#14519)
  testtime: add mockable timers for use in tests (canonical#14672)
  interface/screen_inhibit_control: Improve screen inhibit control for use on core (canonical#14134)
  tests: use images with 20G disk in openstack (canonical#14720)
  i/builtin: allow @ in custom-device filepaths (canonical#14651)
  tests: refactor test-snapd-desktop-layout-with-content
  tests: fix broken app definition
  tests: capitalize sentences in comments
  tests: wrap very long shell line
  tests: fix raciness in async startup and sync install
  ...
@olivercalder olivercalder added this to the 2.67 milestone Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs security review Can only be merged once security gave a :+1:
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants