Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSModifier: Extend EMU API to update verity and root device #10584

Open
wants to merge 15 commits into
base: 3.0-dev
Choose a base branch
from
Open
3 changes: 3 additions & 0 deletions toolkit/tools/imagegen/installutils/installutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ const (
// CmdlineSELinuxEnforcingArg is the arg required for forcing SELinux to be in enforcing mode.
CmdlineSELinuxEnforcingArg = "enforcing=1"

// CmdlineSELinuxPermissiveArg is the arg for SELinux to be in force-permissive mode.
CmdlineSELinuxPermissiveArg = "enforcing=0"

// CmdlineSELinuxSettings is the kernel command-line args for enabling SELinux.
CmdlineSELinuxSettings = CmdlineSELinuxSecurityArg + " " + CmdlineSELinuxEnabledArg

Expand Down
15 changes: 11 additions & 4 deletions toolkit/tools/osmodifierapi/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import (

// OS defines how each system present on the image is supposed to be configured.
type OS struct {
Hostname string `yaml:"hostname"`
SELinux imagecustomizerapi.SELinux `yaml:"selinux"`
Users []imagecustomizerapi.User `yaml:"users"`
Overlays *[]Overlay `yaml:"overlays"`
Hostname string `yaml:"hostname"`
SELinux imagecustomizerapi.SELinux `yaml:"selinux"`
Users []imagecustomizerapi.User `yaml:"users"`
Overlays *[]Overlay `yaml:"overlays"`
Verity *imagecustomizerapi.Verity `yaml:"verity"`
RootDevice string `yaml:"rootDevice"`
}

func (s *OS) IsValid() error {
Expand Down Expand Up @@ -65,5 +67,10 @@ func (s *OS) IsValid() error {
}
}

err = s.Verity.IsValid()
if err != nil {
return fmt.Errorf("invalid verity:\n%w", err)
}

return nil
}
15 changes: 15 additions & 0 deletions toolkit/tools/pkg/imagecustomizerlib/bootcustomizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ func (b *BootCustomizer) UpdateSELinuxCommandLine(selinuxMode imagecustomizerapi
return nil
}

// Update the image's SELinux kernel command-line args.
func (b *BootCustomizer) UpdateSELinuxCommandLineWithEnforcingArg(selinuxMode imagecustomizerapi.SELinuxMode) error {
newSELinuxArgs, err := selinuxModeToArgsWithEnforcingArg(selinuxMode)
if err != nil {
return err
}

err = b.UpdateKernelCommandLineArgs(defaultGrubFileVarNameCmdlineForSELinux, selinuxArgNames, newSELinuxArgs)
if err != nil {
return err
}

return nil
}

func (b *BootCustomizer) UpdateKernelCommandLineArgs(defaultGrubFileVarName defaultGrubFileVarName,
argsToRemove []string, newArgs []string,
) error {
Expand Down
4 changes: 2 additions & 2 deletions toolkit/tools/pkg/imagecustomizerlib/customizeverity.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func updateGrubConfigForVerity(rootfsVerity imagecustomizerapi.Verity, rootHash
return err
}

formattedCorruptionOption, err := systemdFormatCorruptionOption(rootfsVerity.CorruptionOption)
formattedCorruptionOption, err := SystemdFormatCorruptionOption(rootfsVerity.CorruptionOption)
if err != nil {
return err
}
Expand Down Expand Up @@ -231,7 +231,7 @@ func systemdFormatPartitionId(configDeviceId string, mountIdType imagecustomizer
}
}

func systemdFormatCorruptionOption(corruptionOption imagecustomizerapi.CorruptionOption) (string, error) {
func SystemdFormatCorruptionOption(corruptionOption imagecustomizerapi.CorruptionOption) (string, error) {
switch corruptionOption {
case imagecustomizerapi.CorruptionOptionDefault, imagecustomizerapi.CorruptionOptionIoError:
return "", nil
Expand Down
22 changes: 22 additions & 0 deletions toolkit/tools/pkg/imagecustomizerlib/grubcfgutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,28 @@ func selinuxModeToArgs(selinuxMode imagecustomizerapi.SELinuxMode) ([]string, er
return newSELinuxArgs, nil
}

// Converts an SELinux mode into the list of required command-line args for that mode (with enforcing mode).
func selinuxModeToArgsWithEnforcingArg(selinuxMode imagecustomizerapi.SELinuxMode) ([]string, error) {
newSELinuxArgs := []string(nil)
switch selinuxMode {
case imagecustomizerapi.SELinuxModeDisabled:
newSELinuxArgs = []string{installutils.CmdlineSELinuxDisabledArg}

case imagecustomizerapi.SELinuxModeForceEnforcing:
newSELinuxArgs = []string{installutils.CmdlineSELinuxSecurityArg, installutils.CmdlineSELinuxEnabledArg,
installutils.CmdlineSELinuxEnforcingArg}

case imagecustomizerapi.SELinuxModePermissive, imagecustomizerapi.SELinuxModeEnforcing:
newSELinuxArgs = []string{installutils.CmdlineSELinuxSecurityArg, installutils.CmdlineSELinuxEnabledArg,
installutils.CmdlineSELinuxPermissiveArg}

default:
return nil, fmt.Errorf("unknown SELinux mode (%s)", selinuxMode)
}

return newSELinuxArgs, nil
}

// Update the SELinux kernel command-line args.
func updateSELinuxCommandLineHelperAll(grub2Config string, selinuxMode imagecustomizerapi.SELinuxMode, allowMultiple bool, requireKernelOpts bool) (string, error) {
newSELinuxArgs, err := selinuxModeToArgs(selinuxMode)
Expand Down
68 changes: 62 additions & 6 deletions toolkit/tools/pkg/osmodifierlib/modifierutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,67 @@ func doModifications(baseConfigPath string, osConfig *osmodifierapi.OS) error {
}
}

if osConfig.Verity != nil {

bootCustomizer, err := imagecustomizerlib.NewBootCustomizer(dummyChroot)
if err != nil {
return err
}

err = updateDefaultGrubForVerity(osConfig.Verity, bootCustomizer)
if err != nil {
return err
}

err = bootCustomizer.WriteToFile(dummyChroot)
if err != nil {
return err
}
}

if osConfig.RootDevice != "" {

bootCustomizer, err := imagecustomizerlib.NewBootCustomizer(dummyChroot)
if err != nil {
return err
}

err = bootCustomizer.SetRootDevice(osConfig.RootDevice)
if err != nil {
return err
}

err = bootCustomizer.WriteToFile(dummyChroot)
if err != nil {
return err
}
}

return nil
}

func updateDefaultGrubForVerity(verity *imagecustomizerapi.Verity, bootCustomizer *imagecustomizerlib.BootCustomizer) error {

var err error

formattedCorruptionOption, err := imagecustomizerlib.SystemdFormatCorruptionOption(verity.CorruptionOption)
if err != nil {
return err
}

newArgs := []string{
"rd.systemd.verity=1",
fmt.Sprintf("systemd.verity_root_data=%s", verity.DataDeviceId),
fmt.Sprintf("systemd.verity_root_hash=%s", verity.HashDeviceId),
fmt.Sprintf("systemd.verity_root_options=%s", formattedCorruptionOption),
}

err = bootCustomizer.UpdateKernelCommandLineArgs("GRUB_CMDLINE_LINUX", []string{"rd.systemd.verity",
"systemd.verity_root_data", "systemd.verity_root_hash", "systemd.verity_root_options"}, newArgs)
if err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -109,12 +170,7 @@ func handleSELinux(selinuxMode imagecustomizerapi.SELinuxMode, bootCustomizer *i

logger.Log.Infof("Configuring SELinux mode")

err = bootCustomizer.UpdateSELinuxCommandLine(selinuxMode)
if err != nil {
return err
}

err = imagecustomizerlib.UpdateSELinuxModeInConfigFile(selinuxMode, dummyChroot)
err = bootCustomizer.UpdateSELinuxCommandLineWithEnforcingArg(selinuxMode)
if err != nil {
return err
}
Expand Down
7 changes: 1 addition & 6 deletions toolkit/tools/pkg/osmodifierlib/modifydefaultgrub.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ import (

var grubArgs = []string{
"rd.overlayfs",
"roothash",
"root",
"rd.systemd.verity",
"systemd.verity_root_data",
"systemd.verity_root_hash",
"systemd.verity_root_options",
"roothash",
"selinux",
"enforcing",
}
Expand Down Expand Up @@ -92,6 +88,5 @@ func extractValuesFromGrubConfig(imageChroot safechroot.ChrootInterface) ([]stri
}
}
}

return values, rootDevice, nil
}
Loading