Skip to content

Commit

Permalink
run: use internal.GetTempDir with os.MkdirTemp
Browse files Browse the repository at this point in the history
Projects which are using buildah as a library and set `TMPDIR` manually
can stumble upon a use-case where `TMPDIR` was set to a relative path.

Such as `export TMPDIR=.` in such case buildah will try to create a
temporary root using `Mkdirtemp` leading to a point where bundle is not
generated correctly since path was relative.

Following use case can be resolved by making sure that buildah always
converts relative path to absolute path and `GetTempDir` does it well.

Example reproducer with podman

```Dockerfile
FROM alpine
RUN echo hello
```

```console
export TMPDIR=.
podman build --no-cache -t test .
```

Expected failure
```console
STEP 1/2: FROM alpine
STEP 2/2: RUN echo hello
error running container: checking permissions on "buildah2341274198": stat buildah2341274198: no such file or directory
ERRO[0000] did not get container create message from subprocess: EOF
Error: building at STEP "RUN echo hello": while running runtime: exit status 1
```

Closes: RHEL-2598

Signed-off-by: Aditya R <[email protected]>
  • Loading branch information
flouthoc committed Oct 11, 2023
1 parent 21badab commit 0e3f189
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
9 changes: 9 additions & 0 deletions internal/tmpdir/tmpdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tmpdir

import (
"os"
"path/filepath"
"testing"

"github.com/containers/common/pkg/config"
Expand All @@ -26,6 +27,14 @@ func TestGetTempDir(t *testing.T) {
err = os.Unsetenv("TMPDIR")
require.NoError(t, err)

// relative TMPDIR should be automatically converted to absolute
err = os.Setenv("TMPDIR", ".")
require.NoError(t, err)
tmpdir = GetTempDir()
assert.True(t, filepath.IsAbs(tmpdir), "path from GetTempDir should always be absolute")
err = os.Unsetenv("TMPDIR")
require.NoError(t, err)

f, err := os.CreateTemp("", "containers.conf-")
require.NoError(t, err)
// close and remove the temporary file at the end of the program
Expand Down
3 changes: 2 additions & 1 deletion run_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/containers/buildah/copier"
"github.com/containers/buildah/define"
"github.com/containers/buildah/internal"
"github.com/containers/buildah/internal/tmpdir"
"github.com/containers/buildah/pkg/jail"
"github.com/containers/buildah/pkg/overlay"
"github.com/containers/buildah/pkg/parse"
Expand Down Expand Up @@ -72,7 +73,7 @@ func setChildProcess() error {
}

func (b *Builder) Run(command []string, options RunOptions) error {
p, err := os.MkdirTemp("", Package)
p, err := os.MkdirTemp(tmpdir.GetTempDir(), define.Package)
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions run_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/containers/buildah/copier"
"github.com/containers/buildah/define"
"github.com/containers/buildah/internal"
"github.com/containers/buildah/internal/tmpdir"
"github.com/containers/buildah/internal/volumes"
"github.com/containers/buildah/pkg/overlay"
"github.com/containers/buildah/pkg/parse"
Expand Down Expand Up @@ -71,7 +72,7 @@ func setChildProcess() error {

// Run runs the specified command in the container's root filesystem.
func (b *Builder) Run(command []string, options RunOptions) error {
p, err := os.MkdirTemp("", define.Package)
p, err := os.MkdirTemp(tmpdir.GetTempDir(), define.Package)
if err != nil {
return err
}
Expand Down Expand Up @@ -499,7 +500,7 @@ func setupSlirp4netnsNetwork(config *config.Config, netns, cid string, options [
Mask: res.Subnet.Mask,
}}
netStatus := map[string]nettypes.StatusBlock{
slirp4netns.BinaryName: nettypes.StatusBlock{
slirp4netns.BinaryName: {
Interfaces: map[string]nettypes.NetInterface{
"tap0": {
Subnets: []nettypes.NetAddress{{IPNet: subnet}},
Expand Down Expand Up @@ -541,7 +542,7 @@ func setupPasta(config *config.Config, netns string, options []string) (func(),
Mask: net.IPv4Mask(255, 255, 255, 0),
}}
netStatus := map[string]nettypes.StatusBlock{
slirp4netns.BinaryName: nettypes.StatusBlock{
slirp4netns.BinaryName: {
Interfaces: map[string]nettypes.NetInterface{
"tap0": {
Subnets: []nettypes.NetAddress{{IPNet: subnet}},
Expand Down

0 comments on commit 0e3f189

Please sign in to comment.