Skip to content

Commit

Permalink
builder/chroot: Use ChrootShell() instead of ChrootExecStdin()
Browse files Browse the repository at this point in the history
With ChrootShell() we can drop usage of the chroot binary, as well as
choose the initial workdir to spawn in to.

As the files are now build as root, they are owned by root so chroot
as root but start at the buildUser home directory.
  • Loading branch information
joebonrichie committed Sep 16, 2024
1 parent aebc307 commit 9343e6b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
10 changes: 2 additions & 8 deletions builder/chroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,8 @@ func (p *Package) Chroot(notif PidNotifier, pman *EopkgManager, overlay *Overlay
// Allow bash to work
commands.SetStdin(os.Stdin)

// Legacy package format requires root, stay as root.
user := BuildUser
if p.Type == PackageTypeXML {
user = "root"
}

loginCommand := fmt.Sprintf("/bin/su - %s -s %s", user, BuildUserShell)
err := ChrootExecStdin(notif, overlay.MountPoint, loginCommand)
loginCommand := fmt.Sprintf("/bin/su - root -s %s", BuildUserShell)
err := ChrootShell(notif, overlay.MountPoint, loginCommand, BuildUserHome)

commands.SetStdin(nil)
notif.SetActivePID(0)
Expand Down
24 changes: 12 additions & 12 deletions builder/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,30 +231,29 @@ func ChrootExecStdin(notif PidNotifier, dir, command string) error {
}

func ChrootShell(notif PidNotifier, dir, command, workdir string) error {

// Hold an fd for the og root
fd, err := os.Open("/")
if err != nil {
return err
}

// Remember our working directory
wd, err := os.Getwd()
if err != nil {
return err
wd, err2 := os.Getwd()
if err2 != nil {
return err2
}

// Ensure chroot directory is available
if err := os.Chdir(dir); err != nil {
if err = os.Chdir(dir); err != nil {
return err
}

if err := syscall.Chroot(dir); err != nil {
if err = syscall.Chroot(dir); err != nil {
fd.Close()
return err
}

if err := os.Chdir("/"); err != nil {
if err = os.Chdir("/"); err != nil {
return err
}

Expand All @@ -267,28 +266,29 @@ func ChrootShell(notif PidNotifier, dir, command, workdir string) error {
c.Env = ChrootEnvironment
c.Dir = workdir

if err := c.Start(); err != nil {
if err = c.Start(); err != nil {
goto CLEANUP
}

notif.SetActivePID(c.Process.Pid)

if err := c.Wait(); err != nil {
if err = c.Wait(); err != nil {
goto CLEANUP
}

CLEANUP:
// Return to our original root and working directory
defer fd.Close()
if err := fd.Chdir(); err != nil {
if err = fd.Chdir(); err != nil {

Check failure on line 282 in builder/util.go

View workflow job for this annotation

GitHub Actions / lint

if statements should only be cuddled with assignments (wsl)
return err
}
if err := syscall.Chroot("."); err != nil {
if err = syscall.Chroot("."); err != nil {

Check failure on line 285 in builder/util.go

View workflow job for this annotation

GitHub Actions / lint

if statements should only be cuddled with assignments (wsl)
return err
}
if err := os.Chdir(wd); err != nil {
if err = os.Chdir(wd); err != nil {

Check failure on line 288 in builder/util.go

View workflow job for this annotation

GitHub Actions / lint

if statements should only be cuddled with assignments (wsl)
return err
}

return err
}

Expand Down

0 comments on commit 9343e6b

Please sign in to comment.