jbonofre opened a new pull request, #3065:
URL: https://github.com/apache/karaf/pull/3065

   ## Summary
   
   #2878 rejects shell metacharacters in `javaOpts`, but the instance name and 
location still reach `/bin/sh` when a child instance is started, so they can 
run arbitrary OS commands as the Karaf process user:
   
   - `ProcessImpl` substitutes the working directory as plain text into the jpm 
`start.sh` script, unquoted in `cd ${dir}`. A location such as `/tmp/x$(id)` or 
`/tmp/x;id` runs a command.
   - `doStart()` puts the location inside double quotes in the launch command 
line (`-Dkaraf.base="..."` and others). The shell still expands `$(...)` and 
backticks inside double quotes, so quoting doesn't protect it.
   - The name is the default location (`<instances>/<name>`), and it is 
substituted unquoted into the child instance `bin/start` and `bin/stop` scripts 
(`bin/karaf` gets the location).
   
   This is reachable through `instance:create`, `instance:clone` and 
`instance:rename` followed by `instance:start`, the matching `InstancesMBean` 
operations, and the `InstanceService` API. Since #2878 the shell commands and 
the MBean operations require the `admin` role.
   
   ## Changes
   
   - jpm (`ProcessImpl`, `unix/start.sh`): pass the working directory as a 
single-quoted shell literal and use it as `cd "$JPM_DIR"`, so the start script 
never interprets it, whoever the caller is. This also fixes directories that 
contain spaces, which `cd ${dir}` didn't handle.
   - `InstanceServiceImpl`:
     - Restrict instance names to `[A-Za-z0-9_][A-Za-z0-9_.-]*` on create, 
clone and rename. This also rejects `.` and `..`.
     - Reject locations that contain `"`, `$`, `` ` ``, `&`, `|`, `<`, `>`, 
line breaks or, outside Windows, `\`. This is checked on create and clone, and 
again in `doStart()` for instances registered before this change. Spaces are 
still allowed, so paths such as `C:\Program Files\...` keep working.
   - Child instance `bin/karaf`, `bin/start` and `bin/stop`: double-quote the 
substituted values.
   
   Behaviour change: new instance names with spaces, non-ASCII characters, or a 
leading `.` or `-` are now rejected. Existing instances keep their names, but 
an existing instance whose location contains one of the rejected characters no 
longer starts.
   
   ## Test plan
   
   - [x] `mvn -pl util,instance test` passes.
   - [x] New `ProcessTest#testDirectoryIsNotInterpretedByShell` starts a 
process in a directory named ``$(touch m) `touch m`; touch m`` and then in one 
named `it's`. It fails against the previous `ProcessImpl`/`start.sh` because 
the injected command runs, and passes with this change.
   - [x] New `InstanceServiceImplTest` cases cover unsafe names and locations 
on create, clone and rename, and an unsafe stored location on start.
   - [ ] `InstanceTest` itest not run locally. Its instance names 
(`instance-<uuid>`, `new_instance-<uuid>`) match the new name rule.
   - [ ] Backport to `karaf-4.4.x`.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to