-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
scanner: execute syft scan in chroot environment
When scanning a directory, the syft scanner resolves symlinks inside the target directory in the context of the host filesystem. However, for our purposes, the target filesystem is entirely self-contained. In lieu of syft changes, we can temporarily hack around this limitation by executing the scanner in a chroot-ed environment. Signed-off-by: Justin Chadwell <me@jedevc.com>
- Loading branch information
Showing
2 changed files
with
99 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2022 buildkit-syft-scanner authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package internal | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
// withChroot executes a target function inside a chroot environment. | ||
// | ||
// After the function is executed, the chroot is exited and the working | ||
// directory is restored. | ||
func withChroot(dir string, f func() error) error { | ||
// save previous state | ||
oldfd, err := syscall.Open("/", syscall.O_RDONLY, 0) | ||
if err != nil { | ||
return err | ||
} | ||
oldwd, err := syscall.Getwd() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// set new state | ||
if err := syscall.Chroot(dir); err != nil { | ||
return err | ||
} | ||
if err := syscall.Chdir("/"); err != nil { | ||
return err | ||
} | ||
|
||
// execute target function | ||
err = f() | ||
|
||
// restore previous state | ||
if err2 := syscall.Fchdir(oldfd); err2 != nil && err == nil { | ||
return err2 | ||
} | ||
if err2 := syscall.Chroot("."); err2 != nil && err == nil { | ||
return err2 | ||
} | ||
if err2 := syscall.Chdir(oldwd); err2 != nil && err == nil { | ||
return err2 | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters