| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "slices" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/carlosframework/platform/internal/instancectl" |
| 10 | ) |
| 11 | |
| 12 | func TestPlan14EffectiveBoundUnit(t *testing.T) { |
| 13 | _, _, shape := plan14UnitBindingFixture(t, "console.service", "", "") |
| 14 | shape.Program = strings.Repeat("a", 64) |
| 15 | definition, err := claimBoundUnitDefinition(shape) |
| 16 | if err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | values := map[string]string{"FragmentPath": "/etc/systemd/system/console.service", "DropInPaths": "", "NeedDaemonReload": "no", "ExecStart": "{ path=/usr/bin/false ; argv[]=/usr/bin/false ; }", "IPAddressAllow": "", "IPAddressDeny": "", "Slice": instancectl.SliceName(shape.Host)} |
| 20 | for _, line := range strings.Split(string(definition), "\n") { |
| 21 | k, v, ok := strings.Cut(line, "=") |
| 22 | if !ok { |
| 23 | continue |
| 24 | } |
| 25 | if k == "ExecStart" { |
| 26 | continue |
| 27 | } |
| 28 | values[k] = v |
| 29 | } |
| 30 | for _, attack := range []string{"none", "DropInPaths", "User", "Group", "NoNewPrivileges", "ProtectSystem", "PrivatePIDs", "IPAddressAllow", "ExecStart", "NeedDaemonReload", "FragmentPath", "SocketBindAllow", "SocketBindDeny", "BindsTo", "PartOf"} { |
| 31 | t.Run(attack, func(t *testing.T) { |
| 32 | ctl := func(_ context.Context, args ...string) (string, error) { |
| 33 | var out strings.Builder |
| 34 | for _, key := range strings.Split(args[2], ",") { |
| 35 | value := values[key] |
| 36 | if key == attack { |
| 37 | value = "forged" |
| 38 | if key == "BindsTo" || key == "PartOf" { |
| 39 | value = "carlos-edge.service" |
| 40 | } |
| 41 | } |
| 42 | out.WriteString(key + "=" + value + "\n") |
| 43 | } |
| 44 | return out.String(), nil |
| 45 | } |
| 46 | err := claimEffectiveUnit(t.Context(), shape, ctl) |
| 47 | if attack == "none" && err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | if attack != "none" && err == nil { |
| 51 | t.Fatal("effective override accepted", attack) |
| 52 | } |
| 53 | }) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func TestPlan14EffectiveNetworkReduction(t *testing.T) { |
| 58 | a, err := claimIPSet("localhost 169.254.0.0/16 169.254.169.254/32 fc00::/7 fd00:ec2::254/128") |
| 59 | if err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | b, err := claimIPSet("127.0.0.0/8 ::1/128 169.254.0.0/16 fc00::/7") |
| 63 | if err != nil || !slices.Equal(a, b) { |
| 64 | t.Fatal(a, b, err) |
| 65 | } |
| 66 | c, err := claimIPSet("127.0.0.0/8 ::1/128 169.254.0.0/15 fc00::/7") |
| 67 | if err != nil || slices.Equal(a, c) { |
| 68 | t.Fatal("broadened set accepted", a, c, err) |
| 69 | } |
| 70 | } |
| 71 | |