Skip to content

Check the files and URLs opened by the shell built-ins against the shell ACL - #3163

Open
jbonofre wants to merge 1 commit into
apache:mainfrom
jbonofre:shell-builtins-file-access
Open

jbonofre wants to merge 1 commit into
apache:mainfrom
jbonofre:shell-builtins-file-access

Conversation

@jbonofre

Copy link
Copy Markdown
Member

Problem

The Gogo built-ins registered in the shell scope (cat, grep, head, tail, less, ls, sort, wc, cd and tac) open the files, directories and URLs given as arguments with the permissions of the Karaf process. The shipped org.apache.karaf.command.acl.shell has no rule for them, so they are open to any user who can log in to the console. shell:source, which reads its script from a file or URL, is open too.

A rule on the command name wouldn't do: these built-ins are also used to filter or page the output of other commands (bundle:list | grep foo, help | more), which every user needs.

Fix

  • FileAccessGuard subclasses Gogo's Posix and Builtin. It checks each file, directory or URL argument after Gogo has parsed the options, so options are never taken for files: grep's pattern isn't checked, ls with no argument checks the current directory, and - is standard input for the commands that read it. tac FILE, which writes its standard input to FILE, is checked too. The location is made absolute and normalized before the check.
  • SessionFactoryImpl.checkFileAccess() is the hook. SecuredSessionFactoryImpl checks it against a new .file pseudo-command in the shell ACL, next to .invoke and .redirect, as the session's subject.
  • .file fails closed: when no rule matches it, only admin has access. An etc/ kept from an older version, which has no .file rule, is covered too. A rule can open file access to other roles, for example .file = admin,viewer.
  • Standard input isn't checked, so piping the output of a command into grep, less, wc... keeps working for every user.
  • The shipped shell ACL gets .file = admin and source = admin: standard feature.xml, child-instance template, itest fixture.
  • security.adoc documents both rules.

Testing

  • FileAccessGuardTest runs the real built-ins in a headless session: denied files are neither read (cat, head, tail, wc, sort, less, grep) nor written (tac, tac -a); URLs are refused before they are opened; ls, cd and relative paths are resolved and normalized; standard input and commands without files aren't checked.
  • SecuredSessionFactoryImplFileAccessTest covers the ACL decision: no shell ACL, an ACL without a .file rule, the shipped rule, a role list and an argument rule.
  • New ShellCommandSecurityTest#testFileBuiltinsViaSsh: a viewer gets Insufficient credentials for cat, grep, head, shell:ls, a URL and tac FILE (no file is created), and Command not found for shell:source; it can still pipe into grep and wc; admin can still read and list files.
  • Checked by hand on a 4.5.0 distribution with the patched shell.core bundle, with both the old and the new shell ACL. admin is unaffected, and help | more and echo hello | less still page interactively for a viewer.
  • Local runs on JDK 21:
    • mvn test -pl shell/core: 40 passed
    • mvn test -Ptest -Dtest='org/apache/karaf/itests/ssh/*Test' in itests/test: 16 passed
    • mvn apache-rat:check -pl shell/core,itests/test,instance: passed

This touches the same ACL blocks as #3153, so whichever is merged second needs a trivial rebase.

…ell ACL

The Gogo built-ins registered in the shell scope (cat, grep, head, tail,
less, ls, sort, wc, cd and tac) open the files, directories and URLs given
as arguments with the permissions of the Karaf process, and no shipped ACL
rule covered them.

Check each of these arguments, after Gogo has parsed the options, against a
new .file rule in the shell ACL. When no rule matches, only admin has
access, so an etc/ kept from an older version is covered too. Reading
standard input isn't checked, so piping the output of a command into grep,
less or wc keeps working for every user. shell:source, which reads its
script from a file or URL, is restricted to admin.
@github-actions

Copy link
Copy Markdown

Test Results

  656 files   -   476    656 suites   - 476   36m 17s ⏱️ - 2h 50m 41s
1 415 tests +   12  1 276 ✅ +    7  131 💤 ±  0   8 ❌ + 7 
3 374 runs   - 2 230  2 948 ✅  - 2 127  411 💤  - 115  15 ❌ +14 

For more details on these failures, see this check.

Results for commit c2d73c0. ± Comparison against base commit dcc0ea4.

This pull request removes 2 and adds 14 tests. Note that renamed tests count towards both.
org.apache.karaf.itests.examples.DeployerExampleTest ‑ initializationError
org.apache.karaf.itests.examples.NamespaceHandlerExampleTest ‑ initializationError
org.apache.karaf.itests.ssh.ShellCommandSecurityTest ‑ testFileBuiltinsViaSsh
org.apache.karaf.shell.impl.console.FileAccessGuardTest ‑ allowedFileIsCheckedThenRead
org.apache.karaf.shell.impl.console.FileAccessGuardTest ‑ commandsWithoutFilesAreNotChecked
org.apache.karaf.shell.impl.console.FileAccessGuardTest ‑ deniedFilesAreNotRead
org.apache.karaf.shell.impl.console.FileAccessGuardTest ‑ lsAndCdAreChecked
org.apache.karaf.shell.impl.console.FileAccessGuardTest ‑ relativeLocationsAreResolvedAndNormalized
org.apache.karaf.shell.impl.console.FileAccessGuardTest ‑ standardInputIsNotChecked
org.apache.karaf.shell.impl.console.FileAccessGuardTest ‑ tacDoesNotWriteADeniedFile
org.apache.karaf.shell.impl.console.FileAccessGuardTest ‑ urlsAreCheckedBeforeTheyAreOpened
org.apache.karaf.shell.impl.console.osgi.secured.SecuredSessionFactoryImplFileAccessTest ‑ argumentRule
…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant