From bbf200f3b2f89ab540a31fb61cf0e6a1bc1cfd07 Mon Sep 17 00:00:00 2001
From: Chad Smith <chad.smith@canonical.com>
Date: Thu, 8 Dec 2022 22:03:46 -0700
Subject: [PATCH] * net: netplan config root read-only as wifi config can
 contain creds

On netplan systems, network v2 is passed directly though and written
to /etc/netplan/50-cloud-init.yaml without validation. Current
netplan configuration provides the ability to configure sensitive
information such as `wifi:access-points:password`.

Limit permissions for /etc/network/50-cloud-init.yaml as read-only
for root (600). Since configuration or modification or netplan config
needs to be performed by an admin user this permission restriction
aligns with netplan tooling.

Set root read-only only always and not just 'if' sensitive material
exists within custom config because it will add confusion to have
two expected modes for this file based on external conditions.
---
 cloudinit/net/netplan.py                         |  3 ++-
 tests/integration_tests/modules/test_combined.py | 11 +++++++++++
 tests/unittests/test_distros/test_netconfig.py   |  5 +++--
 3 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/cloudinit/net/netplan.py b/cloudinit/net/netplan.py
index 41acf96..72309fd 100644
--- a/cloudinit/net/netplan.py
+++ b/cloudinit/net/netplan.py
@@ -234,7 +234,8 @@ class Renderer(renderer.Renderer):
 
         if not header.endswith("\n"):
             header += "\n"
-        util.write_file(fpnplan, header + content)
+
+        util.write_file(fpnplan, header + content, mode=0o600)
 
         if self.clean_default:
             _clean_default(target=target)
diff --git a/tests/integration_tests/modules/test_combined.py b/tests/integration_tests/modules/test_combined.py
index 9cd1648..bbf59bd 100644
--- a/tests/integration_tests/modules/test_combined.py
+++ b/tests/integration_tests/modules/test_combined.py
@@ -41,6 +41,17 @@ runcmd:
 @pytest.mark.ci
 @pytest.mark.user_data(USER_DATA)
 class TestCombined:
+    @pytest.mark.ubuntu  # Because netplan
+    def test_netplan_permissions(self, class_client: IntegrationInstance):
+        """
+        Test that netplan config file is generated with proper permissions
+        """
+        response = class_client.execute(
+            "stat -c %a /etc/netplan/50-cloud-init.yaml"
+        )
+        assert response.ok, "Unable to check perms on 50-cloud-init.yaml"
+        assert "600" == response.stdout.strip()
+
     def test_final_message(self, class_client: IntegrationInstance):
         """Test that final_message module works as expected.
 
diff --git a/tests/unittests/test_distros/test_netconfig.py b/tests/unittests/test_distros/test_netconfig.py
index d09e46a..37cfcdf 100644
--- a/tests/unittests/test_distros/test_netconfig.py
+++ b/tests/unittests/test_distros/test_netconfig.py
@@ -431,7 +431,7 @@ class TestNetCfgDistroUbuntuNetplan(TestNetCfgDistroBase):
             print(results[cfgpath])
             print("----------")
             self.assertEqual(expected, results[cfgpath])
-            self.assertEqual(0o644, get_mode(cfgpath, tmpd))
+            self.assertEqual(0o600, get_mode(cfgpath, tmpd))
 
     def netplan_path(self):
         return '/etc/netplan/50-cloud-init.yaml'
@@ -720,6 +720,7 @@ class TestNetCfgDistroArch(TestNetCfgDistroBase):
                 apply_fn(config, bringup)
 
         results = dir2dict(tmpd)
+        mode = 0o600 if with_netplan else 0o644
         for cfgpath, expected in expected_cfgs.items():
             print("----------")
             print(expected)
@@ -727,7 +728,7 @@ class TestNetCfgDistroArch(TestNetCfgDistroBase):
             print(results[cfgpath])
             print("----------")
             self.assertEqual(expected, results[cfgpath])
-            self.assertEqual(0o644, get_mode(cfgpath, tmpd))
+            self.assertEqual(mode, get_mode(cfgpath, tmpd))
 
     def netctl_path(self, iface):
         return '/etc/netctl/%s' % iface
-- 
2.33.0

