Skip to content

Commit 75f8545

Browse files
authored
Merge pull request kubernetes#131224 from skitt/go-1.24-osroot
Use Go 1.24 os.Root instead of filepath-securejoin
2 parents 5be7941 + 4f33b74 commit 75f8545

File tree

4 files changed

+32
-96
lines changed

4 files changed

+32
-96
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ require (
2323
github.com/coreos/go-oidc v2.3.0+incompatible
2424
github.com/coreos/go-systemd/v22 v22.5.0
2525
github.com/cpuguy83/go-md2man/v2 v2.0.4
26-
github.com/cyphar/filepath-securejoin v0.4.1
2726
github.com/distribution/reference v0.6.0
2827
github.com/docker/go-units v0.5.0
2928
github.com/emicklei/go-restful/v3 v3.11.0
@@ -142,6 +141,7 @@ require (
142141
github.com/containerd/typeurl/v2 v2.2.2 // indirect
143142
github.com/coredns/caddy v1.1.1 // indirect
144143
github.com/coreos/go-semver v0.3.1 // indirect
144+
github.com/cyphar/filepath-securejoin v0.4.1 // indirect
145145
github.com/davecgh/go-spew v1.1.1 // indirect
146146
github.com/dustin/go-humanize v1.0.1 // indirect
147147
github.com/euank/go-kmsg-parser v2.0.0+incompatible // indirect

pkg/kubelet/kubelet_server_journal.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ import (
3434
"strings"
3535
"time"
3636

37-
securejoin "github.com/cyphar/filepath-securejoin"
38-
3937
utilvalidation "k8s.io/apimachinery/pkg/util/validation"
4038
"k8s.io/apimachinery/pkg/util/validation/field"
4139
)
@@ -229,10 +227,14 @@ func (n *nodeLogQuery) validate() field.ErrorList {
229227
case len(n.Files) == 1 && n.options != (options{}):
230228
allErrs = append(allErrs, field.Invalid(field.NewPath("query"), n.Files, "cannot specify file with options"))
231229
case len(n.Files) == 1:
232-
if fullLogFilename, err := securejoin.SecureJoin(nodeLogDir, n.Files[0]); err != nil {
233-
allErrs = append(allErrs, field.Invalid(field.NewPath("query"), n.Files, err.Error()))
234-
} else if _, err := os.Stat(fullLogFilename); err != nil {
230+
if root, err := os.OpenRoot(nodeLogDir); err != nil {
235231
allErrs = append(allErrs, field.Invalid(field.NewPath("query"), n.Files, err.Error()))
232+
} else {
233+
// root.Close() never returns errors
234+
defer func() { _ = root.Close() }()
235+
if _, err := root.Stat(n.Files[0]); err != nil {
236+
allErrs = append(allErrs, field.Invalid(field.NewPath("query"), n.Files, err.Error()))
237+
}
236238
}
237239
}
238240

@@ -403,6 +405,30 @@ func newReaderCtx(ctx context.Context, r io.Reader) io.Reader {
403405
}
404406
}
405407

408+
// heuristicsCopyFileLog returns the contents of the given logFile
409+
func heuristicsCopyFileLog(ctx context.Context, w io.Writer, logDir, logFileName string) error {
410+
f, err := os.OpenInRoot(logDir, logFileName)
411+
if err != nil {
412+
return err
413+
}
414+
// Ignoring errors when closing a file opened read-only doesn't cause data loss
415+
defer func() { _ = f.Close() }()
416+
fInfo, err := f.Stat()
417+
if err != nil {
418+
return err
419+
}
420+
// This is to account for the heuristics where logs for service foo
421+
// could be in /var/log/foo/
422+
if fInfo.IsDir() {
423+
return os.ErrNotExist
424+
}
425+
426+
if _, err := io.Copy(w, newReaderCtx(ctx, f)); err != nil {
427+
return err
428+
}
429+
return nil
430+
}
431+
406432
func safeServiceName(s string) error {
407433
// Max length of a service name is 256 across supported OSes
408434
if len(s) > maxServiceLength {

pkg/kubelet/kubelet_server_journal_linux.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,8 @@ package kubelet
2121
import (
2222
"context"
2323
"fmt"
24-
"io"
25-
"os"
2624
"os/exec"
2725
"strings"
28-
29-
securejoin "github.com/cyphar/filepath-securejoin"
30-
unix "golang.org/x/sys/unix"
3126
)
3227

3328
// getLoggingCmd returns the journalctl cmd and arguments for the given nodeLogQuery and boot. Note that
@@ -81,32 +76,3 @@ func checkForNativeLogger(ctx context.Context, service string) bool {
8176
// hence we search for it in the list of services known to journalctl
8277
return strings.Contains(string(output), service+".service")
8378
}
84-
85-
// heuristicsCopyFileLog returns the contents of the given logFile
86-
func heuristicsCopyFileLog(ctx context.Context, w io.Writer, logDir, logFileName string) error {
87-
f, err := securejoin.OpenInRoot(logDir, logFileName)
88-
if err != nil {
89-
return err
90-
}
91-
// Ignoring errors when closing a file opened read-only doesn't cause data loss
92-
defer func() { _ = f.Close() }()
93-
fInfo, err := f.Stat()
94-
if err != nil {
95-
return err
96-
}
97-
// This is to account for the heuristics where logs for service foo
98-
// could be in /var/log/foo/
99-
if fInfo.IsDir() {
100-
return os.ErrNotExist
101-
}
102-
rf, err := securejoin.Reopen(f, unix.O_RDONLY)
103-
if err != nil {
104-
return err
105-
}
106-
defer func() { _ = rf.Close() }()
107-
108-
if _, err := io.Copy(w, newReaderCtx(ctx, rf)); err != nil {
109-
return err
110-
}
111-
return nil
112-
}

pkg/kubelet/kubelet_server_journal_nonlinux.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)