Debian Codegraph @

cmd/landlock-restrict/main.go

raw
1 // landlock-restrict executes a process with Landlock filesystem restrictions.
2 //
3 // This is an example tool which does not provide backwards compatibility guarantees.
4 package main
5
6 import (
7 "fmt"
8 "log"
9 "os"
10 "strconv"
11 "strings"
12 "syscall"
13
14 "github.com/landlock-lsm/go-landlock/landlock"
15 )
16
17 func parseFlags(args []string) (verbose bool, cfg landlock.Config, opts []landlock.Rule, cmd []string) {
18 configs := []landlock.Config{landlock.V1, landlock.V2, landlock.V3, landlock.V4, landlock.V5, landlock.V6, landlock.V7, landlock.V8, landlock.V9}
19 cfg = configs[len(configs)-1]
20
21 takeArgs := func(makeOpt func(...string) landlock.FSRule) landlock.Rule {
22 var paths []string
23 needRefer := false
24 needIoctlDev := false
25 needResolveUnix := false
26 for len(args) > 0 && !strings.HasPrefix(args[0], "-") {
27 switch args[0] {
28 case "+refer":
29 needRefer = true
30 case "+ioctl_dev":
31 needIoctlDev = true
32 case "+resolve_unix":
33 needResolveUnix = true
34 default:
35 paths = append(paths, args[0])
36 }
37 args = args[1:]
38 }
39 opt := makeOpt(paths...)
40 if verbose {
41 fmt.Println("Path option:", opt)
42 }
43 if needRefer {
44 opt = opt.WithRefer()
45 }
46 if needIoctlDev {
47 opt = opt.WithIoctlDev()
48 }
49 if needResolveUnix {
50 opt = opt.WithResolveUnix()
51 }
52 if verbose {
53 fmt.Println("Path option:", opt)
54 }
55 return opt
56 }
57
58 bestEffort := true
59 ArgParsing:
60 for len(args) > 0 {
61 switch args[0] {
62 case "-1", "-2", "-3", "-4", "-5", "-6", "-7", "-8", "-9":
63 v, err := strconv.Atoi(args[0][1:])
64 if err != nil {
65 log.Fatal(err)
66 }
67 cfg = configs[v-1]
68 args = args[1:]
69 continue
70 case "-l":
71 cfg = cfg.EnableLoggingForSubprocesses()
72 args = args[1:]
73 continue
74 case "-strict":
75 bestEffort = false
76 args = args[1:]
77 continue
78 case "-v":
79 verbose = true
80 args = args[1:]
81 continue
82 case "-ro":
83 args = args[1:]
84 opts = append(opts, takeArgs(landlock.RODirs))
85 continue
86 case "-rw":
87 args = args[1:]
88 opts = append(opts, takeArgs(landlock.RWDirs))
89 continue
90 case "-rofiles":
91 args = args[1:]
92 opts = append(opts, takeArgs(landlock.ROFiles))
93 continue
94 case "-rwfiles":
95 args = args[1:]
96 opts = append(opts, takeArgs(landlock.RWFiles))
97 continue
98 case "--":
99 args = args[1:]
100 // Remaining args are the command
101 break ArgParsing
102 default:
103 log.Fatalf("Unrecognized option %q", args[0])
104 }
105 }
106
107 cmd = args
108 if bestEffort {
109 cfg = cfg.BestEffort()
110 }
111 return verbose, cfg, opts, cmd
112 }
113
114 func main() {
115 verbose, cfg, opts, cmdArgs := parseFlags(os.Args[1:])
116 if verbose {
117 fmt.Println("Args: ", os.Args)
118 fmt.Println()
119 fmt.Printf("Config: %v\n", cfg)
120 fmt.Println()
121 fmt.Printf("Executing command %v\n", cmdArgs)
122 }
123
124 if len(cmdArgs) < 1 {
125 fmt.Println("Usage:")
126 fmt.Println(" landlock-restrict")
127 fmt.Println(" [-v] [-l]")
128 fmt.Println(" [-1] [-2] [-3] [-4] [-5] [-6] [-7] [-8] [-9] [-strict]")
129 fmt.Println(" [-ro [+refer] PATH...]")
130 fmt.Println(" [-rw [+refer] [+ioctl_dev] [+resolve_unix] PATH...]")
131 fmt.Println(" [-rofiles [+refer] PATH]")
132 fmt.Println(" [-rwfiles [+refer] PATH]")
133 fmt.Println(" -- COMMAND...")
134 fmt.Println()
135 fmt.Println("Options:")
136 fmt.Println(" -ro, -rw, -rofiles, -rwfiles paths to restrict to")
137 fmt.Println(" -1, -2, -3, -4, -5, -6, -7, -8, -9 select Landlock version")
138 fmt.Println(" -strict use strict mode (instead of best effort)")
139 fmt.Println(" -v verbose logging")
140 fmt.Println(" -l audit logging for subprocess")
141 fmt.Println()
142 fmt.Println("A path list that contains the word '+refer' will additionally grant the refer access right.")
143 fmt.Println()
144 fmt.Println("Default mode for Landlock is V9 in best effort mode (best compatibility)")
145 fmt.Println()
146 fmt.Println("\033[31;1m** This is a demo tool for go-landlock and will not provide backwards compatibility. **\033[0m")
147
148 log.Fatalf("Need proper command, got %v", cmdArgs)
149 }
150
151 if !strings.HasPrefix(cmdArgs[0], "/") {
152 log.Fatalf("Need absolute binary path, got %q", cmdArgs[0])
153 }
154
155 err := cfg.RestrictPaths(opts...)
156 if err != nil {
157 log.Fatalf("landlock: %v", err)
158 }
159
160 if err := syscall.Exec(cmdArgs[0], cmdArgs, os.Environ()); err != nil {
161 log.Fatalf("execve: %v", err)
162 }
163 }