|
| 1 | +From afd4339ec8682b92eb6bcc870d138106ffd5f58d Mon Sep 17 00:00:00 2001 |
| 2 | +From: kavyasree < [email protected]> |
| 3 | +Date: Fri, 31 Jan 2025 21:16:51 +0530 |
| 4 | +Subject: [PATCH] Patch CVE-2024-45339 |
| 5 | + |
| 6 | +Reference: https://github.com/golang/glog/pull/74 |
| 7 | +--- |
| 8 | + vendor/github.com/golang/glog/glog_file.go | 60 ++++++++++++++++------ |
| 9 | + 1 file changed, 44 insertions(+), 16 deletions(-) |
| 10 | + |
| 11 | +diff --git a/vendor/github.com/golang/glog/glog_file.go b/vendor/github.com/golang/glog/glog_file.go |
| 12 | +index e7d125c..6d239fa 100644 |
| 13 | +--- a/vendor/github.com/golang/glog/glog_file.go |
| 14 | ++++ b/vendor/github.com/golang/glog/glog_file.go |
| 15 | +@@ -118,32 +118,53 @@ var onceLogDirs sync.Once |
| 16 | + // contains tag ("INFO", "FATAL", etc.) and t. If the file is created |
| 17 | + // successfully, create also attempts to update the symlink for that tag, ignoring |
| 18 | + // errors. |
| 19 | +-func create(tag string, t time.Time) (f *os.File, filename string, err error) { |
| 20 | ++func create(tag string, t time.Time, dir string) (f *os.File, filename string, err error) { |
| 21 | ++ if dir != "" { |
| 22 | ++ f, name, err := createInDir(dir, tag, t) |
| 23 | ++ if err == nil { |
| 24 | ++ return f, name, err |
| 25 | ++ } |
| 26 | ++ return nil, "", fmt.Errorf("log: cannot create log: %v", err) |
| 27 | ++ } |
| 28 | ++ |
| 29 | + onceLogDirs.Do(createLogDirs) |
| 30 | + if len(logDirs) == 0 { |
| 31 | + return nil, "", errors.New("log: no log dirs") |
| 32 | + } |
| 33 | +- name, link := logName(tag, t) |
| 34 | + var lastErr error |
| 35 | + for _, dir := range logDirs { |
| 36 | +- fname := filepath.Join(dir, name) |
| 37 | +- f, err := os.Create(fname) |
| 38 | ++ f, name, err := createInDir(dir, tag, t) |
| 39 | + if err == nil { |
| 40 | +- symlink := filepath.Join(dir, link) |
| 41 | +- os.Remove(symlink) // ignore err |
| 42 | +- os.Symlink(name, symlink) // ignore err |
| 43 | +- if *logLink != "" { |
| 44 | +- lsymlink := filepath.Join(*logLink, link) |
| 45 | +- os.Remove(lsymlink) // ignore err |
| 46 | +- os.Symlink(fname, lsymlink) // ignore err |
| 47 | +- } |
| 48 | +- return f, fname, nil |
| 49 | ++ return f, name, err |
| 50 | + } |
| 51 | + lastErr = err |
| 52 | + } |
| 53 | + return nil, "", fmt.Errorf("log: cannot create log: %v", lastErr) |
| 54 | + } |
| 55 | + |
| 56 | ++func createInDir(dir, tag string, t time.Time) (f *os.File, name string, err error) { |
| 57 | ++ name, link := logName(tag, t) |
| 58 | ++ fname := filepath.Join(dir, name) |
| 59 | ++ // O_EXCL is important here, as it prevents a vulnerability. The general idea is that logs often |
| 60 | ++ // live in an insecure directory (like /tmp), so an unprivileged attacker could create fname in |
| 61 | ++ // advance as a symlink to a file the logging process can access, but the attacker cannot. O_EXCL |
| 62 | ++ // fails the open if it already exists, thus prevent our this code from opening the existing file |
| 63 | ++ // the attacker points us to. |
| 64 | ++ f, err = os.OpenFile(fname, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666) |
| 65 | ++ if err == nil { |
| 66 | ++ symlink := filepath.Join(dir, link) |
| 67 | ++ os.Remove(symlink) // ignore err |
| 68 | ++ os.Symlink(name, symlink) // ignore err |
| 69 | ++ if *logLink != "" { |
| 70 | ++ lsymlink := filepath.Join(*logLink, link) |
| 71 | ++ os.Remove(lsymlink) // ignore err |
| 72 | ++ os.Symlink(fname, lsymlink) // ignore err |
| 73 | ++ } |
| 74 | ++ return f, fname, nil |
| 75 | ++ } |
| 76 | ++ return nil, "", err |
| 77 | ++} |
| 78 | ++ |
| 79 | + // flushSyncWriter is the interface satisfied by logging destinations. |
| 80 | + type flushSyncWriter interface { |
| 81 | + Flush() error |
| 82 | +@@ -247,6 +268,7 @@ type syncBuffer struct { |
| 83 | + names []string |
| 84 | + sev logsink.Severity |
| 85 | + nbytes uint64 // The number of bytes written to this file |
| 86 | ++ madeAt time.Time |
| 87 | + } |
| 88 | + |
| 89 | + func (sb *syncBuffer) Sync() error { |
| 90 | +@@ -254,9 +276,14 @@ func (sb *syncBuffer) Sync() error { |
| 91 | + } |
| 92 | + |
| 93 | + func (sb *syncBuffer) Write(p []byte) (n int, err error) { |
| 94 | ++ // Rotate the file if it is too large, but ensure we only do so, |
| 95 | ++ // if rotate doesn't create a conflicting filename. |
| 96 | + if sb.nbytes+uint64(len(p)) >= MaxSize { |
| 97 | +- if err := sb.rotateFile(time.Now()); err != nil { |
| 98 | +- return 0, err |
| 99 | ++ now := timeNow() |
| 100 | ++ if now.After(sb.madeAt.Add(1*time.Second)) || now.Second() != sb.madeAt.Second() { |
| 101 | ++ if err := sb.rotateFile(now); err != nil { |
| 102 | ++ return 0, err |
| 103 | ++ } |
| 104 | + } |
| 105 | + } |
| 106 | + n, err = sb.Writer.Write(p) |
| 107 | +@@ -274,7 +301,8 @@ const footer = "\nCONTINUED IN NEXT FILE\n" |
| 108 | + func (sb *syncBuffer) rotateFile(now time.Time) error { |
| 109 | + var err error |
| 110 | + pn := "<none>" |
| 111 | +- file, name, err := create(sb.sev.String(), now) |
| 112 | ++ file, name, err := create(sb.sev.String(), now, "") |
| 113 | ++ sb.madeAt = now |
| 114 | + |
| 115 | + if sb.file != nil { |
| 116 | + // The current log file becomes the previous log at the end of |
| 117 | +-- |
| 118 | +2.34.1 |
| 119 | + |
0 commit comments