|
| 1 | +package patchpkg |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "io/fs" |
| 8 | + "log/slog" |
| 9 | + "os/exec" |
| 10 | + "path" |
| 11 | + "slices" |
| 12 | +) |
| 13 | + |
| 14 | +// glibcPatcher patches ELF binaries to use an alternative version of glibc. |
| 15 | +type glibcPatcher struct { |
| 16 | + // ld is the absolute path to the new dynamic linker (ld.so). |
| 17 | + ld string |
| 18 | + |
| 19 | + // lib is the absolute path to the lib directory containing the new libc |
| 20 | + // shared objects (libc.so). |
| 21 | + lib string |
| 22 | +} |
| 23 | + |
| 24 | +// newGlibcPatcher creates a new glibcPatcher and verifies that it can find the |
| 25 | +// shared object files in glibc. |
| 26 | +func newGlibcPatcher(glibc *packageFS) (patcher glibcPatcher, err error) { |
| 27 | + // Verify that we can find a directory with libc in it. |
| 28 | + glob := "lib*/libc.so*" |
| 29 | + matches, _ := fs.Glob(glibc, glob) |
| 30 | + if len(matches) == 0 { |
| 31 | + return glibcPatcher{}, fmt.Errorf("cannot find libc.so file matching %q", glob) |
| 32 | + } |
| 33 | + for i := range matches { |
| 34 | + matches[i] = path.Dir(matches[i]) |
| 35 | + } |
| 36 | + slices.Sort(matches) // pick the shortest name: lib < lib32 < lib64 < libx32 |
| 37 | + patcher.lib, err = glibc.OSPath(matches[0]) |
| 38 | + if err != nil { |
| 39 | + return glibcPatcher{}, err |
| 40 | + } |
| 41 | + slog.Debug("found new libc directory", "path", patcher.lib) |
| 42 | + |
| 43 | + // Verify that we can find the new dynamic linker. |
| 44 | + glob = "lib*/ld-linux*.so*" |
| 45 | + matches, _ = fs.Glob(glibc, glob) |
| 46 | + if len(matches) == 0 { |
| 47 | + return glibcPatcher{}, fmt.Errorf("cannot find ld.so file matching %q", glob) |
| 48 | + } |
| 49 | + slices.Sort(matches) |
| 50 | + patcher.ld, err = glibc.OSPath(matches[0]) |
| 51 | + if err != nil { |
| 52 | + return glibcPatcher{}, err |
| 53 | + } |
| 54 | + slog.Debug("found new dynamic linker", "path", patcher.ld) |
| 55 | + |
| 56 | + return patcher, nil |
| 57 | +} |
| 58 | + |
| 59 | +// patch applies glibc patches to a binary and writes the patched result to |
| 60 | +// outPath. It does not modify the original binary in-place. |
| 61 | +func (g glibcPatcher) patch(ctx context.Context, path, outPath string) error { |
| 62 | + cmd := &patchelf{PrintInterpreter: true} |
| 63 | + out, err := cmd.run(ctx, path) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + oldInterp := string(out) |
| 68 | + |
| 69 | + cmd = &patchelf{PrintRPATH: true} |
| 70 | + out, err = cmd.run(ctx, path) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + oldRpath := string(out) |
| 75 | + |
| 76 | + cmd = &patchelf{ |
| 77 | + SetInterpreter: g.ld, |
| 78 | + Output: outPath, |
| 79 | + } |
| 80 | + if len(oldRpath) == 0 { |
| 81 | + cmd.SetRPATH = g.lib |
| 82 | + } else { |
| 83 | + cmd.SetRPATH = g.lib + ":" + oldRpath |
| 84 | + } |
| 85 | + |
| 86 | + slog.Debug("patching glibc on binary", |
| 87 | + "path", path, "outPath", cmd.Output, |
| 88 | + "old_interp", oldInterp, "new_interp", cmd.SetInterpreter, |
| 89 | + "old_rpath", oldRpath, "new_rpath", cmd.SetRPATH, |
| 90 | + ) |
| 91 | + _, err = cmd.run(ctx, path) |
| 92 | + return err |
| 93 | +} |
| 94 | + |
| 95 | +// patchelf runs the patchelf command. |
| 96 | +type patchelf struct { |
| 97 | + SetRPATH string |
| 98 | + PrintRPATH bool |
| 99 | + |
| 100 | + SetInterpreter string |
| 101 | + PrintInterpreter bool |
| 102 | + |
| 103 | + Output string |
| 104 | +} |
| 105 | + |
| 106 | +// run runs patchelf on an ELF binary and returns its output. |
| 107 | +func (p *patchelf) run(ctx context.Context, elf string) ([]byte, error) { |
| 108 | + cmd := exec.CommandContext(ctx, lookPath("patchelf")) |
| 109 | + if p.SetRPATH != "" { |
| 110 | + cmd.Args = append(cmd.Args, "--force-rpath", "--set-rpath", p.SetRPATH) |
| 111 | + } |
| 112 | + if p.PrintRPATH { |
| 113 | + cmd.Args = append(cmd.Args, "--print-rpath") |
| 114 | + } |
| 115 | + if p.SetInterpreter != "" { |
| 116 | + cmd.Args = append(cmd.Args, "--set-interpreter", p.SetInterpreter) |
| 117 | + } |
| 118 | + if p.PrintInterpreter { |
| 119 | + cmd.Args = append(cmd.Args, "--print-interpreter") |
| 120 | + } |
| 121 | + if p.Output != "" { |
| 122 | + cmd.Args = append(cmd.Args, "--output", p.Output) |
| 123 | + } |
| 124 | + cmd.Args = append(cmd.Args, elf) |
| 125 | + out, err := cmd.Output() |
| 126 | + return bytes.TrimSpace(out), err |
| 127 | +} |
0 commit comments