| 1 |
// landlock-restrict-scoped executes a process with Landlock scope restrictions. |
| 2 |
// |
| 3 |
// This is an example tool which does not provide backwards compatibility guarantees. |
| 4 |
package main |
| 5 |
|
| 6 |
import ( |
| 7 |
"flag" |
| 8 |
"fmt" |
| 9 |
"log" |
| 10 |
"os" |
| 11 |
"syscall" |
| 12 |
|
| 13 |
"github.com/landlock-lsm/go-landlock/landlock" |
| 14 |
) |
| 15 |
|
| 16 |
func usage() { |
| 17 |
var ( |
| 18 |
out = flag.CommandLine.Output() |
| 19 |
name = os.Args[0] |
| 20 |
) |
| 21 |
fmt.Fprintf(out, "Usage of %s:\n", name) |
| 22 |
flag.PrintDefaults() |
| 23 |
fmt.Fprintf(out, "\nExample usages:\n") |
| 24 |
fmt.Fprintf(out, " %s -- /usr/bin/kill -USR1 $$\n", name) |
| 25 |
fmt.Fprintln(out) |
| 26 |
fmt.Fprintln(out, "\033[31;1m** This is a demo tool for go-landlock and will not provide backwards compatibility. **\033[0m") |
| 27 |
} |
| 28 |
|
| 29 |
func main() { |
| 30 |
flag.Usage = usage |
| 31 |
flag.Parse() |
| 32 |
|
| 33 |
var cmd []string |
| 34 |
if flag.NArg() > 1 { |
| 35 |
cmd = flag.Args() |
| 36 |
} else { |
| 37 |
log.Println("missing command to call, using /bin/bash") |
| 38 |
cmd = []string{"/bin/bash"} |
| 39 |
} |
| 40 |
|
| 41 |
if err := landlock.V9.RestrictScoped(); err != nil { |
| 42 |
log.Fatalf("landlock RestrictScoped: %v", err) |
| 43 |
} |
| 44 |
|
| 45 |
log.Printf("Starting %v", cmd) |
| 46 |
if err := syscall.Exec(cmd[0], cmd, os.Environ()); err != nil { |
| 47 |
log.Fatalf("execve: %v", err) |
| 48 |
} |
| 49 |
} |