Skip to content

Commit b0c50fa

Browse files
committed
Extract SELinux policy from container
Currently we are always hardcoding "targeted", which is not working for the centos automotive sig that use a custom policy.
1 parent 07e8bf7 commit b0c50fa

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

bib/cmd/bootc-image-builder/image.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,7 @@ func manifestForDiskImage(c *ManifestConfig, rng *rand.Rand) (*manifest.Manifest
332332
img := image.NewBootcDiskImage(containerSource)
333333
img.Users = users.UsersFromBP(customizations.GetUsers())
334334
img.Groups = users.GroupsFromBP(customizations.GetGroups())
335-
// TODO: get from the bootc container instead of hardcoding it
336-
img.SELinux = "targeted"
335+
img.SELinux = c.SourceInfo.SELinuxPolicy
337336

338337
img.KernelOptionsAppend = []string{
339338
"rw",

bib/internal/source/source.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package source
22

33
import (
4+
"bufio"
5+
"errors"
46
"fmt"
57
"os"
68
"path"
@@ -21,8 +23,9 @@ type OSRelease struct {
2123
}
2224

2325
type Info struct {
24-
OSRelease OSRelease
25-
UEFIVendor string
26+
OSRelease OSRelease
27+
UEFIVendor string
28+
SELinuxPolicy string
2629
}
2730

2831
func validateOSRelease(osrelease map[string]string) error {
@@ -58,6 +61,36 @@ func uefiVendor(root string) (string, error) {
5861
return "", fmt.Errorf("cannot find UEFI vendor in %s", bootupdEfiDir)
5962
}
6063

64+
func readSelinuxPolicy(root string) (string, error) {
65+
configPath := "etc/selinux/config"
66+
f, err := os.Open(path.Join(root, configPath))
67+
if err != nil {
68+
return "", fmt.Errorf("cannot read selinux config %s: %w", configPath, err)
69+
}
70+
policy := ""
71+
scanner := bufio.NewScanner(f)
72+
for scanner.Scan() {
73+
line := strings.TrimSpace(scanner.Text())
74+
if len(line) == 0 {
75+
continue
76+
}
77+
if strings.HasPrefix(line, "#") {
78+
continue
79+
}
80+
81+
parts := strings.SplitN(line, "=", 2)
82+
if len(parts) != 2 {
83+
return "", errors.New("selinux config: invalid input")
84+
}
85+
key := strings.TrimSpace(parts[0])
86+
if key == "SELINUXTYPE" {
87+
policy = strings.TrimSpace(parts[1])
88+
}
89+
}
90+
91+
return policy, nil
92+
}
93+
6194
func LoadInfo(root string) (*Info, error) {
6295
osrelease, err := distro.ReadOSReleaseFromTree(root)
6396
if err != nil {
@@ -71,6 +104,12 @@ func LoadInfo(root string) (*Info, error) {
71104
if err != nil {
72105
logrus.Debugf("cannot read UEFI vendor: %v, setting it to none", err)
73106
}
107+
108+
selinuxPolicy, err := readSelinuxPolicy(root)
109+
if err != nil {
110+
logrus.Debugf("cannot read selinux policy: %v, setting it to none", err)
111+
}
112+
74113
var idLike []string
75114
if osrelease["ID_LIKE"] != "" {
76115
idLike = strings.Split(osrelease["ID_LIKE"], " ")
@@ -86,6 +125,7 @@ func LoadInfo(root string) (*Info, error) {
86125
IDLike: idLike,
87126
},
88127

89-
UEFIVendor: vendor,
128+
UEFIVendor: vendor,
129+
SELinuxPolicy: selinuxPolicy,
90130
}, nil
91131
}

0 commit comments

Comments
 (0)