-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3cf8f33
commit 10db894
Showing
35 changed files
with
1,495 additions
and
330 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
|
||
"github.com/valyentdev/ravel/core/jailer" | ||
) | ||
|
||
func main() { | ||
if err := jailer.Run(); err != nil { | ||
slog.Error("jailer run failed", "error", err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
package config | ||
|
||
type RuntimeConfig struct { | ||
InitBinary string `json:"init_binary" toml:"init_binary"` | ||
LinuxKernel string `json:"linux_kernel" toml:"linux_kernel"` | ||
Snapshotter string `json:"snapshotter" toml:"snapshotter"` | ||
CloudHypervisorBinary string `json:"cloud_hypervisor_binary" toml:"cloud_hypervisor_binary"` | ||
JailerBinary string `json:"jailer_binary" toml:"jailer_binary"` | ||
InitBinary string `json:"init_binary" toml:"init_binary"` | ||
LinuxKernel string `json:"linux_kernel" toml:"linux_kernel"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package jailer | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"syscall" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func chroot(path string) error { | ||
rootDir := "/" | ||
|
||
err := unix.Mount("", rootDir, "", syscall.MS_SLAVE|syscall.MS_REC, "") | ||
if err != nil { | ||
return fmt.Errorf("failed to mount: %w", err) | ||
} | ||
|
||
err = unix.Mount(path, path, "", syscall.MS_BIND|syscall.MS_REC, "") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = syscall.Chdir(path) | ||
if err != nil { | ||
return fmt.Errorf("failed to chdir: %w", err) | ||
} | ||
|
||
err = syscall.Mkdir("old_root", 0755) | ||
if err != nil { | ||
return fmt.Errorf("failed to create old_root: %w", err) | ||
} | ||
|
||
oldRootAbs, err := filepath.Abs("./old_root") | ||
if err != nil { | ||
return fmt.Errorf("failed to get absolute path of old root: %w", err) | ||
} | ||
|
||
err = syscall.PivotRoot(path, oldRootAbs) | ||
if err != nil { | ||
return fmt.Errorf("failed to pivot_root: %w", err) | ||
} | ||
|
||
err = syscall.Chdir("/") | ||
if err != nil { | ||
return fmt.Errorf("failed to chdir: %w", err) | ||
} | ||
|
||
err = syscall.Unmount("old_root", syscall.MNT_DETACH) | ||
if err != nil { | ||
return fmt.Errorf("failed to unmount old_root: %w", err) | ||
} | ||
|
||
err = syscall.Rmdir("old_root") | ||
if err != nil { | ||
return fmt.Errorf("failed to remove old_root: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package jailer | ||
|
||
import ( | ||
"flag" | ||
"os" | ||
"os/exec" | ||
"strconv" | ||
) | ||
|
||
func setupJailerFlags(config *JailerConfig) *flag.FlagSet { | ||
jailerFlagSet := flag.NewFlagSet("", flag.ExitOnError) | ||
jailerFlagSet.Bool("help h", false, "Show help") | ||
jailerFlagSet.IntVar(&config.Uid, "uid", 0, "UID of the process") | ||
jailerFlagSet.IntVar(&config.Gid, "gid", 0, "GID of the process") | ||
jailerFlagSet.StringVar(&config.Netns, "netns", "", "Network namespace to join") | ||
jailerFlagSet.StringVar(&config.NewRoot, "new-root", "", "New root directory") | ||
jailerFlagSet.BoolVar(&config.NewPid, "new-pid", false, "Create new PID namespace") | ||
jailerFlagSet.IntVar(&config.NoFiles, "rlimit-nofiles", 0, "Number of open files") | ||
jailerFlagSet.IntVar(&config.Fsize, "rlimit-fsize", 0, "File size limit") | ||
jailerFlagSet.BoolVar(&config.MountProc, "mount-proc", false, "Mount /proc inside the jail") | ||
jailerFlagSet.StringVar(&config.Cgroup, "cgroup", "", "CGroup to join") | ||
return jailerFlagSet | ||
} | ||
|
||
func makeCmd(jailer string, command []string, opts *options) *exec.Cmd { | ||
args := []string{ | ||
jailer, | ||
"exec", | ||
"--uid", strconv.FormatInt(int64(opts.Uid), 10), | ||
"--gid", strconv.FormatInt(int64(opts.Gid), 10), | ||
"--new-root", opts.NewRoot, | ||
} | ||
|
||
if opts.netns != "" { | ||
args = append(args, "--netns", opts.netns) | ||
} | ||
|
||
if opts.newPidNS { | ||
args = append(args, "--new-pid") | ||
} | ||
|
||
if opts.mountProc { | ||
args = append(args, "--mount-proc") | ||
} | ||
|
||
if opts.setRlimits { | ||
args = append(args, "--rlimit-fsize", strconv.FormatInt(int64(opts.fsize), 10)) | ||
args = append(args, "--rlimit-nofiles", strconv.FormatInt(int64(opts.noFiles), 10)) | ||
} | ||
|
||
if opts.cgroup != "" { | ||
args = append(args, "--cgroup", opts.cgroup) | ||
} | ||
|
||
args = append(args, "--") | ||
args = append(args, command...) | ||
|
||
cmd := &exec.Cmd{ | ||
Path: jailer, | ||
Args: args, | ||
Env: []string{}, | ||
Stdin: os.Stdin, | ||
Stdout: os.Stdout, | ||
Stderr: os.Stderr, | ||
} | ||
|
||
return cmd | ||
} |
Oops, something went wrong.