|
15 | 15 | package sca
|
16 | 16 |
|
17 | 17 | import (
|
| 18 | + "bufio" |
18 | 19 | "bytes"
|
19 | 20 | "context"
|
20 | 21 | "debug/buildinfo"
|
@@ -86,6 +87,84 @@ func isInDir(path string, dirs []string) bool {
|
86 | 87 | return false
|
87 | 88 | }
|
88 | 89 |
|
| 90 | +// getLdSoConfDLibPaths will iterate over the files being installed by |
| 91 | +// the package and all its subpackages, and for each configuration |
| 92 | +// file found under /etc/ld.so.conf.d/ it will parse the file and add |
| 93 | +// its contents to a string vector. This vector will ultimately |
| 94 | +// contain all extra paths that will be considered by ld when doing |
| 95 | +// symbol resolution. |
| 96 | +func getLdSoConfDLibPaths(ctx context.Context, hdl SCAHandle) ([]string, error) { |
| 97 | + var extraLibPaths []string |
| 98 | + targetPackageNames := hdl.RelativeNames() |
| 99 | + |
| 100 | + log := clog.FromContext(ctx) |
| 101 | + |
| 102 | + log.Info("scanning for ld.so.conf.d files...") |
| 103 | + |
| 104 | + for _, pkgName := range targetPackageNames { |
| 105 | + fsys, err := hdl.FilesystemForRelative(pkgName) |
| 106 | + if err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + |
| 110 | + if err := fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error { |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + // We're only interested in files inside /etc/ld.so.conf.d/... |
| 116 | + if !isInDir(path, []string{"etc/ld.so.conf.d"}) { |
| 117 | + return nil |
| 118 | + } |
| 119 | + |
| 120 | + // ... and whose suffix is ".conf"... |
| 121 | + if !strings.HasSuffix(path, ".conf") { |
| 122 | + return nil |
| 123 | + } |
| 124 | + |
| 125 | + fi, err := d.Info() |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + |
| 130 | + if !fi.Mode().IsRegular() { |
| 131 | + return nil |
| 132 | + } |
| 133 | + |
| 134 | + log.Infof(" found ld.so.conf.d file %s", path) |
| 135 | + |
| 136 | + fd, err := fsys.Open(path) |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + defer fd.Close() |
| 141 | + |
| 142 | + scanner := bufio.NewScanner(fd) |
| 143 | + for scanner.Scan() { |
| 144 | + line := scanner.Text() |
| 145 | + if line[0] != '/' { |
| 146 | + continue |
| 147 | + } |
| 148 | + // Strip the initial slash since |
| 149 | + // libDirs paths need to be relative. |
| 150 | + line = line[1:] |
| 151 | + log.Infof(" found extra lib path %s", line) |
| 152 | + extraLibPaths = append(extraLibPaths, line) |
| 153 | + } |
| 154 | + |
| 155 | + if err := scanner.Err(); err != nil { |
| 156 | + return err |
| 157 | + } |
| 158 | + |
| 159 | + return nil |
| 160 | + }); err != nil { |
| 161 | + return nil, err |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return extraLibPaths, nil |
| 166 | +} |
| 167 | + |
89 | 168 | func generateCmdProviders(ctx context.Context, hdl SCAHandle, generated *config.Dependencies) error {
|
90 | 169 | log := clog.FromContext(ctx)
|
91 | 170 |
|
@@ -766,6 +845,17 @@ func generateShbangDeps(ctx context.Context, hdl SCAHandle, generated *config.De
|
766 | 845 | // Analyze runs the SCA analyzers on a given SCA handle, modifying the generated dependencies
|
767 | 846 | // set as needed.
|
768 | 847 | func Analyze(ctx context.Context, hdl SCAHandle, generated *config.Dependencies) error {
|
| 848 | + var oldLibDirs []string |
| 849 | + |
| 850 | + extraLibPaths, err := getLdSoConfDLibPaths(ctx, hdl) |
| 851 | + if err != nil { |
| 852 | + return err |
| 853 | + } |
| 854 | + if extraLibPaths != nil { |
| 855 | + oldLibDirs = libDirs |
| 856 | + libDirs = append(libDirs, extraLibPaths...) |
| 857 | + } |
| 858 | + |
769 | 859 | generators := []DependencyGenerator{
|
770 | 860 | generateSharedObjectNameDeps,
|
771 | 861 | generateCmdProviders,
|
@@ -804,5 +894,9 @@ func Analyze(ctx context.Context, hdl SCAHandle, generated *config.Dependencies)
|
804 | 894 | generated.Provides = nil
|
805 | 895 | }
|
806 | 896 |
|
| 897 | + if oldLibDirs != nil { |
| 898 | + libDirs = oldLibDirs |
| 899 | + } |
| 900 | + |
807 | 901 | return nil
|
808 | 902 | }
|
0 commit comments