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

   Backport of #3065 to `karaf-4.4.x`.
   
   ## Problem
   
   #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.
   
   ## Fix
   
   Same as #3065:
   
   - 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. This also fixes directories that contain spaces.
   - `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. The raw path 
is checked before it is canonicalized, so on Windows an unsafe location is 
rejected with an `IllegalArgumentException` instead of failing in 
`getCanonicalPath()`.
   - 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.
   
   ## Differences from #3065
   
   None in behaviour. The cherry-pick had one conflict in 
`InstanceServiceImpl`: `doStart()` is static on `main` and an instance method 
on `karaf-4.4.x`, so it stays an instance method here. The two commits are 
otherwise the ones from #3065.
   
   #3065 isn't merged yet, so any change made there during review should be 
applied here as well.
   
   ## Testing
   
   - [x] `mvn -pl util,instance test` passes on this branch (53 tests), 
including `ProcessTest#testDirectoryIsNotInterpretedByShell` and the new 
`InstanceServiceImplTest` cases.
   - [ ] `InstanceTest` itest not run locally.
   


-- 
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