Skip to content

Commit c9452b1

Browse files
committed
bib: Extract what SELinux policy to us from container
Currently we are always hardcoding "targeted", which is not working for the centos automotive sig that use a custom policy.
1 parent 8c8fe2b commit c9452b1

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-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: 46 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,39 @@ 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+
// nolint:errcheck
71+
defer f.Close()
72+
73+
policy := ""
74+
scanner := bufio.NewScanner(f)
75+
for scanner.Scan() {
76+
line := strings.TrimSpace(scanner.Text())
77+
if len(line) == 0 {
78+
continue
79+
}
80+
if strings.HasPrefix(line, "#") {
81+
continue
82+
}
83+
84+
parts := strings.SplitN(line, "=", 2)
85+
if len(parts) != 2 {
86+
return "", errors.New("selinux config: invalid input")
87+
}
88+
key := strings.TrimSpace(parts[0])
89+
if key == "SELINUXTYPE" {
90+
policy = strings.TrimSpace(parts[1])
91+
}
92+
}
93+
94+
return policy, nil
95+
}
96+
6197
func LoadInfo(root string) (*Info, error) {
6298
osrelease, err := distro.ReadOSReleaseFromTree(root)
6399
if err != nil {
@@ -71,6 +107,12 @@ func LoadInfo(root string) (*Info, error) {
71107
if err != nil {
72108
logrus.Debugf("cannot read UEFI vendor: %v, setting it to none", err)
73109
}
110+
111+
selinuxPolicy, err := readSelinuxPolicy(root)
112+
if err != nil {
113+
logrus.Debugf("cannot read selinux policy: %v, setting it to none", err)
114+
}
115+
74116
var idLike []string
75117
if osrelease["ID_LIKE"] != "" {
76118
idLike = strings.Split(osrelease["ID_LIKE"], " ")
@@ -86,6 +128,7 @@ func LoadInfo(root string) (*Info, error) {
86128
IDLike: idLike,
87129
},
88130

89-
UEFIVendor: vendor,
131+
UEFIVendor: vendor,
132+
SELinuxPolicy: selinuxPolicy,
90133
}, nil
91134
}

0 commit comments

Comments
 (0)