carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package host
2
3import (
4 "encoding/hex"
5 "strings"
6
7 "github.com/carlosframework/platform/internal/hostbox"
8)
9
10// Handles identify an inode within its filesystem across boots. Current-boot
11// device, mount and inode checks still apply; handles never authorize an open.
12type servingParkPersistentDirectory struct {
13 Filesystem [2]int32
14 Type int32
15 Handle string
16 Owner uint32
17 Mode uint32
18}
19
20type servingParkPersistentFile servingParkPersistentDirectory
21
22func validServingParkHandle(p servingParkPersistentDirectory) bool {
23 b, err := hex.DecodeString(p.Handle)
24 return err == nil && len(b) > 0 && len(b) <= 128 && hex.EncodeToString(b) == p.Handle && p.Type > 0 && p.Filesystem != [2]int32{}
25}
26
27func (p servingParkPersistentDirectory) valid() bool {
28 return validServingParkHandle(p) && p.Mode&0170000 == 0040000 && p.Mode&0022 == 0
29}
30
31func (p servingParkPersistentFile) valid() bool {
32 return validServingParkHandle(servingParkPersistentDirectory(p)) && p.Mode&0170000 == 0100000
33}
34
35// Historical records without handles remain readable. Partial new evidence
36// refuses rather than being treated as an old record eligible for recapture.
37func validateServingParkPersistentRecord(r servingParkRecord) error {
38 p := r.Source.Persistent
39 if p == (servingParkPersistentDirectory{}) {
40 if r.Source.BootID != "" || r.EventPersistent != (servingParkPersistentDirectory{}) {
41 return hostbox.ErrChanged
42 }
43 for _, f := range r.Source.Files {
44 if f.Persistent != (servingParkPersistentFile{}) {
45 return hostbox.ErrChanged
46 }
47 }
48 return nil
49 }
50 if !p.valid() || !validServingParkBootID(r.Source.BootID) {
51 return hostbox.ErrChanged
52 }
53 for _, f := range r.Source.Files {
54 if f.Present {
55 if !f.Persistent.valid() || f.Persistent.Filesystem != p.Filesystem {
56 return hostbox.ErrChanged
57 }
58 } else if f.Persistent != (servingParkPersistentFile{}) {
59 return hostbox.ErrChanged
60 }
61 }
62 if r.Prepared {
63 if !r.EventPersistent.valid() || r.EventPersistent.Filesystem != p.Filesystem || r.EventPersistent.Mode&0077 != 0 {
64 return hostbox.ErrChanged
65 }
66 } else if r.EventPersistent != (servingParkPersistentDirectory{}) {
67 return hostbox.ErrChanged
68 }
69 return nil
70}
71
72func validServingParkBootID(id string) bool {
73 if len(id) != 36 || id[8] != '-' || id[13] != '-' || id[18] != '-' || id[23] != '-' {
74 return false
75 }
76 raw := strings.ReplaceAll(id, "-", "")
77 b, err := hex.DecodeString(raw)
78 return err == nil && len(b) == 16 && hex.EncodeToString(b) == raw && raw != strings.Repeat("0", 32)
79}
80
81// Only the saved source decides whether persistent evidence was captured. New
82// observations cannot upgrade an old intent or permanent legacy consumption.
83func sameServingParkSource(saved, fresh servingParkSource) bool {
84 if saved.Persistent == (servingParkPersistentDirectory{}) && saved.BootID == "" {
85 for _, item := range saved.Files {
86 if item.Persistent != (servingParkPersistentFile{}) {
87 return false
88 }
89 }
90 fresh.Persistent = servingParkPersistentDirectory{}
91 fresh.BootID = ""
92 for i := range fresh.Files {
93 fresh.Files[i].Persistent = servingParkPersistentFile{}
94 }
95 }
96 if saved.ParentPersistent == (servingParkPersistentDirectory{}) {
97 fresh.ParentPersistent = servingParkPersistentDirectory{}
98 }
99 if saved.Volume == "" {
100 fresh.Volume = ""
101 }
102 return saved == fresh
103}
104