Skip to content

Commit 4f71665

Browse files
committed
internal/maincmd: push landlock to after SSH/net.Dial
This allows us to make our Landlock policy stricter.
1 parent da9802e commit 4f71665

File tree

4 files changed

+16
-24
lines changed

4 files changed

+16
-24
lines changed

internal/maincmd/clientmaincmd.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,6 @@ func rsyncMain(ctx context.Context, osenv *rsyncos.Env, opts *rsyncopts.Options,
103103
rwDirs = paths
104104
}
105105
}
106-
if osenv.Restrict() {
107-
if err := restrict.MaybeFileSystem(roDirs, rwDirs); err != nil {
108-
return nil, err
109-
}
110-
}
111106

112107
module := path
113108
if idx := strings.IndexByte(module, '/'); idx > -1 {
@@ -118,7 +113,7 @@ func rsyncMain(ctx context.Context, osenv *rsyncos.Env, opts *rsyncopts.Options,
118113
}
119114

120115
if daemonConnection < 0 {
121-
stats, err := socketClient(ctx, osenv, opts, host, path, port, paths)
116+
stats, err := socketClient(ctx, osenv, opts, host, path, port, paths, roDirs, rwDirs)
122117
if err != nil {
123118
return nil, err
124119
}
@@ -141,6 +136,13 @@ func rsyncMain(ctx context.Context, osenv *rsyncos.Env, opts *rsyncopts.Options,
141136
r: rc,
142137
w: wc,
143138
}
139+
140+
if osenv.Restrict() {
141+
if err := restrict.MaybeFileSystem(roDirs, rwDirs); err != nil {
142+
return nil, err
143+
}
144+
}
145+
144146
negotiate := true
145147
if daemonConnection != 0 {
146148
done, err := startInbandExchange(osenv, opts, conn, module, path)

internal/maincmd/clientserver.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import (
1111
"time"
1212

1313
"github.com/gokrazy/rsync"
14+
"github.com/gokrazy/rsync/internal/restrict"
1415
"github.com/gokrazy/rsync/internal/rsyncopts"
1516
"github.com/gokrazy/rsync/internal/rsyncos"
1617
"github.com/gokrazy/rsync/internal/rsyncstats"
1718
)
1819

1920
// rsync/clientserver.c:start_socket_client
20-
func socketClient(ctx context.Context, osenv *rsyncos.Env, opts *rsyncopts.Options, host string, path string, port int, paths []string) (*rsyncstats.TransferStats, error) {
21+
func socketClient(ctx context.Context, osenv *rsyncos.Env, opts *rsyncopts.Options, host string, path string, port int, paths []string, roDirs, rwDirs []string) (*rsyncstats.TransferStats, error) {
2122
if port < 0 {
2223
if port := opts.RsyncPort(); port > 0 {
2324
host += ":" + strconv.Itoa(port)
@@ -51,6 +52,11 @@ func socketClient(ctx context.Context, osenv *rsyncos.Env, opts *rsyncopts.Optio
5152
module = module[:idx]
5253
}
5354
osenv.Logf("rsync module %q, path %q", module, path)
55+
if osenv.Restrict() {
56+
if err := restrict.MaybeFileSystem(roDirs, rwDirs); err != nil {
57+
return nil, err
58+
}
59+
}
5460
done, err := startInbandExchange(osenv, opts, conn, module, path)
5561
if err != nil {
5662
return nil, err

internal/restrict/restrict_linux.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"log"
88
"os"
9-
"path/filepath"
109

1110
"github.com/landlock-lsm/go-landlock/landlock"
1211
)
@@ -28,19 +27,6 @@ var userLookup = []string{
2827
"/etc/group", // group lookup
2928
}
3029

31-
// ssh(1) needs to read its config and key files
32-
var sshConfigDirs = []string{
33-
filepath.Join(os.Getenv("HOME"), ".ssh"), // user
34-
"/etc/ssh", // system-wide
35-
}
36-
var sshDirs = []string{
37-
"/usr", // for running ssh(1)
38-
"/nix", // for running ssh(1) on NixOS
39-
}
40-
var sshDevices = []string{
41-
"/dev/null",
42-
}
43-
4430
func MaybeFileSystem(roDirsOrFiles []string, rwDirs []string) error {
4531
re := ExtraHook
4632
if re == nil {
@@ -65,9 +51,6 @@ func MaybeFileSystem(roDirsOrFiles []string, rwDirs []string) error {
6551
append(re(), []landlock.Rule{
6652
landlock.ROFiles(dnsLookup...).IgnoreIfMissing(),
6753
landlock.ROFiles(userLookup...).IgnoreIfMissing(),
68-
landlock.RODirs(sshConfigDirs...).IgnoreIfMissing(),
69-
landlock.RODirs(sshDirs...).IgnoreIfMissing(),
70-
landlock.RWFiles(sshDevices...).IgnoreIfMissing(),
7154
landlock.RODirs(roDirs...).IgnoreIfMissing(),
7255
landlock.ROFiles(roFiles...).IgnoreIfMissing(),
7356
landlock.RWDirs(rwDirs...).WithRefer(),

internal/rsynctest/restrict.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func init() {
1212
return []landlock.Rule{
1313
// contains /usr/bin/rsync (and library deps)
1414
landlock.RODirs("/usr"),
15+
landlock.RODirs("/nix").IgnoreIfMissing(),
1516

1617
// for t.TempDir()
1718
landlock.RWDirs(os.TempDir()).WithRefer(),

0 commit comments

Comments
 (0)