karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbono...@apache.org
Subject [karaf] branch karaf-4.2.x updated: KARAF-6405 - Make sure Config files are jailed to the container etc directory
Date Sat, 21 Sep 2019 05:47:07 GMT
This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch karaf-4.2.x
in repository https://gitbox.apache.org/repos/asf/karaf.git


The following commit(s) were added to refs/heads/karaf-4.2.x by this push:
     new 94c932a  KARAF-6405 - Make sure Config files are jailed to the container etc directory
94c932a is described below

commit 94c932acaf02f4afa224d66a8ce53ee6073929ce
Author: Colm O hEigeartaigh <coheigea@apache.org>
AuthorDate: Tue Sep 17 16:46:56 2019 +0100

    KARAF-6405 - Make sure Config files are jailed to the container etc directory
    
    (cherry picked from commit 226e9bb43d8f77009508a7f7584a35e9036b2272)
---
 .../internal/service/FeatureConfigInstaller.java   |  18 +++-
 .../service/FeatureConfigInstallerTest.java        | 119 ++++++++++++++++++++-
 .../features/internal/service/data4/features.xml   |  37 +++++++
 3 files changed, 166 insertions(+), 8 deletions(-)

diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeatureConfigInstaller.java
b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeatureConfigInstaller.java
index 84c8a5d..d817547 100644
--- a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeatureConfigInstaller.java
+++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeatureConfigInstaller.java
@@ -170,7 +170,7 @@ public class FeatureConfigInstaller {
                     if (storage != null) {
                         cfgFile = new File(storage, configId.fullPid + ".cfg");
                     }
-                    if (cfgFile.exists()) {
+                    if (cfgFile.exists() && cfgFile.getCanonicalPath().startsWith(storage.getCanonicalPath()))
{
                         cfgFile.delete();
                     }
                 }
@@ -179,7 +179,7 @@ public class FeatureConfigInstaller {
                 for (ConfigFileInfo configFileInfo : feature.getConfigurationFiles()) {
                     String finalname = substFinalName(configFileInfo.getFinalname());
                     File cfgFile = new File(finalname);
-                    if (cfgFile.exists()) {
+                    if (cfgFile.exists() && cfgFile.getCanonicalPath().startsWith(System.getProperty("karaf.etc")))
{
                         cfgFile.delete();
                     }
                 }
@@ -208,7 +208,7 @@ public class FeatureConfigInstaller {
      * substituted, it will be prefixed with karaf.base (+ file separator), too.
      * </li>
      * </ol>
-     * 
+     *
      * @param finalname
      *            The final name that should be processed.
      * @return the location in the file system that should be accesses.
@@ -252,12 +252,16 @@ public class FeatureConfigInstaller {
         finalname = substFinalName(finalname);
 
         File file = new File(finalname);
+        if (!file.getCanonicalPath().startsWith(System.getProperty("karaf.etc"))) {
+            throw new IOException("Configuration file path must be in the 'karaf.etc' directory");
+        }
+
         if (file.exists()) {
             if (!override) {
-                LOGGER.debug("Configuration file {} already exist, don't override it", finalname);
+                LOGGER.debug("Configuration file {} already exists, don't override it", finalname);
                 return;
             } else {
-                LOGGER.info("Configuration file {} already exist, overriding it", finalname);
+                LOGGER.info("Configuration file {} already exists, overriding it", finalname);
             }
         } else {
             LOGGER.info("Creating configuration file {}", finalname);
@@ -289,6 +293,10 @@ public class FeatureConfigInstaller {
         throws Exception {
         if (storage != null && configCfgStore) {
             File cfgFile = getConfigFile(cid);
+            if (!cfgFile.getCanonicalPath().startsWith(storage.getCanonicalPath())) {
+                throw new IOException("Config name cannot contain .. characters");
+            }
+
             if (!cfgFile.exists()) {
                 props.save(cfgFile);
             } else {
diff --git a/features/core/src/test/java/org/apache/karaf/features/internal/service/FeatureConfigInstallerTest.java
b/features/core/src/test/java/org/apache/karaf/features/internal/service/FeatureConfigInstallerTest.java
index c9497fc..65af057 100644
--- a/features/core/src/test/java/org/apache/karaf/features/internal/service/FeatureConfigInstallerTest.java
+++ b/features/core/src/test/java/org/apache/karaf/features/internal/service/FeatureConfigInstallerTest.java
@@ -16,14 +16,25 @@
  */
 package org.apache.karaf.features.internal.service;
 
+import org.apache.karaf.features.Feature;
+import org.easymock.EasyMock;
 import org.junit.Test;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 
 public class FeatureConfigInstallerTest {
-    
+
     private void substEqual(final String src, final String subst) {
         assertEquals(FeatureConfigInstaller.substFinalName(src), subst);
     }
@@ -33,11 +44,11 @@ public class FeatureConfigInstallerTest {
         final String karafBase = "/tmp/karaf.base";
         final String karafEtc = karafBase + "/etc";
         final String foo = "/foo";
-        
+
         System.setProperty("karaf.base", karafBase);
         System.setProperty("karaf.etc", karafEtc);
         System.setProperty("foo", foo);
-        
+
         substEqual("etc/test.cfg", karafBase + File.separator + "etc/test.cfg");
         substEqual("/etc/test.cfg", karafBase + File.separator + "/etc/test.cfg");
         substEqual("${karaf.etc}/test.cfg", karafEtc + "/test.cfg");
@@ -51,4 +62,106 @@ public class FeatureConfigInstallerTest {
         substEqual("${foo}${bar}/${bar}${foo}", foo + "/" + foo);
     }
 
+    @Test
+    public void testGoodConfigName() throws Exception {
+        String dataDir = "data4";
+
+        Path karafEtc = Files.createTempDirectory("etc");
+
+        System.setProperty("karaf.etc", karafEtc.toString());
+
+        RepositoryImpl repo = new RepositoryImpl(getClass().getResource(dataDir + "/features.xml").toURI());
+        Feature f100 = repo.getFeatures()[0];
+        assertEquals("goodConfigName", f100.getName());
+
+        ConfigurationAdmin configAdmin = EasyMock.createMock(ConfigurationAdmin.class);
+        Configuration config = EasyMock.createMock(Configuration.class);
+
+        EasyMock.expect(configAdmin.listConfigurations(EasyMock.anyString())).andReturn(null);
+        EasyMock.expectLastCall().anyTimes();
+        EasyMock.expect(configAdmin.getConfiguration(EasyMock.anyString(), EasyMock.isNull())).andReturn(config);
+        EasyMock.expectLastCall();
+
+        EasyMock.replay(configAdmin);
+        FeatureConfigInstaller installer = new FeatureConfigInstaller(configAdmin);
+        installer.installFeatureConfigs(f100);
+
+        EasyMock.verify(configAdmin);
+
+        File installedFile = Paths.get(karafEtc.toString(), "config.cfg").toFile();
+        assertTrue(installedFile.exists());
+
+        installedFile.delete();
+        karafEtc.toFile().delete();
+    }
+
+    @Test
+    public void testBadConfigName() throws Exception {
+        String dataDir = "data4";
+
+        Path karafEtc = Files.createTempDirectory("etc");
+
+        System.setProperty("karaf.etc", karafEtc.toString());
+
+        RepositoryImpl repo = new RepositoryImpl(getClass().getResource(dataDir + "/features.xml").toURI());
+        Feature f100 = repo.getFeatures()[1];
+        assertEquals("badConfigName", f100.getName());
+
+        ConfigurationAdmin configAdmin = EasyMock.createMock(ConfigurationAdmin.class);
+        Configuration config = EasyMock.createMock(Configuration.class);
+
+        EasyMock.expect(configAdmin.listConfigurations(EasyMock.anyString())).andReturn(null);
+        EasyMock.expectLastCall().anyTimes();
+        EasyMock.expect(configAdmin.getConfiguration(EasyMock.anyString(), EasyMock.isNull())).andReturn(config);
+        EasyMock.expectLastCall();
+
+        EasyMock.replay(configAdmin);
+        FeatureConfigInstaller installer = new FeatureConfigInstaller(configAdmin);
+        installer.installFeatureConfigs(f100);
+
+        EasyMock.verify(configAdmin);
+
+        // Verify the file was not installed
+        File installedFile = Paths.get(karafEtc.toString(), "../../../../../../../../../../../../tmp/config").toFile();
+        assertFalse(installedFile.exists());
+
+        karafEtc.toFile().delete();
+    }
+
+    @Test
+    public void testBadConfigFileName() throws Exception {
+        String dataDir = "data4";
+
+        Path karafEtc = Files.createTempDirectory("etc");
+        Path karafBase = Files.createTempDirectory("base");
+
+        System.setProperty("karaf.etc", karafEtc.toString());
+        System.setProperty("karaf.base", karafBase.toString());
+
+        RepositoryImpl repo = new RepositoryImpl(getClass().getResource(dataDir + "/features.xml").toURI());
+        Feature f100 = repo.getFeatures()[2];
+        assertEquals("badConfigFileName", f100.getName());
+
+        ConfigurationAdmin configAdmin = EasyMock.createMock(ConfigurationAdmin.class);
+        EasyMock.expect(configAdmin.listConfigurations(EasyMock.anyString())).andReturn(null);
+        EasyMock.expectLastCall().anyTimes();
+
+        EasyMock.replay(configAdmin);
+        FeatureConfigInstaller installer = new FeatureConfigInstaller(configAdmin);
+        try {
+            installer.installFeatureConfigs(f100);
+            fail("Failure expected on a bad config filename");
+        } catch (Throwable t) {
+            // expected
+        }
+        EasyMock.verify(configAdmin);
+
+        // Verify the file was not installed
+        File installedFile = Paths.get(karafBase.toString(), "../../../../../../../../../../../../tmp/config2").toFile();
+        assertFalse(installedFile.exists());
+
+        karafBase.toFile().delete();
+        karafEtc.toFile().delete();
+    }
+
 }
diff --git a/features/core/src/test/resources/org/apache/karaf/features/internal/service/data4/features.xml
b/features/core/src/test/resources/org/apache/karaf/features/internal/service/data4/features.xml
new file mode 100644
index 0000000..46843bb
--- /dev/null
+++ b/features/core/src/test/resources/org/apache/karaf/features/internal/service/data4/features.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements. See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License. You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+
+-->
+<features name="test" xmlns="http://karaf.apache.org/xmlns/features/v1.3.0">
+
+    <feature name="goodConfigName" version="1.0.0">
+        <config name="config"> 
+           myProperty = myValue 
+       </config> 
+    </feature>
+    
+    <feature name="badConfigName" version="1.0.0">
+        <config name="../../../../../../../../../../../../tmp/config"> 
+           myProperty = myValue 
+       </config> 
+    </feature>
+    
+     <feature name="badConfigFileName" version="1.0.0">
+        <configfile finalname="/../../../../../../../../tmp/config2" override="true">file:etc/users.properties</configfile>
+    </feature>
+
+</features>
\ No newline at end of file


Mime
View raw message